From 6875ab9a4a0fad9eb2cb7f33ffb8f077789cdeb4 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Fri, 24 Nov 2023 17:08:15 +0100 Subject: [PATCH] Check workspace script A script to check workspace integrity. Otherwise it constantly gets messed up and i fixed it manually four times now. --- check-workspace.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 check-workspace.py diff --git a/check-workspace.py b/check-workspace.py new file mode 100644 index 0000000..383c394 --- /dev/null +++ b/check-workspace.py @@ -0,0 +1,96 @@ +# Fork from https://github.com/ggwpez/substrate-scripts/blob/91537c4088fc07da52b614e6b3f2f4c8ce8700d9/import-runtime-repos/check-deps.py + +# Ensures that workspace dependencies are resolved via `path` and not something else. +# Also checks that all crates are part of the Rust workspace. + +# Invocation: +# python3 check-deps.py polkadot-sdk/ +# Example output: +# 🔎 Checking folder polkadot-sdk/ +# 📜 Found workspace manifest +# 📜 Found 441 crates in the workspace +# ✅ All 5307 dependency links are correct + +import os +import sys +import toml + +DIR = sys.argv[1] +print("🔎 Checking folder %s" % DIR) + +crates = [] +manifests = [] +in_workspace = [] + +for root, dirs, files in os.walk(DIR): + if "target" in root: + continue + + for file in files: + if file != "Cargo.toml": + continue + + path = os.path.join(root, file) + with open(path, "r") as f: + content = f.read() + manifest = toml.loads(content) + + if 'workspace' in manifest: + for member in manifest['workspace']['members']: + in_workspace.append(member) + continue + + manifests.append(manifest) + +if len(in_workspace) != len(manifests): + print("💥 Crates are missing from the workspace Cargo.toml") + sys.exit(1) + +for manifest in manifests: + name = manifest['package']['name'] + crates.append(name) + +links = [] +git_links = [] +broken = [] +# Now check that all the deps are correct. +for manifest in manifests: + name = manifest['package']['name'] + + def check_deps(deps): + for dep in deps: + # Account for renames: + dep_name = dep + if 'package' in deps[dep]: + dep_name = deps[dep]['package'] + if 'git' in deps[dep]: + git_links.append((name, dep_name)) + if dep_name in crates: + links.append((name, dep_name)) + + if not 'path' in deps[dep]: + broken.append((name, dep_name)) + + if 'dependencies' in manifest: + check_deps(manifest['dependencies']) + if 'dev-dependencies' in manifest: + check_deps(manifest['dev-dependencies']) + if 'build-dependencies' in manifest: + check_deps(manifest['build-dependencies']) + +crates.sort() +links.sort() +broken.sort() + +print("📜 Found %d crates in the workspace" % len(crates)) + +for link in git_links: + print("❗ %s -> %s (uses Git)" % link) +for link in broken: + print("❌ %s -> %s" % link) + +if len(broken) > 0: + print("💥 %d out of %d links are broken" % (len(broken), len(links))) + sys.exit(1) +else: + print("✅ All %d local links are correct." % len(links))