Example #1
0
def clean():
    """Remove the hitch directory entirely."""
    if hitchdir.hitch_exists():
        hitchdir.remove_hitch_directory_if_exists()
    else:
        stderr.write("No hitch directory found. Doing nothing.\n")
        stderr.flush()
Example #2
0
def init():
    """Initialize hitch in this directory."""
    if call(["which", "virtualenv"], stdout=PIPE, stderr=PIPE):
        stderr.write("You must have python-virtualenv installed to use hitch.\n")
        stderr.flush()
        exit(1)

    if call(["which", "python3"], stdout=PIPE, stderr=PIPE):
        stderr.write("To use Hitch, you must have python 3 installed and available on the system path with the name 'python3'.\n")
        stderr.flush()
        exit(1)

    python3 = check_output(["which", "python3"]).decode('utf8').replace("\n", "")

    if hitchdir.hitch_exists():
        stderr.write("Hitch has already been initialized in this directory or a directory above it.\n")
        stderr.write("If you wish to re-initialize hitch in this directory, delete the .hitch directory and run hitch init here again.\n")
        stderr.flush()
        exit(1)

    makedirs(".hitch")
    pip = path.abspath(path.join(".hitch", "virtualenv", "bin", "pip"))

    call(["virtualenv", ".hitch/virtualenv", "--no-site-packages", "--distribute", "-p", python3])
    call([pip, "install", "-U", "pip"])

    if path.exists("hitchreqs.txt"):
        call([pip, "install", "-r", "hitchreqs.txt"])
    else:
        call([pip, "install", "hitchtest"])

        pip_freeze = check_output([pip, "freeze"]).decode('utf8')

        with open("hitchreqs.txt", "w") as hitchreqs_handle:
            hitchreqs_handle.write(pip_freeze)
Example #3
0
def clean():
    """Remove the hitch directory entirely."""
    if hitchdir.hitch_exists():
        hitch_directory = hitchdir.get_hitch_directory_or_fail()
        shutil.rmtree(hitch_directory)
    else:
        stderr.write("No hitch directory found. Doing nothing.\n")
        stderr.flush()
Example #4
0
def run():
    """Run hitch bootstrap CLI"""
    def stop_everything(sig, frame):
        """Exit hitch."""
        exit(1)

    signal.signal(signal.SIGINT, stop_everything)
    signal.signal(signal.SIGTERM, stop_everything)
    signal.signal(signal.SIGHUP, stop_everything)
    signal.signal(signal.SIGQUIT, stop_everything)

    if hitchdir.hitch_exists():
        if not path.exists(path.join(hitchdir.get_hitch_directory(), "virtualenv", "bin")):
            stderr.write("Hitch was initialized in this directory (or one above it), but something.\n")
            stderr.write("was corrupted. Try running 'hitch clean' and then run 'hitch init' again.")
            stderr.flush()
            exit(1)

        # Get packages from bin folder that are hitch related
        python_bin = path.join(hitchdir.get_hitch_directory(), "virtualenv", "bin", "python")
        packages = [
            package.replace("hitch", "") for package in listdir(
                path.join(hitchdir.get_hitch_directory(), "virtualenv", "bin")
            )
            if package.startswith("hitch") and package != "hitch"
        ]

        # Add packages that start with hitch* to the list of commands available
        for package in packages:
            cmd = copy.deepcopy(runpackage)
            cmd.name = package
            try:
                description = check_output([
                    python_bin, '-c',
                    'import sys;sys.stdout.write(__import__("hitch{}").commandline.cli.help)'.format(
                        package
                    )
                ]).decode('utf8')
            except CalledProcessError:
                description = ""
            cmd.help = description
            cmd.short_help = description
            cli.add_command(cmd)


        cli.add_command(install)
        cli.add_command(uninstall)
        cli.add_command(upgrade)
        cli.add_command(clean)
        cli.add_command(freeze)
        cli.add_command(init)
        cli.help = "Hitch test runner for:\n\n  {0}.".format(hitchdir.get_hitch_directory())
    else:
        cli.add_command(init)
        cli.add_command(clean)
        cli.help = "Hitch bootstrapper - '.hitch' directory not detected here."
    cli()
