Skip to content

Commit 28d5477

Browse files
author
Cruz Monrreal
authored
Merge pull request #9319 from kfnta/importer_fix
tools/importer script changes
2 parents 2454b25 + dba44e6 commit 28d5477

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

tools/importer/importer.py

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import subprocess
55
import logging
66
import argparse
7-
from os.path import dirname, abspath, join
7+
from os.path import dirname, abspath, join, isfile, normpath
88

99
# Be sure that the tools directory is in the search path
1010
ROOT = abspath(join(dirname(__file__), "../.."))
@@ -22,10 +22,10 @@ def del_file(name):
2222
for path in search_path:
2323
for root, dirs, files in os.walk(path):
2424
if name in files:
25-
result.append(os.path.join(root, name))
25+
result.append(join(root, name))
2626
for file in result:
2727
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))
2929

3030
def copy_folder(src, dest):
3131
""" Copy contents of folder in mbed-os listed path
@@ -35,10 +35,10 @@ def copy_folder(src, dest):
3535
"""
3636
files = os.listdir(src)
3737
for file in files:
38-
abs_src_file = os.path.join(src, file)
38+
abs_src_file = join(src, file)
3939
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))
4242
copy_file(abs_src_file, abs_dst_file)
4343

4444
def run_cmd_with_output(command, exit_on_failure=False):
@@ -60,7 +60,7 @@ def run_cmd_with_output(command, exit_on_failure=False):
6060
returncode = 0
6161
output = ""
6262
try:
63-
output = subprocess.check_output(command, shell=True)
63+
output = subprocess.check_output(command)
6464
except subprocess.CalledProcessError as e:
6565
returncode = e.returncode
6666

@@ -81,7 +81,7 @@ def get_curr_sha(repo_path):
8181
cwd = os.getcwd()
8282
os.chdir(abspath(repo_path))
8383

84-
cmd = "git log --pretty=format:%h -n 1"
84+
cmd = ['git', 'log', '--pretty=format:%h', '-n', '1']
8585
_, sha = run_cmd_with_output(cmd, exit_on_failure=True)
8686

8787
os.chdir(cwd)
@@ -96,7 +96,7 @@ def branch_exists(name):
9696
True - If branch is already present
9797
"""
9898

99-
cmd = "git branch"
99+
cmd = ['git', 'branch']
100100
_, output = run_cmd_with_output(cmd, exit_on_failure=False)
101101
if name in output:
102102
return True
@@ -108,8 +108,9 @@ def branch_checkout(name):
108108
Args:
109109
name - branch name
110110
"""
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)
113114

114115
def get_last_cherry_pick_sha(branch):
115116
"""
@@ -120,11 +121,11 @@ def get_last_cherry_pick_sha(branch):
120121
branch - Hash to be verified.
121122
Returns - SHA if found, else None
122123
"""
123-
cmd = "git checkout " + branch
124+
cmd = ['git', 'checkout', branch]
124125
run_cmd_with_output(cmd, exit_on_failure=False)
125126

126127
sha = None
127-
get_commit = "git log -n 1"
128+
get_commit = ['git', 'log', '-n', '1']
128129
_, output = run_cmd_with_output(get_commit, exit_on_failure=True)
129130
lines = output.split('\n')
130131
for line in lines:
@@ -159,12 +160,12 @@ def get_last_cherry_pick_sha(branch):
159160
rel_log.error("Repository path and config file required as input. Use \"--help\" for more info.")
160161
exit(1)
161162

162-
json_file = os.path.abspath(args.config_file)
163+
json_file = abspath(args.config_file)
163164
if not os.path.isfile(json_file):
164165
rel_log.error("%s not found.", args.config_file)
165166
exit(1)
166167

167-
repo = os.path.abspath(args.repo_path)
168+
repo = abspath(args.repo_path)
168169
if not os.path.exists(repo):
169170
rel_log.error("%s not found.", args.repo_path)
170171
exit(1)
@@ -197,39 +198,43 @@ def get_last_cherry_pick_sha(branch):
197198
for file in data_files:
198199
src_file = file['src_file']
199200
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'])
200205

201206
for folder in data_folders:
202207
dest_folder = folder['dest_folder']
203208
delete_dir_files(dest_folder)
204-
rel_log.debug("Deleted = %s", folder)
209+
rel_log.debug("Deleted: %s", folder['dest_folder'])
205210

206211
rel_log.info("Removed files/folders listed in json file")
207212

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
209214
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))
213218
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))
215220

216221
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'])
219224
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))
221226

222227
## Create new branch with all changes
223-
create_branch = "git checkout -b "+ branch
228+
create_branch = ['git', 'checkout', '-b', branch]
224229
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)
226231

227-
add_files = "git add -A"
232+
add_files = ['git', 'add', '-A']
228233
run_cmd_with_output(add_files, exit_on_failure=True)
229234

230-
commit_branch = "git commit -m \"" + commit_msg + "\""
235+
commit_branch = ['git', 'commit', '-m', commit_msg]
231236
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)
233238

234239
## Checkout the feature branch
235240
branch_checkout(branch)
@@ -238,9 +243,9 @@ def get_last_cherry_pick_sha(branch):
238243
if not last_sha:
239244
## Apply commits specific to mbed-os changes
240245
for sha in commit_sha:
241-
cherry_pick_sha = "git cherry-pick -x " + sha
246+
cherry_pick_sha = ['git', 'cherry-pick', '-x', sha]
242247
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)
244249
## Few commits are already applied, check the next in sequence
245250
## and skip to last applied
246251
else:
@@ -250,5 +255,6 @@ def get_last_cherry_pick_sha(branch):
250255
found = True
251256
continue
252257
if found is True:
253-
cherry_pick_sha = "git cherry-pick -x " + sha
258+
cherry_pick_sha = ['git', 'cherry-pick', '-x', sha]
254259
run_cmd_with_output(cherry_pick_sha, exit_on_failure=True)
260+
rel_log.info("Cherry-picked commit = %s", sha)

0 commit comments

Comments
 (0)