25
25
from os .path import dirname , abspath , join , isfile , normpath
26
26
27
27
# Be sure that the tools directory is in the search path
28
- ROOT = abspath (join (dirname (__file__ ), "../.." ))
28
+ ROOT = abspath (join (dirname (__file__ ), os . path . pardir , os . path . pardir ))
29
29
sys .path .insert (0 , ROOT )
30
30
31
- from tools .utils import run_cmd , delete_dir_files , mkdir , copy_file
31
+ from tools .utils import delete_dir_files , mkdir , copy_file
32
+
32
33
33
34
def del_file (name ):
34
35
""" Delete the file in RTOS/CMSIS/features directory of mbed-os
35
36
Args:
36
37
name - name of the file
37
38
"""
38
39
result = []
39
- search_path = [join (ROOT , 'rtos' ), join (ROOT , 'cmsis' ), join (ROOT , 'features' )]
40
+ search_path = [join (ROOT , 'rtos' ), join (ROOT , 'cmsis' ),
41
+ join (ROOT , 'features' )]
40
42
for path in search_path :
41
43
for root , dirs , files in os .walk (path ):
42
44
if name in files :
43
45
result .append (join (root , name ))
44
- for file in result :
45
- os .remove (file )
46
+ for f in result :
47
+ os .remove (f )
46
48
rel_log .debug ("Deleted %s" , os .path .relpath (file , ROOT ))
47
49
50
+
48
51
def copy_folder (src , dest ):
49
52
""" Copy contents of folder in mbed-os listed path
50
53
Args:
51
54
src - src folder path
52
55
dest - destination folder path
53
56
"""
54
57
files = os .listdir (src )
55
- for file in files :
56
- abs_src_file = join (src , file )
58
+ for f in files :
59
+ abs_src_file = join (src , f )
57
60
if os .path .isfile (abs_src_file ):
58
- abs_dst_file = join (dest , file )
61
+ abs_dst_file = join (dest , f )
59
62
mkdir (dirname (abs_dst_file ))
60
63
copy_file (abs_src_file , abs_dst_file )
61
64
65
+
62
66
def run_cmd_with_output (command , exit_on_failure = False ):
63
67
""" Passes a command to the system and returns a True/False result once the
64
68
command has been executed, indicating success/failure. If the command was
@@ -84,10 +88,11 @@ def run_cmd_with_output(command, exit_on_failure=False):
84
88
85
89
if exit_on_failure :
86
90
rel_log .error ("The command %s failed with return code: %s" ,
87
- (' ' .join (command )), returncode )
91
+ (' ' .join (command )), returncode )
88
92
sys .exit (1 )
89
93
return returncode , output
90
94
95
+
91
96
def get_curr_sha (repo_path ):
92
97
""" Gets the latest SHA for the specified repo
93
98
Args:
@@ -96,14 +101,12 @@ def get_curr_sha(repo_path):
96
101
Returns:
97
102
sha - last commit SHA
98
103
"""
99
- cwd = os .getcwd ()
100
- os .chdir (abspath (repo_path ))
104
+ repo_path = abspath (repo_path )
105
+ cmd = ['git' , '-C' , repo_path , 'log' , '--pretty=format:%h' , '-n' , '1' ]
106
+ _ , _sha = run_cmd_with_output (cmd , exit_on_failure = True )
101
107
102
- cmd = ['git' , 'log' , '--pretty=format:%h' , '-n' , '1' ]
103
- _ , sha = run_cmd_with_output (cmd , exit_on_failure = True )
108
+ return _sha
104
109
105
- os .chdir (cwd )
106
- return sha
107
110
108
111
def branch_exists (name ):
109
112
""" Check if branch already exists in mbed-os local repository.
@@ -120,6 +123,7 @@ def branch_exists(name):
120
123
return True
121
124
return False
122
125
126
+
123
127
def branch_checkout (name ):
124
128
"""
125
129
Checkout the required branch
@@ -130,6 +134,7 @@ def branch_checkout(name):
130
134
_ , _ = run_cmd_with_output (cmd , exit_on_failure = False )
131
135
rel_log .info ("Checkout to branch %s" , name )
132
136
137
+
133
138
def get_last_cherry_pick_sha (branch ):
134
139
"""
135
140
SHA of last cherry pick commit is returned. SHA should be added to all
@@ -152,6 +157,7 @@ def get_last_cherry_pick_sha(branch):
152
157
return sha [:- 1 ]
153
158
return sha
154
159
160
+
155
161
if __name__ == "__main__" :
156
162
157
163
parser = argparse .ArgumentParser (description = __doc__ ,
@@ -175,23 +181,25 @@ def get_last_cherry_pick_sha(branch):
175
181
rel_log = logging .getLogger ("Importer" )
176
182
177
183
if (args .repo_path is None ) or (args .config_file is None ):
178
- rel_log .error ("Repository path and config file required as input. Use \" --help\" for more info." )
179
- exit (1 )
184
+ rel_log .error (
185
+ "Repository path and config file required as input. "
186
+ "Use \" --help\" for more info." )
187
+ sys .exit (1 )
180
188
181
189
json_file = abspath (args .config_file )
182
190
if not os .path .isfile (json_file ):
183
191
rel_log .error ("%s not found." , args .config_file )
184
- exit (1 )
192
+ sys . exit (1 )
185
193
186
194
repo = abspath (args .repo_path )
187
195
if not os .path .exists (repo ):
188
196
rel_log .error ("%s not found." , args .repo_path )
189
- exit (1 )
197
+ sys . exit (1 )
190
198
191
199
sha = get_curr_sha (repo )
192
200
if not sha :
193
201
rel_log .error ("Could not obtain latest SHA" )
194
- exit (1 )
202
+ sys . exit (1 )
195
203
rel_log .info ("%s SHA = %s" , os .path .basename (repo ), sha )
196
204
197
205
branch = 'feature_' + os .path .basename (repo ) + '_' + sha
@@ -212,14 +220,14 @@ def get_last_cherry_pick_sha(branch):
212
220
data_files = json_data ["files" ]
213
221
data_folders = json_data ["folders" ]
214
222
215
- # # Remove all files listed in .json from mbed-os repo to avoid duplications
216
- for file in data_files :
217
- src_file = file ['src_file' ]
223
+ # Remove all files listed in .json from mbed-os repo to avoid duplications
224
+ for fh in data_files :
225
+ src_file = fh ['src_file' ]
218
226
del_file (os .path .basename (src_file ))
219
- dest_file = join (ROOT , file ['dest_file' ])
227
+ dest_file = join (ROOT , fh ['dest_file' ])
220
228
if isfile (dest_file ):
221
229
os .remove (join (ROOT , dest_file ))
222
- rel_log .debug ("Deleted %s" , file ['dest_file' ])
230
+ rel_log .debug ("Deleted %s" , fh ['dest_file' ])
223
231
224
232
for folder in data_folders :
225
233
dest_folder = folder ['dest_folder' ]
@@ -228,21 +236,23 @@ def get_last_cherry_pick_sha(branch):
228
236
229
237
rel_log .info ("Removed files/folders listed in json file" )
230
238
231
- ## Copy all the files listed in json file to mbed-os
232
- for file in data_files :
233
- repo_file = join (repo , file ['src_file' ])
234
- mbed_path = join (ROOT , file ['dest_file' ])
239
+ # Copy all the files listed in json file to mbed-os
240
+ for fh in data_files :
241
+ repo_file = join (repo , fh ['src_file' ])
242
+ mbed_path = join (ROOT , fh ['dest_file' ])
235
243
mkdir (dirname (mbed_path ))
236
244
copy_file (repo_file , mbed_path )
237
- rel_log .debug ("Copied %s to %s" , normpath (repo_file ), normpath (mbed_path ))
245
+ rel_log .debug ("Copied %s to %s" , normpath (repo_file ),
246
+ normpath (mbed_path ))
238
247
239
248
for folder in data_folders :
240
249
repo_folder = join (repo , folder ['src_folder' ])
241
250
mbed_path = join (ROOT , folder ['dest_folder' ])
242
251
copy_folder (repo_folder , mbed_path )
243
- rel_log .debug ("Copied %s to %s" , normpath (repo_folder ), normpath (mbed_path ))
252
+ rel_log .debug ("Copied %s to %s" , normpath (repo_folder ),
253
+ normpath (mbed_path ))
244
254
245
- ## Create new branch with all changes
255
+ # Create new branch with all changes
246
256
create_branch = ['git' , 'checkout' , '-b' , branch ]
247
257
run_cmd_with_output (create_branch , exit_on_failure = True )
248
258
rel_log .info ("Branch created: %s" , branch )
@@ -254,7 +264,7 @@ def get_last_cherry_pick_sha(branch):
254
264
run_cmd_with_output (commit_branch , exit_on_failure = True )
255
265
rel_log .info ('Commit added: "%s"' , commit_msg )
256
266
257
- ## Checkout the feature branch
267
+ # Checkout the feature branch
258
268
branch_checkout (branch )
259
269
commit_sha = json_data ["commit_sha" ]
260
270
last_sha = get_last_cherry_pick_sha (branch )
0 commit comments