aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/sdks/agl/db-dump
blob: aa8b30df5b72055302aa2c90643d9ce73b634179 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#! /usr/bin/env nodejs

/**************************************************************************
 * Copyright 2017-2018 IoT.bzh
 *
 * author: Sebastien Douheret <sebastien@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **************************************************************************/

const fs = require('fs');
const process = require('process');
const execSync = require('child_process').execSync;
const path = require('path');


// Only used for debug purpose
const DEBUG = false || (process.argv.length > 2 && process.argv[2] == '-debug');
dbgPrint = function () {
    if (DEBUG) console.log.apply(console, arguments);
}
// Get env vars
var envMap = {};
envData = execSync(path.join(__dirname, '_env-init.sh -print'));
envData.toString().split('\n').forEach(e => envMap[e.split('=')[0]] = e.split('=')[1]);
const opts = {
    cwd: __dirname,
    env: envMap
};

// Get list of available SDKs
sdksDBFile = path.join(envMap["SDK_ROOT_DIR"], "sdks_latest.json")
try {
    // Fetch SDK Json database file when not existing
    if (!fs.existsSync(sdksDBFile)) {
        var data = execSync(path.join(__dirname, 'db-update ' + sdksDBFile), opts);
    }
    // Read SDK Json database file
    var data = fs.readFileSync(sdksDBFile);
    var sdks = JSON.parse(data);

    // Force some default fields value
    sdks.forEach(sdk => {
        sdk.status = 'Not Installed';
    });
} catch (err) {
    dbgPrint('ERROR: ', err);
    process.exit(-1)
}

// Get list of installed SDKs
try {
    const cmd = 'find "${SDK_ROOT_DIR}" -maxdepth 4 -name "${SDK_ENV_SETUP_FILENAME}"';
    var data = execSync(cmd, opts);
    data.toString().split('\n').forEach(envFile => {
        if (envFile == '') return;

        dbgPrint('Processing ', envFile);
        const profile = envFile.split('/')[3];
        const version = envFile.split('/')[4];
        const arch = envFile.split('/')[5];
        const dir = path.dirname(envFile);
        if (profile == '' || version == '' || arch == '' || dir == '') {
            return;
        }

        sdkDate = ''
        versionFile = path.join(path.dirname(envFile), 'version-*')
        try {
            cmdVer = "[ -f " + versionFile + " ] && grep Timestamp " + versionFile + " |cut -d' ' -f2"
            var data = execSync(cmdVer);
        } catch (err) {
            dbgPrint('IGNORING SDK ', dir);
            dbgPrint(err.toString());
            if (DEBUG) {
                process.exit(-1);
            } else {
                return;
            }
        }
        d = data.toString()
        if (d != "") {
            sdkDate = d.substring(0, 4) + "-" + d.substring(4, 6) + "-" + d.substring(6, 8)
            sdkDate += " " + d.substring(8, 10) + ":" + d.substring(10, 12)
        }

        var found = false;
        sdks.forEach(sdk => {
            // Update sdk with local info when found
            if (profile == sdk.profile && version == sdk.version && arch == sdk.arch) {
                found = true;
                dbgPrint(" OK found, updating...");
                sdk.path = dir;
                sdk.status = 'Installed';
                sdk.data = sdkDate;
                sdk.setupFile = envFile;
                return
            }
        });
        if (found == false) {
            dbgPrint(" NOT found in database, adding it...");
            sdks.push({
                name: profile + '-' + arch + '-' + version,
                description: 'AGL SDK ' + arch + ' (version ' + version + ')',
                profile: profile,
                version: version,
                arch: arch,
                path: dir,
                url: "",
                status: "Installed",
                date: sdkDate,
                size: "",
                md5sum: "",
                setupFile: envFile
            });
        }
    });

} catch (err) {
    dbgPrint('ERROR: ', err);
    process.exit(-1)
}

// Print result
console.log(JSON.stringify(sdks));

process.exit(0)