From: Sebastien Douheret Date: Mon, 22 Jan 2018 13:17:39 +0000 (+0100) Subject: Migrate db-dump script in python avoid nodejs dep X-Git-Tag: 5.0.1~2 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-server.git;a=commitdiff_plain;h=297955b744110f67dad475e628bf068ec849b190 Migrate db-dump script in python avoid nodejs dep - Also, workaround SPEC-1252 matching first detected SDK and break. - Fixed SDK date extraction. Bug-AGL: SPEC-1249 Change-Id: Ia1cb3e9466d0f8f958f6e8759287f801a5b81d04 Signed-off-by: Romain Forlot Signed-off-by: Sebastien Douheret --- diff --git a/scripts/sdks/agl/db-dump b/scripts/sdks/agl/db-dump index aa8b30d..77f729e 100755 --- a/scripts/sdks/agl/db-dump +++ b/scripts/sdks/agl/db-dump @@ -1,137 +1,130 @@ -#! /usr/bin/env nodejs - -/************************************************************************** - * Copyright 2017-2018 IoT.bzh - * - * author: Sebastien Douheret - * - * 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 +#!/usr/bin/python +# +#/************************************************************************** +# * Copyright 2017-2018 IoT.bzh +# * +# * author: Romain Forlot +# * +# * 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. +# **************************************************************************/ + +import os +import json +import logging +import inspect +import fnmatch +import argparse +import subprocess + +PARSER = argparse.ArgumentParser(description='Lists available and installed SDKs') +PARSER.add_argument('-debug', dest='debug', action='store_true', + help='Output debug log messages') + +ARGS = PARSER.parse_args() + +if ARGS.debug: + logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s: %(message)s') +else: + logging.basicConfig(level=logging.INFO, format='%(asctime)s:%(levelname)s: %(message)s') + +SCRIPT_PATH = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + +ENV = subprocess.check_output([os.path.join(SCRIPT_PATH, './_env-init.sh'), '-print']).splitlines() + +for elt in ENV: + k, v = elt.split('=', 1) + if k == 'SDK_ROOT_DIR': + SDK_ROOT_DIR = v + elif k == 'SDK_ENV_SETUP_FILENAME': + SDK_ENV_SETUP_FILENAME = v + +if SDK_ROOT_DIR is None: + logging.error('No SDK_ROOT_DIR environment variable found.') + exit(1) +elif SDK_ENV_SETUP_FILENAME is None: + SDK_ENV_SETUP_FILENAME = 'environment-setup*' + +# Get list of available SDKs +SDK_DB_FILEPATH = os.path.join(SDK_ROOT_DIR, "sdks_latest.json") + +if not os.path.exists(SDK_DB_FILEPATH): + DB_UPDATE_FILEPATH = os.path.join(SCRIPT_PATH, 'db-update') + os.system(DB_UPDATE_FILEPATH + " " + SDK_DB_FILEPATH) + +SDK_DB_JSON = json.load(open(SDK_DB_FILEPATH, 'r')) + +for one_sdk in SDK_DB_JSON: + one_sdk['status'] = 'Not Installed' + +INSTALLED_SDK = [] +for root, dirs, files in os.walk(SDK_ROOT_DIR): + depth = root[len(SDK_ROOT_DIR) + len(os.path.sep):].count(os.path.sep) + if depth >= 4: + dirs[:] = [] + EF, VF = '', '' + for one_file in files: + if fnmatch.fnmatch(one_file, SDK_ENV_SETUP_FILENAME): + EF = os.path.join(root, one_file) + if fnmatch.fnmatch(one_file, 'version-*'): + VF = os.path.join(root, one_file) + if EF != '' and VF != '': + INSTALLED_SDK.append({'ENV_FILE': EF, 'VERSION_FILE': VF}) + +for one_sdk in INSTALLED_SDK: + logging.debug("Processing %s", one_sdk['ENV_FILE']) + PROFILE = one_sdk['ENV_FILE'].split('/')[3] + VERSION = one_sdk['ENV_FILE'].split('/')[4] + ARCH = one_sdk['ENV_FILE'].split('/')[5] + DIR = os.path.dirname(one_sdk['ENV_FILE']) + if PROFILE == '' or VERSION == '' or ARCH == '' or DIR == '': + logging.debug('Path not compliant, skipping') + continue + + SDK_DATE = '' + for line in open(one_sdk['VERSION_FILE']).readlines(): + if line.startswith('Timestamp'): + D = line.split(':')[1] + if D: + D = D.strip() + SDK_DATE = '{}-{}-{} {}:{}'.format(D[0:4], D[4:6], D[6:8], D[8:10], D[10:12]) + logging.debug('Found date: %s', SDK_DATE) + + found = False + for sdk in SDK_DB_JSON: + if sdk['profile'] == PROFILE and sdk['version'] == VERSION and sdk['arch'] == ARCH: + found = True + sdk['status'] = 'Installed' + sdk['date'] = SDK_DATE + sdk['setupFile'] = one_sdk['ENV_FILE'] + sdk['path'] = DIR + break + + if not found: + logging.debug('Not found in database, adding it...') + NEW_SDK = { + 'name': PROFILE + '-' + ARCH + '-' + VERSION, + 'description': 'AGL SDK ' + ARCH + ' (version ' + VERSION + ')', + 'profile': PROFILE, + 'version': VERSION, + 'arch': ARCH, + 'path': DIR, + 'url': "", + 'status': "Installed", + 'date': SDK_DATE, + 'size': "", + 'md5sum': "", + 'setupFile': one_sdk['ENV_FILE'] } - }); - 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) + SDK_DB_JSON.append(NEW_SDK) + +print json.dumps(SDK_DB_JSON)