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
pull/5407/head
deepikabhavnani 2017-10-31 14:33:40 -05:00
parent 78fdb7bcd6
commit d4e6decf32
1 changed files with 61 additions and 45 deletions

View File

@ -1,9 +1,8 @@
import os, json, stat, sys, shutil, errno, subprocess
import os, json, stat, sys, shutil, errno, subprocess, logging
from os.path import dirname, abspath, basename, join
import argparse
ROOT = abspath(join(dirname(__file__), "../.."))
CMSIS_REPO = "CMSIS_Repo"
CMSIS_PATH = abspath(join(dirname(__file__), CMSIS_REPO))
RTOS_UPDATE_BRANCH = "rtos_update"
def del_file(name):
@ -79,18 +78,46 @@ def get_repo(repo, branch, folder):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-l', '--log-level',
help="Level for providing logging output",
default='INFO')
parser.add_argument('-r', '--repo-path',
help="Git Repository to be imported",
default=None)
parser.add_argument('-c', '--config-file',
help="Configuration file",
default=None)
args = parser.parse_args()
default = getattr(logging, 'INFO')
level = getattr(logging, args.log_level.upper(), default)
# Set logging level
logging.basicConfig(level=level)
rel_log = logging.getLogger("Importer")
if (args.repo_path is None) or (args.config_file is None) :
rel_log.error("Repository path and config file required as input. Use \"--help\" for more info.")
exit(1)
repo = os.path.abspath(args.repo_path)
if not os.path.exists(repo):
rel_log.error("%s not found.", args.repo_path)
exit(1)
json_file = os.path.abspath(args.config_file)
if not os.path.isfile(json_file):
rel_log.error("%s not found." , args.config_file)
exit(1)
# Read configuration data
with open(os.path.join(os.path.dirname(__file__), "cmsis_importer.json"), 'r') as config:
with open(json_file, 'r') as config:
json_data = json.load(config)
config = json_data["config"]
cmsis_repo = config['cmsis_repo']
cmsis_branch = config['cmsis_branch']
data_files = json_data["files"]
data_folders = json_data["folders"]
print "Fetching git repo"
get_repo(cmsis_repo, cmsis_branch, CMSIS_REPO)
## Remove all files listed in .json from mbed-os repo to avoid duplications
print "Cleaning up:"
for file in data_files:
@ -98,25 +125,21 @@ if __name__ == "__main__":
del_file(os.path.basename(cmsis_file))
for folder in data_folders:
cmsis_folder = os.path.join(CMSIS_PATH, folder['cmsis_folder'])
files = os.listdir(cmsis_folder)
files = os.listdir(repo_path)
for file in files:
del_file(os.path.basename(file))
## Copy all the CMSIS files listed in json file to mbed-os
print "Files Copied:"
for file in data_files:
cmsis_file = os.path.join(CMSIS_PATH, file['cmsis_file'])
repo_file = os.path.join(repo_path, file['cmsis_file'])
mbed_path = os.path.join(ROOT, file['mbed_file'])
copy_file(cmsis_file, mbed_path)
copy_file(repo_file, mbed_path)
for folder in data_folders:
cmsis_folder = os.path.join(CMSIS_PATH, folder['cmsis_folder'])
repo_folder = os.path.join(repo_path, folder['cmsis_folder'])
mbed_path = os.path.join(ROOT, folder['mbed_folder'])
copy_folder(cmsis_folder, mbed_path)
#Remove CMSIS Repo
remove_repo(CMSIS_REPO)
copy_folder(repo_folder, mbed_path)
## Create new branch with all changes
create_branch = ['git', 'checkout', '-b', RTOS_UPDATE_BRANCH]
@ -134,10 +157,3 @@ if __name__ == "__main__":
for sha in mbed_sha:
cherry_pick_sha = ['git', 'cherry-pick', sha]
run_cmd(cherry_pick_sha, exit_on_failure=True)