Пример #1
0
        PackagePhysicalFileMetadata?AttemptToGetPackageFromCache(string packageId, IVersion version, string cacheDirectory)
        {
            Log.VerboseFormat("Checking package cache for package {0} v{1}", packageId, version.ToString());

            var files = fileSystem.EnumerateFilesRecursively(cacheDirectory, PackageName.ToSearchPatterns(packageId, version, new[] { ".nupkg" }));

            foreach (var file in files)
            {
                var package = PackageName.FromFile(file);
                if (package == null)
                {
                    continue;
                }

                var idMatches         = string.Equals(package.PackageId, packageId, StringComparison.OrdinalIgnoreCase);
                var versionExactMatch = string.Equals(package.Version.ToString(), version.ToString(), StringComparison.OrdinalIgnoreCase);
                var nugetVerMatches   = package.Version.Equals(version);

                if (idMatches && (nugetVerMatches || versionExactMatch))
                {
                    return(PackagePhysicalFileMetadata.Build(file, package));
                }
            }

            return(null);
        }
Пример #2
0
        PackagePhysicalFileMetadata DownloadPackage(
            string packageId,
            IVersion version,
            Uri feedUri,
            ICredentials feedCredentials,
            string cacheDirectory,
            int maxDownloadAttempts,
            TimeSpan downloadAttemptBackoff)
        {
            Log.Info("Downloading NuGet package {0} v{1} from feed: '{2}'", packageId, version, feedUri);
            Log.VerboseFormat("Downloaded package will be stored in: '{0}'", cacheDirectory);
            freeSpaceChecker.EnsureDiskHasEnoughFreeSpace(cacheDirectory);

            var fullPathToDownloadTo = Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, ".nupkg"));

            var downloader = new InternalNuGetPackageDownloader(fileSystem);

            downloader.DownloadPackage(packageId,
                                       version,
                                       feedUri,
                                       feedCredentials,
                                       fullPathToDownloadTo,
                                       maxDownloadAttempts,
                                       downloadAttemptBackoff);

            var pkg = PackagePhysicalFileMetadata.Build(fullPathToDownloadTo);

            if (pkg == null)
            {
                throw new CommandException($"Package metadata for {packageId}, version {version}, could not be determined");
            }

            CheckWhetherThePackageHasDependencies(pkg);
            return(pkg);
        }
Пример #3
0
        PackagePhysicalFileMetadata DownloadChart(string packageId, IVersion version, Uri feedUri,
                                                  ICredentials feedCredentials, string cacheDirectory)
        {
            var cred = feedCredentials.GetCredential(feedUri, "basic");

            var tempDirectory = fileSystem.CreateTemporaryDirectory();

            using (new TemporaryDirectory(tempDirectory))
            {
                var homeDir = Path.Combine(tempDirectory, "helm");
                if (!Directory.Exists(homeDir))
                {
                    Directory.CreateDirectory(homeDir);
                }
                var stagingDir = Path.Combine(tempDirectory, "staging");
                if (!Directory.Exists(stagingDir))
                {
                    Directory.CreateDirectory(stagingDir);
                }

                var log = new LogWrapper();
                Invoke($"init --home \"{homeDir}\" --client-only", tempDirectory, log);
                Invoke($"repo add --home \"{homeDir}\" {(string.IsNullOrEmpty(cred.UserName) ? "" : $"--username \"{cred.UserName}\" --password \"{cred.Password}\"")} {TempRepoName} {feedUri.ToString()}", tempDirectory, log);
                Invoke($"fetch --home \"{homeDir}\"  --version \"{version}\" --destination \"{stagingDir}\" {TempRepoName}/{packageId}", tempDirectory, log);

                var localDownloadName =
                    Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, Extension));

                fileSystem.MoveFile(Directory.GetFiles(stagingDir)[0], localDownloadName);
                return(PackagePhysicalFileMetadata.Build(localDownloadName));
            }