Example #5
0
def run():
    """Run hitch bootstrap CLI"""
    signal.signal(signal.SIGINT, stop_everything)
    signal.signal(signal.SIGTERM, stop_everything)
    signal.signal(signal.SIGHUP, stop_everything)
    signal.signal(signal.SIGQUIT, stop_everything)

    if hitchdir.hitch_exists():
        # Get packages from bin folder that are hitch related
        python_bin = path.join(hitchdir.get_hitch_directory(), "virtualenv",
                               "bin", "python")
        if path.exists(python_bin):
            packages = [
                package.replace("hitch", "") for package in listdir(
                    path.join(hitchdir.get_hitch_directory(), "virtualenv",
                              "bin"))
                if package.startswith("hitch") and package != "hitch"
            ]

            # Add commands that start with "hitch" to the list of commands available (e.g. hitchtest, hitchsmtp)
            for package in packages:
                cmd = copy.deepcopy(runpackage)
                cmd.name = package
                try:
                    description = check_output([
                        python_bin, '-c',
                        'import sys;sys.stdout.write(__import__("hitch{0}").commandline.cli.help)'
                        .format(package)
                    ]).decode('utf8')
                except CalledProcessError:
                    description = ""
                cmd.help = description
                cmd.short_help = description
                cli.add_command(cmd)

            cli.add_command(install)
            cli.add_command(uninstall)
            cli.add_command(upgrade)
            cli.add_command(freeze)
        else:
            stderr.write(languagestrings.SOMETHING_CORRUPTED)

        cli.add_command(clean)
        cli.add_command(cleanpkg)
        cli.add_command(init)
        cli.help = "Hitch test runner for:\n\n  {0}.".format(
            hitchdir.get_hitch_directory())
    else:
        cli.add_command(init)
        cli.add_command(clean)
        cli.add_command(cleanpkg)
        cli.help = "Hitch bootstrapper - '.hitch' directory not detected here."
    cli()
Example #6
0
def init(python, virtualenv):
    """Initialize hitch in this directory."""
    if virtualenv is None:
        if call(["which", "virtualenv"], stdout=PIPE, stderr=PIPE) != 0:
            stderr.write(languagestrings.YOU_MUST_HAVE_VIRTUALENV_INSTALLED)
            stderr.flush()
            exit(1)
        virtualenv = check_output(["which", "virtualenv"
                                   ]).decode('utf8').replace("\n", "")
    else:
        if path.exists(virtualenv):
            if python is None:
                python = path.join(path.dirname(virtualenv), "python")
        else:
            stderr.write("{0} not found.\n".format(virtualenv))

    if python is None:
        if call(["which", "python3"], stdout=PIPE, stderr=PIPE) != 0:
            stderr.write(languagestrings.YOU_MUST_HAVE_PYTHON3_INSTALLED)
            stderr.flush()
            exit(1)
        python3 = check_output(["which",
                                "python3"]).decode('utf8').replace("\n", "")
    else:
        if path.exists(python):
            python3 = python
        else:
            stderr.write("{0} not found.\n".format(python))
            exit(1)

    python_version = check_output([python3, "-V"],
                                  stderr=STDOUT).decode('utf8')
    replacements = ('Python ', ''), ('\n', '')
    str_version = reduce(lambda a, kv: a.replace(*kv), replacements,
                         python_version)
    tuple_version = tuple([int(x) for x in str_version.split('.')[:2]])

    if tuple_version < (3, 3):
        stderr.write(languagestrings.YOU_MUST_HAVE_VERSION_ABOVE_PYTHON33)
        exit(1)

    if hitchdir.hitch_exists():
        stderr.write(languagestrings.HITCH_ALREADY_INITIALIZED)
        stderr.flush()
        exit(0)

    makedirs(".hitch")

    # Store absolute directory in .hitch directory to guard against the directory being moved
    hitch_dir = path.abspath(".hitch")
    with open(path.join(hitch_dir, "absdir"), "w") as absdir_handle:
        absdir_handle.write(hitch_dir)

    pip = path.abspath(path.join(".hitch", "virtualenv", "bin", "pip"))

    try:
        check_call([
            virtualenv, ".hitch/virtualenv", "--no-site-packages",
            "--distribute", "-p", python3
        ])
        check_call([pip, "install", "-U", "pip"])
        check_call([pip, "install", "unixpackage", "hitchsystem"])

        installpackages()

        if path.exists("hitchreqs.txt"):
            check_call([pip, "install", "-r", "hitchreqs.txt"])
        else:
            check_call([pip, "install", "hitchtest"])

            pip_freeze = check_output([pip, "freeze"]).decode('utf8')

            with open("hitchreqs.txt", "w") as hitchreqs_handle:
                hitchreqs_handle.write(pip_freeze)

        installpackages()
    except CalledProcessError:
        stderr.write(languagestrings.ERROR_INITIALIZING_HITCH)
        hitchdir.remove_hitch_directory_if_exists()
        exit(1)
