def test_dump(self): conf_file = os.path.join(self.__priv_dir, 'test.json') with open(conf_file, 'w') as _: _.write('{\n' '"index": "my_index.markdown",\n' '"test_index": "/home/meh/test_index.markdown",\n' '"test_sources": ["*.x"],\n' '"test_source_filters": ["foobar.x"]\n' '}\n') here = os.path.abspath(os.path.dirname(__file__)) invoke_dir = os.getcwd() relpath = os.path.relpath(here, invoke_dir) overriden_src_dir = os.path.join(relpath, 'overridden_sources') cli = { 'index': 'another_index.markdown', 'test_sources': ['%s/*.x' % overriden_src_dir], 'test_source_filters': ['%s/ignored.x' % overriden_src_dir] } cp = ConfigParser(command_line_args=cli, conf_file=conf_file) cp.dump(conf_file=conf_file) ncp = ConfigParser(conf_file=conf_file) self.assertEqual(ncp.get_index(), os.path.join(invoke_dir, 'another_index.markdown')) self.assertListEqual(ncp.get('test_sources'), [u'../overridden_sources/*.x'])
def test_dependencies(self): conf_file = os.path.join(self.__priv_dir, 'test.json') with open(conf_file, 'w') as _: _.write('{\n' '"index": "my_index.markdown",\n' '"test_index": "/home/meh/test_index.markdown",\n' '"test_sources": ["*.x"],\n' '"test_source_filters": ["foobar.x"]\n' '}\n') touch(os.path.join(self.__priv_dir, 'foo.x')) touch(os.path.join(self.__priv_dir, 'bar.x')) touch(os.path.join(self.__priv_dir, 'baz.x')) touch(os.path.join(self.__priv_dir, 'foobar.x')) cp = ConfigParser(conf_file=conf_file) deps = set([os.path.abspath(dep) for dep in cp.get_dependencies()]) self.assertSetEqual( deps, set([ os.path.join(self.__priv_dir, 'foo.x'), os.path.join(self.__priv_dir, 'bar.x'), os.path.join(self.__priv_dir, 'baz.x'), conf_file ]))
def main(): if len(sys.argv) != 2: print("USAGE: %s path/to/conf/file" % sys.argv[0]) sys.exit(1) PARSER = ConfigParser(conf_file=sys.argv[1]) PARSER.print_make_dependencies() sys.exit(0)
def test_path(self): conf_file = os.path.join(self.__priv_dir, 'test.json') with open(conf_file, 'w') as _: _.write('{\n' '"my_path_argument": "somewhere/plop.x"\n' '}\n') cli = {'my_cli_path_argument': 'elsewhere/foo.x'} cp = ConfigParser(command_line_args=cli, conf_file=conf_file) self.assertEqual(cp.get_path('my_path_argument'), os.path.join(self.__priv_dir, 'somewhere', 'plop.x')) invoke_dir = os.getcwd() self.assertEqual(cp.get_path('my_cli_path_argument'), os.path.join(invoke_dir, 'elsewhere', 'foo.x'))
def test_cli_overrides(self): conf_file = os.path.join(self.__priv_dir, 'test.json') with open(conf_file, 'w') as _: _.write('{\n' '"index": "my_index.markdown",\n' '"test_index": "/home/meh/test_index.markdown",\n' '"test_sources": ["*.x"],\n' '"test_source_filters": ["foobar.x"]\n' '}\n') touch(os.path.join(self.__priv_dir, 'foo.x')) touch(os.path.join(self.__priv_dir, 'foobar.x')) here = os.path.abspath(os.path.dirname(__file__)) invoke_dir = os.getcwd() relpath = os.path.relpath(here, invoke_dir) overriden_src_dir = os.path.join(relpath, 'overridden_sources') shutil.rmtree(overriden_src_dir, ignore_errors=True) os.mkdir(overriden_src_dir) touch(os.path.join(overriden_src_dir, 'other.x')) touch(os.path.join(overriden_src_dir, 'foobar.x')) touch(os.path.join(overriden_src_dir, 'ignored.x')) cli = { 'index': 'another_index.markdown', 'test_sources': ['%s/*.x' % overriden_src_dir], 'test_source_filters': ['%s/ignored.x' % overriden_src_dir] } cp = ConfigParser(command_line_args=cli, conf_file=conf_file) self.assertEqual(cp.get('index'), 'another_index.markdown') self.assertEqual(cp.get_index(), os.path.join(invoke_dir, 'another_index.markdown')) overriden_abs_dir = os.path.join(invoke_dir, overriden_src_dir) self.assertSetEqual( set(cp.get_sources('test_')), set([ os.path.join(overriden_abs_dir, 'other.x'), os.path.join(overriden_abs_dir, 'foobar.x') ])) shutil.rmtree(overriden_src_dir, ignore_errors=True)
def test_sources(self): conf_file = os.path.join(self.__priv_dir, 'test.json') with open(conf_file, 'w') as _: _.write('{\n' '"test_sources": ["*.x"],\n' '"test_source_filters": ["foobar.x"]\n' '}\n') touch(os.path.join(self.__priv_dir, 'foo.x')) touch(os.path.join(self.__priv_dir, 'bar.x')) touch(os.path.join(self.__priv_dir, 'baz.x')) touch(os.path.join(self.__priv_dir, 'foobar.x')) cp = ConfigParser(conf_file=conf_file) self.assertSetEqual( set(cp.get_sources('test_')), set([ os.path.join(self.__priv_dir, 'foo.x'), os.path.join(self.__priv_dir, 'bar.x'), os.path.join(self.__priv_dir, 'baz.x') ]))
def load_conf_file(self, conf_file, overrides): """ Load the project from a configuration file and key-value overides. """ if conf_file is None and os.path.exists('hotdoc.json'): conf_file = 'hotdoc.json' self.__conf_file = conf_file if conf_file and not os.path.exists(conf_file): error('invalid-config', "No configuration file was found at %s" % conf_file) actual_args = {} defaults = {'output_format': 'html'} for key, value in overrides.items(): if key in ('cmd', 'conf_file', 'dry'): continue if value != self.parser.get_default(key): actual_args[key] = value if self.parser.get_default(key) is not None: defaults[key] = value self.config = ConfigParser(command_line_args=actual_args, conf_file=conf_file, defaults=defaults) index = self.config.get_index() if index: hash_obj = hashlib.md5(self.config.get_index()) priv_name = 'hotdoc-private-' + hash_obj.hexdigest() else: priv_name = 'hotdoc-private' self.__private_folder = os.path.abspath(priv_name)
def test_index(self): conf_file = os.path.join(self.__priv_dir, 'test.json') with open(conf_file, 'w') as _: _.write('{\n' '"index": "my_index.markdown",\n' '"test_index": "/home/meh/test_index.markdown",\n' '"test_other_index": "other_index.markdown"\n' '}\n') cp = ConfigParser(conf_file=conf_file) # A relative path was passed, the parser should return an # absolute path with the path to the conf file as root. self.assertEqual(cp.get_index(), os.path.join(self.__priv_dir, 'my_index.markdown')) # An absolute path was passed, and must thus be retrieved self.assertEqual(cp.get_index('test_'), '/home/meh/test_index.markdown') self.assertIsNone(cp.get_index('invalid_prefix')) self.assertEqual(cp.get_index('test_other_'), os.path.join(self.__priv_dir, 'other_index.markdown'))