Skip to content

Commit edcde5e

Browse files
author
Oren Cohen
committed
Reformat and Improve
1 parent f28b82b commit edcde5e

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

tools/importer/importer.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,44 @@
2525
from os.path import dirname, abspath, join, isfile, normpath
2626

2727
# 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))
2929
sys.path.insert(0, ROOT)
3030

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+
3233

3334
def del_file(name):
3435
""" Delete the file in RTOS/CMSIS/features directory of mbed-os
3536
Args:
3637
name - name of the file
3738
"""
3839
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')]
4042
for path in search_path:
4143
for root, dirs, files in os.walk(path):
4244
if name in files:
4345
result.append(join(root, name))
44-
for file in result:
45-
os.remove(file)
46+
for f in result:
47+
os.remove(f)
4648
rel_log.debug("Deleted %s", os.path.relpath(file, ROOT))
4749

50+
4851
def copy_folder(src, dest):
4952
""" Copy contents of folder in mbed-os listed path
5053
Args:
5154
src - src folder path
5255
dest - destination folder path
5356
"""
5457
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)
5760
if os.path.isfile(abs_src_file):
58-
abs_dst_file = join(dest, file)
61+
abs_dst_file = join(dest, f)
5962
mkdir(dirname(abs_dst_file))
6063
copy_file(abs_src_file, abs_dst_file)
6164

65+
6266
def run_cmd_with_output(command, exit_on_failure=False):
6367
""" Passes a command to the system and returns a True/False result once the
6468
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):
8488

8589
if exit_on_failure:
8690
rel_log.error("The command %s failed with return code: %s",
87-
(' '.join(command)), returncode)
91+
(' '.join(command)), returncode)
8892
sys.exit(1)
8993
return returncode, output
9094

95+
9196
def get_curr_sha(repo_path):
9297
""" Gets the latest SHA for the specified repo
9398
Args:
@@ -96,14 +101,12 @@ def get_curr_sha(repo_path):
96101
Returns:
97102
sha - last commit SHA
98103
"""
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)
101107

102-
cmd = ['git', 'log', '--pretty=format:%h', '-n', '1']
103-
_, sha = run_cmd_with_output(cmd, exit_on_failure=True)
108+
return _sha
104109

105-
os.chdir(cwd)
106-
return sha
107110

108111
def branch_exists(name):
109112
""" Check if branch already exists in mbed-os local repository.
@@ -120,6 +123,7 @@ def branch_exists(name):
120123
return True
121124
return False
122125

126+
123127
def branch_checkout(name):
124128
"""
125129
Checkout the required branch
@@ -130,6 +134,7 @@ def branch_checkout(name):
130134
_, _ = run_cmd_with_output(cmd, exit_on_failure=False)
131135
rel_log.info("Checkout to branch %s", name)
132136

137+
133138
def get_last_cherry_pick_sha(branch):
134139
"""
135140
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):
152157
return sha[:-1]
153158
return sha
154159

160+
155161
if __name__ == "__main__":
156162

157163
parser = argparse.ArgumentParser(description=__doc__,
@@ -175,23 +181,25 @@ def get_last_cherry_pick_sha(branch):
175181
rel_log = logging.getLogger("Importer")
176182

177183
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)
180188

181189
json_file = abspath(args.config_file)
182190
if not os.path.isfile(json_file):
183191
rel_log.error("%s not found.", args.config_file)
184-
exit(1)
192+
sys.exit(1)
185193

186194
repo = abspath(args.repo_path)
187195
if not os.path.exists(repo):
188196
rel_log.error("%s not found.", args.repo_path)
189-
exit(1)
197+
sys.exit(1)
190198

191199
sha = get_curr_sha(repo)
192200
if not sha:
193201
rel_log.error("Could not obtain latest SHA")
194-
exit(1)
202+
sys.exit(1)
195203
rel_log.info("%s SHA = %s", os.path.basename(repo), sha)
196204

197205
branch = 'feature_' + os.path.basename(repo) + '_' + sha
@@ -212,14 +220,14 @@ def get_last_cherry_pick_sha(branch):
212220
data_files = json_data["files"]
213221
data_folders = json_data["folders"]
214222

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']
218226
del_file(os.path.basename(src_file))
219-
dest_file = join(ROOT, file['dest_file'])
227+
dest_file = join(ROOT, fh['dest_file'])
220228
if isfile(dest_file):
221229
os.remove(join(ROOT, dest_file))
222-
rel_log.debug("Deleted %s", file['dest_file'])
230+
rel_log.debug("Deleted %s", fh['dest_file'])
223231

224232
for folder in data_folders:
225233
dest_folder = folder['dest_folder']
@@ -228,21 +236,23 @@ def get_last_cherry_pick_sha(branch):
228236

229237
rel_log.info("Removed files/folders listed in json file")
230238

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'])
235243
mkdir(dirname(mbed_path))
236244
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))
238247

239248
for folder in data_folders:
240249
repo_folder = join(repo, folder['src_folder'])
241250
mbed_path = join(ROOT, folder['dest_folder'])
242251
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))
244254

245-
## Create new branch with all changes
255+
# Create new branch with all changes
246256
create_branch = ['git', 'checkout', '-b', branch]
247257
run_cmd_with_output(create_branch, exit_on_failure=True)
248258
rel_log.info("Branch created: %s", branch)
@@ -254,7 +264,7 @@ def get_last_cherry_pick_sha(branch):
254264
run_cmd_with_output(commit_branch, exit_on_failure=True)
255265
rel_log.info('Commit added: "%s"', commit_msg)
256266

257-
## Checkout the feature branch
267+
# Checkout the feature branch
258268
branch_checkout(branch)
259269
commit_sha = json_data["commit_sha"]
260270
last_sha = get_last_cherry_pick_sha(branch)

0 commit comments

Comments
 (0)