Ejemplo n.º 1
0
def install_environment(repo_cmd_runner, version, additional_dependencies):
    helpers.assert_version_default('golang', version)
    directory = repo_cmd_runner.path(
        helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
    )

    with clean_path_on_failure(directory):
        remote = git.get_remote_url(repo_cmd_runner.path())
        repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote))

        # Clone into the goenv we'll create
        helpers.run_setup_cmd(
            repo_cmd_runner, ('git', 'clone', '.', repo_src_dir),
        )

        if sys.platform == 'cygwin':  # pragma: no cover
            _, gopath, _ = cmd_output('cygpath', '-w', directory)
            gopath = gopath.strip()
        else:
            gopath = directory
        env = dict(os.environ, GOPATH=gopath)
        cmd_output('go', 'get', './...', cwd=repo_src_dir, env=env)
        for dependency in additional_dependencies:
            cmd_output('go', 'get', dependency, cwd=repo_src_dir, env=env)
        # Same some disk space, we don't need these after installation
        rmtree(repo_cmd_runner.path(directory, 'src'))
        rmtree(repo_cmd_runner.path(directory, 'pkg'))
Ejemplo n.º 2
0
 def delete_repo(self, db_repo_name, ref, path):
     with self.connect() as db:
         db.execute(
             'DELETE FROM repos WHERE repo = ? and ref = ?',
             (db_repo_name, ref),
         )
     rmtree(path)
Ejemplo n.º 3
0
def clean(store):
    legacy_path = os.path.expanduser('~/.pre-commit')
    for directory in (store.directory, legacy_path):
        if os.path.exists(directory):
            rmtree(directory)
            output.write_line('Cleaned {}.'.format(directory))
    return 0
Ejemplo n.º 4
0
def clean(store: Store) -> int:
    legacy_path = os.path.expanduser('~/.pre-commit')
    for directory in (store.directory, legacy_path):
        if os.path.exists(directory):
            rmtree(directory)
            output.write_line(f'Cleaned {directory}.')
    return 0
Ejemplo n.º 5
0
def install_environment(prefix, version, additional_dependencies):
    helpers.assert_version_default('golang', version)
    directory = prefix.path(
        helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT), )

    with clean_path_on_failure(directory):
        remote = git.get_remote_url(prefix.prefix_dir)
        repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote))

        # Clone into the goenv we'll create
        helpers.run_setup_cmd(prefix, ('git', 'clone', '.', repo_src_dir))

        if sys.platform == 'cygwin':  # pragma: no cover
            _, gopath, _ = cmd_output('cygpath', '-w', directory)
            gopath = gopath.strip()
        else:
            gopath = directory
        env = dict(os.environ, GOPATH=gopath)
        cmd_output('go', 'get', './...', cwd=repo_src_dir, env=env)
        for dependency in additional_dependencies:
            cmd_output('go', 'get', dependency, cwd=repo_src_dir, env=env)
        # Same some disk space, we don't need these after installation
        rmtree(prefix.path(directory, 'src'))
        pkgdir = prefix.path(directory, 'pkg')
        if os.path.exists(pkgdir):  # pragma: no cover (go<1.10)
            rmtree(pkgdir)
Ejemplo n.º 6
0
def _hook_install(hook: Hook) -> None:
    logger.info(f'Installing environment for {hook.src}.')
    logger.info('Once installed this environment will be reused.')
    logger.info('This may take a few minutes...')

    lang = languages[hook.language]
    assert lang.ENVIRONMENT_DIR is not None
    venv = environment_dir(lang.ENVIRONMENT_DIR, hook.language_version)

    # There's potentially incomplete cleanup from previous runs
    # Clean it up!
    if hook.prefix.exists(venv):
        rmtree(hook.prefix.path(venv))

    lang.install_environment(
        hook.prefix,
        hook.language_version,
        hook.additional_dependencies,
    )
    if not lang.healthy(hook.prefix, hook.language_version):
        raise AssertionError(
            f'BUG: expected environment for {hook.language} to be healthy() '
            f'immediately after install, please open an issue describing '
            f'your environment', )
    # Write our state to indicate we're installed
    _write_state(hook.prefix, venv, _state(hook.additional_dependencies))
