Skip to content

Commit d4e6dec

Browse files
author
deepikabhavnani
committed
Added arguments to importer script
Repository and json file will be mandotory inputs to script file. As part of this change we do not need to clone/remove CMSIS repository
1 parent 78fdb7b commit d4e6dec

File tree

1 file changed

+61
-45
lines changed

1 file changed

+61
-45
lines changed

tools/importer/importer.py

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import os, json, stat, sys, shutil, errno, subprocess
1+
import os, json, stat, sys, shutil, errno, subprocess, logging
22
from os.path import dirname, abspath, basename, join
3+
import argparse
34

45
ROOT = abspath(join(dirname(__file__), "../.."))
5-
CMSIS_REPO = "CMSIS_Repo"
6-
CMSIS_PATH = abspath(join(dirname(__file__), CMSIS_REPO))
76
RTOS_UPDATE_BRANCH = "rtos_update"
87

98
def del_file(name):
@@ -30,36 +29,36 @@ def rmtree(top):
3029
def copy_file(file, path):
3130
try:
3231
shutil.copy(file, path)
33-
except IOError as e:
32+
except IOError as e:
3433
if e.errno != errno.ENOENT:
3534
raise
3635
## Create directories
3736
os.makedirs(os.path.dirname(path))
3837
shutil.copy(file, path)
3938
print os.path.relpath(path, ROOT)
40-
41-
def copy_folder(folder, path):
42-
files = os.listdir(folder)
39+
40+
def copy_folder(folder, path):
41+
files = os.listdir(folder)
4342
for file in files:
4443
abs_src_file = os.path.join(folder, file)
4544
if os.path.isfile(abs_src_file):
4645
abs_dst_file = os.path.join(path, file)
4746
copy_file(abs_src_file, abs_dst_file)
48-
47+
4948
def run_cmd(command, exit_on_failure=False):
50-
""" Passes a command to the system and returns a True/False result once the
49+
""" Passes a command to the system and returns a True/False result once the
5150
command has been executed, indicating success/failure. Commands are passed
52-
as a list of tokens.
51+
as a list of tokens.
5352
E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
5453
"""
5554
return_code = subprocess.call(command, shell=True)
56-
55+
5756
if return_code:
5857
print("The command %s failed with return code: %s"
5958
%(' '.join(command), return_code))
6059
if exit_on_failure:
6160
sys.exit(1)
62-
61+
6362
return return_code
6463

6564
def remove_repo(folder):
@@ -76,47 +75,71 @@ def get_repo(repo, branch, folder):
7675
remove_repo(folder)
7776
clone_cmd = ['git', 'clone', repo, "-b", branch, "--depth", '1', folder]
7877
run_cmd(clone_cmd, exit_on_failure=True)
79-
78+
8079
if __name__ == "__main__":
81-
80+
81+
parser = argparse.ArgumentParser(description=__doc__,
82+
formatter_class=argparse.RawDescriptionHelpFormatter)
83+
parser.add_argument('-l', '--log-level',
84+
help="Level for providing logging output",
85+
default='INFO')
86+
parser.add_argument('-r', '--repo-path',
87+
help="Git Repository to be imported",
88+
default=None)
89+
parser.add_argument('-c', '--config-file',
90+
help="Configuration file",
91+
default=None)
92+
args = parser.parse_args()
93+
94+
default = getattr(logging, 'INFO')
95+
level = getattr(logging, args.log_level.upper(), default)
96+
97+
# Set logging level
98+
logging.basicConfig(level=level)
99+
rel_log = logging.getLogger("Importer")
100+
101+
if (args.repo_path is None) or (args.config_file is None) :
102+
rel_log.error("Repository path and config file required as input. Use \"--help\" for more info.")
103+
exit(1)
104+
105+
repo = os.path.abspath(args.repo_path)
106+
if not os.path.exists(repo):
107+
rel_log.error("%s not found.", args.repo_path)
108+
exit(1)
109+
110+
json_file = os.path.abspath(args.config_file)
111+
if not os.path.isfile(json_file):
112+
rel_log.error("%s not found." , args.config_file)
113+
exit(1)
114+
82115
# Read configuration data
83-
with open(os.path.join(os.path.dirname(__file__), "cmsis_importer.json"), 'r') as config:
116+
with open(json_file, 'r') as config:
84117
json_data = json.load(config)
85-
config = json_data["config"]
86-
cmsis_repo = config['cmsis_repo']
87-
cmsis_branch = config['cmsis_branch']
88118
data_files = json_data["files"]
89119
data_folders = json_data["folders"]
90-
91-
print "Fetching git repo"
92-
get_repo(cmsis_repo, cmsis_branch, CMSIS_REPO)
93-
120+
94121
## Remove all files listed in .json from mbed-os repo to avoid duplications
95122
print "Cleaning up:"
96123
for file in data_files:
97124
cmsis_file = file['cmsis_file']
98125
del_file(os.path.basename(cmsis_file))
99-
126+
100127
for folder in data_folders:
101-
cmsis_folder = os.path.join(CMSIS_PATH, folder['cmsis_folder'])
102-
files = os.listdir(cmsis_folder)
103-
for file in files:
128+
files = os.listdir(repo_path)
129+
for file in files:
104130
del_file(os.path.basename(file))
105131

106-
## Copy all the CMSIS files listed in json file to mbed-os
107-
print "Files Copied:"
108-
for file in data_files:
109-
cmsis_file = os.path.join(CMSIS_PATH, file['cmsis_file'])
132+
## Copy all the CMSIS files listed in json file to mbed-os
133+
print "Files Copied:"
134+
for file in data_files:
135+
repo_file = os.path.join(repo_path, file['cmsis_file'])
110136
mbed_path = os.path.join(ROOT, file['mbed_file'])
111-
copy_file(cmsis_file, mbed_path)
137+
copy_file(repo_file, mbed_path)
112138

113139
for folder in data_folders:
114-
cmsis_folder = os.path.join(CMSIS_PATH, folder['cmsis_folder'])
115-
mbed_path = os.path.join(ROOT, folder['mbed_folder'])
116-
copy_folder(cmsis_folder, mbed_path)
117-
118-
#Remove CMSIS Repo
119-
remove_repo(CMSIS_REPO)
140+
repo_folder = os.path.join(repo_path, folder['cmsis_folder'])
141+
mbed_path = os.path.join(ROOT, folder['mbed_folder'])
142+
copy_folder(repo_folder, mbed_path)
120143

121144
## Create new branch with all changes
122145
create_branch = ['git', 'checkout', '-b', RTOS_UPDATE_BRANCH]
@@ -127,17 +150,10 @@ def get_repo(repo, branch, folder):
127150

128151
commit_branch = ['git', 'commit', '-m', "CMSIS/RTX: Update CMSIS/RTX"]
129152
run_cmd(commit_branch, exit_on_failure=True)
130-
153+
131154
## Apply commits specific to mbed-os changes
132155
mbed_sha = json_data["Mbed_sha"]
133156

134157
for sha in mbed_sha:
135158
cherry_pick_sha = ['git', 'cherry-pick', sha]
136159
run_cmd(cherry_pick_sha, exit_on_failure=True)
137-
138-
139-
140-
141-
142-
143-

0 commit comments

Comments
 (0)