Ejemplo n.º 1
0
def run_graph_easy(entries, renderer, outfile=None):
    """ Given the path edge entries, run the graphing tools and produce the
        output.

        Parameters
        ----------
        entries :    `list`
            Path edges
        renderer :   `str`
            One of 'ascii', 'boxart' or 'graphviz'
        outfile :   `str`
            File to save to, only for graphviz. If None, it will delete the
            generated file.
    """
    if not CONFIG.graph_easy:
        log.warn("Graph-Easy not configured, please set graph_easy variable in pkglib config")
        return

    from path import path
    if renderer == 'graphviz' and not os.getenv('DISPLAY'):
        log.info("No DISPLAY set, using ascii renderer")
        renderer = 'ascii'

    instream = '\n'.join(entries)
    if os.path.isfile(CONFIG.graph_easy / 'bin' / 'graph-easy'):
        with chdir(CONFIG.graph_easy / 'lib'):
            if renderer == 'graphviz':
                delete = False
                if not outfile:
                    tmpdir = path(tempfile.mkdtemp())
                    outfile = tmpdir / 'depgraph.png'
                    delete = True
                outfile = path(outfile)
                outfile = outfile.abspath()

                run(['-c', ('../bin/graph-easy --as={0} | '
                            '/usr/bin/dot -Tpng -o {1}'
                            .format(renderer, outfile))],
                    capture_stdout=False, stdin=instream, shell=True)
                if not outfile.isfile:
                    log.error("Failed to create image file.")
                    return
                webbrowser.open('file://%s' % outfile)
                if delete:
                    time.sleep(5)
                    shutil.rmtree(tmpdir)
                else:
                    log.info("Created graph at %s" % outfile)
            else:
                run(['../bin/graph-easy', '--as=%s' % renderer],
                    capture_stdout=False, stdin=instream)
        return
    log.warn("Can't find graphing tool at %s" % CONFIG.graph_easy)
Ejemplo n.º 2
0
def test_get_mirror_config(workspace):
    pypi = CluePyPiApi('http://foo')
    with chdir(workspace.workspace):
        with open('mirror.cfg', 'wb') as fp:
            fp.write(MIRROR_CONFIG)
        cfg = pypi.get_mirror_config('mirror.cfg')
        assert len(cfg) == 2
        for c in cfg:
            assert c['target_host'] in ('foohost', 'barhost')
            if c['target_host'] == 'foohost':
                assert c['target_dir'] == '/path/to/foodir'
            else:
                assert c['target_dir'] == '/path/to/bardir'
Ejemplo n.º 3
0
def run_with_coverage(cmd, pytestconfig, coverage='coverage', cd=None, **kwargs):
    """
    Run a given command with coverage enabled. This won't make any sense
    if the command isn't a python script.

    This must be run within a pytest session that has been setup with
    the '--cov=xxx' options, and therefore requires the pytestconfig
    argument that can be retrieved from the standard pytest funcarg
    of the same name.

    Parameters
    ----------
    cmd: `List`
        Command to run
    pytestconfig: `pytest._config.Config`
        Pytest configuration object
    coverage: `str`
        Path to the coverage executable
    cd: `str`
        If not None, will change to this directory before running the cmd.
        This is the directory that the coverage files will be created in.
    kwargs: keyword arguments
        Any extra arguments to pass to `pkglib.cmdline.run`

    Returns
    -------
    `str` standard output

    Examples
    --------

    >>> def test_example(pytestconfig):
    ...   cmd = ['python','myscript.py']
    ...   run_with_coverage(cmd, pytestconfig)
    """
    if isinstance(cmd, str):
        cmd = [cmd]

    args = [coverage, 'run', '-p']
    if pytestconfig.option.cov_source:
        source_dirs = ",".join(pytestconfig.option.cov_source)
        args += ['--source=%s' % source_dirs]
    args += cmd
    if cd:
        with manage.chdir(cd):
            return run(args, **kwargs)
    return run(args, **kwargs)
