def xw_sub(cmd, item): """ word = regexp extracting the word containing '%' stant = word ~ s/%/item string = string ~ s/word/stant word/ Replace '%' in cmd with items subject to contextual rules about the occurrence of '%'. Specifically, 'foobar' => 'foobar <item1> <item2> ... <item3>' 'foo% bar' => 'foo<item1> foo<item2> ... foo<itemn> bar' 'foo %bar' => 'foo <item1>bar <item2>bar ... <itemn>bar' 'foo % bar' => 'foo <item1> <item2> ... <itemn> bar' """ if '~' in cmd or '$' in cmd: cmd = tbx.expand(cmd) if '%' in cmd: [word] = re.findall(r"\S*%\S*", cmd) stant = re.sub('%', item, word) xp = "{} {}".format(stant, word) rval = re.sub(word, xp, cmd) else: rval = cmd + " " + item return rval
def subst_command(options, arglist): """ Run the command for each filename in arglist. """ for filename in arglist: cmd = tbx.expand(re.sub('%', filename, options['COMMAND'])) psys(cmd, options)
def read_cfg_file(filename): """ Load config data from *filename* """ rval = {'root': None, 'packages': {}} with open(filename, 'r') as rbl: cpkg = None for line in rbl: # throw away any comment at the end of the line # the '#' must be followed by whitespace to be a valid comment line = re.sub(r"\s*#\s.*$", "", line) # ignore blank lines if re.match(r"^\s*$", line): continue (key, val) = line.split() if ':' in key: msg = "Syntax error in config file: colons not allowed" raise pyppi_error(msg) key = key.strip() val = val.strip() if key == 'root': if rval['root'] is None: rval['root'] = tbx.expand(val) else: raise pyppi_error("root was already set") elif key == 'package': rval['packages'][val] = [] cpkg = rval['packages'][val] elif key == 'version': cpkg.append({'version': val}) elif key == 'url': cpkg[-1]['url'] = val elif key == 'minpy': cpkg[-1]['minpy'] = val return rval
def test_flake(): """ Check code quality """ pytest.dbgfunc() files = lglob("pyppi/*.py", "tests/*.py") cmd = "flake8 --ignore $FLAKE_IGNORE {}".format(" ".join(files)) result = tbx.run(tbx.expand(cmd)) assert result == ""
def test_code_quality(): """ Run flake8 to assess the quality of the package per pep8 """ pytest.dbgfunc() globble = sorted(glob.glob("gh/*.py")) globble.extend(sorted(glob.glob("tests/*.py"))) cmd = "flake8 --ignore \"$FLAKE_IGNORE\" {}".format(" ".join(globble)) result = tbx.run(tbx.expand(cmd)) assert result == ""
def test_expand(inp, exp): """ Expand all environment variables, then expand '~' into $HOME. Note: os.path.expanduser() only expands '~' if it's at the beginning of the string (not what I want) """ pytest.dbgfunc() with tbx.envset(HOME='/home/dir', FOO='my home dir = ~', EVAR='value'): assert tbx.expand(inp) == exp
def iterate_command(options, arglist): """ Run a command once for each of a sequence of numbers. Possible enhancements would be to handle low/high/step tuples, and to handle an arbitrary comma delimited list of values. """ (low, high) = options['RANGE'].split(':') for idx in range(int(low), int(high)): cmd = tbx.expand(re.sub('%', str(idx), options['COMMAND'])) psys(cmd, options)
def fx_cmd(**kw): """ Run the command for each filename in arglist. """ if kw['d']: pdb.set_trace() cmd_t = kw['COMMAND'] (dryrun, quiet) = (kw['n'], kw['q']) for filename in kw['FILE']: cmd = tbx.expand(re.sub('%', filename, cmd_t)) dq_run(cmd, dryrun, quiet)
def fx_count(**kw): """ Run a command once for each of a sequence of numbers. Possible enhancements would be to handle low/high/step tuples, and to handle an arbitrary comma delimited list of values. """ if kw['d']: pdb.set_trace() cmd_t = kw['COMMAND'] (dryrun, quiet) = (kw['n'], kw['q']) (low, high) = kw['i'].split(':') for num in range(int(low), int(high) + 1): cmd = tbx.expand(re.sub('%', str(num), cmd_t)) dq_run(cmd, dryrun, quiet)