def _find_herring_file(self, herringfile): """ Tries to locate the herringfile in the current directory, if not found then tries the parent directory, repeat until either found or the root is hit. If not found, then create a herringfile in the current directory. :param herringfile: the base file name for the herringfile :type herringfile: str :return: the filespec to the found herringfile :rtype: str """ cwd = os.getcwd() while cwd: try: file_spec = os.path.join(cwd, herringfile) with open(file_spec): pass return file_spec except IOError: # not in current directory so go up a directory and look again cwd = os.sep.join(cwd.split(os.sep)[0:-1]) # not found, so create in current directory file_spec = os.path.join(os.getcwd(), herringfile) touch(file_spec) return file_spec
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)