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(
30 description='Lists available and installed SDKs')
31 PARSER.add_argument('-debug', dest='debug', action='store_true',
32 help='Output debug log messages')
34 ARGS = PARSER.parse_args()
37 logging.basicConfig(level=logging.DEBUG,
38 format='%(asctime)s:%(levelname)s: %(message)s')
40 logging.basicConfig(level=logging.INFO,
41 format='%(asctime)s:%(levelname)s: %(message)s')
43 SCRIPT_PATH = os.path.dirname(os.path.abspath(
44 inspect.getfile(inspect.currentframe())))
46 ENV = subprocess.check_output(
47 [os.path.join(SCRIPT_PATH, './_env-init.sh'), '-print']).splitlines()
51 k, v = elt.decode().split('=', 1)
52 if k == 'SDK_ROOT_DIR':
53 SDK_ROOT_DIR = v.rstrip('/')
54 elif k == 'SDK_ENV_SETUP_FILENAME':
55 SDK_ENV_SETUP_FILENAME = v
57 if SDK_ROOT_DIR is None:
58 logging.error('No SDK_ROOT_DIR environment variable found.')
60 elif SDK_ENV_SETUP_FILENAME is None:
61 SDK_ENV_SETUP_FILENAME = 'environment-setup*'
63 # Get list of available SDKs
64 SDK_DB_FILEPATH = os.path.join(SDK_ROOT_DIR, "sdks_latest.json")
66 if not os.path.exists(SDK_DB_FILEPATH):
67 DB_UPDATE_FILEPATH = os.path.join(SCRIPT_PATH, 'db-update')
68 os.system(DB_UPDATE_FILEPATH + " " + SDK_DB_FILEPATH)
70 SDK_DB_JSON = json.load(open(SDK_DB_FILEPATH, 'r'))
72 for one_sdk in SDK_DB_JSON:
73 one_sdk['status'] = 'Not Installed'
76 for root, dirs, files in os.walk(SDK_ROOT_DIR):
77 depth = root[len(SDK_ROOT_DIR) + len(os.path.sep):].count(os.path.sep)
78 # Limit the walking depth of processed directories
81 # Only process SDK dir matching profile/version/arch or
82 # profile/version/arch/tag
83 elif depth != 2 and depth != 3:
86 for one_file in files:
87 if fnmatch.fnmatch(one_file, SDK_ENV_SETUP_FILENAME):
88 EF = os.path.join(root, one_file)
89 if fnmatch.fnmatch(one_file, 'version-*'):
90 VF = os.path.join(root, one_file)
91 if EF != '' and VF != '':
92 logging.debug('Adding installed SDK ' + root)
93 INSTALLED_SDK.append({'ENV_FILE': EF, 'VERSION_FILE': VF})
94 elif (EF == '' and VF != '') or (EF != '' and VF == ''):
96 'WARNING SDK ignored : root=%s, EnvFile=%s, VersFile=%s', root, EF, VF)
98 for one_sdk in INSTALLED_SDK:
99 logging.debug("Processing %s", one_sdk['ENV_FILE'])
100 envFile = one_sdk['ENV_FILE'].split(SDK_ROOT_DIR+'/')[1]
101 PROFILE = envFile.split('/')[0]
102 VERSION = envFile.split('/')[1]
103 ARCH = envFile.split('/')[2]
104 DIR = os.path.dirname(one_sdk['ENV_FILE'])
105 if PROFILE == '' or VERSION == '' or ARCH == '' or DIR == '':
106 logging.debug('Path not compliant, skipping')
110 for line in open(one_sdk['VERSION_FILE']).readlines():
111 if line.startswith('Timestamp'):
112 D = line.split(':')[1]
115 SDK_DATE = '{}-{}-{} {}:{}'.format(
116 D[0:4], D[4:6], D[6:8], D[8:10], D[10:12])
117 logging.debug('Found date: %s', SDK_DATE)
120 for sdk in SDK_DB_JSON:
121 if sdk['profile'] == PROFILE and sdk['version'] == VERSION and sdk['arch'] == ARCH:
122 if sdk['status'] == 'Installed':
125 sdk['status'] = 'Installed'
126 sdk['date'] = SDK_DATE
127 sdk['setupFile'] = one_sdk['ENV_FILE']
132 logging.debug('Not found in database, add: ' +
133 PROFILE + '-' + ARCH + '-' + VERSION)
135 'name': PROFILE + '-' + ARCH + '-' + VERSION,
136 'description': 'AGL SDK ' + ARCH + ' (version ' + VERSION + ')',
142 'status': "Installed",
146 'setupFile': one_sdk['ENV_FILE']
148 SDK_DB_JSON.append(NEW_SDK)
150 print(json.dumps(SDK_DB_JSON))