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 from os.path import dirname, abspath, basename, join
import argparse
ROOT = abspath(join(dirname(__file__), "../..")) ROOT = abspath(join(dirname(__file__), "../.."))
CMSIS_REPO = "CMSIS_Repo"
CMSIS_PATH = abspath(join(dirname(__file__), CMSIS_REPO))
RTOS_UPDATE_BRANCH = "rtos_update" RTOS_UPDATE_BRANCH = "rtos_update"
def del_file(name): def del_file(name):
@ -30,36 +29,36 @@ def rmtree(top):
def copy_file(file, path): def copy_file(file, path):
try: try:
shutil.copy(file, path) shutil.copy(file, path)
except IOError as e: except IOError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise raise
## Create directories ## Create directories
os.makedirs(os.path.dirname(path)) os.makedirs(os.path.dirname(path))
shutil.copy(file, path) shutil.copy(file, path)
print os.path.relpath(path, ROOT) print os.path.relpath(path, ROOT)
def copy_folder(folder, path): def copy_folder(folder, path):
files = os.listdir(folder) files = os.listdir(folder)
for file in files: for file in files:
abs_src_file = os.path.join(folder, file) abs_src_file = os.path.join(folder, file)
if os.path.isfile(abs_src_file): if os.path.isfile(abs_src_file):
abs_dst_file = os.path.join(path, file) abs_dst_file = os.path.join(path, file)
copy_file(abs_src_file, abs_dst_file) copy_file(abs_src_file, abs_dst_file)
def run_cmd(command, exit_on_failure=False): def run_cmd(command, exit_on_failure=False):
""" Passes a command to the system and returns a True/False result once the """ Passes a command to the system and returns a True/False result once the
command has been executed, indicating success/failure. Commands are passed command has been executed, indicating success/failure. Commands are passed
as a list of tokens. as a list of tokens.
E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v'] E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
""" """
return_code = subprocess.call(command, shell=True) return_code = subprocess.call(command, shell=True)
if return_code: if return_code:
print("The command %s failed with return code: %s" print("The command %s failed with return code: %s"
%(' '.join(command), return_code)) %(' '.join(command), return_code))
if exit_on_failure: if exit_on_failure:
sys.exit(1) sys.exit(1)
return return_code return return_code
def remove_repo(folder): def remove_repo(folder):
@ -76,47 +75,71 @@ def get_repo(repo, branch, folder):
remove_repo(folder) remove_repo(folder)
clone_cmd = ['git', 'clone', repo, "-b", branch, "--depth", '1', folder] clone_cmd = ['git', 'clone', repo, "-b", branch, "--depth", '1', folder]
run_cmd(clone_cmd, exit_on_failure=True) run_cmd(clone_cmd, exit_on_failure=True)
if __name__ == "__main__": 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 # 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) 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_files = json_data["files"]
data_folders = json_data["folders"] 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 ## Remove all files listed in .json from mbed-os repo to avoid duplications
print "Cleaning up:" print "Cleaning up:"
for file in data_files: for file in data_files:
cmsis_file = file['cmsis_file'] cmsis_file = file['cmsis_file']
del_file(os.path.basename(cmsis_file)) del_file(os.path.basename(cmsis_file))
for folder in data_folders: for folder in data_folders:
cmsis_folder = os.path.join(CMSIS_PATH, folder['cmsis_folder']) files = os.listdir(repo_path)
files = os.listdir(cmsis_folder) for file in files:
for file in files:
del_file(os.path.basename(file)) del_file(os.path.basename(file))
## Copy all the CMSIS files listed in json file to mbed-os ## Copy all the CMSIS files listed in json file to mbed-os
print "Files Copied:" print "Files Copied:"
for file in data_files: 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']) 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: 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']) mbed_path = os.path.join(ROOT, folder['mbed_folder'])
copy_folder(cmsis_folder, mbed_path) copy_folder(repo_folder, mbed_path)
#Remove CMSIS Repo
remove_repo(CMSIS_REPO)
## Create new branch with all changes ## Create new branch with all changes
create_branch = ['git', 'checkout', '-b', RTOS_UPDATE_BRANCH] create_branch = ['git', 'checkout', '-b', RTOS_UPDATE_BRANCH]
@ -127,17 +150,10 @@ if __name__ == "__main__":
commit_branch = ['git', 'commit', '-m', "CMSIS/RTX: Update CMSIS/RTX"] commit_branch = ['git', 'commit', '-m', "CMSIS/RTX: Update CMSIS/RTX"]
run_cmd(commit_branch, exit_on_failure=True) run_cmd(commit_branch, exit_on_failure=True)
## Apply commits specific to mbed-os changes ## Apply commits specific to mbed-os changes
mbed_sha = json_data["Mbed_sha"] mbed_sha = json_data["Mbed_sha"]
for sha in mbed_sha: for sha in mbed_sha:
cherry_pick_sha = ['git', 'cherry-pick', sha] cherry_pick_sha = ['git', 'cherry-pick', sha]
run_cmd(cherry_pick_sha, exit_on_failure=True) run_cmd(cherry_pick_sha, exit_on_failure=True)