Ejemplo n.º 7
0
def install_environment(
    prefix: Prefix,
    version: str,
    additional_dependencies: Sequence[str],
) -> None:
    helpers.assert_version_default("golang", version)
    directory = prefix.path(
        helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT), )

    with clean_path_on_failure(directory):
        remote = git.get_remote_url(prefix.prefix_dir)
        repo_src_dir = os.path.join(directory, "src", guess_go_dir(remote))

        # Clone into the goenv we'll create
        helpers.run_setup_cmd(prefix, ("git", "clone", ".", repo_src_dir))

        if sys.platform == "cygwin":  # pragma: no cover
            _, gopath, _ = cmd_output("cygpath", "-w", directory)
            gopath = gopath.strip()
        else:
            gopath = directory
        env = dict(os.environ, GOPATH=gopath)
        env.pop("GOBIN", None)
        cmd_output_b("go", "get", "./...", cwd=repo_src_dir, env=env)
        for dependency in additional_dependencies:
            cmd_output_b("go", "get", dependency, cwd=repo_src_dir, env=env)
        # Same some disk space, we don't need these after installation
        rmtree(prefix.path(directory, "src"))
        pkgdir = prefix.path(directory, "pkg")
        if os.path.exists(pkgdir):  # pragma: no cover (go<1.10)
            rmtree(pkgdir)
Ejemplo n.º 8
0
 def delete_repo(self, db_repo_name, ref, path):
     with self.connect() as db:
         db.execute(
             'DELETE FROM repos WHERE repo = ? and ref = ?',
             (db_repo_name, ref),
         )
     rmtree(path)
Ejemplo n.º 9
0
def install_environment(
        repo_cmd_runner,
        version='default',
        additional_dependencies=(),
):
    helpers.assert_version_default('golang', version)
    directory = repo_cmd_runner.path(
        helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
    )

    with clean_path_on_failure(directory):
        remote = git.get_remote_url(repo_cmd_runner.path())
        repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote))

        # Clone into the goenv we'll create
        helpers.run_setup_cmd(
            repo_cmd_runner, ('git', 'clone', '.', repo_src_dir),
        )

        env = dict(os.environ, GOPATH=directory)
        cmd_output('go', 'get', './...', cwd=repo_src_dir, env=env)
        for dependency in additional_dependencies:
            cmd_output('go', 'get', dependency, cwd=repo_src_dir, env=env)
        # Same some disk space, we don't need these after installation
        rmtree(repo_cmd_runner.path(directory, 'src'))
        rmtree(repo_cmd_runner.path(directory, 'pkg'))
Ejemplo n.º 10
0
def make_archive(name, repo, ref, destdir):
    """Makes an archive of a repository in the given destdir.

    :param text name: Name to give the archive.  For instance foo.  The file
    that is created will be called foo.tar.gz.
    :param text repo: Repository to clone.
    :param text ref: Tag/SHA/branch to check out.
    :param text destdir: Directory to place archives in.
    """
    output_path = os.path.join(destdir, name + '.tar.gz')
    with tmpdir() as tempdir:
        # Clone the repository to the temporary directory
        cmd_output('git', 'clone', repo, tempdir)
        with cwd(tempdir):
            cmd_output('git', 'checkout', ref)

        # We don't want the '.git' directory
        # It adds a bunch of size to the archive and we don't use it at
        # runtime
        rmtree(os.path.join(tempdir, '.git'))

        with tarfile.open(five.n(output_path), 'w|gz') as tf:
            tf.add(tempdir, name)

    return output_path
Ejemplo n.º 11
0
def make_archive(name, repo, ref, destdir):
    """Makes an archive of a repository in the given destdir.

    :param text name: Name to give the archive.  For instance foo.  The file
    that is created will be called foo.tar.gz.
    :param text repo: Repository to clone.
    :param text ref: Tag/SHA/branch to check out.
    :param text destdir: Directory to place archives in.
    """
    output_path = os.path.join(destdir, name + '.tar.gz')
    with tmpdir() as tempdir:
        # Clone the repository to the temporary directory
        cmd_output('git', 'clone', repo, tempdir)
        with cwd(tempdir):
            cmd_output('git', 'checkout', ref)

        # We don't want the '.git' directory
        # It adds a bunch of size to the archive and we don't use it at
        # runtime
        rmtree(os.path.join(tempdir, '.git'))

        with tarfile.open(five.n(output_path), 'w|gz') as tf:
            tf.add(tempdir, name)

    return output_path
