1 | import fs from 'fs'
|
2 |
|
3 | type PythonSystemPackageLookupMap = Map<string, Map<string, Map<string, Map<string, Array<string> | null>>>>
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | function valueToMap (value: any): any {
|
10 | if (!Array.isArray(value) && typeof value === 'object') {
|
11 | let m = new Map<any, any>()
|
12 | for (let key in value) {
|
13 | if (!value.hasOwnProperty(key)) {
|
14 | continue
|
15 | }
|
16 |
|
17 | m.set(key, valueToMap(value[key]))
|
18 | }
|
19 | return m
|
20 | }
|
21 | return value
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | export default class PythonSystemPackageLookup {
|
29 | |
30 |
|
31 |
|
32 | constructor (private readonly packageLookup: PythonSystemPackageLookupMap) {
|
33 | }
|
34 |
|
35 | |
36 |
|
37 |
|
38 | static fromFile (path: string): PythonSystemPackageLookup {
|
39 | const dependencyLookupRaw = JSON.parse(fs.readFileSync(path, 'utf8'))
|
40 | return new PythonSystemPackageLookup(valueToMap(dependencyLookupRaw))
|
41 | }
|
42 |
|
43 | |
44 |
|
45 |
|
46 |
|
47 | lookupSystemPackage (pythonPackage: string, pythonMajorVersion: number, systemPackageType: string, systemVersion: string): Array<string> {
|
48 | const pyPackageMap = this.packageLookup.get(pythonPackage)
|
49 |
|
50 | if (!pyPackageMap) {
|
51 | return []
|
52 | }
|
53 |
|
54 | const pyVersionStr = `${pythonMajorVersion}`
|
55 |
|
56 | let pyVersionMap
|
57 |
|
58 | if (pyPackageMap.has(pyVersionStr)) {
|
59 | pyVersionMap = pyPackageMap.get(pyVersionStr)
|
60 | } else {
|
61 | pyVersionMap = pyPackageMap.get('default')
|
62 | }
|
63 |
|
64 | if (!pyVersionMap) {
|
65 | return []
|
66 | }
|
67 |
|
68 | let systemVersionMap
|
69 |
|
70 | if (pyVersionMap.has(systemPackageType)) {
|
71 | systemVersionMap = pyVersionMap.get(systemPackageType)
|
72 | } else {
|
73 | systemVersionMap = pyVersionMap.get('default')
|
74 | }
|
75 |
|
76 | if (!systemVersionMap) {
|
77 | return []
|
78 | }
|
79 |
|
80 | let systemPackages
|
81 |
|
82 | if (systemVersionMap.has(systemVersion)) {
|
83 | systemPackages = systemVersionMap.get(systemVersion)
|
84 | } else {
|
85 | systemPackages = systemVersionMap.get('default')
|
86 | }
|
87 |
|
88 | return systemPackages || []
|
89 | }
|
90 | }
|