forked from microsoft/OSSGadget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ExtractArchive into a helper class, and a few other changes. (…
…microsoft#324) * Refactor the ExtractArchive logic to a helper instead of being in BaseProjectManager. * Rename PackageManagerFactory to ProjectManagerFactory, and remove the static constructors. * Encode the mutated names in mutations to make sure there's no HTTP encoding issues. * Update PackageDownloader exception message. * Change PackageMetadata.User to be a record instead of class to help with json parsing. * Make BaseProjectManager abstract as we never want just a BaseProjectManager. * Add 'Type' as a property in every project manager that maps the manager to the type defined in the package-url spec. (Doesn't get used in this PR though). * Add includePrerelease to BaseProjectManager.EnumerateVersions to filter out pre-release/beta versions if they aren't wanted. Defaults to true, so there are no changes from what is currently in production. * Make BaseProjectManager.GetPackageMetadata return a nullable PackageMetadata object. * Fix URLProjectManager.EnumerateVersions to remove the need for the disabling CS1998. * Fix issues with download not going to the specified path if it wasn't being extracted. So the default oss-download would always download the artifact to "." For every package manager except Cargo and GitHub. Also fixed pypi's download to include the file extension, which it wasn't doing. * Add a unit test to make sure no regressions occur with the URL encoding of mutated names. * Remove public const string Type from BaseProjectManager as it never gets used anyways. Just implemented on the individual BaseProjectManager implementations. * Create a unit test to test ArchiveHelper.ExtractArchiveAsync. * Add a non-static abstract string ManagerType to BaseProjectManager to get the package-url spec type name in a non-static context.
- Loading branch information
Showing
26 changed files
with
453 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Corporation. Licensed under the MIT License. | ||
|
||
namespace Microsoft.CST.OpenSource.Helpers; | ||
|
||
using RecursiveExtractor; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
public static class ArchiveHelper | ||
{ | ||
/// <summary> | ||
/// Logger for each of the subclasses | ||
/// </summary> | ||
static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); | ||
|
||
/// <summary> | ||
/// Extracts an archive (given by 'bytes') into a directory named 'directoryName', | ||
/// recursively, using RecursiveExtractor. | ||
/// </summary> | ||
/// <param name="topLevelDirectory">The top level directory content should be extracted to.</param> | ||
/// <param name="directoryName">directory to extract content into (within <paramref name="topLevelDirectory"/>)</param> | ||
/// <param name="content">stream of the contents to extract (should be an archive file)</param> | ||
/// <param name="cached">If the archive has been cached.</param> | ||
/// <returns>The path that the archive was extracted to.</returns> | ||
public static async Task<string> ExtractArchiveAsync( | ||
string topLevelDirectory, | ||
string directoryName, | ||
Stream content, | ||
bool cached = false) | ||
{ | ||
Logger.Trace("ExtractArchive({0}, <stream> len={1})", directoryName, content.Length); | ||
|
||
Directory.CreateDirectory(topLevelDirectory); | ||
|
||
StringBuilder dirBuilder = new(directoryName); | ||
|
||
foreach (char c in Path.GetInvalidPathChars()) | ||
{ | ||
dirBuilder.Replace(c, '-'); // ignore: lgtm [cs/string-concatenation-in-loop] | ||
} | ||
|
||
string fullTargetPath = Path.Combine(topLevelDirectory, dirBuilder.ToString()); | ||
|
||
if (!cached) | ||
{ | ||
while (Directory.Exists(fullTargetPath) || File.Exists(fullTargetPath)) | ||
{ | ||
dirBuilder.Append("-" + DateTime.Now.Ticks); | ||
fullTargetPath = Path.Combine(topLevelDirectory, dirBuilder.ToString()); | ||
} | ||
} | ||
|
||
Extractor extractor = new(); | ||
ExtractorOptions extractorOptions = new() | ||
{ | ||
ExtractSelfOnFail = true, Parallel = true | ||
// MaxExtractedBytes = 1000 * 1000 * 10; // 10 MB maximum package size | ||
}; | ||
ExtractionStatusCode result = await extractor.ExtractToDirectoryAsync(topLevelDirectory, dirBuilder.ToString(), | ||
content, extractorOptions); | ||
if (result == ExtractionStatusCode.Ok) | ||
{ | ||
Logger.Debug("Archive extracted to {0}", fullTargetPath); | ||
} | ||
else | ||
{ | ||
Logger.Warn("Error extracting archive {0} ({1})", fullTargetPath, result); | ||
} | ||
|
||
return fullTargetPath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.