Ejemplo n.º 12
0
def test_rmtree_read_only_directories(tmpdir):
    """Simulates the go module tree.  See #1042"""
    tmpdir.join('x/y/z').ensure_dir().join('a').ensure()
    mode = os.stat(str(tmpdir.join('x'))).st_mode
    mode_no_w = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
    tmpdir.join('x/y/z').chmod(mode_no_w)
    tmpdir.join('x/y/z').chmod(mode_no_w)
    tmpdir.join('x/y/z').chmod(mode_no_w)
    rmtree(str(tmpdir.join('x')))
Ejemplo n.º 13
0
def test_store_require_created_does_not_create_twice(store):
    assert not os.path.exists(store.directory)
    store.require_created()
    # We intentionally delete the directory here so we can figure out if it
    # calls it again.
    rmtree(store.directory)
    assert not os.path.exists(store.directory)
    # Call require_created, this should not trigger a call to create
    store.require_created()
    assert not os.path.exists(store.directory)
Ejemplo n.º 14
0
def install_environment(
    prefix: Prefix,
    version: str,
    additional_dependencies: Sequence[str],
) -> None:
    helpers.assert_version_default('dotnet', version)
    helpers.assert_no_additional_deps('dotnet', additional_dependencies)

    envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
    with clean_path_on_failure(envdir):
        build_dir = 'pre-commit-build'

        # Build & pack nupkg file
        helpers.run_setup_cmd(
            prefix,
            (
                'dotnet',
                'pack',
                '--configuration',
                'Release',
                '--output',
                build_dir,
            ),
        )

        # Determine tool from the packaged file <tool_name>.<version>.nupkg
        build_outputs = os.listdir(os.path.join(prefix.prefix_dir, build_dir))
        if len(build_outputs) != 1:
            raise NotImplementedError(
                f"Can't handle multiple build outputs. Got {build_outputs}", )
        tool_name = build_outputs[0].split('.')[0]

        # Install to bin dir
        helpers.run_setup_cmd(
            prefix,
            (
                'dotnet',
                'tool',
                'install',
                '--tool-path',
                os.path.join(envdir, BIN_DIR),
                '--add-source',
                build_dir,
                tool_name,
            ),
        )

        # Cleanup build output
        for d in ('bin', 'obj', build_dir):
            rmtree(prefix.path(d))
Ejemplo n.º 15
0
def install_environment(
    prefix: Prefix,
    version: str,
    additional_dependencies: Sequence[str],
) -> None:
    additional_dependencies = tuple(additional_dependencies)
    assert prefix.exists('package.json')
    envdir = _envdir(prefix, version)

    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx?f=255&MSPPError=-2147217396#maxpath
    if sys.platform == 'win32':  # pragma: no cover
        envdir = fr'\\?\{os.path.normpath(envdir)}'
    with clean_path_on_failure(envdir):
        cmd = [
            sys.executable,
            '-mnodeenv',
            '--prebuilt',
            '--clean-src',
            envdir,
        ]
        if version != C.DEFAULT:
            cmd.extend(['-n', version])
        cmd_output_b(*cmd)

        with in_env(prefix, version):
            # https://npm.community/t/npm-install-g-git-vs-git-clone-cd-npm-install-g/5449
            # install as if we installed from git

            local_install_cmd = (
                'npm',
                'install',
                '--dev',
                '--prod',
                '--ignore-prepublish',
                '--no-progress',
                '--no-save',
            )
            helpers.run_setup_cmd(prefix, local_install_cmd)

            _, pkg, _ = cmd_output('npm', 'pack', cwd=prefix.prefix_dir)
            pkg = prefix.path(pkg.strip())

            install = ('npm', 'install', '-g', pkg, *additional_dependencies)
            helpers.run_setup_cmd(prefix, install)

            # clean these up after installation
            if prefix.exists('node_modules'):  # pragma: win32 no cover
                rmtree(prefix.path('node_modules'))
            os.remove(pkg)
