Skip to content

Commit d1fa7bd

Browse files
author
Oren Cohen
committed
Use a directory context to simplify git commands
1 parent be8a1e8 commit d1fa7bd

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

tools/importer/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,3 @@ Note: You must resolve any conflicts that arise during this cherry-pick process.
5454
For example the command below can be used to update CMSIS:
5555
`python tools\importer\importer.py -c tools\importer\cmsis_importer.json -r <path to cmsis repo>`
5656

57-
Note: This script must be run from the mbed-os directory to work correctly.

tools/importer/importer.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ def __call__(self, parser, namespace, values, option_string=None):
4949
logging.basicConfig(level=values)
5050

5151

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+
5266
def del_file(name):
5367
"""
5468
Delete the file in RTOS/CMSIS/features directory of mbed-os.
@@ -183,7 +197,7 @@ def branch_exists(name):
183197
:return: True if branch is already present, False otherwise.
184198
"""
185199

186-
cmd = ['git', '-C', ROOT, 'branch']
200+
cmd = ['git', 'branch']
187201
_, output = run_cmd_with_output(cmd, exit_on_failure=False)
188202

189203
return name in output
@@ -210,7 +224,7 @@ def get_last_cherry_pick_sha():
210224
:return: SHA if found, None otherwise.
211225
"""
212226

213-
get_commit = ['git', '-C', ROOT, 'log', '-n', '1']
227+
get_commit = ['git', 'log', '-n', '1']
214228
_, output = run_cmd_with_output(get_commit, exit_on_failure=True)
215229

216230
shas = re.findall(
@@ -235,7 +249,7 @@ def normalize_commit_sha(sha_lst):
235249
return [_sha['sha'] if isinstance(_sha, dict) else _sha for _sha in sha_lst]
236250

237251

238-
if __name__ == "__main__":
252+
def get_parser():
239253
parser = argparse.ArgumentParser(
240254
description=__doc__,
241255
formatter_class=argparse.RawDescriptionHelpFormatter)
@@ -254,10 +268,11 @@ def normalize_commit_sha(sha_lst):
254268
required=True,
255269
type=argparse.FileType('r'))
256270

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
260272

273+
274+
def main():
275+
parser = get_parser()
261276
args = parser.parse_args()
262277
sha = get_curr_sha(args.repo_path)
263278
repo_dir = os.path.basename(args.repo_path)
@@ -315,3 +330,8 @@ def normalize_commit_sha(sha_lst):
315330
run_cmd_with_output(cherry_pick_sha, exit_on_failure=True)
316331

317332
rel_log.info("Finished import successfully :)")
333+
334+
335+
if __name__ == "__main__":
336+
with DirContext(ROOT):
337+
main()

0 commit comments

Comments
 (0)