af1d19f931c9f3c763859814aa3cf211ec88a05e
[src/xds/xds-server.git] / scripts / sdks / agl / list
1 #! /usr/bin/env nodejs
2
3 /**************************************************************************
4  * Copyright 2017 IoT.bzh
5  *
6  * author: Sebastien Douheret <sebastien@iot.bzh>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  **************************************************************************/
20
21 const fs = require('fs');
22 const process = require('process');
23 const execSync = require('child_process').execSync;
24 const path = require('path');
25
26
27 // Only used for debug purpose
28 const DEBUG = false || (process.argv.length > 2 && process.argv[2] == '-debug');
29 dbgPrint = function () {
30     if (DEBUG) console.log.apply(console, arguments);
31 }
32 // Get env vars
33 var envMap = {};
34 envData = execSync(path.join(__dirname, '_env-init.sh -print'));
35 envData.toString().split('\n').forEach(e => envMap[e.split('=')[0]] = e.split('=')[1]);
36 const opts = {
37     cwd: __dirname,
38     env: envMap
39 };
40
41 // Get list of available SDKs
42 sdksDBFile = path.join(envMap["SDK_ROOT_DIR"], "sdks_latest.json")
43 try {
44     // Fetch SDK Json database file when not existing
45     if (!fs.existsSync(sdksDBFile)) {
46
47         var data = execSync(path.join(__dirname, 'update ' + sdksDBFile), opts);
48     }
49     // Read SDK Json database file
50     var data = fs.readFileSync(sdksDBFile);
51     var sdks = JSON.parse(data);
52
53     // Force some default fields value
54     sdks.forEach(sdk => {
55         sdk.status = 'Not Installed';
56     });
57 } catch (err) {
58     dbgPrint('ERROR: ', err);
59     process.exit(-1)
60 }
61
62 // Get list of installed SDKs
63 try {
64     const cmd = 'find "${SDK_ROOT_DIR}" -maxdepth 4 -name "${SDK_ENV_SETUP_FILENAME}"';
65     var data = execSync(cmd, opts);
66     data.toString().split('\n').forEach(envFile => {
67         if (envFile == '') return;
68
69         dbgPrint('Processing ', envFile);
70         const profile = envFile.split('/')[3];
71         const version = envFile.split('/')[4];
72         const arch = envFile.split('/')[5];
73         const dir = path.dirname(envFile);
74         if (profile == '' || version == '' || arch == '' || dir == '') {
75             return;
76         }
77
78         sdkDate = ''
79         versionFile = path.join(path.dirname(envFile), 'version-*')
80         try {
81             cmdVer = "[ -f " + versionFile + " ] && grep Timestamp " + versionFile + " |cut -d' ' -f2"
82             var data = execSync(cmdVer);
83         } catch (err) {
84             dbgPrint('IGNORING SDK ', dir);
85             dbgPrint(err.toString());
86             if (DEBUG) {
87                 process.exit(-1);
88             } else {
89                 return;
90             }
91         }
92         d = data.toString()
93         if (d != "") {
94             sdkDate = d.substring(0, 4) + "-" + d.substring(4, 6) + "-" + d.substring(6, 8)
95             sdkDate += " " + d.substring(8, 10) + ":" + d.substring(10, 12)
96         }
97
98         var found = false;
99         sdks.forEach(sdk => {
100             // Update sdk with local info when found
101             if (profile == sdk.profile && version == sdk.version && arch == sdk.arch) {
102                 found = true;
103                 dbgPrint(" OK found, updating...");
104                 sdk.path = dir;
105                 sdk.status = 'Installed';
106                 sdk.data = sdkDate;
107                 sdk.setupFile = envFile;
108                 return
109             }
110         });
111         if (found == false) {
112             dbgPrint(" NOT found in database, adding it...");
113             sdks.push({
114                 name: profile + '-' + arch + '-' + version,
115                 description: 'AGL SDK ' + arch + ' (version ' + version + ')',
116                 profile: profile,
117                 version: version,
118                 arch: arch,
119                 path: dir,
120                 url: "",
121                 status: "Installed",
122                 date: sdkDate,
123                 size: "",
124                 md5sum: "",
125                 setupFile: envFile
126             });
127         }
128     });
129
130 } catch (err) {
131     dbgPrint('ERROR: ', err);
132     process.exit(-1)
133 }
134
135 // Print result
136 console.log(JSON.stringify(sdks));
137
138 process.exit(0)