forked from praeclarum/FuGetGallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLicense.cs
286 lines (264 loc) · 12 KB
/
License.cs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Diagnostics;
namespace FuGetGallery
{
public class License
{
public string Name { get; set; } = "";
public bool AllowsDecompilation { get; set; }
public List<string> KeyStrings { get; set; } = new List<string> ();
public HashSet<string> KnownUrls { get; set; } = new HashSet<string> ();
string templateName = "";
public string TemplateName {
get => templateName;
set { templateName = value; LoadTemplate (); }
}
public string TemplateText { get; private set; }
public HashSet<string> TemplateBigrams { get; private set; }
public override string ToString () => Name;
readonly static List<License> known = new List<License> ();
static License ()
{
known.Add (new License {
Name = "Apache License 2",
AllowsDecompilation = true,
KeyStrings = {
"Apache License",
},
KnownUrls = {
"http://choosealicense.com/licenses/apache/",
"https://choosealicense.com/licenses/apache/",
"http://choosealicense.com/licenses/apache-2.0/",
"https://choosealicense.com/licenses/apache-2.0/",
"http://www.apache.org/licenses/LICENSE-2.0",
"https://www.apache.org/licenses/LICENSE-2.0",
"http://opensource.org/licenses/Apache-2.0",
"https://opensource.org/licenses/Apache-2.0",
},
TemplateName = "Apache2",
});
known.Add (new License {
Name = "3-Clause BSD License",
AllowsDecompilation = true,
KnownUrls = {
"http://choosealicense.com/licenses/bsd-3-clause-clear/",
"https://choosealicense.com/licenses/bsd-3-clause-clear/",
"http://opensource.org/licenses/BSD-3-Clause",
"https://opensource.org/licenses/BSD-3-Clause",
},
TemplateName = "BSD3Clause",
});
known.Add (new License {
Name = "2-Clause BSD License",
AllowsDecompilation = true,
KeyStrings = {
"BSD 2-Clause License",
"2‐clause BSD License",
"Simplified BSD License",
},
KnownUrls = {
"http://choosealicense.com/licenses/bsd-2-clause/",
"https://choosealicense.com/licenses/bsd-2-clause/",
"http://opensource.org/licenses/BSD-2-Clause",
"https://opensource.org/licenses/BSD-2-Clause",
},
TemplateName = "BSD2Clause",
});
known.Add (new License {
Name = "GNU GPL 2",
AllowsDecompilation = true,
KnownUrls = {
"http://www.gnu.org/licenses/old-licenses/gpl-2.0.html",
"https://www.gnu.org/licenses/old-licenses/gpl-2.0.html",
"http://choosealicense.com/licenses/gpl-v2/",
"https://choosealicense.com/licenses/gpl-v2/",
"http://choosealicense.com/licenses/gpl-2.0/",
"https://choosealicense.com/licenses/gpl-2.0/",
"http://choosealicense.com/licenses/gpl-v2",
"https://choosealicense.com/licenses/gpl-v2",
"http://choosealicense.com/licenses/gpl-2.0",
"https://choosealicense.com/licenses/gpl-2.0",
"http://www.gnu.org/licenses/gpl-2.0.txt",
"https://www.gnu.org/licenses/gpl-2.0.txt",
"http://www.gnu.org/licenses/gpl-2.0.html",
"https://www.gnu.org/licenses/gpl-2.0.html",
"http://opensource.org/licenses/GPL-2.0",
"https://opensource.org/licenses/GPL-2.0",
"https://downloads.mysql.com/docs/licenses/connector-net-8.0-gpl-en.pdf",
},
TemplateName = "GPL2",
});
known.Add (new License {
Name = "GNU GPL 3",
AllowsDecompilation = true,
KnownUrls = {
"http://choosealicense.com/licenses/gpl-v3/",
"https://choosealicense.com/licenses/gpl-v3/",
"http://choosealicense.com/licenses/gpl-3.0/",
"https://choosealicense.com/licenses/gpl-3.0/",
"http://choosealicense.com/licenses/gpl-v3",
"https://choosealicense.com/licenses/gpl-v3",
"http://choosealicense.com/licenses/gpl-3.0",
"https://choosealicense.com/licenses/gpl-3.0",
"http://www.gnu.org/licenses/gpl.html#content",
"https://www.gnu.org/licenses/gpl.html#content",
"http://www.gnu.org/licenses/gpl.html",
"https://www.gnu.org/licenses/gpl.html",
"http://www.gnu.org/licenses/gpl-3.0.txt",
"https://www.gnu.org/licenses/gpl-3.0.txt",
"http://www.gnu.org/licenses/gpl-3.0.html",
"https://www.gnu.org/licenses/gpl-3.0.html",
"http://opensource.org/licenses/GPL-3.0",
"https://opensource.org/licenses/GPL-3.0",
},
TemplateName = "GPL3",
});
known.Add (new License {
Name = "GNU LGPL 2.1",
AllowsDecompilation = true,
KnownUrls = {
"http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html",
"https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html",
"http://www.gnu.org/licenses/lgpl-2.1.txt",
"https://www.gnu.org/licenses/lgpl-2.1.txt",
"http://www.gnu.org/licenses/lgpl-2.1.html",
"https://www.gnu.org/licenses/lgpl-2.1.html",
"http://opensource.org/licenses/LGPL-2.1",
"https://opensource.org/licenses/LGPL-2.1",
},
TemplateName = "LGPL21",
});
known.Add (new License {
Name = "GNU LGPL 3",
AllowsDecompilation = true,
KnownUrls = {
"http://www.gnu.org/licenses/lgpl.html",
"https://www.gnu.org/licenses/lgpl.html",
"http://choosealicense.com/licenses/lgpl-3.0/",
"https://choosealicense.com/licenses/lgpl-3.0/",
"http://choosealicense.com/licenses/lgpl-3.0",
"https://choosealicense.com/licenses/lgpl-3.0",
"http://www.gnu.org/licenses/lgpl-3.0.txt",
"https://www.gnu.org/licenses/lgpl-3.0.txt",
"http://www.gnu.org/licenses/lgpl-3.0.html",
"https://www.gnu.org/licenses/lgpl-3.0.html",
"http://opensource.org/licenses/LGPL-3.0",
"https://opensource.org/licenses/LGPL-3.0",
},
TemplateName = "LGPL3",
});
known.Add (new License {
Name = "MIT License",
AllowsDecompilation = true,
KeyStrings = {
"MIT License"
},
KnownUrls = {
"http://choosealicense.com/licenses/mit/",
"https://choosealicense.com/licenses/mit/",
"http://choosealicense.com/licenses/mit",
"https://choosealicense.com/licenses/mit",
"http://opensource.org/licenses/mit-license.php",
"https://opensource.org/licenses/mit-license.php",
"http://opensource.org/licenses/MIT",
"https://opensource.org/licenses/MIT",
},
TemplateName = "MIT",
});
known.Add (new License {
Name = "Mozilla Public License 1.1",
AllowsDecompilation = true,
KnownUrls = {
"http://www.mozilla.org/MPL/1.1/",
"https://www.mozilla.org/MPL/1.1/",
"http://www.mozilla.org/en-US/MPL/1.1/",
"https://www.mozilla.org/en-US/MPL/1.1/",
"http://opensource.org/licenses/MPL-1.1",
"https://opensource.org/licenses/MPL-1.1",
},
TemplateName = "MPL11",
});
known.Add (new License {
Name = "Mozilla Public License 2",
AllowsDecompilation = true,
KnownUrls = {
"http://www.mozilla.org/MPL/2.0/",
"https://www.mozilla.org/MPL/2.0/",
"http://choosealicense.com/licenses/mpl-2.0/",
"https://choosealicense.com/licenses/mpl-2.0/",
"http://www.mozilla.org/en-US/MPL/2.0/",
"https://www.mozilla.org/en-US/MPL/2.0/",
"http://opensource.org/licenses/MPL-2.0",
"https://opensource.org/licenses/MPL-2.0",
},
TemplateName = "MPL2",
});
known.Add (new License {
Name = "MS-PL",
AllowsDecompilation = true,
KnownUrls = {
"http://opensource.org/licenses/MS-PL",
"https://opensource.org/licenses/MS-PL",
"http://opensource.org/licenses/ms-pl",
"https://opensource.org/licenses/ms-pl",
},
TemplateName = "MSPL",
});
}
public static License FindLicenseWithUrl (string url)
{
return known.FirstOrDefault (x => x.KnownUrls.Contains (url));
}
public static License FindLicenseWithText (string text)
{
var keyed = known.FirstOrDefault (x => x.KeyStrings.Count > 0 && x.KeyStrings.Any (s => text.Contains (s)));
if (keyed != null)
return keyed;
var bigrams = BuildBigramSet (text);
var q =
from l in known
let dice = DiceCoefficient (l.TemplateBigrams, bigrams)
where dice > 0.9
orderby dice descending
select Tuple.Create (dice, l);
var r = q.FirstOrDefault ();
if (r != null) Debug.WriteLine (r);
return r?.Item2;
}
void LoadTemplate ()
{
using (var s = GetType ().Assembly.GetManifestResourceStream ($"FuGetGallery.Resources.Licenses.{TemplateName}.txt")) {
if (s == null) {
throw new Exception ("Missing license " + TemplateName);
}
using (var r = new StreamReader (s)) {
TemplateText = r.ReadToEnd ();
TemplateBigrams = BuildBigramSet (TemplateText);
}
}
}
/// <summary>
/// From https://gist.github.com/ssajous/3539848
/// </summary>
static HashSet<string> BuildBigramSet (string input)
{
HashSet<string> bigrams = new HashSet<string> ();
for (int i = 0; i < input.Length - 1; i++) {
bigrams.Add (input.Substring (i, 2));
}
return bigrams;
}
/// <summary>
/// From https://gist.github.com/ssajous/3539848
/// </summary>
public static double DiceCoefficient (HashSet<string> x, HashSet<string> y)
{
HashSet<string> intersection = new HashSet<string> (x);
intersection.IntersectWith (y);
return (2.0 * intersection.Count) / (x.Count + y.Count);
}
}
}