Add gitlab issue/merge request templates
[src/xds/xds-server.git] / scripts / sdks / agl / db-dump
1 #! /usr/bin/env nodejs
2
3 /**************************************************************************
4  * Copyright 2017-2018 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         var data = execSync(path.join(__dirname, 'db-update ' + sdksDBFile), opts);
47     }
48     // Read SDK Json database file
49     var data = fs.readFileSync(sdksDBFile);
50     var sdks = JSON.parse(data);
51
52     // Force some default fields value
53     sdks.forEach(sdk => {
54         sdk.status = 'Not Installed';
55     });
56 } catch (err) {
57     dbgPrint('ERROR: ', err);
58     process.exit(-1)
59 }
60
61 // Get list of installed SDKs
62 try {
63     const cmd = 'find "${SDK_ROOT_DIR}" -maxdepth 4 -name "${SDK_ENV_SETUP_FILENAME}"';
64     var data = execSync(cmd, opts);
65     data.toString().split('\n').forEach(envFile => {
66         if (envFile == '') return;
67
68         dbgPrint('Processing ', envFile);
69         const profile = envFile.split('/')[3];
70         const version = envFile.split('/')[4];
71         const arch = envFile.split('/')[5];
72         const dir = path.dirname(envFile);
73         if (profile == '' || version == '' || arch == '' || dir == '') {
74             return;
75         }
76
77         sdkDate = ''
78         versionFile = path.join(path.dirname(envFile), 'version-*')
79         try {
80             cmdVer = "[ -f " + versionFile + " ] && grep Timestamp " + versionFile + " |cut -d' ' -f2"
81             var data = execSync(cmdVer);
82         } catch (err) {
83             dbgPrint('IGNORING SDK ', dir);
84             dbgPrint(err.toString());
85             if (DEBUG) {
86                 process.exit(-1);
87             } else {
88                 return;
89             }
90         }
91         d = data.toString()
92         if (d != "") {
93             sdkDate = d.substring(0, 4) + "-" + d.substring(4, 6) + "-" + d.substring(6, 8)
94             sdkDate += " " + d.substring(8, 10) + ":" + d.substring(10, 12)
95         }
96
97         var found = false;
98         sdks.forEach(sdk => {
99             // Update sdk with local info when found
100             if (profile == sdk.profile && version == sdk.version && arch == sdk.arch) {
101                 found = true;
102                 dbgPrint(" OK found, updating...");
103                 sdk.path = dir;
104                 sdk.status = 'Installed';
105                 sdk.data = sdkDate;
106                 sdk.setupFile = envFile;
107                 return
108             }
109         });
110         if (found == false) {
111             dbgPrint(" NOT found in database, adding it...");
112             sdks.push({
113                 name: profile + '-' + arch + '-' + version,
114                 description: 'AGL SDK ' + arch + ' (version ' + version + ')',
115                 profile: profile,
116                 version: version,
117                 arch: arch,
118                 path: dir,
119                 url: "",
120                 status: "Installed",
121                 date: sdkDate,
122                 size: "",
123                 md5sum: "",
124                 setupFile: envFile
125             });
126         }
127     });
128
129 } catch (err) {
130     dbgPrint('ERROR: ', err);
131     process.exit(-1)
132 }
133
134 // Print result
135 console.log(JSON.stringify(sdks));
136
137 process.exit(0)