Ejemplo n.º 16
0
    def install(self):
        logger.info('Installing environment for {}.'.format(self.src))
        logger.info('Once installed this environment will be reused.')
        logger.info('This may take a few minutes...')

        lang = languages[self.language]
        venv = environment_dir(lang.ENVIRONMENT_DIR, self.language_version)

        # There's potentially incomplete cleanup from previous runs
        # Clean it up!
        if self.prefix.exists(venv):
            rmtree(self.prefix.path(venv))

        lang.install_environment(
            self.prefix, self.language_version, self.additional_dependencies,
        )
        # Write our state to indicate we're installed
        _write_state(self.prefix, venv, _state(self.additional_dependencies))
Ejemplo n.º 17
0
    def install(self):
        logger.info('Installing environment for {}.'.format(self.src))
        logger.info('Once installed this environment will be reused.')
        logger.info('This may take a few minutes...')

        lang = languages[self.language]
        venv = environment_dir(lang.ENVIRONMENT_DIR, self.language_version)

        # There's potentially incomplete cleanup from previous runs
        # Clean it up!
        if self.prefix.exists(venv):
            rmtree(self.prefix.path(venv))

        lang.install_environment(
            self.prefix, self.language_version, self.additional_dependencies,
        )
        # Write our state to indicate we're installed
        _write_state(self.prefix, venv, _state(self.additional_dependencies))
Ejemplo n.º 18
0
def _hook_install(hook: Hook) -> None:
    logger.info(f"Installing environment for {hook.src}.")
    logger.info("Once installed this environment will be reused.")
    logger.info("This may take a few minutes...")

    lang = languages[hook.language]
    assert lang.ENVIRONMENT_DIR is not None
    venv = environment_dir(lang.ENVIRONMENT_DIR, hook.language_version)

    # There's potentially incomplete cleanup from previous runs
    # Clean it up!
    if hook.prefix.exists(venv):
        rmtree(hook.prefix.path(venv))

    lang.install_environment(
        hook.prefix, hook.language_version, hook.additional_dependencies,
    )
    # Write our state to indicate we're installed
    _write_state(hook.prefix, venv, _state(hook.additional_dependencies))
Ejemplo n.º 19
0
def make_archive(name: str, repo: str, ref: str, destdir: str) -> str:
    """Makes an archive of a repository in the given destdir.

    :param text name: Name to give the archive.  For instance foo.  The file
    that is created will be called foo.tar.gz.
    :param text repo: Repository to clone.
    :param text ref: Tag/SHA/branch to check out.
    :param text destdir: Directory to place archives in.
    """
    output_path = os.path.join(destdir, f"{name}.tar.gz")
    with tmpdir() as tempdir:
        # Clone the repository to the temporary directory
        cmd_output_b("git", "clone", repo, tempdir)
        cmd_output_b("git", "checkout", ref, cwd=tempdir)

        # We don't want the '.git' directory
        # It adds a bunch of size to the archive and we don't use it at
        # runtime
        rmtree(os.path.join(tempdir, ".git"))

        with tarfile.open(output_path, "w|gz") as tf:
            tf.add(tempdir, name)

    return output_path
Ejemplo n.º 20
0
def clean(runner):
    if os.path.exists(runner.store.directory):
        rmtree(runner.store.directory)
        print('Cleaned {0}.'.format(runner.store.directory))
    return 0
Ejemplo n.º 21
0
def clean(runner):
    if os.path.exists(runner.store.directory):
        rmtree(runner.store.directory)
        print('Cleaned {}.'.format(runner.store.directory))
    return 0
Ejemplo n.º 22
0
def clean(runner):
    if os.path.exists(runner.store.directory):
        rmtree(runner.store.directory)
        output.write_line('Cleaned {}.'.format(runner.store.directory))
    return 0
Ejemplo n.º 23
0
 def delete_repo(self, db_repo_name: str, ref: str, path: str) -> None:
     with self.connect() as db:
         db.execute(
             "DELETE FROM repos WHERE repo = ? and ref = ?", (db_repo_name, ref),
         )
     rmtree(path)
Ejemplo n.º 24
0
def test_clean_empty(runner_with_mocked_store):
    """Make sure clean succeeds when we the directory doesn't exist."""
    rmtree(runner_with_mocked_store.store.directory)
    assert not os.path.exists(runner_with_mocked_store.store.directory)
    clean(runner_with_mocked_store)
    assert not os.path.exists(runner_with_mocked_store.store.directory)