def create_inline(self):
        from venv import EnvBuilder

        builder = EnvBuilder(
            system_site_packages=self.enable_system_site_package, clear=False, symlinks=self.symlinks, with_pip=False,
        )
        builder.create(str(self.dest))
def test_install(tmp_path: Path):
    env = EnvBuilder(with_pip=True)
    env.create(env_dir=tmp_path)
    context = env.ensure_directories(tmp_path)
    root = Path(__file__).parent.parent.resolve()
    if sys.platform == "win32":
        wheelpath = list((root / "dist").glob("playwright*win_amd64*.whl"))[0]
    elif sys.platform == "linux":
        wheelpath = list((root / "dist").glob("playwright*manylinux_2_27_*.whl"))[0]
    elif sys.platform == "darwin":
        wheelpath = list((root / "dist").glob("playwright*macosx_10_*.whl"))[0]
    subprocess.check_output([context.env_exe, "-m", "pip", "install", "pip", "-U"])
    subprocess.check_output(
        [
            context.env_exe,
            "-m",
            "pip",
            "install",
            str(wheelpath),
        ]
    )
    environ = os.environ.copy()
    environ["PLAYWRIGHT_BROWSERS_PATH"] = str(tmp_path)
    subprocess.check_output(
        [context.env_exe, "-m", "playwright", "install"], env=environ
    )
    shutil.copyfile(root / "tests" / "assets" / "client.py", tmp_path / "main.py")
    subprocess.check_output([context.env_exe, str(tmp_path / "main.py")], env=environ)
    assert (tmp_path / "chromium.png").exists()
    assert (tmp_path / "firefox.png").exists()
    assert (tmp_path / "webkit.png").exists()
Example #3
0
def _make_venv_at(root: Path, name: str, builder: venv.EnvBuilder):
    venv_path = root / name
    os.makedirs(root, exist_ok=True)
    if venv_path.exists():
        shutil.rmtree(venv_path)

    builder.create(venv_path)
    return venv_path
Example #4
0
def create_env(path, options):

    DIR = os.path.join(path)

    logging.info('creating virtual environment')
    builder = EnvBuilder(**options, with_pip=True)
    builder.create(DIR)

    logging.info('Created virtual environment')
Example #5
0
def main() -> None:
    parser = argparse.ArgumentParser(
        description="Test wheel or source distribution for basic functionality."
    )
    parser.add_argument("pypi", type=str)
    arguments = parser.parse_args()
    pypi = arguments.pypi
    with tempfile.TemporaryDirectory() as temporary_venv:
        venv = Path(temporary_venv)
        builder = EnvBuilder(system_site_packages=False, clear=True, with_pip=True)
        builder.create(venv)

        pyre_path = venv / "bin" / "pyre"

        # Confirm that pypi package can be successfully installed
        subprocess.run([venv / "bin" / "pip", "install", pypi])
        production_assert(pyre_path.exists(), "Pyre was not installed.")

        # Create test project.
        with tempfile.TemporaryDirectory() as temporary_project:
            temporary_project_path = Path(temporary_project)
            python_file_path = temporary_project_path / "a.py"
            python_file_path.touch()
            python_file_path.write_text("# pyre-strict \ndef foo():\n\treturn 1")
            # Confirm we can run `pyre init` successfully.
            init_process = subprocess.run(
                [str(pyre_path), "init"],
                cwd=temporary_project_path,
                input=b"n\n.\n",
                capture_output=True,
            )
            error_message = init_process.stderr.decode()
            production_assert(
                init_process.returncode == 0,
                f"Failed to run `pyre init` successfully: {error_message}",
            )
            validate_configuration(temporary_project_path)

            # Confirm Pyre reports errors as expected.
            result = subprocess.run(
                [pyre_path, "--output=json", "check"],
                capture_output=True,
                cwd=temporary_project_path,
            )
            try:
                errors = json.loads(result.stdout)
            except json.JSONDecodeError:
                error_message = result.stderr
                raise AssertionError(
                    f"Pyre did not successfully finish type checking: {error_message}"
                )
            production_assert(
                errors and errors[0]["name"] == "Missing return annotation",
                "Incorrect pyre errors returned."
                if errors
                else "Expected pyre errors but none returned.",
            )
Example #6
0
def _create_venv(tmp_dir):
    import six
    if six.PY2:
        import os
        os.system('virtualenv {}'.format(tmp_dir))
    else:
        from venv import EnvBuilder
        builder = EnvBuilder()
        builder.create(tmp_dir)
