@@ -49,6 +49,20 @@ def __call__(self, parser, namespace, values, option_string=None):
49
49
logging .basicConfig (level = values )
50
50
51
51
52
+ class DirContext (object ):
53
+ def __init__ (self , dir ):
54
+ self .dir = dir
55
+
56
+ def __enter__ (self ):
57
+ _backup_dir = os .getcwd ()
58
+ os .chdir (self .dir )
59
+ self .dir = _backup_dir
60
+ return self
61
+
62
+ def __exit__ (self , exc_type , exc_val , exc_tb ):
63
+ os .chdir (self .dir )
64
+
65
+
52
66
def del_file (name ):
53
67
"""
54
68
Delete the file in RTOS/CMSIS/features directory of mbed-os.
@@ -183,7 +197,7 @@ def branch_exists(name):
183
197
:return: True if branch is already present, False otherwise.
184
198
"""
185
199
186
- cmd = ['git' , '-C' , ROOT , ' branch' ]
200
+ cmd = ['git' , 'branch' ]
187
201
_ , output = run_cmd_with_output (cmd , exit_on_failure = False )
188
202
189
203
return name in output
@@ -210,7 +224,7 @@ def get_last_cherry_pick_sha():
210
224
:return: SHA if found, None otherwise.
211
225
"""
212
226
213
- get_commit = ['git' , '-C' , ROOT , ' log' , '-n' , '1' ]
227
+ get_commit = ['git' , 'log' , '-n' , '1' ]
214
228
_ , output = run_cmd_with_output (get_commit , exit_on_failure = True )
215
229
216
230
shas = re .findall (
@@ -235,7 +249,7 @@ def normalize_commit_sha(sha_lst):
235
249
return [_sha ['sha' ] if isinstance (_sha , dict ) else _sha for _sha in sha_lst ]
236
250
237
251
238
- if __name__ == "__main__" :
252
+ def get_parser () :
239
253
parser = argparse .ArgumentParser (
240
254
description = __doc__ ,
241
255
formatter_class = argparse .RawDescriptionHelpFormatter )
@@ -254,10 +268,11 @@ def normalize_commit_sha(sha_lst):
254
268
required = True ,
255
269
type = argparse .FileType ('r' ))
256
270
257
- if ROOT not in abspath (os .curdir ):
258
- parser .error ("This script must be run from the mbed-os directory "
259
- "to work correctly." )
271
+ return parser
260
272
273
+
274
+ def main ():
275
+ parser = get_parser ()
261
276
args = parser .parse_args ()
262
277
sha = get_curr_sha (args .repo_path )
263
278
repo_dir = os .path .basename (args .repo_path )
@@ -315,3 +330,8 @@ def normalize_commit_sha(sha_lst):
315
330
run_cmd_with_output (cherry_pick_sha , exit_on_failure = True )
316
331
317
332
rel_log .info ("Finished import successfully :)" )
333
+
334
+
335
+ if __name__ == "__main__" :
336
+ with DirContext (ROOT ):
337
+ main ()
0 commit comments