def activate() -> None: """ Source the venv so that its packages may be used. """ path = 'backend/Scripts/' if sys_calls.is_windows_environment( ) else 'backend/bin' files.source(file='activate', path=path)
def commit(message: str) -> None: """ Commit with message. """ if sys_calls.is_windows_environment(): # Windows must wrap message with "" because of how bash expansion works message = f'"{message}"' sys_calls.run(["git", "commit", "-m", message])
def kill_process(pid: PID) -> None: """ Kills the specified PID. """ if sys_calls.is_windows_environment(): command = ["taskkill", "/F", "/PID"] else: command = ["kill"] sys_calls.run(command + [pid])
def check_is_installed(programs: List[ProgramName], *, windows_support=True, posix_support=True) -> None: """ Raise exception if any of the given programs not installed. Can conditionally only check Windows or Posix systems. """ not_installed_programs: List[ProgramName] = [] if windows_support and posix_support: not_installed_programs = find_not_installed(programs) elif windows_support and sys_calls.is_windows_environment(): not_installed_programs = find_not_installed(programs) elif posix_support and not sys_calls.is_windows_environment(): not_installed_programs = find_not_installed(programs) if not_installed_programs: program_list = ' and '.join(not_installed_programs) raise SystemExit(f'{program_list} not installed')
def kill_process(pid: PID) -> None: """ Kills the specified PID. """ if sys_calls.is_windows_environment(): command = ['taskkill', '/F', '/PID'] else: command = ['kill'] sys_calls.run(command + [pid])
def operate_on_all_venv_files(func: Callable[[List[str]], Any]) -> Any: """ Performs passed function on every venv file. """ paths = ['pyvenv.cfg', 'pip-selfcheck.json'] paths += [ 'Scripts', 'Include', 'Lib', 'lib64' ] if sys_calls.is_windows_environment() else ['bin/', 'include/', 'lib/'] paths = list(map(lambda x: 'backend/' + x, paths)) return func(paths)
def remove_old_venv() -> None: """ Remove old venv in favor of Pipenv. """ paths = ["pyvenv.cfg", "pip-selfcheck.json"] paths += ([ "Scripts", "Include", "Lib", "lib64" ] if sys_calls.is_windows_environment() else ["bin/", "include/", "lib/"]) paths = [f"backend/{path}" for path in paths] files.remove(paths)
def test() -> None: """ Run unit tests. """ venv.activate() if sys_calls.is_windows_environment(): py = 'python' else: py = 'python3' sys_calls.run([py, '-m', 'unittest', 'discover', 'tests'], cwd='backend/')
def find_pid_on_port(port: Port) -> PID: """ Finds and returns PID of process listening on specified port. """ # determine environment if sys_calls.is_windows_environment(): command = f"netstat -aon | findstr :{port} | awk '{{ print $5 }}'" else: command = f"lsof -n -i4TCP:{port} | grep LISTEN | awk '{{ print $2 }}'" # find PID pid = sys_calls.get_stdout_as_shell(command) if not pid: raise SystemExit(f"No process found running on port {port}.") return PID(pid)
def source(*, file: File, path: str) -> None: """ Mirrors the source command by adding values to local environment. See https://stackoverflow.com/questions/3503719/emulating-bash-source-in-python """ if sys_calls.is_windows_environment(): command = ['cmd', '/C', f'{file} && set'] else: command = ['bash', '-c', f'source {file} && env'] process = subprocess.Popen(command, stdout=subprocess.PIPE, cwd=path) for line in process.stdout: (key, _, value) = line.decode("utf-8").strip().partition("=") os.environ[key] = value process.communicate()
def source(*, file: File, path: str) -> None: """ Mirrors the source command by adding values to local environment. See https://stackoverflow.com/questions/3503719/emulating-bash-source-in-python """ if sys_calls.is_windows_environment(): command = ["cmd", "/C", f"./{file} && set"] else: command = ["bash", "-c", f"source ./{file} && env"] process = subprocess.Popen(command, stdout=subprocess.PIPE, cwd=path) for line in process.stdout: (key, _, value) = line.decode("utf-8").strip().partition("=") sys_calls.export(key, value) process.communicate()