Example #7
0
def init(python, virtualenv):
    """Initialize hitch in this directory."""
    if virtualenv is None:
        if call(["which", "virtualenv"], stdout=PIPE, stderr=PIPE):
            stderr.write("You must have virtualenv installed to use hitch.\n")
            stderr.flush()
            exit(1)
        virtualenv = check_output(["which", "virtualenv"]).decode('utf8').replace("\n", "")
    else:
        if path.exists(virtualenv):
            if python is None:
                python = path.join(path.dirname(virtualenv), "python")
        else:
            stderr.write("{} not found.\n".format(virtualenv))

    if python is None:
        if call(["which", "python3"], stdout=PIPE, stderr=PIPE):
            stderr.write(
                "To use Hitch, you must have python 3 installed on your system "
                "and available. If your python3 is not on the system path with "
                "the name python3, specify its exact location using --python.\n"
            )
            stderr.flush()
            exit(1)
        python3 = check_output(["which", "python3"]).decode('utf8').replace("\n", "")
    else:
        if path.exists(python):
            python3 = python
        else:
            stderr.write("{} not found.\n".format(python))
            exit(1)

    str_version = check_output([python3, "-V"], stderr=STDOUT).decode('utf8').replace('\n', '')
    tuple_version = tuple([int(v) for v in str_version.replace('Python ', '').split('.')])

    if tuple_version < (3, 3):
        stderr.write(
            "The hitch environment must have python >=3.3 installed to be built.\n Your "
            "app can run with earlier versions of python, but the testing environment can't.\n"
        )
        exit(1)

    if hitchdir.hitch_exists():
        stderr.write("Hitch has already been initialized in this directory or a directory above it.\n")
        stderr.write("If you wish to re-initialize hitch in this directory, run 'hitch clean' in the")
        stderr.write("directory containing the .hitch directory and run hitch init here again.\n")
        stderr.flush()
        exit(1)

    makedirs(".hitch")
    pip = path.abspath(path.join(".hitch", "virtualenv", "bin", "pip"))

    call([virtualenv, ".hitch/virtualenv", "--no-site-packages", "--distribute", "-p", python3])
    call([pip, "install", "-U", "pip"])

    if path.exists("hitchreqs.txt"):
        call([pip, "install", "-r", "hitchreqs.txt"])
    else:
        call([pip, "install", "hitchtest"])

        pip_freeze = check_output([pip, "freeze"]).decode('utf8')

        with open("hitchreqs.txt", "w") as hitchreqs_handle:
            hitchreqs_handle.write(pip_freeze)