Пример #4
0
        public PackagePhysicalFileMetadata?GetPackage(string packageId, IVersion version, string hash)
        {
            fileSystem.EnsureDirectoryExists(GetPackagesDirectory());
            foreach (var file in PackageFiles(packageId, version))
            {
                var packageNameMetadata = PackageMetadata(file);
                if (packageNameMetadata == null)
                {
                    continue;
                }

                if (!string.Equals(packageNameMetadata.PackageId, packageId, StringComparison.OrdinalIgnoreCase) ||
                    !packageNameMetadata.Version.Equals(version))
                {
                    continue;
                }

                var physicalPackageMetadata = PackagePhysicalFileMetadata.Build(file, packageNameMetadata);

                if (string.IsNullOrWhiteSpace(hash) || hash == physicalPackageMetadata?.Hash)
                {
                    return(physicalPackageMetadata);
                }
            }

            return(null);
        }
        /// <summary>
        /// Actually download the maven file.
        /// </summary>
        /// <returns>The path to the downloaded file</returns>
        PackagePhysicalFileMetadata DownloadArtifact(
            MavenPackageID mavenGavFirst,
            string packageId,
            IVersion version,
            Uri feedUri,
            ICredentials feedCredentials,
            string cacheDirectory,
            int maxDownloadAttempts,
            TimeSpan downloadAttemptBackoff,
            XmlDocument?snapshotMetadata)
        {
            Guard.NotNull(mavenGavFirst, "mavenGavFirst can not be null");
            Guard.NotNullOrWhiteSpace(packageId, "packageId can not be null");
            Guard.NotNull(version, "version can not be null");
            Guard.NotNullOrWhiteSpace(cacheDirectory, "cacheDirectory can not be null");
            Guard.NotNull(feedUri, "feedUri can not be null");

            var localDownloadName = Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, "." + mavenGavFirst.Packaging));
            var downloadUrl       = feedUri.ToString().TrimEnd('/') +
                                    (snapshotMetadata == null
                    ? mavenGavFirst.DefaultArtifactPath
                    : mavenGavFirst.SnapshotArtifactPath(GetLatestSnapshotRelease(
                                                             snapshotMetadata,
                                                             mavenGavFirst.Packaging,
                                                             mavenGavFirst.Classifier,
                                                             mavenGavFirst.Version)));

            for (var retry = 0; retry < maxDownloadAttempts; ++retry)
            {
                try
                {
                    Log.Verbose($"Downloading Attempt {downloadUrl} TO {localDownloadName}");
                    using (var client = new WebClient
                    {
                        Credentials = feedCredentials
                    })
                    {
                        client.DownloadFile(downloadUrl, localDownloadName);
                        var packagePhysicalFileMetadata = PackagePhysicalFileMetadata.Build(localDownloadName);
                        return(packagePhysicalFileMetadata
                               ?? throw new CommandException($"Unable to retrieve metadata for package {packageId}, version {version}"));
                    }
                }
                catch (Exception ex)
                {
                    if ((retry + 1) == maxDownloadAttempts)
                    {
                        throw new MavenDownloadException("Failed to download the Maven artifact.\r\nLast Exception Message: " + ex.Message);
                    }
                    Thread.Sleep(downloadAttemptBackoff);
                }
            }

            throw new MavenDownloadException("Failed to download the Maven artifact");
        }
Пример #6
0
        PackagePhysicalFileMetadata DownloadFile(string uri,
                                                 string cacheDirectory,
                                                 string packageId,
                                                 IVersion version,
                                                 string?authorizationToken,
                                                 int maxDownloadAttempts,
                                                 TimeSpan downloadAttemptBackoff)
        {
            var localDownloadName =
                Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, Extension));

            var tempPath = Path.GetTempFileName();

            if (File.Exists(tempPath))
            {
                File.Delete(tempPath);
            }

            for (var retry = 0; retry < maxDownloadAttempts; ++retry)
            {
                try
                {
                    if (retry != 0)
                    {
                        Log.Verbose($"Download Attempt #{retry + 1}");
                    }

                    using (var client = new WebClient())
                    {
                        client.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable);
                        client.Headers.Set(HttpRequestHeader.UserAgent, GetUserAgent());
                        AddAuthorization(client, authorizationToken);
                        client.DownloadFileWithProgress(uri, tempPath, (progress, total) => log.Progress(progress, $"Downloading {packageId} v{version}"));

                        DeNestContents(tempPath, localDownloadName);
                        var packagePhysicalFileMetadata = PackagePhysicalFileMetadata.Build(localDownloadName);
                        return(packagePhysicalFileMetadata
                               ?? throw new CommandException($"Unable to retrieve metadata for package {packageId}, version {version}"));
                    }
                }
                catch (WebException)
                {
                    Thread.Sleep(downloadAttemptBackoff);
                }
            }

            throw new Exception("Failed to download the package.");
        }
Пример #7
0
        public IEnumerable <PackagePhysicalFileMetadata> GetNearestPackages(string packageId, IVersion version, int take = 5)
        {
            fileSystem.EnsureDirectoryExists(GetPackagesDirectory());

            var zipPackages =
                from filePath in PackageFiles(packageId)
                let zip = PackageMetadata(filePath)
                          where zip != null && string.Equals(zip.PackageId, packageId, StringComparison.OrdinalIgnoreCase) && zip.Version.CompareTo(version) <= 0
                          orderby zip.Version descending
                          select new { zip, filePath };

            return
                (from zipPackage in zipPackages.Take(take)
                 let package = PackagePhysicalFileMetadata.Build(zipPackage.filePath, zipPackage.zip)
                               where package != null
                               select package);
        }
