#!/usr/bin/env python # # __init__.py - Miscellaneous functions used throughout fsl_ci. # # Author: Paul McCarthy # import os import shlex import string import tempfile import contextlib as ctxlib import subprocess as sp USERNAME = 'fsl-ci-rules' """Username to be used for all git interactions which require one. """ EMAIL = 'fsl-ci-rules@git.fmrib.ox.ac.uk' """Password to be used for all git interactions which require one. """ def fprint(*args, **kwargs): """Print with flush=True. """ print(*args, **kwargs, flush=True) @ctxlib.contextmanager def tempdir(): """Context manager to create, and change into, a temporary directory, and then afterwards delete it and change back to the original working directory. """ with tempfile.TemporaryDirectory() as td: prevdir = os.getcwd() os.chdir(td) try: yield td finally: os.chdir(prevdir) @ctxlib.contextmanager def indir(dirname): """Context manager to change into a directory, and then afterwards change back to the original working directory. """ prevdir = os.getcwd() os.chdir(dirname) try: yield finally: os.chdir(prevdir) def sprun(cmd, **kwargs): """Runs the given command with subprocess.run. """ fprint(f'Running {cmd}') cmd = shlex.split(cmd) return sp.run(cmd, check=True, **kwargs) def is_valid_project_version(version): """Return True if the given version/tag is "valid" - it must be a sequence of integers, separated by periods, with an optional leading 'v'. """ if version.lower().startswith('v'): version = version[1:] for part in version.split('.'): if not all([c in string.digits for c in part]): return False return True