def bower(file_map, config=None): """create bower tasks @param file_map: dict with destination file: source i.e. {'chai.js': 'chai/chai.js', 'polymer-platform.js': 'polymer-platform/platform.js'} """ default_config = Config(components_dir='components') config = default_config.make(config) # download packages yield { 'basename': 'bower', 'name': 'install', 'actions': ['bower install'], 'file_dep': ['bower.json'], 'targets': ['bower_components'], } # create destination folder yield { 'basename': 'bower', 'name': config['components_dir'], 'actions': ['mkdir -p {}'.format(config['components_dir'])], } # copy mapped files into destination static folder for dst, src in file_map.items(): yield { 'basename': 'bower', 'name': dst, 'actions': ['cp %(dependencies)s %(targets)s'], 'file_dep': ['bower_components/{}'.format(src)], 'targets': ['{}/{}'.format(config['components_dir'], dst)], }
class Pyflakes(object): """generate tasks for pyflakes """ #: :class:`confclass.Config` #: #: :var str base_dir: list of path patterns of files to be linted #: :var list-str exclude_patterns: list of pattern of files to be removed #: from selection #: :var list-str exclude_paths: list of path of files to be removed #: from selection config = Config( base_dir='.', exclude_patterns=[], exclude_paths=[], ) def __init__(self, **kwargs): """:param kwargs: config params """ self.config = self.config.make(kwargs) def __call__(self, py_file): """Return a task for single file. :param str pyfile: path to file :return: task metadata to run pyflakes on a single module """ # 'unicode' is a builtin in py2 but not on py3. # Make sure pyflakes consider 'unicode' as a builtin so # it does not fail on py3. Checker.builtIns.add('unicode') return { 'name': py_file, 'actions': [(check_path, [py_file])], 'file_dep': [py_file], } def tasks(self, pattern, **kwargs): """run pyflakes on python module yield one task for each file as given by pattern :param str pattern: path pattern of files to be linted """ config = self.config.make(**kwargs) # yield a task for every py file in selection base = Path(config['base_dir']) excluded = set([base.joinpath(e) for e in config['exclude_paths']]) for src in base.glob(pattern): if src in excluded: continue for exclude_pattern in config['exclude_patterns']: if src.match(exclude_pattern): break else: yield self(str(src))
def test_cover_all_multiprocessing(self): pkg = PythonPackage(SAMPLE) cov = Coverage([pkg], config=Config(concurrency='multiprocessing')) task = list(cov.all('my_cov'))[0] assert task['verbosity'] == 2 assert task['basename'] == 'my_cov' assert task['actions'] == [ 'coverage run --branch --concurrency multiprocessing `which py.test`', 'coverage report --show-missing {}'.format(' '.join( pkg.all_modules())) ]
def test_cover_all_parallel(self): pkg = PythonPackage(SAMPLE) cov = Coverage([pkg], config=Config(parallel=True, branch=False, omit=['abc'])) task = list(cov.all('my_cov'))[0] assert task['verbosity'] == 2 assert task['basename'] == 'my_cov' assert task['actions'] == [ 'coverage run --parallel-mode `which py.test`', 'coverage combine', 'coverage report --show-missing --omit abc {}'.format(' '.join( pkg.all_modules())) ]
class PythonPackage(PythonFiles): """Contain list of modules of the package (does not handle sub-packages) :ivar str src_dir: path to dir containing package modules :ivar str test_dir: path to dir containing package tests :ivar list-str src: list of path of source modules :ivar list-str test: list of path of all modules from test folder """ # TODO should track sub-packages #: :class:`confclass.Config` #: #: :var str test_prefix: string prefix on name of files #: :var str pkg_test_dir: path to location of test files config = Config( test_prefix='test_', pkg_test_dir='tests', ) def __init__(self, path, test_path=None, config=None): """ :param str/pathlib.Path path: dir path to package. :param str/pathlib.Path test_path: if test_path is not given assume it is on config.pkg_test_dir inside source package. """ self.config = self.config.make(config) self.test_prefix = self.config['test_prefix'] self.src_dir = str(path) if path else '' if test_path is None: self.test_dir = '{}/{}'.format(self.src_dir, self.config['pkg_test_dir']) else: self.test_dir = str(test_path) self.src = glob.glob("{}/*.py".format(self.src_dir)) self.test = glob.glob("{}/*.py".format(self.test_dir))
def test_make_None(self): config = Config({"foo": "bar"}) c2 = config.make(None) assert c2["foo"] == "bar"
class Coverage(object): """generate tasks for coverage.py""" #: :class:`confclass.Config` #: #: :var string cmd_run_test: shell command used to run tests #: :var branch bool: measure branche coverage #: :var parallel bool: measure using `--parallel-mode` (needed for #: subprocess and multiprocess coverage #: :var concurrency str: --concurrency library #: :var list-str omit: list of paths to omit from coverage config = Config(cmd_run_test="`which py.test`", branch=True, parallel=False, concurrency='', omit=[]) def __init__(self, pkgs, config=None): """ :param list-PythonFiles pkgs: packages/modules to measure coverage """ self.config = self.config.make(config) self.pkgs = [] for pkg in pkgs: assert isinstance(pkg, PythonFiles) self.pkgs.append(pkg) def _action_list(self, modules, test=None): """return list of actions to be used in a doit task""" run_options = [] if self.config['branch']: run_options.append('--branch') if self.config['parallel']: run_options.append('--parallel-mode') if self.config['concurrency']: run_options.append('--concurrency') run_options.append(self.config['concurrency']) report_options = [] if self.config['omit']: omit_list = ','.join(self.config['omit']) report_options.append('--omit {}'.format(omit_list)) actions = [ sep("coverage run", sep(*run_options), self.config['cmd_run_test'], test) ] if self.config['parallel']: actions.append('coverage combine') actions.append( sep("coverage report --show-missing", sep(*report_options), sep(*modules))) return actions def all(self, basename='coverage'): """show coverage for all modules including tests""" all_modules = [] for pkg in self.pkgs: for module in pkg.all_modules(): all_modules.append(module) yield { 'basename': basename, 'actions': self._action_list(all_modules), 'verbosity': 2, } def src(self, basename='coverage_src'): """show coverage for all modules (exclude tests)""" all_modules = [] for pkg in self.pkgs: for module in pkg.src: all_modules.append(module) yield { 'basename': basename, 'actions': self._action_list(all_modules), 'verbosity': 2, } def by_module(self, basename='coverage_module'): """show coverage for individual modules""" for pkg in self.pkgs: prefix = '{}/{}'.format(pkg.test_dir, pkg.test_prefix) to_strip = len(prefix) tests = glob.glob('{}*.py'.format(prefix)) for test in tests: source = pkg.src_dir + '/' + test[to_strip:] yield { 'basename': basename, 'name': test, 'actions': self._action_list([source, test], test), 'verbosity': 2, }
def test_merge_with_dict(self): config = Config({"foo": "bar"}) config.merge({"foo": "baz"}) assert config["foo"] == "baz"
def test_update_keyword(self): config = Config({"foo": "bar"}) config.update(foo="baz") assert config["foo"] == "baz"
def test_merge(self): config = Config({"foo": ["bar"]}) c2 = config.make({"foo": ["baz"]}) assert config["foo"] == ["bar"] assert c2["foo"] == ["bar", "baz"]
def getConfigArgs(): root_dir = Config(configINIFile).get("protoconfig", "root_dir") proto_dir = Config(configINIFile).get("protoconfig", "proto_dir") js_out_dir = Config(configINIFile).get("protoconfig", "js_out_dir") java_out_dir = Config(configINIFile).get("protoconfig", "java_out_dir") return root_dir, proto_dir, js_out_dir, java_out_dir
def test_update_with_dict(self): config = Config({"foo": "bar"}) config.update({"foo": "baz"}) assert config["foo"] == "baz"
def test_make_new_value(self): config = Config({"foo": "bar"}) c2 = config.make({"foo": "baz"}) assert config["foo"] == "bar" assert c2["foo"] == "baz"
def test_copy(): config = Config({"foo": "bar"}) assert isinstance(config.copy(), Config)
def test_setdefault_ok(self): """set default does not overwrite non-default value""" config = Config({"foo": "bar"}) assert config.setdefault("foo", "baz") == "bar"
def test_merge_list(self): config = Config({"foo": ["bar"]}) config.merge({"foo": ["baz"]}) assert config["foo"] == ["bar", "baz"]