Пример #8
0
        PackagePhysicalFileMetadata SourceFromCache(string packageId, IVersion version, string cacheDirectory)
        {
            Log.VerboseFormat("Checking package cache for package {0} v{1}", packageId, version.ToString());

            var files = fileSystem.EnumerateFilesRecursively(cacheDirectory, PackageName.ToSearchPatterns(packageId, version, new [] { Extension }));

            foreach (var file in files)
            {
                var package = PackageName.FromFile(file);
                if (package == null)
                {
                    continue;
                }

                if (string.Equals(package.PackageId, packageId, StringComparison.OrdinalIgnoreCase) && package.Version.Equals(version))
                {
                    return(PackagePhysicalFileMetadata.Build(file, package));
                }
            }

            return(null);
        }
Пример #9
0
        private PackagePhysicalFileMetadata DownloadChart(string packageId, IVersion version, Uri feedUri,
                                                          ICredentials feedCredentials, string cacheDirectory)
        {
            var cred = feedCredentials.GetCredential(feedUri, "basic");

            var syntax = new[] { ScriptSyntax.PowerShell, ScriptSyntax.Bash }.First(syntx =>
                                                                                    scriptEngine.GetSupportedTypes().Contains(syntx));

            var tempDirectory = fileSystem.CreateTemporaryDirectory();

            using (new TemporaryDirectory(tempDirectory))
            {
                var file   = GetFetchScript(tempDirectory, syntax);
                var result = scriptEngine.Execute(new Script(file), new CalamariVariableDictionary()
                {
                    ["Password"] = cred.Password,
                    ["Username"] = cred.UserName,
                    ["Version"]  = version.OriginalString,
                    ["Url"]      = feedUri.ToString(),
                    ["Package"]  = packageId,
                }, commandLineRunner,
                                                  new Dictionary <string, string>());
                if (!result.HasErrors)
                {
                    var localDownloadName =
                        Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, Extension));

                    var packageFile = fileSystem.EnumerateFiles(Path.Combine(tempDirectory, "staging")).First();
                    fileSystem.MoveFile(packageFile, localDownloadName);
                    return(PackagePhysicalFileMetadata.Build(localDownloadName));
                }
                else
                {
                    throw new Exception("Unable to download chart");
                }
            }
        }
Пример #10
0
        public override int Execute(string[] commandLineArguments)
        {
            Options.Parse(commandLineArguments);
            string deltaFilePath;
            string newFilePath;
            string basisFilePath;

            try
            {
                ValidateParameters(out basisFilePath, out deltaFilePath, out newFilePath);
                fileSystem.EnsureDiskHasEnoughFreeSpace(PackageStore.GetPackagesDirectory());

                var tempNewFilePath = newFilePath + ".partial";
#if USE_OCTODIFF_EXE
                var factory = new OctoDiffCommandLineRunner();
#else
                var factory = new OctoDiffLibraryCallRunner();
#endif
                var octoDiff = factory.OctoDiff
                               .Action("patch")
                               .PositionalArgument(basisFilePath)
                               .PositionalArgument(deltaFilePath)
                               .PositionalArgument(tempNewFilePath);

                if (skipVerification)
                {
                    octoDiff.Flag("skip-verification");
                }

                if (showProgress)
                {
                    octoDiff.Flag("progress");
                }

                Log.Info("Applying delta to {0} with hash {1} and storing as {2}", basisFilePath, fileHash, newFilePath);

                var result = factory.Execute();
                if (result.ExitCode != 0)
                {
                    fileSystem.DeleteFile(tempNewFilePath, FailureOptions.ThrowOnFailure);
                    throw new CommandLineException("OctoDiff", result.ExitCode, result.Errors);
                }

                File.Move(tempNewFilePath, newFilePath);

                if (!File.Exists(newFilePath))
                {
                    throw new CommandException($"Failed to apply delta file {deltaFilePath} to {basisFilePath}");
                }
            }
            catch (Exception e) when(e is CommandLineException || e is CommandException)
            {
                Log.ServiceMessages.DeltaVerificationError(e.Message);
                return(0);
            }

            var package = PackagePhysicalFileMetadata.Build(newFilePath);
            if (package == null)
            {
                return(0);
            }

            Log.ServiceMessages.DeltaVerification(newFilePath, package.Hash, package.Size);
            return(0);
        }