Ejemplo n.º 4
0
def test_get_pkg_description():
    with Workspace() as workspace:
        with manage.chdir(workspace.workspace):
            # README.txt only
            with open('README.txt', 'w') as fp:
                fp.write('README\n')
            assert manage.get_pkg_description({}) == 'README\n\n\n'
            # README.txt plus CHANGES.txt
            with open('CHANGES.txt', 'w') as fp:
                fp.write('CHANGES\n')
            assert manage.get_pkg_description({}) == 'README\n\n\nCHANGES\n'
            # Just CHANGES.txt, pulling README from the package
            os.unlink('README.txt')
            with open('foo.py', 'w') as fp:
                fp.write('"""\nFOO\n"""\n')
            sys.path.append(workspace.workspace)
            assert manage.get_pkg_description({'name': 'foo'}) == '\nFOO\n\n\nCHANGES\n'
Ejemplo n.º 5
0
def test_get_pkg_description():
    with Workspace() as workspace:
        with manage.chdir(workspace.workspace):
            # README.txt only
            with open('README.txt', 'w') as fp:
                fp.write('README\n')
            assert manage.get_pkg_description({}) == 'README\n\n\n'
            # README.txt plus CHANGES.txt
            with open('CHANGES.txt', 'w') as fp:
                fp.write('CHANGES\n')
            assert manage.get_pkg_description({}) == 'README\n\n\nCHANGES\n'
            # Just CHANGES.txt, pulling README from the package
            os.unlink('README.txt')
            with open('foo.py', 'w') as fp:
                fp.write('"""\nFOO\n"""\n')
            sys.path.append(workspace.workspace)
            assert manage.get_pkg_description({'name': 'foo'
                                               }) == '\nFOO\n\n\nCHANGES\n'
Ejemplo n.º 6
0
def setup():
    """ Mirror pkglib's setup() method for each sub-package in this repository.
    """
    top_level_parser = config.parse.get_pkg_cfg_parser()
    cfg = config._parse_metadata(top_level_parser, 'multipkg', ['pkg_dirs'])
    rc = [0]
    for dirname in cfg['pkg_dirs']:
        with manage.chdir(dirname):
            # Update sub-package setup.cfg with top-level version
            sub_parser = config.parse.get_pkg_cfg_parser()
            sub_cfg = config.parse_pkg_metadata(sub_parser)
            if sub_cfg['version'] != cfg['version']:
                print ("Updating setup.cfg version for {0}: {1} -> {2}"
                       .format(dirname, sub_cfg['version'], cfg['version']))
                sub_parser.set('metadata', 'version', cfg['version'])
                with open('setup.cfg', 'w') as sub_cfg_file:
                    sub_parser.write(sub_cfg_file)

            cmd = [sys.executable, "setup.py"] + sys.argv[1:]
            print ("In directory {0}: Running '{1}'"
                   .format(dirname, ' '.join(cmd)))
            p = subprocess.Popen(cmd)
            p.communicate()
            if p.returncode != 0:
                # Here we exit straight away, unless this was a run as
                # 'python setup.py test'. Reason for this is that we want to
                # run all the packages' tests through and gather the results.
                # Exception: using the -x/--exitfirst option.
                # For any other setup.py command, a failure here is likely
                # some sort of build or config issue and it's best not to
                # plow on.
                print "Command failed with exit code {0}".format(p.returncode)
                if 'test' in cmd and not '-x' in ' '.join(cmd)  \
                                and not '--exitfirst' in ' '.join(cmd):
                    rc[0] = p.returncode
                else:
                    sys.exit(p.returncode)
    sys.exit(rc[0])
Ejemplo n.º 7
0
 def create_relative_link(self, src, dest):
     """ Create relative symlink from src -> dest
     """
     with manage.chdir(os.path.dirname(src)):
         os.symlink(dest, os.path.basename(src))
Ejemplo n.º 8
0
 def check_and_remove(adir):
     with chdir(adir):
         for name in names:
             for item in glob.iglob(name):
                 self.remove_object(adir, item)
Ejemplo n.º 9
0
def test_chdir():
    here = os.getcwd()
    with manage.chdir('/bin'):
        assert os.getcwd() == '/bin'
    assert os.getcwd() == here