예제 #1
0
        private bool DoDownload(PackageDownload download, out string tempdir, out string filename, Request request)
        {
            bool created = false;

            tempdir = "";

            while (!created)
            {
                tempdir = Path.GetTempPath() + Guid.NewGuid().ToString();
                if (Win32CreateDirectory(tempdir, IntPtr.Zero))
                {
                    created = true;
                }
                else
                {
                    if (Marshal.GetLastWin32Error() != 183) // ERROR_ALREADY_EXISTS
                    {
                        throw new Win32Exception();
                    }
                }
            }
            filename = Path.Combine(tempdir, download.basename);
            request.ProviderServices.DownloadFile(new Uri(download.url), filename, request);

            if (!string.IsNullOrWhiteSpace(download.md5_digest))
            {
                request.Debug("expected md5sum: {0}", download.md5_digest);

                using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
                {
                    using (MD5 md5 = MD5.Create())
                    {
                        byte[] actual_hash        = md5.ComputeHash(fs);
                        string actual_hash_string = hash_to_string(actual_hash);
                        request.Debug("actual md5sum: {0}", actual_hash_string);
                        if (actual_hash_string != download.md5_digest)
                        {
                            request.Error(ErrorCategory.MetadataError, name, "Downloaded file has incorrect MD5 sum");
                            File.Delete(filename);
                            Directory.Delete(tempdir);
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
예제 #2
0
        private static PackageDownload[] ParseUrls(JToken token)
        {
            JArray urls = (JArray)token;

            PackageDownload[] result = new PackageDownload[urls.Count];
            for (int i = 0; i < urls.Count; i++)
            {
                JObject uri = (JObject)urls[i];
                result[i].url         = uri["url"].ToString();
                result[i].basename    = uri["filename"].ToString();
                result[i].md5_digest  = uri["md5_digest"].ToString();
                result[i].packagetype = uri["packagetype"].ToString();
                result[i].size        = (long)uri["size"];
            }
            return(result);
        }
예제 #3
0
        private bool CanInstall(PythonInstall install, PackageDownload download, out bool install_specific, Request request)
        {
            install_specific = false;
            if (download.packagetype == "bdist_wheel")
            {
                string tag = download.basename;
                if (tag.EndsWith(".whl"))
                {
                    tag = tag.Substring(0, tag.Length - 4);
                }
                int platform_dash = tag.LastIndexOf('-');
                if (platform_dash <= 0)
                {
                    return(false);
                }
                int abi_dash = tag.LastIndexOf('-', platform_dash - 1);
                if (abi_dash <= 0)
                {
                    return(false);
                }
                int python_dash = tag.LastIndexOf('-', abi_dash - 1);
                if (python_dash <= 0)
                {
                    return(false);
                }
                tag = tag.Substring(python_dash + 1);

                install_specific = true;

                if (install.CompatibleWithTag(tag))
                {
                    return(true);
                }

                return(false);
            }
            else if (download.packagetype == "sdist")
            {
                install_specific = false;
                return(true);
            }
            return(true);
        }
예제 #4
0
        private bool Install(PythonInstall install, PackageDownload download, Request request)
        {
            if (download.packagetype != "bdist_wheel")
            {
                if (!install.InstallPip(request))
                {
                    return(false);
                }
            }

            string tempdir, filename;

            if (!DoDownload(download, out tempdir, out filename, request))
            {
                return(false);
            }

            try
            {
                if (download.packagetype == "bdist_wheel")
                {
                    foreach (var package in PackagesFromFile(filename, request))
                    {
                        if (package.MatchesName(name) && package.version.raw_version_string == version.raw_version_string)
                        {
                            return(package.Install(install, request));
                        }
                    }
                    request.Error(ErrorCategory.MetadataError, name, "Downloaded package file doesn't contain the expected package.");
                    return(false);
                }
                else
                {
                    return(install.InstallViaPip(filename, request));
                }
            }
            finally
            {
                File.Delete(filename);
                Directory.Delete(tempdir);
            }
        }