Exemplo n.º 1
0
        private static string GetVirtualEnvPath(VirtualEnvName envName, string rootDirectory = null)
        {
            if (rootDirectory != null)
            {
                return(rootDirectory);
            }
            var root = TestData.GetTempPath("envs");

            return(Path.Combine(root, envName.ToString()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a Python virtual environment in specified directory and installs the specified packages.
        /// </summary>
        public static string CreateVirtualEnv(this PythonVersion pyVersion, VirtualEnvName envName, IEnumerable <string> packages, string rootDirectory = null)
        {
            var envPath          = pyVersion.CreateVirtualEnv(envName, rootDirectory);
            var envPythonExePath = Path.Combine(envPath, "scripts", "python.exe");

            foreach (var package in packages.MaybeEnumerate())
            {
                using (var output = ProcessOutput.RunHiddenAndCapture(envPythonExePath, "-m", "pip", "install", package)) {
                    Assert.IsTrue(output.Wait(TimeSpan.FromSeconds(30)));
                    Assert.AreEqual(0, output.ExitCode);
                }
            }
            return(envPath);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a Python virtual environment in specified directory.
        /// </summary>
        public static string CreateVirtualEnv(this PythonVersion pyVersion, VirtualEnvName envName, string rootDirectory = null)
        {
            // Only create this virtual env if it doesn't already exist.
            var envPath = GetVirtualEnvPath(envName, rootDirectory);

            if (!File.Exists(Path.Combine(envPath, "scripts", "python.exe")))
            {
                var virtualEnvModule = (pyVersion.Version < PythonLanguageVersion.V30) ? "virtualenv" : "venv";
                using (var p = ProcessOutput.RunHiddenAndCapture(pyVersion.InterpreterPath, "-m", virtualEnvModule, envPath)) {
                    Console.WriteLine(p.Arguments);
                    Assert.IsTrue(p.Wait(TimeSpan.FromMinutes(3)));
                    Console.WriteLine(string.Join(Environment.NewLine, p.StandardOutputLines.Concat(p.StandardErrorLines)));
                    Assert.AreEqual(0, p.ExitCode);
                }
            }

            Assert.IsTrue(File.Exists(Path.Combine(envPath, "scripts", "python.exe")));
            return(envPath);
        }
Exemplo n.º 4
0
        private void RefreshCanCreateVirtualEnv()
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke((Action)(() => RefreshCanCreateVirtualEnv()));
                return;
            }

            string path = string.IsNullOrEmpty(LocationPath)
                ? string.Empty :
                          Path.Combine(LocationPath, VirtualEnvName);

            if (Interpreters == null || Interpreters.Count == 0)
            {
                WillCreateVirtualEnv    = false;
                CannotCreateVirtualEnv  = false;
                NoInterpretersInstalled = true;
            }
            else if (!IsValidVirtualEnvPath(path) || BaseInterpreter == null || IsFolderNotEmpty(path) || IsInvalidDescription())
            {
                WillCreateVirtualEnv    = false;
                CannotCreateVirtualEnv  = true;
                NoInterpretersInstalled = false;
            }
            else
            {
                LocationPath            = PathUtils.GetParent(path);
                WillCreateVirtualEnv    = true;
                CannotCreateVirtualEnv  = false;
                NoInterpretersInstalled = false;
            }

            if (string.IsNullOrEmpty(VirtualEnvName.Trim()))
            {
                SetError(nameof(VirtualEnvName), Strings.AddVirtualEnvironmentNameEmpty);
            }
            else if (!IsValidVirtualEnvPath(path))
            {
                SetError(nameof(VirtualEnvName), Strings.AddVirtualEnvironmentLocationInvalid.FormatUI(path));
            }
            else if (IsFolderNotEmpty(path))
            {
                SetError(nameof(VirtualEnvName), Strings.AddVirtualEnvironmentLocationNotEmpty.FormatUI(path));
            }
            else
            {
                ClearErrors(nameof(VirtualEnvName));
            }

            bool canRegisterGlobally = false;

            if (IsRegisterCustomEnv && string.IsNullOrEmpty(Description))
            {
                SetError(nameof(Description), Strings.AddEnvironmentDescriptionEmpty);
            }
            else if (IsRegisterCustomEnv && Description.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
            {
                SetError(nameof(Description), Strings.AddEnvironmentDescriptionInvalid);
            }
            else
            {
                ClearErrors(nameof(Description));
                canRegisterGlobally = true;
            }

            if (!string.IsNullOrEmpty(RequirementsPath) && !File.Exists(RequirementsPath))
            {
                SetError(nameof(RequirementsPath), Strings.AddVirtualEnvironmentFileInvalid.FormatUI(RequirementsPath));
            }
            else
            {
                ClearErrors(nameof(RequirementsPath));
            }

            CanInstallRequirementsTxt  = File.Exists(RequirementsPath);
            WillInstallRequirementsTxt = CanInstallRequirementsTxt && WillCreateVirtualEnv;
            WillRegisterGlobally       = IsRegisterCustomEnv && canRegisterGlobally && WillCreateVirtualEnv;

            // For now, we enable but prompt when they click accept
            //IsAcceptEnabled = WillCreateVirtualEnv && !Progress.IsProgressDisplayed;
            IsAcceptEnabled      = !Progress.IsProgressDisplayed;
            AcceptCaption        = Strings.AddEnvironmentCreateButton;
            AcceptAutomationName = Strings.AddEnvironmentCreateButtonAutomationName;
        }