Example #7
0
def build():
    # Create python virtual env
    if not os.path.isdir("venv"):
        print("Creating virtual env --> venv/")
        ebuilder = EnvBuilder(with_pip=True)
        ebuilder.create("venv")

    run("venv\\Scripts\\pip install --ignore-installed -r requirements.txt")

    # Download / build VCPKG environment
    if not os.path.isdir("vcpkg"):
        if args.build_vcpkg:
            print("TODO")
            # git clone vcpkg repo
            # bootstrap
            # install requirements

        else:
            if not os.path.exists("vcpkg-env.zip"):
                print("Downloading %s" % args.vcpkg_archive_url)
                with urllib.request.urlopen(
                        args.vcpkg_archive_url) as response, open(
                            "vcpkg-env.zip", 'wb') as out_file:
                    shutil.copyfileobj(response, out_file)
            if not os.path.exists("vcpkg"):
                print("Extracting vcpkg-env.zip --> vcpkg/")
                with zipfile.ZipFile("vcpkg-env.zip") as z:
                    top_dir = z.namelist()[0]
                    z.extractall(".")

                    if os.path.exists(top_dir):
                        os.rename(top_dir, "vcpkg")
                    else:
                        print(
                            "Warning! Something looks wrong in the VCPKG archive... check the vcpkg/ directory."
                        )
                safe_remove("vcpkg-env.zip")

    if not os.path.exists(os.path.join(
            "SuperBuild", "build")) or not os.path.exists(
                os.path.join("SuperBuild", "install")):
        print("Compiling SuperBuild")

        build_dir = os.path.join("SuperBuild", "build")
        if not os.path.isdir(build_dir):
            os.mkdir(build_dir)

        toolchain_file = os.path.join(os.getcwd(), "vcpkg", "scripts",
                                      "buildsystems", "vcpkg.cmake")
        run("cmake .. -DCMAKE_TOOLCHAIN_FILE=\"%s\"" % toolchain_file,
            cwd=build_dir)
        run("cmake --build . --config Release", cwd=build_dir)
Example #8
0
def new_project(project_name, conf):
    '''
    create the virtual environment for the project in the position defined in
    the configuration file
    '''

    # get the default directory for virtual environments
    dir_gvenv = conf.projects_dir_name
    dir_gvenv_project = path.join(dir_gvenv, project_name)

    env = EnvBuilder()

    # create new directory of the virtual environment
    print(('Generating new environment in {0}'.format(dir_gvenv)))
    env.create(dir_gvenv_project)
    print('done.')
Example #9
0
    def upgrade(self):
        clear, upgrade = self._prepare()

        if upgrade:
            self._update_symlinks(sys.executable)

        builder = EnvBuilder(system_site_packages=False,
                             clear=clear,
                             symlinks=True,
                             upgrade=upgrade,
                             with_pip=True,
                             upgrade_deps=True)
        builder.create(self.venv_path)
        subprocess.run(
            [str(self.venv_path / 'bin' / 'pip'), 'install', '-U', 'wheel'],
            check=True)
Example #10
0
    def build_venv(cls, path, executable=None):
        if executable is not None:
            # Create virtualenv by using an external executable
            try:
                p = subprocess.Popen(" ".join([executable, "-"]),
                                     stdin=subprocess.PIPE,
                                     shell=True)
                p.communicate(encode(CREATE_VENV_COMMAND.format(path)))
            except CalledProcessError as e:
                raise EnvCommandError(e)

            return

        try:
            from venv import EnvBuilder

            # use the same defaults as python -m venv
            if os.name == "nt":
                use_symlinks = False
            else:
                use_symlinks = True

            builder = EnvBuilder(with_pip=True, symlinks=use_symlinks)
            build = builder.create
        except ImportError:
            # We fallback on virtualenv for Python 2.7
            from virtualenv import create_environment

            build = create_environment

        build(path)
Example #11
0
    def run(self):
        """
        Execute command. Create runenv and install dependencies
        """
        if not exists("setup.py"):
            raise ValueError(f"Has to be run from root of the project")
        builder = EnvBuilder(system_site_packages=False,
                             clear=False,
                             symlinks=False,
                             with_pip=False)
        builder.create(self.dir)
        site_packages = (check_output([
            f"{self.dir}/bin/python", "-Ic", "import sys; print(sys.path[-1])"
        ]).decode(locale.getpreferredencoding(False)).strip())

        check_call(["pip", "install", ".", "--target", site_packages])
Example #12
0
def virtualenv_type(none) -> None:
    try:
        from venv import EnvBuilder

        venv_manager = EnvBuilder(
            system_site_packages=False,
            clear=True,
            symlinks=False,
            with_pip=True,
        )
        venv_manager.create(".venv")

        with open(f"requirements.txt", "w") as f:
            f.write(phrases["requirements"])

    except ModuleNotFoundError:
        print(
            "venv module not found. you can install and create yourself manually\n"
        )
