1import datetime 2import keyword 3import logging 4import shlex 5import subprocess 6import textwrap 7 8import medikit 9 10logger = logging.getLogger(__name__) 11 12 13def is_identifier(ident: str) -> bool: 14 """Determines if string is valid Python identifier.""" 15 16 if not isinstance(ident, str): 17 raise TypeError("expected str, but got {!r}".format(type(ident))) 18 19 if not ident.isidentifier(): 20 return False 21 22 if keyword.iskeyword(ident): 23 return False 24 25 return True 26 27 28def format_file_content(s): 29 return textwrap.dedent(s).strip() + "\n" 30 31 32def get_override_warning_banner(*, prefix="# ", above=None, bellow=None): 33 return "\n".join( 34 filter( 35 None, 36 ( 37 above, 38 textwrap.indent( 39 "\n".join( 40 ( 41 "Generated by Medikit " 42 + medikit.__version__ 43 + " on " 44 + str(datetime.datetime.now().date()) 45 + ".", 46 "All changes will be overriden.", 47 "Edit Projectfile and run “make update” (or “medikit update”) to regenerate.", 48 ) 49 ), 50 prefix=prefix, 51 ), 52 bellow, 53 ), 54 ) 55 ) 56 57 58def run_command(command, *, logger=logger): 59 logger.info("Running command %s", command) 60 result = subprocess.run(shlex.split(command), stdout=subprocess.PIPE) 61 if result.returncode: 62 raise RuntimeError( 63 '"{command}" exited with status {returncode}.'.format(command=command, returncode=result.returncode) 64 ) 65 return result.stdout.decode("utf-8").strip() 66