3 #/**************************************************************************
4 # * Copyright 2017-2018 IoT.bzh
6 # * author: Romain Forlot <romain.forlot@iot.bzh>
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
12 # * http://www.apache.org/licenses/LICENSE-2.0
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 # **************************************************************************/
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')
33 ARGS = PARSER.parse_args()
36 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s: %(message)s')
38 logging.basicConfig(level=logging.INFO, format='%(asctime)s:%(levelname)s: %(message)s')
40 SCRIPT_PATH = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
42 ENV = subprocess.check_output([os.path.join(SCRIPT_PATH, './_env-init.sh'), '-print']).splitlines()
45 k, v = elt.split('=', 1)
46 if k == 'SDK_ROOT_DIR':
48 elif k == 'SDK_ENV_SETUP_FILENAME':
49 SDK_ENV_SETUP_FILENAME = v
51 if SDK_ROOT_DIR is None:
52 logging.error('No SDK_ROOT_DIR environment variable found.')
54 elif SDK_ENV_SETUP_FILENAME is None:
55 SDK_ENV_SETUP_FILENAME = 'environment-setup*'
57 # Get list of available SDKs
58 SDK_DB_FILEPATH = os.path.join(SDK_ROOT_DIR, "sdks_latest.json")
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)
64 SDK_DB_JSON = json.load(open(SDK_DB_FILEPATH, 'r'))
66 for one_sdk in SDK_DB_JSON:
67 one_sdk['status'] = 'Not Installed'
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)
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})
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')
94 for line in open(one_sdk['VERSION_FILE']).readlines():
95 if line.startswith('Timestamp'):
96 D = line.split(':')[1]
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)
103 for sdk in SDK_DB_JSON:
104 if sdk['profile'] == PROFILE and sdk['version'] == VERSION and sdk['arch'] == ARCH:
106 sdk['status'] = 'Installed'
107 sdk['date'] = SDK_DATE
108 sdk['setupFile'] = one_sdk['ENV_FILE']
113 logging.debug('Not found in database, adding it...')
115 'name': PROFILE + '-' + ARCH + '-' + VERSION,
116 'description': 'AGL SDK ' + ARCH + ' (version ' + VERSION + ')',
122 'status': "Installed",
126 'setupFile': one_sdk['ENV_FILE']
128 SDK_DB_JSON.append(NEW_SDK)
130 print json.dumps(SDK_DB_JSON)