Reformat and Improve

pull/10505/head
Oren Cohen 2019-04-28 16:09:12 +03:00
parent f28b82b831
commit edcde5ebc0
1 changed files with 43 additions and 33 deletions

View File

@ -25,10 +25,11 @@ import argparse
from os.path import dirname, abspath, join, isfile, normpath from os.path import dirname, abspath, join, isfile, normpath
# Be sure that the tools directory is in the search path # Be sure that the tools directory is in the search path
ROOT = abspath(join(dirname(__file__), "../..")) ROOT = abspath(join(dirname(__file__), os.path.pardir, os.path.pardir))
sys.path.insert(0, ROOT) sys.path.insert(0, ROOT)
from tools.utils import run_cmd, delete_dir_files, mkdir, copy_file from tools.utils import delete_dir_files, mkdir, copy_file
def del_file(name): def del_file(name):
""" Delete the file in RTOS/CMSIS/features directory of mbed-os """ Delete the file in RTOS/CMSIS/features directory of mbed-os
@ -36,15 +37,17 @@ def del_file(name):
name - name of the file name - name of the file
""" """
result = [] result = []
search_path = [join(ROOT, 'rtos'), join(ROOT, 'cmsis'), join(ROOT, 'features')] search_path = [join(ROOT, 'rtos'), join(ROOT, 'cmsis'),
join(ROOT, 'features')]
for path in search_path: for path in search_path:
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
if name in files: if name in files:
result.append(join(root, name)) result.append(join(root, name))
for file in result: for f in result:
os.remove(file) os.remove(f)
rel_log.debug("Deleted %s", os.path.relpath(file, ROOT)) rel_log.debug("Deleted %s", os.path.relpath(file, ROOT))
def copy_folder(src, dest): def copy_folder(src, dest):
""" Copy contents of folder in mbed-os listed path """ Copy contents of folder in mbed-os listed path
Args: Args:
@ -52,13 +55,14 @@ def copy_folder(src, dest):
dest - destination folder path dest - destination folder path
""" """
files = os.listdir(src) files = os.listdir(src)
for file in files: for f in files:
abs_src_file = join(src, file) abs_src_file = join(src, f)
if os.path.isfile(abs_src_file): if os.path.isfile(abs_src_file):
abs_dst_file = join(dest, file) abs_dst_file = join(dest, f)
mkdir(dirname(abs_dst_file)) mkdir(dirname(abs_dst_file))
copy_file(abs_src_file, abs_dst_file) copy_file(abs_src_file, abs_dst_file)
def run_cmd_with_output(command, exit_on_failure=False): def run_cmd_with_output(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. If the command was command has been executed, indicating success/failure. If the command was
@ -88,6 +92,7 @@ def run_cmd_with_output(command, exit_on_failure=False):
sys.exit(1) sys.exit(1)
return returncode, output return returncode, output
def get_curr_sha(repo_path): def get_curr_sha(repo_path):
""" Gets the latest SHA for the specified repo """ Gets the latest SHA for the specified repo
Args: Args:
@ -96,14 +101,12 @@ def get_curr_sha(repo_path):
Returns: Returns:
sha - last commit SHA sha - last commit SHA
""" """
cwd = os.getcwd() repo_path = abspath(repo_path)
os.chdir(abspath(repo_path)) cmd = ['git', '-C', repo_path, 'log', '--pretty=format:%h', '-n', '1']
_, _sha = run_cmd_with_output(cmd, exit_on_failure=True)
cmd = ['git', 'log', '--pretty=format:%h', '-n', '1'] return _sha
_, sha = run_cmd_with_output(cmd, exit_on_failure=True)
os.chdir(cwd)
return sha
def branch_exists(name): def branch_exists(name):
""" Check if branch already exists in mbed-os local repository. """ Check if branch already exists in mbed-os local repository.
@ -120,6 +123,7 @@ def branch_exists(name):
return True return True
return False return False
def branch_checkout(name): def branch_checkout(name):
""" """
Checkout the required branch Checkout the required branch
@ -130,6 +134,7 @@ def branch_checkout(name):
_, _ = run_cmd_with_output(cmd, exit_on_failure=False) _, _ = run_cmd_with_output(cmd, exit_on_failure=False)
rel_log.info("Checkout to branch %s", name) rel_log.info("Checkout to branch %s", name)
def get_last_cherry_pick_sha(branch): def get_last_cherry_pick_sha(branch):
""" """
SHA of last cherry pick commit is returned. SHA should be added to all 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):
return sha[:-1] return sha[:-1]
return sha return sha
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__, parser = argparse.ArgumentParser(description=__doc__,
@ -175,23 +181,25 @@ if __name__ == "__main__":
rel_log = logging.getLogger("Importer") rel_log = logging.getLogger("Importer")
if (args.repo_path is None) or (args.config_file is None): 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.") rel_log.error(
exit(1) "Repository path and config file required as input. "
"Use \"--help\" for more info.")
sys.exit(1)
json_file = abspath(args.config_file) json_file = abspath(args.config_file)
if not os.path.isfile(json_file): if not os.path.isfile(json_file):
rel_log.error("%s not found.", args.config_file) rel_log.error("%s not found.", args.config_file)
exit(1) sys.exit(1)
repo = abspath(args.repo_path) repo = abspath(args.repo_path)
if not os.path.exists(repo): if not os.path.exists(repo):
rel_log.error("%s not found.", args.repo_path) rel_log.error("%s not found.", args.repo_path)
exit(1) sys.exit(1)
sha = get_curr_sha(repo) sha = get_curr_sha(repo)
if not sha: if not sha:
rel_log.error("Could not obtain latest SHA") rel_log.error("Could not obtain latest SHA")
exit(1) sys.exit(1)
rel_log.info("%s SHA = %s", os.path.basename(repo), sha) rel_log.info("%s SHA = %s", os.path.basename(repo), sha)
branch = 'feature_' + os.path.basename(repo) + '_' + sha branch = 'feature_' + os.path.basename(repo) + '_' + sha
@ -212,14 +220,14 @@ if __name__ == "__main__":
data_files = json_data["files"] data_files = json_data["files"]
data_folders = json_data["folders"] data_folders = json_data["folders"]
## 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
for file in data_files: for fh in data_files:
src_file = file['src_file'] src_file = fh['src_file']
del_file(os.path.basename(src_file)) del_file(os.path.basename(src_file))
dest_file = join(ROOT, file['dest_file']) dest_file = join(ROOT, fh['dest_file'])
if isfile(dest_file): if isfile(dest_file):
os.remove(join(ROOT, dest_file)) os.remove(join(ROOT, dest_file))
rel_log.debug("Deleted %s", file['dest_file']) rel_log.debug("Deleted %s", fh['dest_file'])
for folder in data_folders: for folder in data_folders:
dest_folder = folder['dest_folder'] dest_folder = folder['dest_folder']
@ -228,21 +236,23 @@ if __name__ == "__main__":
rel_log.info("Removed files/folders listed in json file") rel_log.info("Removed files/folders listed in json file")
## Copy all the files listed in json file to mbed-os # Copy all the files listed in json file to mbed-os
for file in data_files: for fh in data_files:
repo_file = join(repo, file['src_file']) repo_file = join(repo, fh['src_file'])
mbed_path = join(ROOT, file['dest_file']) mbed_path = join(ROOT, fh['dest_file'])
mkdir(dirname(mbed_path)) mkdir(dirname(mbed_path))
copy_file(repo_file, mbed_path) copy_file(repo_file, mbed_path)
rel_log.debug("Copied %s to %s", normpath(repo_file), normpath(mbed_path)) rel_log.debug("Copied %s to %s", normpath(repo_file),
normpath(mbed_path))
for folder in data_folders: for folder in data_folders:
repo_folder = join(repo, folder['src_folder']) repo_folder = join(repo, folder['src_folder'])
mbed_path = join(ROOT, folder['dest_folder']) mbed_path = join(ROOT, folder['dest_folder'])
copy_folder(repo_folder, mbed_path) copy_folder(repo_folder, mbed_path)
rel_log.debug("Copied %s to %s", normpath(repo_folder), normpath(mbed_path)) rel_log.debug("Copied %s to %s", normpath(repo_folder),
normpath(mbed_path))
## Create new branch with all changes # Create new branch with all changes
create_branch = ['git', 'checkout', '-b', branch] create_branch = ['git', 'checkout', '-b', branch]
run_cmd_with_output(create_branch, exit_on_failure=True) run_cmd_with_output(create_branch, exit_on_failure=True)
rel_log.info("Branch created: %s", branch) rel_log.info("Branch created: %s", branch)
@ -254,7 +264,7 @@ if __name__ == "__main__":
run_cmd_with_output(commit_branch, exit_on_failure=True) run_cmd_with_output(commit_branch, exit_on_failure=True)
rel_log.info('Commit added: "%s"', commit_msg) rel_log.info('Commit added: "%s"', commit_msg)
## Checkout the feature branch # Checkout the feature branch
branch_checkout(branch) branch_checkout(branch)
commit_sha = json_data["commit_sha"] commit_sha = json_data["commit_sha"]
last_sha = get_last_cherry_pick_sha(branch) last_sha = get_last_cherry_pick_sha(branch)