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 # **************************************************************************/
31 PARSER = argparse.ArgumentParser(
32 description='Lists available and installed SDKs')
33 PARSER.add_argument('-debug', dest='debug', action='store_true',
34 help='Output debug log messages')
36 ARGS = PARSER.parse_args()
39 logging.basicConfig(level=logging.DEBUG,
40 format='%(asctime)s:%(levelname)s: %(message)s')
42 logging.basicConfig(level=logging.INFO,
43 format='%(asctime)s:%(levelname)s: %(message)s')
45 SCRIPT_PATH = os.path.dirname(os.path.abspath(
46 inspect.getfile(inspect.currentframe())))
48 ENV = subprocess.check_output(
49 [os.path.join(SCRIPT_PATH, './_env-init.sh'), '-print']).splitlines()
53 #only match what defines a variable
54 z = re.match(r"^(\w+)=([^']*)$", elt.decode())
58 if k == 'SDK_ROOT_DIR':
59 SDK_ROOT_DIR = v.rstrip('/')
60 elif k == 'SDK_ENV_SETUP_FILENAME':
61 SDK_ENV_SETUP_FILENAME = v
63 if SDK_ROOT_DIR is None:
64 logging.error('No SDK_ROOT_DIR environment variable found.')
66 elif SDK_ENV_SETUP_FILENAME is None:
67 SDK_ENV_SETUP_FILENAME = 'environment-setup*'
69 # Get list of available SDKs
70 SDK_DB_FILEPATH = os.path.join(SDK_ROOT_DIR, "sdks_latest.json")
72 if not os.path.exists(SDK_DB_FILEPATH):
73 DB_UPDATE_FILEPATH = os.path.join(SCRIPT_PATH, 'db-update')
74 os.system(DB_UPDATE_FILEPATH + " " + SDK_DB_FILEPATH)
76 SDK_DB_JSON = json.load(open(SDK_DB_FILEPATH, 'r'))
78 for one_sdk in SDK_DB_JSON:
79 one_sdk['status'] = 'Not Installed'
83 for root, dirs, files in os.walk(SDK_ROOT_DIR):
84 depth = root[len(SDK_ROOT_DIR) + len(os.path.sep):].count(os.path.sep)
85 # Limit the walking depth of processed directories
88 # Only process SDK dir matching profile/version/arch or
89 # profile/version/arch/tag
90 elif depth != 2 and depth != 3:
93 for one_file in files:
94 if fnmatch.fnmatch(one_file, SDK_ENV_SETUP_FILENAME):
95 EF = os.path.join(root, one_file)
96 if fnmatch.fnmatch(one_file, 'version-*'):
97 VF = os.path.join(root, one_file)
98 if EF != '' and VF != '':
99 logging.debug('Adding installed SDK ' + root)
100 INSTALLED_SDK.append({'ENV_FILE': EF, 'VERSION_FILE': VF})
101 elif (EF == '' and VF != '') or (EF != '' and VF == ''):
103 'WARNING SDK ignored : root=%s, EnvFile=%s, VersFile=%s', root, EF, VF)
105 for one_sdk in INSTALLED_SDK:
106 logging.debug("Processing %s", one_sdk['ENV_FILE'])
107 envFile = one_sdk['ENV_FILE'].split(SDK_ROOT_DIR+'/')[1]
108 PROFILE = envFile.split('/')[0]
109 VERSION = envFile.split('/')[1]
110 ARCH = envFile.split('/')[2]
111 DIR = os.path.dirname(one_sdk['ENV_FILE'])
112 if PROFILE == '' or VERSION == '' or ARCH == '' or DIR == '':
113 logging.debug('Path not compliant, skipping')
116 UUID = os.path.basename(os.path.normpath(DIR))
119 for line in open(one_sdk['VERSION_FILE']).readlines():
120 if line.startswith('Timestamp'):
121 D = line.split(':')[1]
124 SDK_DATE = '{}-{}-{} {}:{}'.format(
125 D[0:4], D[4:6], D[6:8], D[8:10], D[10:12])
126 logging.debug('Found date: %s', SDK_DATE)
129 for sdk in SDK_DB_JSON:
130 if sdk['profile'] == PROFILE and sdk['version'] == VERSION and sdk['arch'] == ARCH:
131 if sdk['status'] == 'Installed':
133 # Additional verification based on url used to generate UUID (see also same logic in add script)
136 ps = subprocess.Popen(('echo', sdk['url']), stdout=subprocess.PIPE)
137 uuid_md5 = subprocess.check_output(('md5sum'), stdin=ps.stdout)
139 if str(uuid_md5).split(' ')[0][2:] != UUID:
142 e = sys.exc_info()[0]
143 logging.error("Error while checking UUID: " % e)
146 sdk['status'] = 'Installed'
147 sdk['date'] = SDK_DATE
148 sdk['setupFile'] = one_sdk['ENV_FILE']
154 logging.debug('Not found in database, add: ' +
155 PROFILE + '-' + ARCH + '-' + VERSION)
157 'name': PROFILE + '-' + ARCH + '-' + VERSION,
159 'description': 'AGL SDK ' + ARCH + ' (version ' + VERSION + ')',
165 'status': "Installed",
169 'setupFile': one_sdk['ENV_FILE']
171 SDK_DB_JSON.append(NEW_SDK)
173 print(json.dumps(SDK_DB_JSON))