From b02502916c3816fbc3148c793c3502f45261389d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gonz=C3=A1lez?= Date: Tue, 7 Nov 2023 13:51:42 +0000 Subject: [PATCH] utils/release_tracking.py: Uses toml [patch] instead of cloning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * cargo update before building is key for avoiding an unused patch version, as the version in the Cargo.lock file will otherwise be preferred. * Improve python code quality. Signed-off-by: Tomás González --- ci.sh | 3 +- utils/dependency_cross_matcher.py | 2 +- utils/release_tracking.py | 69 ++++++++++++------------------- 3 files changed, 28 insertions(+), 46 deletions(-) diff --git a/ci.sh b/ci.sh index 1fe88dd9..e17d9491 100755 --- a/ci.sh +++ b/ci.sh @@ -212,8 +212,7 @@ done if [ "$TEST_NEXT_BRANCH_TRACKING" ]; then echo "Track next branches for parallaxsecond repositories" - mkdir -p /tmp/clonings - python3 $(pwd)/utils/release_tracking.py --clone_dir /tmp/clonings $(pwd)/Cargo.toml $(pwd)/e2e_tests/Cargo.toml + python3 $(pwd)/utils/release_tracking.py $(pwd)/Cargo.toml $(pwd)/e2e_tests/Cargo.toml next_branch_result=$? if [ "$next_branch_result" -ne 0 ]; then error_msg "Failed to track next branches of parallaxsecond repositories." diff --git a/utils/dependency_cross_matcher.py b/utils/dependency_cross_matcher.py index 4b608831..b2699543 100644 --- a/utils/dependency_cross_matcher.py +++ b/utils/dependency_cross_matcher.py @@ -10,7 +10,7 @@ def run_cargo_tree(path): cmd += '--features tss-esapi/generate-bindings,cryptoki/generate-bindings -d' prev_dir = os.getcwd() os.chdir(os.path.join(path)) - return subprocess.check_output(cmd.split(' ')).decode() + return subprocess.check_output(cmd, shell=True).decode() def run_deps_mismatcher(lines): diff --git a/utils/release_tracking.py b/utils/release_tracking.py index 8386298f..a60ff8a9 100644 --- a/utils/release_tracking.py +++ b/utils/release_tracking.py @@ -8,44 +8,40 @@ def run_cargo_build(path): print(f"cargo build, path: {path}") command = f'cargo build' - subprocess.check_output(command.split(), cwd=path) + return subprocess.check_output(command, shell=True, cwd=path) -def clone_repo(clone_dir, repo_name, branch): - git_repo = f"https://github.com/parallaxsecond/{repo_name}.git" - future_repo_dir = os.path.join(clone_dir, repo_name) - if not os.path.isdir(future_repo_dir): - command = f"git clone {git_repo} -b {branch} {future_repo_dir}" - subprocess.check_output(command.split()) - command = f"git submodule update --init" - subprocess.check_output(command.split(), cwd=future_repo_dir) +def run_cargo_update(path, dep): + print(f"cargo update, dep: {dep}") + command = f'cargo update --package {dep}' + return subprocess.check_output(command, shell=True, cwd=path) -def git_toml_deps(toml_path, updatable_deps, deps_repos): +def git_toml_deps(toml_path, deps_repo_links, deps_branches): lines = None with open(toml_path, 'r') as f: lines = f.readlines() - for i in range(len(lines)): - line = lines[i] - for dep in updatable_deps.keys(): - if line.startswith(dep + " ="): - # All occurences - line = line.replace("'", '"') - if "{" not in line: - # First occurence - line = line.replace('"', '{ version = "', 1) - line = line.rstrip() + ' }\n' + to_update = [] + output_lines = lines + ['[patch.crates-io]\n'] + for line in lines: + for dep in deps_repo_links.keys(): + starter = dep + " =" + if line.startswith(starter): + to_update.append(dep) + new_line = f'git = "{deps_repo_links[dep]}", branch = "{deps_branches[dep]}"' + new_line = starter + ' { ' + new_line + ' }\n' + output_lines.append(new_line) - if 'path' not in line: - line = re.sub(r'version = "[0-9\.]+"', f'path = "{deps_repos[dep]}"', line) - lines[i] = line - - dirname = os.path.relpath('.') + for updatable in to_update: + run_cargo_update(os.path.dirname(toml_path), updatable) with open(toml_path, 'w') as f: - f.writelines(lines) - print(subprocess.check_output(['git', 'diff'], cwd=os.path.dirname(toml_path)).decode('utf-8')) + f.writelines(output_lines) + git_cmd = 'git diff' + print(subprocess.check_output(git_cmd, + shell=True, + cwd=os.path.dirname(toml_path)).decode('utf-8')) def main(argv=[], prog_name=''): @@ -53,10 +49,7 @@ def main(argv=[], prog_name=''): description='Modifies the parsec Cargo.toml files to use the ' 'main branches of parallaxsecond dependencies in ' 'preparation for their publishing and release') - parser.add_argument('--clone_dir', - required=True, - help='Existing directory into which repositories should be cloned') - parser.add_argument('paths', nargs='+', help='Paths to Cargo.toml files to be modified') + parser.add_argument('paths', nargs='+', help='Absolute paths to the Cargo.toml files') args = parser.parse_args() # The order is important! @@ -71,25 +64,15 @@ def main(argv=[], prog_name=''): 'parsec-client': 'parsec-client-rust', } - repo_paths = { repo_name: f'{args.clone_dir}/{repo_folder}/{repo_name}' \ + repo_links = { repo_name: f"https://github.com/parallaxsecond/{repo_folder}.git" \ for repo_name, repo_folder in parallaxsecond_deps.items() } - repo_paths['parsec-interface'] = f'{args.clone_dir}/parsec-interface-rs' - repo_paths['parsec-client'] = f'{args.clone_dir}/parsec-client-rust' repo_branches = { repo_name: 'main' for repo_name in parallaxsecond_deps.keys() } repo_branches['tss-esapi-sys'] = '7.x.y' repo_branches['tss-esapi'] = '7.x.y' - for repo_name, repo_folder in parallaxsecond_deps.items(): - clone_repo(args.clone_dir, repo_folder, repo_branches[repo_name]) - toml_path = os.path.join(repo_paths[repo_name], 'Cargo.toml') - git_toml_deps(toml_path, parallaxsecond_deps, repo_paths) - - for repo_path in repo_paths.values(): - run_cargo_build(repo_path) - for path in args.paths: - git_toml_deps(path, parallaxsecond_deps, repo_paths) + git_toml_deps(path, repo_links, repo_branches) run_cargo_build(os.path.dirname(path)) return 0