Example #13
0
    def _create_venv(self, package: str, system: bool = False) -> str:
        """Create package dedicated virtualenv.

        :param package: Package name
        :type package: str
        :param system: Enable system packages, defaults to False
        :type system: bool, optional
        """

        package = self._normalize_name(package)
        venv_dir = self._venv_dir_path(package)
        # create or update venv
        venv_build = EnvBuilder(system_site_packages=system,
                                clear=False,
                                symlinks=True,
                                with_pip=True)
        if not Path(venv_dir).is_dir():
            venv_build.create(venv_dir)

        return venv_dir
Example #14
0
 def ensure_virtualenv_exists(self, full_rebuild):
     # if we need to create the virtualenv, then we must do that from
     # outside the virtualenv. The code inside this if statement should only
     # be run outside the virtualenv.
     if full_rebuild and path.exists(self.ve_dir):
         shutil.rmtree(self.ve_dir)
     if not path.exists(self.ve_dir):
         if not self.check_current_python_version():
             print(
                 "Running wrong version of python for virtualenv creation")
             return 1
         from venv import EnvBuilder
         env_builder = EnvBuilder(
             system_site_packages=self.use_site_packages,
             clear=True,
             symlinks=True,
             upgrade=False,
             with_pip=True)
         env_builder.create(self.ve_dir)
     return 0
    def install(self):
        if os.path.exists(self.projectPathDict["venvdir"]):
            message = QtWidgets.QMessageBox.information(
                self, "Install", "Virtual environment already installed.")
            return
        reply = QtWidgets.QMessageBox.warning(
            self, "Install",
            "This will install a new virtual environment.\n\nProceed?",
            QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
        if reply == QtWidgets.QMessageBox.Yes:
            if len(self.useData.SETTINGS["InstalledInterpreters"]) == 0:
                message = QtWidgets.QMessageBox.information(
                    self, "Install",
                    "There is no Python installation to install against.\n\nPlease make sure Python is installed."
                )
                return
            if len(self.useData.SETTINGS["InstalledInterpreters"]) == 1:
                pythonPath = self.useData.SETTINGS["InstalledInterpreters"][0]
            else:
                pythonPath = SelectBox(
                    "Choose Python installation",
                    self.useData.SETTINGS["InstalledInterpreters"], self)
                if pythonPath.accepted:
                    pythonPath = pythonPath.item
                else:
                    return
            try:
                builder = EnvBuilder(pythonPath)
                builder.create(self.projectPathDict["venvdir"])
                self.treeView.setModel(self.newFileSystemModel())
                self.treeView.setRootIndex(self.treeView.model().index(
                    self.packagesPath))
                self.currentVersionLabel.setText(self.setVesionFromVenv())

                message = QtWidgets.QMessageBox.information(
                    self, "Install", "Install virtual environment completed.")
            except Exception as err:
                message = QtWidgets.QMessageBox.warning(
                    self, "Failed Install", str(err))
        else:
            return
Example #16
0
    def build_venv(
            cls,
            path,
            executable=None):  # type: (Union[Path,str], Optional[str]) -> ()
        if executable is not None:
            # Create virtualenv by using an external executable
            try:
                p = subprocess.Popen(
                    list_to_shell_command([executable, "-"]),
                    stdin=subprocess.PIPE,
                    shell=True,
                )
                p.communicate(encode(CREATE_VENV_COMMAND.format(path)))
            except CalledProcessError as e:
                raise EnvCommandError(e)

            return

        try:
            from venv import EnvBuilder

            # use the same defaults as python -m venv
            if os.name == "nt":
                use_symlinks = False
            else:
                use_symlinks = True

            builder = EnvBuilder(with_pip=True, symlinks=use_symlinks)
            builder.create(str(path))
        except ImportError:
            try:
                # We fallback on virtualenv for Python 2.7
                from virtualenv import create_environment

                create_environment(str(path))
            except ImportError:
                # since virtualenv>20 we have to use cli_run
                from virtualenv import cli_run

                cli_run([str(path)])
Example #17
0
    def build_venv(cls, path):
        try:
            from venv import EnvBuilder

            builder = EnvBuilder(with_pip=True)
            build = builder.create
        except ImportError:
            # We fallback on virtualenv for Python 2.7
            from virtualenv import create_environment

            build = create_environment

        build(path)
Example #18
0
    def install(self):
        if os.path.exists(self.projectPathDict["venvdir"]):
            message = QtGui.QMessageBox.information(
                self, "Install", "Virtual environment already installed.")
            return
        reply = QtGui.QMessageBox.warning(self, "Install",
                                         "This will install a new virtual environment.\n\nProceed?",
                                         QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            if len(self.useData.SETTINGS["InstalledInterpreters"]) == 0:
                message = QtGui.QMessageBox.information(
                    self, "Install", "There is no Python installation to install against.\n\nPlease make sure Python is installed.")
                return
            if len(self.useData.SETTINGS["InstalledInterpreters"]) == 1:
                pythonPath = self.useData.SETTINGS["InstalledInterpreters"][0]
            else:
                pythonPath = SelectBox(
                    "Choose Python installation", self.useData.SETTINGS["InstalledInterpreters"], self)
                if pythonPath.accepted:
                    pythonPath = pythonPath.item
                else:
                    return
            try:
                builder = EnvBuilder(pythonPath)
                builder.create(self.projectPathDict["venvdir"])
                self.treeView.setModel(self.newFileSystemModel())
                self.treeView.setRootIndex(
                    self.treeView.model().index(self.packagesPath))
                self.currentVersionLabel.setText(self.setVesionFromVenv())

                message = QtGui.QMessageBox.information(
                    self, "Install", "Install virtual environment completed.")
            except Exception as err:
                message = QtGui.QMessageBox.warning(
                    self, "Failed Install", str(err))
        else:
            return
Example #19
0
    def build_venv(cls, path):
        try:
            from venv import EnvBuilder

            # use the same defaults as python -m venv
            if os.name == "nt":
                use_symlinks = False
            else:
                use_symlinks = True

            builder = EnvBuilder(with_pip=True, symlinks=use_symlinks)
            build = builder.create
        except ImportError:
            # We fallback on virtualenv for Python 2.7
            from virtualenv import create_environment

            build = create_environment

        build(path)
Example #20
0
def deep_freeze(requirements: str, output: str) -> None:
    """
    Freeze all dependencies and their dependencies from the given requirements file. Use a
    temporary virtual environment.
    """
    with TemporaryDirectory() as venv_dir:
        EnvBuilder(with_pip=True).create(venv_dir)
        pip_cmd = path.join(venv_dir, 'bin', 'pip')
        p = run([pip_cmd, 'install', '-U', 'pip', '-r', requirements])
        if p.returncode == 0:
            with open(output, 'w') as frozen_requirements:
                frozen_requirements.write(
                    f'# generated by {path.basename(__file__)}\n')
                frozen_requirements.flush()
                p = run([pip_cmd, 'freeze'], stdout=frozen_requirements)
                if p.returncode != 0:
                    print(f'error during dependencies freeze: {p.stderr}')
        else:
            print(f'could not install dependencies: {p.stderr}')
Example #21
0
    def create(self) -> None:
        """Create the virtualenv if it does not already exist.

        Raises
        ------
        subprocess.CalledProcessError
            On failure to install the wheel package or if the command given
            sets the ``check`` argument.
        """
        if self._path.is_dir():
            return
        EnvBuilder(with_pip=True).create(str(self._path))
        env = self._build_env()
        subprocess.run(
            ["pip", "install", "wheel"],
            env=env,
            cwd=str(self._path),
            check=True,
            capture_output=True,
        )
Example #22
0
    s_deps.add_argument("deps", nargs="*", default=())

    return parser.parse_args()


args = parse_args()
command: Union[Literal["deps"], Literal["run"]] = args.command

if command == "deps":
    deps: Sequence[str] = args.deps

    if not deps or "runtime" in deps:
        builder = EnvBuilder(
            system_site_packages=False,
            with_pip=True,
            upgrade=True,
            symlinks=True,
            clear=True,
        )
        builder.create(RT_DIR)
        check_call((
            RT_PY,
            "-m",
            "pip",
            "install",
            "--upgrade",
            "--requirement",
            REQUIREMENTS,
        ))

    if not deps or "packages" in deps:
Example #23
0
def create_venv(dir: str):

    virtualenv = EnvBuilder(with_pip=True)
    virtualenv.create(f"./{dir}")
Example #24
0
    def get_or_create_venv(self, path: Path) -> Path:
        """
        Gets the location of  the virtualenv from :meth:`get_virtualenv_path`, checks if it exists already,
        creates it and installs requirements otherwise. The virtualenvs are stored in a folder based
        on the :class:`Arca` ``base_dir`` setting.

        :param path: :class:`Path <pathlib.Path>` to the cloned repository.
        """
        requirements_option, requirements_hash = self.get_requirements_information(
            path)

        venv_path = self.get_virtualenv_path(requirements_option,
                                             requirements_hash)

        if not venv_path.exists():
            logger.info(f"Creating a venv in {venv_path}")
            builder = EnvBuilder(with_pip=True)
            builder.create(venv_path)

            shell = False
            cmd = None
            cwd = None

            if requirements_option == RequirementsOptions.pipfile:
                cmd = [
                    "source", (str(venv_path / "bin" / "activate")), "&&",
                    "pipenv", "install", "--deploy", "--ignore-pipfile"
                ]

                cmd = " ".join(cmd)

                cwd = path / self.pipfile_location
                shell = True
            elif requirements_option == RequirementsOptions.requirements_txt:
                requirements_file = path / self.requirements_location

                logger.debug("Requirements file:")
                logger.debug(requirements_file.read_text())
                logger.info("Installing requirements from %s",
                            requirements_file)

                cmd = [
                    str(venv_path / "bin" / "python3"), "-m", "pip", "install",
                    "-r",
                    shlex.quote(str(requirements_file))
                ]

            if cmd is not None:
                logger.info("Running Popen cmd %s, with shell %s", cmd, shell)

                process = subprocess.Popen(cmd,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE,
                                           shell=shell,
                                           cwd=cwd)

                try:
                    out_stream, err_stream = process.communicate(
                        timeout=self.requirements_timeout)
                except subprocess.TimeoutExpired:
                    process.kill()
                    logger.warning(
                        "The install command timed out, deleting the virtualenv"
                    )
                    shutil.rmtree(str(venv_path), ignore_errors=True)

                    raise BuildTimeoutError(
                        f"Installing of requirements timeouted after "
                        f"{self.requirements_timeout} seconds.")

                out_stream = out_stream.decode("utf-8")
                err_stream = err_stream.decode("utf-8")

                logger.debug("Return code is %s", process.returncode)
                logger.debug(out_stream)
                logger.debug(err_stream)

                if process.returncode:
                    logger.warning(
                        "The install command failed, deleting the virtualenv")
                    shutil.rmtree(str(venv_path), ignore_errors=True)
                    raise BuildError("Unable to install requirements.txt",
                                     extra_info={
                                         "out_stream": out_stream,
                                         "err_stream": err_stream,
                                         "returncode": process.returncode
                                     })

            else:
                logger.info(
                    "Requirements file not present in repo, empty venv it is.")
        else:
            logger.info(f"Venv already exists in {venv_path}")

        return venv_path
Example #25
0
def build_env(path, **kwargs):
    print(f'building new virtualenv in at : {path} ...')
    builder = EnvBuilder(**kwargs)
    builder.create(path)
def main() -> None:
    parser = argparse.ArgumentParser(
        description="Test wheel or source distribution for basic functionality."
    )
    parser.add_argument("pypi", type=str)
    arguments = parser.parse_args()
    pypi = arguments.pypi
    with tempfile.TemporaryDirectory() as temporary_venv:
        venv = Path(temporary_venv)
        builder = EnvBuilder(system_site_packages=False, clear=True, with_pip=True)
        builder.create(venv)

        pyre_path = venv / "bin" / "pyre"

        # Confirm that pypi package can be successfully installed
        subprocess.run([venv / "bin" / "pip", "install", pypi])
        production_assert(pyre_path.exists(), "Pyre was not installed.")

        # Create test project.
        with tempfile.TemporaryDirectory() as temporary_project:
            temporary_project_path = Path(temporary_project)
            python_file_path = temporary_project_path / "a.py"
            python_file_path.touch()
            python_file_path.write_text("# pyre-strict \ndef foo():\n\treturn 1")
            # Confirm we can run `pyre init` successfully.
            subprocess.run(
                [str(pyre_path), "init"], cwd=temporary_project_path, input=b"n\n.\n"
            )
            configuration = json.loads(
                (temporary_project_path / ".pyre_configuration").read_text()
            )
            print(json.dumps(configuration, indent=2))

            # Confirm configuration contains actual typeshed and taint files.
            typeshed_path = Path(configuration["typeshed"])
            taint_path = Path(configuration["taint_models_path"])
            binary = Path(configuration["binary"])

            production_assert(taint_path.is_dir(), "Taint path is not a directory.")
            production_assert(
                (taint_path / "taint.config").is_file(), "Taint config is not included."
            )

            production_assert(typeshed_path.is_dir(), "Typeshed was not installed.")
            production_assert(
                (typeshed_path / "third_party").is_dir(),
                "`third_party` was not included in typeshed.",
            )
            production_assert(
                (typeshed_path / "stdlib").is_dir(),
                "`stdlib` was not included in typeshed.",
            )

            production_assert(
                binary.is_file(), "Binary was not included in pypi package."
            )

            # Confirm Pyre reports errors as expected.
            result = subprocess.run(
                [pyre_path, "--output=json", "check"],
                capture_output=True,
                cwd=temporary_project_path,
            )
            errors = json.loads(result.stdout)
            production_assert(
                errors[0]["name"] == "Missing return annotation",
                "Incorrect pyre error returned.",
            )
Example #27
0
_IN_VENV = _RT_PY == _EXEC_PATH


if command == "deps":
    assert not _IN_VENV

    io_out = StringIO()
    try:
        from venv import EnvBuilder

        print("...", flush=True)
        with redirect_stdout(io_out), redirect_stderr(io_out):
            EnvBuilder(
                system_site_packages=False,
                with_pip=True,
                upgrade=True,
                symlinks=not IS_WIN,
                clear=True,
            ).create(_RT_DIR)
    except (ImportError, CalledProcessError):
        msg = "Please install python3-venv separately. (apt, yum, apk, etc)"
        io_out.seek(0)
        print(msg, io_out.read(), file=stderr)
        exit(1)
    else:
        proc = run(
            (
                _RT_PY,
                "-m",
                "pip",
                "install",
Example #28
0
def run_sanity_test(version: str, use_wheel: bool) -> None:
    message = "wheel" if use_wheel else "source distribution"
    print(f"Sanity testing {message}")
    with tempfile.TemporaryDirectory() as temporary_venv:
        venv = Path(temporary_venv)
        builder = EnvBuilder(system_site_packages=False,
                             clear=True,
                             with_pip=True)
        builder.create(venv)

        pyre_path = venv / "bin" / "pyre"
        pyre_upgrade_path = venv / "bin" / "pyre-upgrade"

        # Confirm that pypi package can be successfully installed
        wheel_flag = "--only-binary" if use_wheel else "--no-binary"
        subprocess.run([
            venv / "bin" / "pip",
            "install",
            "--index-url",
            "https://test.pypi.org/simple/",
            "--extra-index-url",
            "https://pypi.org/simple",
            wheel_flag,
            "pyre-check",
            f"pyre-check=={version}",
        ])
        production_assert(pyre_path.exists(), "Pyre was not installed.")
        production_assert(pyre_upgrade_path.exists(),
                          "Pyre upgrade was not installed.")

        # Create test project.
        with tempfile.TemporaryDirectory() as temporary_project:
            temporary_project_path = Path(temporary_project)
            python_file_path = temporary_project_path / "a.py"
            python_file_path.touch()
            python_file_path.write_text(
                "# pyre-strict \ndef foo():\n\treturn 1")
            # Confirm we can run `pyre init` successfully.
            init_process = subprocess.run(
                [str(pyre_path), "init"],
                cwd=temporary_project_path,
                input=b"n\n.\n",
                capture_output=True,
            )
            error_message = init_process.stderr.decode()
            production_assert(
                init_process.returncode == 0,
                f"Failed to run `pyre init` successfully: {error_message}",
            )
            validate_configuration(temporary_project_path)

            # Confirm `pyre` reports errors as expected.
            result = subprocess.run(
                [pyre_path, "--output=json", "check"],
                capture_output=True,
                cwd=temporary_project_path,
            )
            try:
                errors = json.loads(result.stdout)
            except json.JSONDecodeError:
                error_message = result.stderr
                raise AssertionError(
                    f"Pyre did not successfully finish type checking: {error_message}"
                )
            production_assert(
                errors and errors[0]["name"] == "Missing return annotation",
                "Incorrect pyre errors returned."
                if errors else "Expected pyre errors but none returned.",
            )

            # Confirm `pyre-upgrade` runs successfully.
            upgrade_process = subprocess.run(
                [str(pyre_upgrade_path), "fixme"],
                cwd=temporary_project_path,
                input=b"[]",
                capture_output=True,
            )
            error_message = upgrade_process.stderr.decode()
            production_assert(
                upgrade_process.returncode == 0,
                f"Failed to run `pyre-upgrade` successfully: {error_message}",
            )
Example #29
0
    def get_or_create_venv(self, path: Path) -> Path:
        """
        Gets the name of the virtualenv from :meth:`get_virtualenv_name`, checks if it exists already,
        creates it and installs requirements otherwise. The virtualenvs are stored in a folder based
        on the :class:`Arca` ``base_dir`` setting.

        :param path: :class:`Path <pathlib.Path>` to the cloned repository.
        """
        requirements_file = self.get_requirements_file(path)
        venv_name = self.get_virtualenv_name(requirements_file)

        venv_path = Path(self._arca.base_dir) / "venvs" / venv_name

        if not venv_path.exists():
            logger.info(f"Creating a venv in {venv_path}")
            builder = EnvBuilder(with_pip=True)
            builder.create(venv_path)

            if requirements_file is not None:

                logger.debug("Requirements file:")
                logger.debug(requirements_file.read_text())
                logger.info("Installing requirements from %s",
                            requirements_file)

                process = subprocess.Popen([
                    str(venv_path / "bin" / "python3"), "-m", "pip", "install",
                    "-r",
                    str(requirements_file)
                ],
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE)

                try:
                    out_stream, err_stream = process.communicate(
                        timeout=self.requirements_timeout)
                except subprocess.TimeoutExpired:
                    process.kill()
                    shutil.rmtree(venv_path, ignore_errors=True)

                    raise BuildTimeoutError(
                        f"Installing of requirements timeouted after "
                        f"{self.requirements_timeout} seconds.")

                out_stream = out_stream.decode("utf-8")
                err_stream = err_stream.decode("utf-8")

                logger.debug("Return code is %s", process.returncode)
                logger.debug(out_stream)
                logger.debug(err_stream)

                if process.returncode:
                    venv_path.rmdir()
                    raise BuildError("Unable to install requirements.txt",
                                     extra_info={
                                         "out_stream": out_stream,
                                         "err_stream": err_stream,
                                         "returncode": process.returncode
                                     })

            else:
                logger.info(
                    "Requirements file not present in repo, empty venv it is.")
        else:
            logger.info(f"Venv already eixsts in {venv_path}")

        return venv_path
Example #30
0
    def create(cls, io, name=None) -> 'Venv':
        if 'VIRTUAL_ENV' not in os.environ:
            # Not in a virtualenv
            # Checking if we need to create one
            config = Config.create('config.toml')

            create_venv = config.setting('settings.virtualenvs.create')

            venv_path = config.setting('settings.virtualenvs.path')
            if venv_path is None:
                venv_path = Path(CACHE_DIR) / 'virtualenvs'
            else:
                venv_path = Path(venv_path)

            if not name:
                name = Path.cwd().name

            name = f'{name}-py{".".join([str(v) for v in sys.version_info[:2]])}'

            venv = venv_path / name
            if not venv.exists():
                if create_venv is False:
                    io.writeln(
                        '<fg=black;bg=yellow>'
                        'Skipping virtualenv creation, '
                        'as specified in config file.'
                        '</>'
                    )

                    return cls()

                io.writeln(
                    f'Creating virtualenv <info>{name}</> in {str(venv_path)}'
                )
                builder = EnvBuilder(with_pip=True)
                builder.create(str(venv))
            else:
                if io.is_very_verbose():
                    io.writeln(f'Virtualenv <info>{name}</> already exists.')

            os.environ['VIRTUAL_ENV'] = str(venv)

        # venv detection:
        # stdlib venv may symlink sys.executable, so we can't use realpath.
        # but others can symlink *to* the venv Python,
        # so we can't just use sys.executable.
        # So we just check every item in the symlink tree (generally <= 3)
        p = os.path.normcase(sys.executable)
        paths = [p]
        while os.path.islink(p):
            p = os.path.normcase(
                os.path.join(os.path.dirname(p), os.readlink(p)))
            paths.append(p)

        p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
        if any(p.startswith(p_venv) for p in paths):
            # Running properly in the virtualenv, don't need to do anything
            return cls()

        venv = os.environ['VIRTUAL_ENV']

        return cls(venv)
Example #31
0
def make_project(package_name=None, package_description=None, auto=False):
    """
    Interactive method used by :mod:`.coco` to create a new project.
    The project templates are located in directory ``/template``. A combination
    of :mod:`Jinja2` and file-/folder name substitution is used to create a
    new project from this blueprint. A Python virtual environment is created
    installing core4 package using core4 configuration ``core4_origin``.

    :param package_name: str
    :param package_description: str
    :param auto: bool (default ``False``) to skip confirmation
    """
    kwargs = {
        "package_name": package_name,
        "package_description": package_description,
        "package_version": "0.0.0",
    }
    if kwargs["package_name"] and not kwargs["package_name"].isidentifier():
        print("this is not a valid package name")
        sys.exit(1)
    print()
    print("core4 project creation")
    print("======================")
    if kwargs["package_name"] is None:
        kwargs["package_name"] = input_loop("Name: ", identifier=True)
    else:
        print("Name:", kwargs["package_name"])
    if kwargs["package_description"] is None:
        kwargs["package_description"] = input_loop("Description: ")
    else:
        print("Description:", kwargs["package_description"])
    root_path = os.path.abspath(".")
    full_path = os.path.join(root_path, kwargs["package_name"])
    kwargs["full_path"] = full_path
    kwargs["venv"] = VENV
    if os.path.exists(full_path):
        exist = "WARNING! The directory exists. Missing project files will " \
                "be created. All\n    existing files will not be touched."
    else:
        exist = "The directory does not exists and will be created. All " \
                "project files will\n    be created."

    print("""
    A project directory ./{package_name:s} will be created at
        > {full_path:s}

    {exist:s}

    Inside this project directory, a Python virtual environment will be created 
    if it does not exist, yet at
        > {venv:s}

    Inside this project directory a bare git repository will be created if it
    does not exist, yet at
        > {repository:s}

    This repository will have an initial commit and two branches:
        1. master
        2. develop

    To share this git repository with other users you have to manually 
    synchronise this bare repository with a git repository accessible by your
    team. Once this has been done, you can remove the bare repository on this
    computer.

    To start working on your project, enter the Python virtual environment with
        $ cd ./{package_name:s}
        $ source enter_env

    To exit the Python virtual environment type
        $ deactivate        
    """.format(
        root=root_path, package_name=kwargs["package_name"], venv=VENV,
        repository=REPOSITORY, exist=exist, full_path=full_path))

    while not auto and True:
        i = input("type [yes] to continue or press CTRL+C: ")
        if i.strip().lower() == "yes":
            break

    print("\nbare git repository")
    print("-------------------\n")
    repos_path = os.path.join(full_path, REPOSITORY)

    initial_commit = False
    temp_repos_path = None
    git_path = os.path.join(full_path, ".git")
    if not os.path.exists(git_path):
        if not os.path.exists(repos_path):
            temp_path = tempfile.mkdtemp()
            temp_repos_path = os.path.join(temp_path, REPOSITORY)
            printout("    %s ... " % (temp_repos_path))
            os.makedirs(temp_repos_path)
            sh.git(["init", "--shared", "--bare", temp_repos_path])
            print("created")

            printout("    clone into %s ... " % (full_path))
            sh.git(["clone", "file://%s" % (temp_repos_path), full_path])
            print("done")

            initial_commit = True
        else:
            print("    clone %s ... skipped" % (repos_path))
    else:
        curr_dir = os.path.abspath(os.path.curdir)
        os.chdir(os.path.abspath(os.path.dirname(git_path)))
        origin_url = None
        for line in sh.git("config", "--list").split("\n"):
            if line.startswith("remote.origin.url="):
                origin_url = line[len("remote.origin.url="):]
                break
        os.chdir(curr_dir)
        print("    git origin %s ... exists" % (origin_url))

    print("\ncopy files")
    print("----------\n")
    template = os.path.join(os.path.dirname(__file__), "template")
    for root, dirs, files in os.walk(template):
        targetpath = root[len(template):].replace(
            "__name__", kwargs["package_name"])
        if targetpath.startswith("/"):
            targetpath = targetpath[1:]
        if targetpath.endswith("__pycache__"):
            continue
        targetpath = os.path.join(kwargs["package_name"], targetpath)
        if not os.path.exists(targetpath):
            os.mkdir(targetpath)
        for file in files:
            targetfile = file.replace("__name__", kwargs["package_name"])
            targetfile = targetfile.replace("__py__", "py")
            fulltarget = os.path.join(targetpath, targetfile)
            fullsource = os.path.join(root, file)
            printout("    %s ... " % (os.path.abspath(fulltarget)))
            if os.path.exists(fulltarget):
                print("skipped")
            else:
                with open(fullsource, "r") as fh:
                    body = fh.read()
                Template(body).stream(**kwargs).dump(fulltarget)
                print("created")

    if initial_commit:
        print("\ninitial commit")
        print("--------------\n")

        git_dir = ["--git-dir", os.path.join(full_path, ".git"),
                   "--work-tree", full_path]

        printout("    initial commit ... ")
        sh.git(git_dir + ["add", "*"])
        sh.git(git_dir + ["commit", ".", "-m", "initial commit"])
        sh.git(git_dir + ["push"])
        print("done")

        printout("    create branch develop ... ")
        sh.git(git_dir + ["checkout", "-b", "develop"])
        sh.git(git_dir + ["push", "origin", "develop"])
        sh.git(git_dir + ["branch", "--set-upstream-to", "origin/develop",
                          "develop"])
        sh.git(git_dir + ["checkout", "master"])
        print("done")

        printout("    move %s to %s ... " % (temp_repos_path, full_path))
        sh.mv(temp_repos_path, full_path + "/")
        print("done")

        printout("    move origin to %s  ... " % (repos_path))
        sh.git(git_dir + ["remote", "set-url", "origin",
                          "file://" + repos_path])
        print("done")

    print("\nPython virtual environment")
    print("--------------------------\n")

    venv = os.path.join(full_path, VENV)
    if not os.path.exists(venv):
        printout("    create at %s ... " % (venv))
        builder = EnvBuilder(system_site_packages=False, clear=False,
                             symlinks=False, upgrade=False, with_pip=True)
        builder.create(venv)
        print("done")

    env = os.environ.copy()
    if "PYTHONPATH" in env:
        del env["PYTHONPATH"]

    print("\nupgrade pip")
    print("-----------\n")

    pip = os.path.join(venv, "bin", "pip")
    subprocess.check_call([pip, "install", "--upgrade", "pip"], env=env)

    print("\ninstall project")
    print("---------------\n")

    subprocess.check_call([pip, "install", "--upgrade", "-e", full_path],
                          env=env)