This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPackageLicenseToCsvDriver.cs
More file actions
175 lines (154 loc) · 7.29 KB
/
PackageLicenseToCsvDriver.cs
File metadata and controls
175 lines (154 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Text.RegularExpressions;
using NuGet.Packaging;
using NuGet.Packaging.Licenses;
namespace NuGet.Insights.Worker.PackageLicenseToCsv
{
public class PackageLicenseToCsvDriver : ICatalogLeafToCsvDriver<PackageLicense>, ICsvResultStorage<PackageLicense>
{
private readonly CatalogClient _catalogClient;
private readonly FlatContainerClient _flatContainerClient;
private readonly IOptions<NuGetInsightsWorkerSettings> _options;
public PackageLicenseToCsvDriver(
CatalogClient catalogClient,
FlatContainerClient flatContainerClient,
IOptions<NuGetInsightsWorkerSettings> options)
{
_catalogClient = catalogClient;
_flatContainerClient = flatContainerClient;
_options = options;
}
public string ResultContainerName => _options.Value.PackageLicenseContainerName;
public bool SingleMessagePerId => false;
public Task InitializeAsync()
{
return Task.CompletedTask;
}
public Task DestroyAsync()
{
return Task.CompletedTask;
}
public async Task<DriverResult<IReadOnlyList<PackageLicense>>> ProcessLeafAsync(CatalogLeafScan leafScan)
{
(var resultType, var records) = await ProcessLeafInternalAsync(leafScan);
if (resultType == TempStreamResultType.SemaphoreNotAvailable)
{
return DriverResult.TryAgainLater<IReadOnlyList<PackageLicense>>();
}
return DriverResult.Success(records);
}
private async Task<(TempStreamResultType, IReadOnlyList<PackageLicense>)> ProcessLeafInternalAsync(CatalogLeafScan leafScan)
{
var scanId = Guid.NewGuid();
var scanTimestamp = DateTimeOffset.UtcNow;
if (leafScan.LeafType == CatalogLeafType.PackageDelete)
{
var leaf = (PackageDeleteCatalogLeaf)await _catalogClient.GetCatalogLeafAsync(leafScan.LeafType, leafScan.Url);
return (
TempStreamResultType.Success,
new List<PackageLicense> { new PackageLicense(scanId, scanTimestamp, leaf) }
);
}
else
{
var leaf = (PackageDetailsCatalogLeaf)await _catalogClient.GetCatalogLeafAsync(leafScan.LeafType, leafScan.Url);
PackageLicenseResultType resultType;
if (!string.IsNullOrWhiteSpace(leaf.LicenseFile))
{
resultType = PackageLicenseResultType.File;
}
else if (!string.IsNullOrEmpty(leaf.LicenseExpression))
{
resultType = PackageLicenseResultType.Expression;
}
else if (!string.IsNullOrEmpty(leaf.LicenseUrl))
{
resultType = PackageLicenseResultType.Url;
}
else
{
resultType = PackageLicenseResultType.None;
}
var record = new PackageLicense(scanId, scanTimestamp, leaf)
{
ResultType = resultType,
Url = leaf.LicenseUrl,
Expression = leaf.LicenseExpression,
File = leaf.LicenseFile,
};
if (!string.IsNullOrWhiteSpace(leaf.LicenseExpression))
{
try
{
var parsedExpression = NuGetLicenseExpression.Parse(leaf.LicenseExpression);
var licenses = new HashSet<string>();
var nonStandardLicenses = new HashSet<string>();
var exceptions = new HashSet<string>();
parsedExpression.OnEachLeafNode(
license =>
{
licenses.Add(license.Identifier);
if (!license.IsStandardLicense)
{
nonStandardLicenses.Add(license.Identifier);
}
},
exception => exceptions.Add(exception.Identifier));
record.ExpressionParsed = KustoDynamicSerializer.Serialize(parsedExpression);
record.ExpressionLicenses = KustoDynamicSerializer.Serialize(licenses.OrderBy(x => x, StringComparer.Ordinal).ToList());
record.ExpressionExceptions = KustoDynamicSerializer.Serialize(exceptions.OrderBy(x => x, StringComparer.Ordinal).ToList());
record.ExpressionNonStandardLicenses = KustoDynamicSerializer.Serialize(nonStandardLicenses.OrderBy(x => x, StringComparer.Ordinal).ToList());
record.GeneratedUrl = new LicenseMetadata(
LicenseType.Expression,
leaf.LicenseExpression,
parsedExpression,
Array.Empty<string>(),
LicenseMetadata.CurrentVersion).LicenseUrl.AbsoluteUri;
record.ExpressionHasDeprecatedIdentifier = false;
}
catch (NuGetLicenseExpressionParsingException ex) when (Regex.IsMatch(ex.Message, "^The identifier '.+' is deprecated.$"))
{
record.ExpressionHasDeprecatedIdentifier = true;
}
}
if (!string.IsNullOrEmpty(leaf.LicenseFile))
{
record.GeneratedUrl = new LicenseMetadata(
LicenseType.File,
license: leaf.LicenseFile,
expression: null,
Array.Empty<string>(),
LicenseMetadata.CurrentVersion).LicenseUrl.AbsoluteUri;
}
var result = await _flatContainerClient.DownloadPackageLicenseToFileAsync(
leafScan.PackageId,
leafScan.PackageVersion,
CancellationToken.None);
if (result != null)
{
await using (result)
{
if (result.Type == TempStreamResultType.SemaphoreNotAvailable)
{
return (
TempStreamResultType.SemaphoreNotAvailable,
null
);
}
record.FileLength = result.Stream.Length;
record.FileSHA256 = result.Hash.SHA256.ToBase64();
using (var reader = new StreamReader(result.Stream))
{
record.FileContent = reader.ReadToEnd();
}
}
}
return (
TempStreamResultType.Success,
new List<PackageLicense> { record }
);
}
}
}
}