def __init__(self, file=None): """ Initialize the Replay object, if no file is provided angreal will attempt to find one in parent directories. """ if not file: # Default, go try and find it file = [] directory = get_angreal_path() for f in os.listdir(directory): if fnmatch.fnmatch(f, 'angreal-replay.json'): file.append(f) if len(file) > 1: raise ValueError( 'Found multiple files matching the replay pattern.') file = os.path.join(directory, file[0]) else: if not os.path.isfile(file): raise FileNotFoundError() self.file = file with open(self.file, 'r') as f: here = json.load(f) super(Replay, self).__init__(**here)
def print_nested_help(repository): """ Prints a general :param repository: :return: """ try: tmp_dir = tempfile.mkdtemp() project_path = initialize_cutter(repository, no_input=True, output_dir=tmp_dir) os.chdir(project_path) mod = import_from_file(os.path.join(get_angreal_path(), 'init.py')) mod = mod.init click.echo( "\nThese are the options for the repository ({}) you are attempting to initialize\n" .format(repository)) with click.Context(mod) as ctx: click.echo(mod.get_help(ctx)) shutil.rmtree(tmp_dir) except Exception: click.echo( "Something happened while fetching this temlpates help information, you should check the source for more information" ) exit(0)
def get_angreal_commands(): """ Get everything from the .angreal folder :return: """ available_commands = defaultdict(str) # Get everything we can in the .angreal try: angreal_dir = get_angreal_path() for f in os.listdir(angreal_dir): f = os.path.join(angreal_dir, f) if os.path.basename(f) not in ['init.py', '__init__.py']: try: mod = import_from_file(f) except AttributeError: continue except ImportError: continue for m in mod.__dict__: if isinstance(mod.__dict__[m], click.core.Command): name = os.path.basename(f) name = name[5:] name = name[:-3] available_commands[name] = mod.__dict__[m].help except FileNotFoundError: pass return available_commands
def base_init(repository, init_args, help, no_input=False): """ Initialize an angreal based project. """ if help: print_nested_help(repository) exit(0) try: project_path = initialize_cutter(repository, no_input=no_input, replay=True) except OutputDirExistsException: exit("Output directory exists") os.chdir(project_path) # Check compat of executing angreal binary against .angreal/VERSION template_version = get_template_version() if template_version: if not is_compat(template_version): raise ValueError( 'This template needs to be run using angreal {}'.format( template_version)) file = os.path.join(get_angreal_path(), 'init.py') try: # First try to import the file mod = import_from_file(file) try: # Try to run the "init" function in the task_init file, pass through all of the init_args mod.init(init_args) except AttributeError as e: pass except Exception as e: # Something happened in the sub init execution shutil.rmtree(project_path) exit("Exiting at line 103:\n{}".format(e)) # Init commands should only be run ONCE os.unlink(file) except FileNotFoundError: # No init.py means no special startup instructions. pass except (ImportError, AttributeError): # if the init function doesn't exist or import fails pass response = input( "Import error on init.py, do you want to keep the generated project? [y/n]" ) if response == 'n': shutil.rmtree(project_path) return
def test_get_angreal_task_path(): """ test that we can find the path to the projects angreal directory """ original_dir = os.getcwd() os.chdir(os.path.join(os.path.dirname(__file__), 'fake-repo')) path = get_angreal_path(dir='.angreal') assert os.path.exists(path) os.chdir(original_dir)
def test_from_not_root(): """ test that we recurse up correctly """ original_dir = os.getcwd() os.chdir(os.path.join(os.path.dirname(__file__), 'fake-repo','fake-project')) path = get_angreal_path(dir='.angreal') assert os.path.exists(path) os.chdir(original_dir)
def get_template_version(): """ Get the required version from the template. :return: the semantic version required to operate :rtype: str """ version_file = os.path.join(get_angreal_path(), 'VERSION') if os.path.exists(version_file): return open(version_file).read().strip() return None
def test_get_angreal_task_path_bad(): """ bad file path raises FileNotFoundError """ with pytest.raises(FileNotFoundError): get_angreal_path(dir='.noangreal')