mbed-os/tools/importer/importer.py

320 lines
10 KiB
Python
Raw Normal View History

2019-04-28 20:53:00 +00:00
#!/usr/bin/python
2019-02-15 13:19:05 +00:00
"""
Copyright (c) 2017-2019 ARM Limited. All rights reserved.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import json
import sys
import subprocess
import logging
import argparse
2019-04-28 20:44:18 +00:00
import re
2018-12-20 11:30:41 +00:00
from os.path import dirname, abspath, join, isfile, normpath
# Be sure that the tools directory is in the search path
2019-04-28 13:09:12 +00:00
ROOT = abspath(join(dirname(__file__), os.path.pardir, os.path.pardir))
sys.path.insert(0, ROOT)
2019-04-28 13:09:12 +00:00
from tools.utils import delete_dir_files, mkdir, copy_file
2019-04-28 20:44:18 +00:00
cherry_pick_re = re.compile(
'\s*\(cherry picked from commit (([0-9]|[a-f]|[A-F])+)\)')
class StoreDir(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
2019-04-28 20:53:00 +00:00
directory = abspath(values)
if not os.path.isdir(directory):
raise argparse.ArgumentError(
None, "The directory %s does not exist!" % directory)
setattr(namespace, self.dest, directory)
class StoreValidFile(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
2019-04-28 20:53:00 +00:00
fn = abspath(values)
if not isfile(fn):
raise argparse.ArgumentError(
None, "The file %s does not exist!" % fn)
setattr(namespace, self.dest, fn)
def del_file(name):
"""
2019-04-28 21:11:39 +00:00
Delete the file in RTOS/CMSIS/features directory of mbed-os.
:param name: Name of the file.
:return: None.
"""
result = []
2019-04-28 13:09:12 +00:00
search_path = [join(ROOT, 'rtos'), join(ROOT, 'cmsis'),
join(ROOT, 'features')]
for path in search_path:
for root, dirs, files in os.walk(path):
if name in files:
2018-12-20 11:30:41 +00:00
result.append(join(root, name))
2019-04-28 13:09:12 +00:00
for f in result:
os.remove(f)
2019-04-28 20:53:00 +00:00
rel_log.debug("Deleted %s", os.path.relpath(f, ROOT))
2019-04-28 13:09:12 +00:00
2019-04-28 21:11:39 +00:00
def copy_folder(src, dst):
"""
Copy contents of folder in mbed-os listed path.
:param src: Source folder path.
:param dst: Destination folder path.
:return: None.
"""
2019-04-28 21:11:39 +00:00
files = os.listdir(src)
2019-04-28 13:09:12 +00:00
for f in files:
abs_src_file = join(src, f)
2019-04-28 20:53:00 +00:00
if isfile(abs_src_file):
2019-04-28 21:11:39 +00:00
abs_dst_file = join(dst, f)
2018-12-20 11:30:41 +00:00
mkdir(dirname(abs_dst_file))
copy_file(abs_src_file, abs_dst_file)
2019-04-28 13:09:12 +00:00
def run_cmd_with_output(command, exit_on_failure=False):
"""
2019-04-28 21:11:39 +00:00
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
successful then the output from the command is returned to the caller.
Commands are passed as a list of tokens.
E.g. The command 'git remote -v' would be passed in as:
['git', 'remote', '-v']
:param command: System command as a list of tokens.
:param exit_on_failure: Exit the program on failure (default=False)
:return: Command return status code and output as tuple.
"""
rel_log.debug('[Exec] %s', ' '.join(command))
2019-04-28 20:53:00 +00:00
return_code = 0
output = ""
try:
2018-12-20 11:30:41 +00:00
output = subprocess.check_output(command)
except subprocess.CalledProcessError as e:
2019-04-28 20:53:00 +00:00
return_code = e.returncode
if exit_on_failure:
rel_log.error("The command %s failed with return code: %s",
2019-04-28 20:53:00 +00:00
(' '.join(command)), return_code)
sys.exit(1)
2019-04-28 20:53:00 +00:00
return return_code, output
2019-04-28 13:09:12 +00:00
def get_curr_sha(repo_path):
2019-04-28 21:11:39 +00:00
"""
Gets the latest SHA for the specified repo.
2019-04-28 21:11:39 +00:00
:param repo_path: Path to a git repository.
:return: Last commit SHA.
"""
2019-04-28 20:53:00 +00:00
2019-04-28 13:09:12 +00:00
cmd = ['git', '-C', repo_path, 'log', '--pretty=format:%h', '-n', '1']
_, _sha = run_cmd_with_output(cmd, exit_on_failure=True)
2019-04-28 20:53:00 +00:00
if not _sha:
rel_log.error("Could not obtain latest SHA")
sys.exit(1)
rel_log.info("%s SHA = %s", repo_path, sha)
2019-04-28 13:09:12 +00:00
return _sha
def branch_exists(name):
2019-04-28 21:11:39 +00:00
"""
Check if branch already exists in mbed-os local repository.
:param name: Branch name.
:return: True if branch is already present, False otherwise.
"""
2019-04-28 21:11:39 +00:00
cmd = ['git', '-C', ROOT, 'branch']
_, output = run_cmd_with_output(cmd, exit_on_failure=False)
2019-04-28 21:11:39 +00:00
return name in output
2019-04-28 13:09:12 +00:00
def branch_checkout(name):
"""
2019-04-28 21:11:39 +00:00
Checkout the required git branch.
:param name: Branch to checkout.
:return: None.
"""
2019-04-28 21:11:39 +00:00
2018-12-20 11:30:41 +00:00
cmd = ['git', 'checkout', name]
_, _ = run_cmd_with_output(cmd, exit_on_failure=False)
rel_log.info("Checkout to branch %s", name)
2019-04-28 13:09:12 +00:00
2019-04-28 20:53:00 +00:00
def get_last_cherry_pick_sha():
"""
2019-04-28 21:11:39 +00:00
Finds the SHA of last cherry picked commit.
SHA should be added to cherry-pick commits with -x option.
2019-04-28 21:11:39 +00:00
:return: SHA if found, None otherwise.
"""
2019-04-28 20:53:00 +00:00
get_commit = ['git', '-C', ROOT, 'log', '-n', '1']
_, output = run_cmd_with_output(get_commit, exit_on_failure=True)
2019-04-28 20:44:18 +00:00
lines = output.splitlines()
lines.reverse()
for line in lines:
2019-04-28 20:44:18 +00:00
match = cherry_pick_re.match(line)
if match:
return match.group(1)
return None
2019-04-28 13:09:12 +00:00
2019-04-28 17:05:41 +00:00
def normalize_commit_sha(sha_lst):
2019-04-28 21:11:39 +00:00
"""
The commit_sha section of the config file can hold commits in 2 ways:
* "<SHA>" - E.g. "428acae1b2ac15c3ad523e8d40755a9301220822".
* {"sha": "<SHA>", "msg": "<HELP>"} - E.g.
{"sha": "d9d622afe0ca8c7ab9d24c17f9fe59b54dcc61c9", "msg": "Fix ..."}.
:param sha_lst: JSON data from config file.
:return: list of commit SHA.
"""
2019-04-28 17:05:41 +00:00
return [_sha['sha'] if isinstance(_sha, dict) else _sha for _sha in sha_lst]
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",
required=True,
action=StoreDir)
parser.add_argument('-c', '--config-file',
help="Configuration file",
required=True,
action=StoreValidFile)
args = parser.parse_args()
level = getattr(logging, args.log_level.upper())
if ROOT not in abspath(os.curdir):
parser.error("This script must be run from the mbed-os directory "
"to work correctly.")
# Set logging level
logging.basicConfig(level=level)
rel_log = logging.getLogger("Importer")
sha = get_curr_sha(args.repo_path)
repo_dir = os.path.basename(args.repo_path)
branch = 'feature_' + repo_dir + '_' + sha
commit_msg = "[" + repo_dir + "]" + ": Updated to " + sha
# Read configuration data
with open(args.config_file, 'r') as config:
json_data = json.load(config)
'''
Check if branch exists already, in case branch is present
we will skip all file transfer and merge operations and will
jump to cherry-pick
'''
if branch_exists(branch):
rel_log.info("Branch present = %s", branch)
else:
data_files = json_data["files"]
data_folders = json_data["folders"]
2019-04-28 13:09:12 +00:00
# Remove all files listed in .json from mbed-os repo to avoid duplications
for fh in data_files:
src_file = fh['src_file']
del_file(os.path.basename(src_file))
2019-04-28 13:09:12 +00:00
dest_file = join(ROOT, fh['dest_file'])
2018-12-20 11:30:41 +00:00
if isfile(dest_file):
os.remove(join(ROOT, dest_file))
2019-04-28 13:09:12 +00:00
rel_log.debug("Deleted %s", fh['dest_file'])
for folder in data_folders:
dest_folder = folder['dest_folder']
delete_dir_files(dest_folder)
2018-12-20 11:30:41 +00:00
rel_log.debug("Deleted: %s", folder['dest_folder'])
rel_log.info("Removed files/folders listed in json file")
2019-04-28 13:09:12 +00:00
# Copy all the files listed in json file to mbed-os
for fh in data_files:
repo_file = join(args.repo_path, fh['src_file'])
2019-04-28 13:09:12 +00:00
mbed_path = join(ROOT, fh['dest_file'])
2018-12-20 11:30:41 +00:00
mkdir(dirname(mbed_path))
copy_file(repo_file, mbed_path)
2019-04-28 13:09:12 +00:00
rel_log.debug("Copied %s to %s", normpath(repo_file),
normpath(mbed_path))
for folder in data_folders:
repo_folder = join(args.repo_path, folder['src_folder'])
2018-12-20 11:30:41 +00:00
mbed_path = join(ROOT, folder['dest_folder'])
copy_folder(repo_folder, mbed_path)
2019-04-28 13:09:12 +00:00
rel_log.debug("Copied %s to %s", normpath(repo_folder),
normpath(mbed_path))
2019-04-28 13:09:12 +00:00
# Create new branch with all changes
2018-12-20 11:30:41 +00:00
create_branch = ['git', 'checkout', '-b', branch]
run_cmd_with_output(create_branch, exit_on_failure=True)
2018-12-20 11:30:41 +00:00
rel_log.info("Branch created: %s", branch)
2018-12-20 11:30:41 +00:00
add_files = ['git', 'add', '-A']
run_cmd_with_output(add_files, exit_on_failure=True)
2018-12-20 11:30:41 +00:00
commit_branch = ['git', 'commit', '-m', commit_msg]
run_cmd_with_output(commit_branch, exit_on_failure=True)
2018-12-20 11:30:41 +00:00
rel_log.info('Commit added: "%s"', commit_msg)
2019-04-28 13:09:12 +00:00
# Checkout the feature branch
branch_checkout(branch)
2019-04-28 17:05:41 +00:00
commit_sha = normalize_commit_sha(json_data["commit_sha"])
2019-04-28 20:53:00 +00:00
last_sha = get_last_cherry_pick_sha()
2019-04-28 17:03:08 +00:00
# Few commits are already applied, check the next in sequence
# and skip to next commit
if last_sha:
assert last_sha in commit_sha, "%s not found in config file" % last_sha
# Calculate the index of the next sha to be applied
next_sha_idx = commit_sha.index(last_sha) + 1
if next_sha_idx >= len(commit_sha):
rel_log.info("No more commits to apply")
sys.exit(0)
# Skipping applied commits
commit_sha = commit_sha[next_sha_idx:]
# Apply commits specific to mbed-os changes
for sha in commit_sha:
cherry_pick_sha = ['git', 'cherry-pick', '-x', sha]
rel_log.info("Cherry-picking commit = %s", sha)
run_cmd_with_output(cherry_pick_sha, exit_on_failure=True)
rel_log.info("Finished import successfully :)")