1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const path_1 = __importDefault(require("path"));
|
7 | const PackageGenerator_1 = __importDefault(require("./PackageGenerator"));
|
8 | const PythonSystemPackageLookup_1 = __importDefault(require("./PythonSystemPackageLookup"));
|
9 | const GENERATED_REQUIREMENTS_FILE = '.requirements.txt';
|
10 |
|
11 |
|
12 |
|
13 | class PythonGenerator extends PackageGenerator_1.default {
|
14 |
|
15 | constructor(urlFetcher, pkg, folder, pythonMajorVersion = 3) {
|
16 | super(urlFetcher, pkg, folder);
|
17 | this.pythonMajorVersion = pythonMajorVersion;
|
18 | this.systemPackageLookup = PythonSystemPackageLookup_1.default.fromFile(path_1.default.join(__dirname, 'PythonSystemDependencies.json'));
|
19 | }
|
20 | |
21 |
|
22 |
|
23 |
|
24 | pythonVersionSuffix() {
|
25 | return this.pythonMajorVersion === 2 ? '' : `${this.pythonMajorVersion}`;
|
26 | }
|
27 | |
28 |
|
29 |
|
30 | applies() {
|
31 | return this.package.runtimePlatform === 'Python';
|
32 | }
|
33 | |
34 |
|
35 |
|
36 | aptPackages(sysVersion) {
|
37 | let aptRequirements = [];
|
38 | this.package.softwareRequirements.map(requirement => {
|
39 | aptRequirements = aptRequirements.concat(this.systemPackageLookup.lookupSystemPackage(requirement.name, this.pythonMajorVersion, 'deb', sysVersion));
|
40 | });
|
41 | let dedupedRequirements = [];
|
42 | aptRequirements.map(aptRequirement => {
|
43 | if (!dedupedRequirements.includes(aptRequirement)) {
|
44 | dedupedRequirements.push(aptRequirement);
|
45 | }
|
46 | });
|
47 | return [`python${this.pythonVersionSuffix()}`, `python${this.pythonVersionSuffix()}-pip`].concat(dedupedRequirements);
|
48 | }
|
49 | |
50 |
|
51 |
|
52 | generateRequirementsContent() {
|
53 | if (!this.package.softwareRequirements) {
|
54 | return '';
|
55 | }
|
56 | return this.filterPackages('Python').map(requirement => `${requirement.name}${requirement.version}`).join('\n');
|
57 | }
|
58 | |
59 |
|
60 |
|
61 | stencilaInstall(sysVersion) {
|
62 | return `pip${this.pythonVersionSuffix()} install --no-cache-dir https://github.com/stencila/py/archive/91a05a139ac120a89fc001d9d267989f062ad374.zip`;
|
63 | }
|
64 | |
65 |
|
66 |
|
67 |
|
68 | installFiles(sysVersion) {
|
69 | let requirementsContent = this.generateRequirementsContent();
|
70 | if (requirementsContent !== '') {
|
71 | this.write(GENERATED_REQUIREMENTS_FILE, requirementsContent);
|
72 | return [[GENERATED_REQUIREMENTS_FILE, 'requirements.txt']];
|
73 | }
|
74 | if (this.exists('requirements.txt')) {
|
75 | return [['requirements.txt', 'requirements.txt']];
|
76 | }
|
77 | return [];
|
78 | }
|
79 | |
80 |
|
81 |
|
82 | installCommand(sysVersion) {
|
83 | return `pip${this.pythonVersionSuffix()} install --requirement requirements.txt`;
|
84 | }
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 | projectFiles() {
|
91 | const pyFiles = this.glob('**/*.py');
|
92 | return pyFiles.map(file => [file, file]);
|
93 | }
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | runCommand() {
|
101 | const pyFiles = this.glob('**/*.py');
|
102 | if (pyFiles.length === 0)
|
103 | return;
|
104 | let script;
|
105 | if (pyFiles.includes('main.py'))
|
106 | script = 'main.py';
|
107 | else if (pyFiles.includes('cmd.py'))
|
108 | script = 'cmd.py';
|
109 | else
|
110 | script = pyFiles[0];
|
111 | return `python${this.pythonVersionSuffix()} ${script}`;
|
112 | }
|
113 | }
|
114 | exports.default = PythonGenerator;
|