/
RequirementsTxtExtractor.ts
70 lines (58 loc) · 1.98 KB
/
RequirementsTxtExtractor.ts
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
import Extractor from "./Extractor";
import ExtractorFile from "./ExtractorFile";
import MatchConfig from "../matcher/MatchConfig";
import {ManifestFile} from "@depscloud/api/v1beta";
import Languages from "./Languages";
import inferPythonName from "./utils/inferPythonName";
import {ManifestDependency} from "@depscloud/api/depscloud/api/v1beta/manifest";
const operators = [
"~", "=", "!", ">", "<"
];
export default class RequirementsTxtExtractor implements Extractor {
public async extract(url: string, files: { [p: string]: ExtractorFile }): Promise<ManifestFile> {
const dependencies: Array<ManifestDependency> = [];
const contents = files["requirements.txt"].raw();
const lines = contents.split("\n");
lines.forEach((line) => {
const commentIndex = line.indexOf("#");
if (commentIndex > -1) {
line = line.substring(0, commentIndex);
}
line = line.trim()
if (!line || line[0] === "-") {
return
}
let l = line.length;
for (const operator of operators) {
const i = line.indexOf(operator);
if (i > -1) {
l = Math.min(l, i);
}
}
const name = line.substring(0, l).trim();
const versionConstraint = line.substring(l).trim();
dependencies.push({
name,
versionConstraint: (versionConstraint || "*"),
scopes: [],
});
});
return {
language: Languages.PYTHON,
system: "pip",
sourceUrl: "",
name: inferPythonName(url),
version: "*",
dependencies,
};
}
public matchConfig(): MatchConfig {
return {
includes: [ "**/requirements.txt" ],
excludes: [],
};
}
public requires(): string[] {
return [ "requirements.txt" ];
}
}