def _load_tasks(self, herringfile, settings): """ Loads the given herringfile then loads any herringlib files. :param herringfile: the herringfile path :type herringfile: str :return: None """ herringfile_path = Path(herringfile).parent library_paths = self._locate_library(herringfile_path, settings) self.union_dir = mkdir_p(os.path.join(tempfile.mkdtemp(), 'herringlib')) for src_dir in [os.path.abspath(str(path)) for path in reversed(library_paths)]: if not settings.json: info("src_dir: %s" % src_dir) for src_root, dirs, files in os.walk(src_dir): files = [f for f in files if not (f[0] == '.' or f.endswith('.pyc'))] dirs[:] = [d for d in dirs if not (d[0] == '.' or d == '__pycache__')] rel_root = os.path.relpath(src_root, start=src_dir) dest_root = os.path.join(self.union_dir, rel_root) mkdir_p(dest_root) for basename in [name for name in files]: src_name = os.path.join(src_root, basename) dest_name = os.path.join(dest_root, basename) try: shutil.copy(src_name, dest_name) except shutil.Error: pass self._load_modules(herringfile, [Path(self.union_dir)])
def _populate_union_dir(self, union_dir, library_paths, output_json): for src_dir in [os.path.abspath(str(path)) for path in reversed(library_paths)]: if not output_json: info("src_dir: %s" % src_dir) for src_root, dirs, files in os.walk(src_dir): files[:] = filter(lambda file_: not file_.startswith('.') and not file_.endswith('.pyc'), files) dirs[:] = filter(lambda dir_: not dir_.startswith('.') and dir_ != '__pycache__', dirs) rel_root = os.path.relpath(src_root, start=src_dir) dest_root = os.path.join(union_dir, rel_root) mkdir_p(dest_root) # for basename in [name for name in files]: for basename in files: try: shutil.copy(os.path.join(src_root, basename), os.path.join(dest_root, basename)) except shutil.Error: pass
def load_tasks(self, herringfile): """ Loads the given herringfile then loads any herringlib files. :param herringfile: the herringfile path :type herringfile: str :return: None """ herringfile_path = Path(herringfile).parent library_paths = self._locate_library(herringfile_path, self.settings) # if only one herringlib directory then use it. # otherwise create a temp directory and copy each of the source herringlib directories # into the the temp directory without overwriting any files. This populated temp # directory is then our herringlib union directory. if len(library_paths) == 1: self._load_modules(herringfile, [Path(library_paths[0])]) else: self.union_dir = mkdir_p(os.path.join(tempfile.mkdtemp(), 'herringlib')) self._populate_union_dir(union_dir=self.union_dir, library_paths=library_paths, output_json=self.settings.json) self._load_modules(herringfile, [Path(self.union_dir)])
def _create_herring_conf_file(self, herring_conf): herring_conf_dir = os.path.dirname(herring_conf) mkdir_p(herring_conf_dir) user = os.getenv('USER', 'nobody') email = '{user}@localhost'.format(user=user) try: with io.open(herring_conf, 'w', encoding="utf-8") as conf_file: conf_file.write(textwrap.dedent(u"""\ [Herring] [project] author: {author} author_email: {email} dist_host: localhost pypi_path: /var/pypi/dev """.format(author=user, email=email))) except IOError as ex: warning("Could not create ~/.herring/herring.conf ({file}) - {err}".format(file=herring_conf, err=str(ex)))
def test_unionfs(): """ test loading one module from two directories test + foo + alpha.py + bar + bravo.py import alpha import bravo """ test = 'test' foo_dir = os.path.join(test, 'foo') bar_dir = os.path.join(test, 'bar') mount_dir = os.path.join(test, 'mount') foo_init = os.path.join(foo_dir, '__init__.py') bar_init = os.path.join(bar_dir, '__init__.py') alpha_file = os.path.join(foo_dir, 'alpha.py') bravo_file = os.path.join(bar_dir, 'bravo.py') mkdir_p(foo_dir) mkdir_p(bar_dir) mkdir_p(mount_dir) touch(foo_init) touch(bar_init) with safe_edit(alpha_file) as files: files['out'].write(dedent("""\ def alpha(): return 'alpha' """)) with safe_edit(bravo_file) as files: files['out'].write(dedent("""\ def bravo(): return 'bravo' """)) old_sys_path = sys.path[:] sys.path.insert(0, test) with unionfs(source_dirs=[foo_dir, bar_dir], mount_dir=mount_dir): globals()['alpha2'] = getattr(__import__('mount.alpha', globals(), locals(), ['alpha']), 'alpha') globals()['bravo2'] = getattr(__import__('mount.bravo', globals(), locals(), ['bravo']), 'bravo') pprint(locals()) assert alpha2() == 'alpha' assert bravo2() == 'bravo' shutil.rmtree(foo_dir) shutil.rmtree(bar_dir)