4
4
import subprocess
5
5
import logging
6
6
import argparse
7
- from os .path import dirname , abspath , join
7
+ from os .path import dirname , abspath , join , isfile , normpath
8
8
9
9
# Be sure that the tools directory is in the search path
10
10
ROOT = abspath (join (dirname (__file__ ), "../.." ))
@@ -22,10 +22,10 @@ def del_file(name):
22
22
for path in search_path :
23
23
for root , dirs , files in os .walk (path ):
24
24
if name in files :
25
- result .append (os . path . join (root , name ))
25
+ result .append (join (root , name ))
26
26
for file in result :
27
27
os .remove (file )
28
- rel_log .debug ("Deleted: %s" , os .path .relpath (file , ROOT ))
28
+ rel_log .debug ("Deleted %s" , os .path .relpath (file , ROOT ))
29
29
30
30
def copy_folder (src , dest ):
31
31
""" Copy contents of folder in mbed-os listed path
@@ -35,10 +35,10 @@ def copy_folder(src, dest):
35
35
"""
36
36
files = os .listdir (src )
37
37
for file in files :
38
- abs_src_file = os . path . join (src , file )
38
+ abs_src_file = join (src , file )
39
39
if os .path .isfile (abs_src_file ):
40
- abs_dst_file = os . path . join (dest , file )
41
- mkdir (os . path . dirname (abs_dst_file ))
40
+ abs_dst_file = join (dest , file )
41
+ mkdir (dirname (abs_dst_file ))
42
42
copy_file (abs_src_file , abs_dst_file )
43
43
44
44
def run_cmd_with_output (command , exit_on_failure = False ):
@@ -60,7 +60,7 @@ def run_cmd_with_output(command, exit_on_failure=False):
60
60
returncode = 0
61
61
output = ""
62
62
try :
63
- output = subprocess .check_output (command , shell = True )
63
+ output = subprocess .check_output (command )
64
64
except subprocess .CalledProcessError as e :
65
65
returncode = e .returncode
66
66
@@ -81,7 +81,7 @@ def get_curr_sha(repo_path):
81
81
cwd = os .getcwd ()
82
82
os .chdir (abspath (repo_path ))
83
83
84
- cmd = " git log --pretty=format:%h -n 1"
84
+ cmd = [ ' git' , ' log' , ' --pretty=format:%h' , '-n' , '1' ]
85
85
_ , sha = run_cmd_with_output (cmd , exit_on_failure = True )
86
86
87
87
os .chdir (cwd )
@@ -96,7 +96,7 @@ def branch_exists(name):
96
96
True - If branch is already present
97
97
"""
98
98
99
- cmd = " git branch"
99
+ cmd = [ ' git' , ' branch' ]
100
100
_ , output = run_cmd_with_output (cmd , exit_on_failure = False )
101
101
if name in output :
102
102
return True
@@ -108,8 +108,9 @@ def branch_checkout(name):
108
108
Args:
109
109
name - branch name
110
110
"""
111
- cmd = "git checkout " + name
112
- run_cmd_with_output (cmd , exit_on_failure = False )
111
+ cmd = ['git' , 'checkout' , name ]
112
+ _ , _ = run_cmd_with_output (cmd , exit_on_failure = False )
113
+ rel_log .info ("Checkout to branch %s" , name )
113
114
114
115
def get_last_cherry_pick_sha (branch ):
115
116
"""
@@ -120,11 +121,11 @@ def get_last_cherry_pick_sha(branch):
120
121
branch - Hash to be verified.
121
122
Returns - SHA if found, else None
122
123
"""
123
- cmd = " git checkout " + branch
124
+ cmd = [ ' git' , ' checkout' , branch ]
124
125
run_cmd_with_output (cmd , exit_on_failure = False )
125
126
126
127
sha = None
127
- get_commit = " git log -n 1"
128
+ get_commit = [ ' git' , ' log' , '-n' , '1' ]
128
129
_ , output = run_cmd_with_output (get_commit , exit_on_failure = True )
129
130
lines = output .split ('\n ' )
130
131
for line in lines :
@@ -159,12 +160,12 @@ def get_last_cherry_pick_sha(branch):
159
160
rel_log .error ("Repository path and config file required as input. Use \" --help\" for more info." )
160
161
exit (1 )
161
162
162
- json_file = os . path . abspath (args .config_file )
163
+ json_file = abspath (args .config_file )
163
164
if not os .path .isfile (json_file ):
164
165
rel_log .error ("%s not found." , args .config_file )
165
166
exit (1 )
166
167
167
- repo = os . path . abspath (args .repo_path )
168
+ repo = abspath (args .repo_path )
168
169
if not os .path .exists (repo ):
169
170
rel_log .error ("%s not found." , args .repo_path )
170
171
exit (1 )
@@ -197,39 +198,43 @@ def get_last_cherry_pick_sha(branch):
197
198
for file in data_files :
198
199
src_file = file ['src_file' ]
199
200
del_file (os .path .basename (src_file ))
201
+ dest_file = join (ROOT , file ['dest_file' ])
202
+ if isfile (dest_file ):
203
+ os .remove (join (ROOT , dest_file ))
204
+ rel_log .debug ("Deleted %s" , file ['dest_file' ])
200
205
201
206
for folder in data_folders :
202
207
dest_folder = folder ['dest_folder' ]
203
208
delete_dir_files (dest_folder )
204
- rel_log .debug ("Deleted = %s" , folder )
209
+ rel_log .debug ("Deleted: %s" , folder [ 'dest_folder' ] )
205
210
206
211
rel_log .info ("Removed files/folders listed in json file" )
207
212
208
- ## Copy all the CMSIS files listed in json file to mbed-os
213
+ ## Copy all the files listed in json file to mbed-os
209
214
for file in data_files :
210
- repo_file = os . path . join (repo , file ['src_file' ])
211
- mbed_path = os . path . join (ROOT , file ['dest_file' ])
212
- mkdir (os . path . dirname (mbed_path ))
215
+ repo_file = join (repo , file ['src_file' ])
216
+ mbed_path = join (ROOT , file ['dest_file' ])
217
+ mkdir (dirname (mbed_path ))
213
218
copy_file (repo_file , mbed_path )
214
- rel_log .debug ("Copied = %s" , mbed_path )
219
+ rel_log .debug ("Copied %s to %s" , normpath ( repo_file ), normpath ( mbed_path ) )
215
220
216
221
for folder in data_folders :
217
- repo_folder = os . path . join (repo , folder ['src_folder' ])
218
- mbed_path = os . path . join (ROOT , folder ['dest_folder' ])
222
+ repo_folder = join (repo , folder ['src_folder' ])
223
+ mbed_path = join (ROOT , folder ['dest_folder' ])
219
224
copy_folder (repo_folder , mbed_path )
220
- rel_log .debug ("Copied = %s" , mbed_path )
225
+ rel_log .debug ("Copied %s to %s" , normpath ( repo_folder ), normpath ( mbed_path ) )
221
226
222
227
## Create new branch with all changes
223
- create_branch = " git checkout -b " + branch
228
+ create_branch = [ ' git' , ' checkout' , '-b' , branch ]
224
229
run_cmd_with_output (create_branch , exit_on_failure = True )
225
- rel_log .info ("Branch created = %s" , branch )
230
+ rel_log .info ("Branch created: %s" , branch )
226
231
227
- add_files = " git add -A"
232
+ add_files = [ ' git' , ' add' , '-A' ]
228
233
run_cmd_with_output (add_files , exit_on_failure = True )
229
234
230
- commit_branch = " git commit -m \" " + commit_msg + " \" "
235
+ commit_branch = [ ' git' , ' commit' , '-m' , commit_msg ]
231
236
run_cmd_with_output (commit_branch , exit_on_failure = True )
232
- rel_log .info (" Commit added = %s", mbed_path )
237
+ rel_log .info (' Commit added: " %s"' , commit_msg )
233
238
234
239
## Checkout the feature branch
235
240
branch_checkout (branch )
@@ -238,9 +243,9 @@ def get_last_cherry_pick_sha(branch):
238
243
if not last_sha :
239
244
## Apply commits specific to mbed-os changes
240
245
for sha in commit_sha :
241
- cherry_pick_sha = " git cherry-pick -x " + sha
246
+ cherry_pick_sha = [ ' git' , ' cherry-pick' , '-x' , sha ]
242
247
run_cmd_with_output (cherry_pick_sha , exit_on_failure = True )
243
- rel_log .info ("Commit added = %s" , cherry_pick_sha )
248
+ rel_log .info ("Cherry-picked commit = %s" , sha )
244
249
## Few commits are already applied, check the next in sequence
245
250
## and skip to last applied
246
251
else :
@@ -250,5 +255,6 @@ def get_last_cherry_pick_sha(branch):
250
255
found = True
251
256
continue
252
257
if found is True :
253
- cherry_pick_sha = " git cherry-pick -x " + sha
258
+ cherry_pick_sha = [ ' git' , ' cherry-pick' , '-x' , sha ]
254
259
run_cmd_with_output (cherry_pick_sha , exit_on_failure = True )
260
+ rel_log .info ("Cherry-picked commit = %s" , sha )
0 commit comments