Exemplo n.º 1
0
def chutify(arguments):
    """
    Usage: %prog [-d DIRNAME] <location> [<MODULE>...]
           %prog <location> (-l | -h)

    Generate binary scripts from all @console_script contained in <location>
    <location> can be a directory, a python file or a dotted name.

    -h, --help                         Print this help
    -d DIRNAME, --destination=DIRNAME  Destination [default: dist/scripts]
    -l, --list-entry-points            List console script entry points
    """
    location = arguments['<location>']
    location = os.path.expanduser(location)
    if os.path.isfile(location):
        generate(arguments, location)
    elif os.path.isdir(location):
        filenames = []
        filenames = sh.grep('-lRE --include=*.py @.*console_script',
                            location) | sh.grep('-v site-packages')
        filenames = [s.strip() for s in filenames]
        for filename in filenames:
            generate(arguments, filename)
    else:
        generate(arguments, location)
Exemplo n.º 2
0
 def test_slices(self):
     pipe = sh.cat('tmp') | sh.grep('tmp') | sh.wc('-l')
     self.assertEqual(pipe[0:1]._binary, 'cat')
     self.assertEqual(pipe.__getitem__(slice(0, 1))._binary, 'cat')
     self.assertEqual(pipe.__getslice__(0, 1)._binary, 'cat')
     self.assertEqual(pipe[1:]._binary, 'wc')
     self.assertEqual(pipe.__getitem__(slice(1, 3))._binary, 'wc')
     self.assertEqual(pipe.__getslice__(1, 3)._binary, 'wc')
Exemplo n.º 3
0
def chutify(args):
    """
    Usage: %prog [options] [<location>]

    Generate binary scripts from all @console_script contained in <location>
    <location> can be a directory, a python file or a dotted name.

    If <location> is .git then generate a pre-commit hook which generate script
    from the current directory.

    <location> and --destination can be set in a .chut file:

        [chut]
        destination = bin/
        location = scripts

    Options:

    -l, --loop                    Generate scripts when the source change
    -s NAME, --section NAME       Use NAME section in .chut [default: chut]
    --devel                       Install develop scripts in bin/
    --upgrade                     Upgrade virtualenv dependencies
    -d DIR, --destination=DIR     Destination [default: dist/scripts]
    -i X, --interpreter=X         Python interpreter to use
    -n X, --new-version=X         Set new scripts version
    %options-30s
    """
    config = sh.ini(".chut")
    if sh.env.git_dir:  # pragma: no cover
        cfg = config["githook"] or config[args["--section"]]
    else:
        cfg = config[args["--section"]]

    interpreter = args["--interpreter"] or cfg.interpreter or "python"
    if interpreter in ("2", "3"):  # pragma: no cover
        interpreter = "python" + interpreter
    args["--interpreter"] = interpreter

    location = args.get("<location>") or cfg.location or os.getcwd()
    location = os.path.expanduser(location)

    if location.endswith(".git"):
        hooks = sh.path.join(location, "hooks")
        hook = sh.path.join(hooks, "pre-commit")
        args["destination"] = hooks
        generator = sh.Generator(**args)
        if not __file__.endswith("chutify"):
            filename = __file__.replace(".pyc", ".py")
            script = generator(filename)[0]
            sh.mv(script, hook)
        else:  # pragma: no cover
            # install git hook
            sh.cp(__file__, hook)
        executable = sh.chmod("+x", hook)
        if executable:
            print(executable.commands_line)
        return 0

    if cfg.destination and args["--destination"] == "dist/scripts":
        args["--destination"] = cfg.destination

    if os.path.isfile("setup.py"):
        version = sh.grep("-E ^version setup.py")
        if version:
            version = str(version).split("=")[1].strip("'\" ")
            if "dev" in version:
                version = version.split("dev")[0].strip(".")
            args["version"] = version

    generator = sh.Generator(**args)

    commands = cfg.run.as_list("\n")
    commands = [c.strip() for c in commands if c.strip()]

    def gen():
        scripts = generator(location)
        for cmd in commands:
            print("$ %s" % cmd)
            if " " in cmd:
                binary, args = cmd.split(" ", 1)
                sh[binary](args) > 2
            else:
                sh[cmd]() > 2
        return scripts

    if args["--loop"]:  # pragma: no cover
        import time

        while True:
            try:
                gen()
                time.sleep(0.1)
            except KeyboardInterrupt:
                return 0
    else:
        scripts = gen()
        if sh.env.git_dir:  # pragma: no cover
            sh.git("add -f", *scripts) > 1
    return 0