|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def run_cargo_tree(path): |
| 9 | + cmd = 'cargo tree --all-features -d' |
| 10 | + prev_dir = os.getcwd() |
| 11 | + os.chdir(os.path.join(path)) |
| 12 | + return subprocess.check_output(cmd, shell=True).decode() |
| 13 | + |
| 14 | + |
| 15 | +def run_deps_mismatcher(lines): |
| 16 | + pat = re.compile('([a-zA-Z]\S+)\s(v\S+)') |
| 17 | + deps = dict() |
| 18 | + for line in lines.split('\n'): |
| 19 | + m = pat.search(line) |
| 20 | + if m is not None: |
| 21 | + if m.group(1) in deps.keys(): |
| 22 | + if m.group(2) not in deps[m.group(1)]: |
| 23 | + deps[m.group(1)].append(m.group(2)) |
| 24 | + else: |
| 25 | + deps[m.group(1)] = [m.group(2)] |
| 26 | + return deps |
| 27 | + |
| 28 | + |
| 29 | +def get_deps_with_more_than_1v(deps_and_versions): |
| 30 | + new_dict = dict() |
| 31 | + for dep_name, versions in deps_and_versions.items(): |
| 32 | + if len(versions) > 1: |
| 33 | + new_dict[dep_name] = versions |
| 34 | + return new_dict |
| 35 | + |
| 36 | + |
| 37 | +def print_deps(deps_and_versions): |
| 38 | + for dep_name, versions in deps_and_versions.items(): |
| 39 | + print(f"{dep_name:<25} {versions}") |
| 40 | + |
| 41 | + |
| 42 | +def main(argv=[], prog_name=''): |
| 43 | + parser = argparse.ArgumentParser(prog='DependencyCrossmatcher', |
| 44 | + description='Checks the version mismatches for dependencies ' |
| 45 | + 'in Cargo based repositories') |
| 46 | + parser.add_argument('--deps_dir', |
| 47 | + required=True, |
| 48 | + help='Existing directory that contains the Cargo.toml for analyzing' |
| 49 | + 'dependencies') |
| 50 | + args = parser.parse_args() |
| 51 | + |
| 52 | + mismatches = run_deps_mismatcher(run_cargo_tree(args.deps_dir)) |
| 53 | + print_deps(mismatches) |
| 54 | + |
| 55 | + mismatches = get_deps_with_more_than_1v(mismatches) |
| 56 | + |
| 57 | + print('---------------------mistmatches----------------------\n\n') |
| 58 | + print_deps(mismatches) |
| 59 | + |
| 60 | + exceptions = { |
| 61 | + 'base64': ['v0.13.1', 'v0.21.4'], |
| 62 | + 'bitflags': ['v1.3.2', 'v2.4.1'], |
| 63 | + 'nom': ['v5.1.3', 'v7.1.3'], |
| 64 | + 'syn': ['v1.0.109', 'v2.0.38'], |
| 65 | + 'yasna': ['v0.4.0', 'v0.5.2'], |
| 66 | + } |
| 67 | + |
| 68 | + if exceptions != mismatches: |
| 69 | + return 1 |
| 70 | + |
| 71 | + return 0 |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + sys.exit(main(sys.argv[1:], sys.argv[0])) |
0 commit comments