demo/secure_element/resources/generation_scripts/import_gos_sdk_utils.py

1 #
2 # EVALUATION AND USE OF THIS SOFTWARE IS SUBJECT TO THE TERMS AND
3 # CONDITIONS OF THE CONTROLLING LICENSE AGREEMENT FOUND AT LICENSE.md
4 # IN THIS SDK. IF YOU DO NOT AGREE TO THE LICENSE TERMS AND CONDITIONS,
5 # PLEASE RETURN ALL SOURCE FILES TO SILICON LABORATORIES.
6 # (c) Copyright 2018, Silicon Laboratories Inc. All rights reserved.
7 #
8 
9 # Attempt to find the <sdk root>/tools/gecko_os_utils
10 # directory and add it to the system path.
11 # This is done by moving up to parent directories and searching for either:
12 #
13 # ./tools/gecko_os_utils/common/__init__.py
14 #
15 # OR
16 #
17 # make.bat or make.sh
18 # and extracting GECKO_OS_SDK_PATH=
19 #
20 
21 
22 import sys, os
23 
24 
25 
26 GOS_UTILS_DIRECTORY = '/tools/gecko_os_utils'
27 GOS_UTILS_COMMON = '/common/__init__.py'
28 
29 
30 current_directory = os.path.abspath(__file__).replace('\\', '/')
31 
32 
33 def parse_make_script(path):
34  try:
35  with open(path, 'rb') as f:
36  for line in f.readlines():
37  index = line.find('GECKO_OS_SDK_PATH=')
38  if index == -1:
39  continue
40 
41  sdk_path = line[index + len('GECKO_OS_SDK_PATH='):].strip()
42  return sdk_path
43  except:
44  pass
45 
46 
47 def test_utils_directory(sdk_directory):
48  utils_directory = sdk_directory.replace('\\', '/') + GOS_UTILS_DIRECTORY
49 
50  if os.path.exists(utils_directory + GOS_UTILS_COMMON):
51  #print 'FOUND: %s' % utils_directory
52  sys.path.append(sdk_directory)
53  os.environ['GECKO_OS_SDK_PATH'] = os.path.realpath(sdk_directory)
54  sys.path.append(utils_directory)
55  return True
56 
57 
58 
59 
60 
61 found = False
62 while not found:
63  if current_directory.find('/') == -1 or os.path.dirname(current_directory) == current_directory:
64  break
65 
66  current_directory = os.path.dirname(current_directory)
67 
68  # print 'Current directory: %s' % current_directory
69 
70  if test_utils_directory(current_directory):
71  found = True
72  break
73 
74  make_script = current_directory + '/make.bat'
75  if os.path.exists(make_script):
76  sdk_directory = parse_make_script(make_script)
77  if sdk_directory:
78  if test_utils_directory(sdk_directory):
79  found = True
80  break
81 
82  make_script = current_directory + '/make.sh'
83  if os.path.exists(make_script):
84  sdk_directory = parse_make_script(make_script)
85  if sdk_directory:
86  if test_utils_directory(sdk_directory):
87  found = True
88  break
89 
90 if not found:
91  raise Exception('Failed to find Gecko OS Python utilities directory in SDK')
92