77f729ee36dc7d6e7d5432b3bec53fd0c9033f4c
[src/xds/xds-server.git] / scripts / sdks / agl / db-dump
1 #!/usr/bin/python
2 #
3 #/**************************************************************************
4 # * Copyright 2017-2018 IoT.bzh
5 # *
6 # * author: Romain Forlot <romain.forlot@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 import os
22 import json
23 import logging
24 import inspect
25 import fnmatch
26 import argparse
27 import subprocess
28
29 PARSER = argparse.ArgumentParser(description='Lists available and installed SDKs')
30 PARSER.add_argument('-debug', dest='debug', action='store_true',
31                     help='Output debug log messages')
32
33 ARGS = PARSER.parse_args()
34
35 if ARGS.debug:
36     logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s: %(message)s')
37 else:
38     logging.basicConfig(level=logging.INFO, format='%(asctime)s:%(levelname)s: %(message)s')
39
40 SCRIPT_PATH = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
41
42 ENV = subprocess.check_output([os.path.join(SCRIPT_PATH, './_env-init.sh'), '-print']).splitlines()
43
44 for elt in ENV:
45     k, v = elt.split('=', 1)
46     if k == 'SDK_ROOT_DIR':
47         SDK_ROOT_DIR = v
48     elif k == 'SDK_ENV_SETUP_FILENAME':
49         SDK_ENV_SETUP_FILENAME = v
50
51 if SDK_ROOT_DIR is None:
52     logging.error('No SDK_ROOT_DIR environment variable found.')
53     exit(1)
54 elif SDK_ENV_SETUP_FILENAME is None:
55     SDK_ENV_SETUP_FILENAME = 'environment-setup*'
56
57 # Get list of available SDKs
58 SDK_DB_FILEPATH = os.path.join(SDK_ROOT_DIR, "sdks_latest.json")
59
60 if not os.path.exists(SDK_DB_FILEPATH):
61     DB_UPDATE_FILEPATH = os.path.join(SCRIPT_PATH, 'db-update')
62     os.system(DB_UPDATE_FILEPATH + " " + SDK_DB_FILEPATH)
63
64 SDK_DB_JSON = json.load(open(SDK_DB_FILEPATH, 'r'))
65
66 for one_sdk in SDK_DB_JSON:
67     one_sdk['status'] = 'Not Installed'
68
69 INSTALLED_SDK = []
70 for root, dirs, files in os.walk(SDK_ROOT_DIR):
71     depth = root[len(SDK_ROOT_DIR) + len(os.path.sep):].count(os.path.sep)
72     if depth >= 4:
73         dirs[:] = []
74     EF, VF = '', ''
75     for one_file in files:
76         if fnmatch.fnmatch(one_file, SDK_ENV_SETUP_FILENAME):
77             EF = os.path.join(root, one_file)
78         if fnmatch.fnmatch(one_file, 'version-*'):
79             VF = os.path.join(root, one_file)
80     if EF != '' and VF != '':
81         INSTALLED_SDK.append({'ENV_FILE': EF, 'VERSION_FILE': VF})
82
83 for one_sdk in INSTALLED_SDK:
84     logging.debug("Processing %s", one_sdk['ENV_FILE'])
85     PROFILE = one_sdk['ENV_FILE'].split('/')[3]
86     VERSION = one_sdk['ENV_FILE'].split('/')[4]
87     ARCH = one_sdk['ENV_FILE'].split('/')[5]
88     DIR = os.path.dirname(one_sdk['ENV_FILE'])
89     if PROFILE == '' or VERSION == '' or ARCH == '' or DIR == '':
90         logging.debug('Path not compliant, skipping')
91         continue
92
93     SDK_DATE = ''
94     for line in open(one_sdk['VERSION_FILE']).readlines():
95         if line.startswith('Timestamp'):
96             D = line.split(':')[1]
97             if D:
98                 D = D.strip()
99                 SDK_DATE = '{}-{}-{} {}:{}'.format(D[0:4], D[4:6], D[6:8], D[8:10], D[10:12])
100                 logging.debug('Found date: %s', SDK_DATE)
101
102     found = False
103     for sdk in SDK_DB_JSON:
104         if sdk['profile'] == PROFILE and sdk['version'] == VERSION and sdk['arch'] == ARCH:
105             found = True
106             sdk['status'] = 'Installed'
107             sdk['date'] = SDK_DATE
108             sdk['setupFile'] = one_sdk['ENV_FILE']
109             sdk['path'] = DIR
110             break
111
112     if not found:
113         logging.debug('Not found in database, adding it...')
114         NEW_SDK = {
115             'name': PROFILE + '-' + ARCH + '-' + VERSION,
116             'description': 'AGL SDK ' + ARCH + ' (version ' + VERSION + ')',
117             'profile': PROFILE,
118             'version': VERSION,
119             'arch': ARCH,
120             'path': DIR,
121             'url': "",
122             'status': "Installed",
123             'date': SDK_DATE,
124             'size': "",
125             'md5sum': "",
126             'setupFile': one_sdk['ENV_FILE']
127             }
128         SDK_DB_JSON.append(NEW_SDK)
129
130 print json.dumps(SDK_DB_JSON)