| 1 |
# Checks that the example Cargo.toml snippet from rsvg/src/lib.rs has the same versions for |
| 2 |
# dependencies that librsvg uses during compilation. |
| 3 |
|
| 4 |
import sys |
| 5 |
import toml |
| 6 |
|
| 7 |
# Looks for a crate version in the 'dependencies' section of a TOML document, either of these: |
| 8 |
# |
| 9 |
# [dependencies] |
| 10 |
# foo = "1.2.3" |
| 11 |
# bar = { version = "4.5.6", features=["something", "else", "here"] |
| 12 |
def get_crate_version(toml_doc, crate_name): |
| 13 |
if 'dependencies' in toml_doc: |
| 14 |
crate_decl = toml_doc['dependencies'][crate_name] |
| 15 |
else: |
| 16 |
crate_decl = toml_doc['workspace']['dependencies'][crate_name] |
| 17 |
|
| 18 |
if isinstance(crate_decl, str): |
| 19 |
version = crate_decl |
| 20 |
else: |
| 21 |
version = crate_decl['version'] |
| 22 |
|
| 23 |
return version |
| 24 |
|
| 25 |
# Given a Rust file that has a toplevel comment somewhere like |
| 26 |
# |
| 27 |
# //! ```toml |
| 28 |
# //! [dependencies] |
| 29 |
# //! librsvg = "2.57.0-beta.2" |
| 30 |
# //! cairo-rs = "0.18" |
| 31 |
# //! gio = "0.18" # only if you need streams |
| 32 |
# //! ``` |
| 33 |
# |
| 34 |
# extracts just the TOML as a string, without the //! prefix. |
| 35 |
def find_toml_in_rust_toplevel_docs(lines): |
| 36 |
found_start = False |
| 37 |
start_index = 0 |
| 38 |
end_index = 0 |
| 39 |
|
| 40 |
for (i, line) in enumerate(lines): |
| 41 |
if not found_start and line.startswith('//! ```toml'): |
| 42 |
found_start = True |
| 43 |
start_index = i |
| 44 |
end_index = i |
| 45 |
elif found_start and line.startswith('//! ```'): |
| 46 |
end_index = i |
| 47 |
break |
| 48 |
|
| 49 |
if not found_start: |
| 50 |
raise Exception( |
| 51 |
"did not find start of ```toml block in the toplevel documentation comments" |
| 52 |
) |
| 53 |
|
| 54 |
snippet = lines[(start_index + 1):end_index] |
| 55 |
|
| 56 |
without_comment = [s.removeprefix('//! ') for s in snippet] |
| 57 |
|
| 58 |
return "".join(without_comment) |
| 59 |
|
| 60 |
def check_dependency_version(cargo_toml_filename, cargo_toml, other_filename, other_toml, |
| 61 |
dependency_name): |
| 62 |
dep_in_cargo_toml = get_crate_version(cargo_toml, dependency_name) |
| 63 |
dep_in_other = get_crate_version(other_toml, dependency_name) |
| 64 |
|
| 65 |
if dep_in_cargo_toml != dep_in_other: |
| 66 |
raise Exception( |
| 67 |
f"""{dependency_name} version in {cargo_toml_filename} is {dep_in_cargo_toml} but |
| 68 |
is referenced in {other_filename} as {dep_in_other}""" |
| 69 |
) |
| 70 |
|
| 71 |
def check(): |
| 72 |
cargo_toml = toml.load('rsvg/Cargo.toml') |
| 73 |
librsvg_version = cargo_toml['package']['version'] |
| 74 |
|
| 75 |
example_file = open('rsvg/src/lib.rs') |
| 76 |
example_contents = example_file.readlines() |
| 77 |
example_toml_str = find_toml_in_rust_toplevel_docs(example_contents) |
| 78 |
example_toml = toml.loads(example_toml_str) |
| 79 |
|
| 80 |
example_version = get_crate_version(example_toml, 'librsvg') |
| 81 |
|
| 82 |
if librsvg_version != example_version: |
| 83 |
raise Exception( |
| 84 |
f"""librsvg version in rsvg/Cargo.toml is {librsvg_version} but is referenced as |
| 85 |
{example_version} in rsvg/src/lib.rs""" |
| 86 |
) |
| 87 |
|
| 88 |
DEPENDENCIES = [ |
| 89 |
'cairo-rs', |
| 90 |
'gio', |
| 91 |
] |
| 92 |
|
| 93 |
cargo_toml = toml.load('Cargo.toml') |
| 94 |
for dependency_name in DEPENDENCIES: |
| 95 |
check_dependency_version( |
| 96 |
'Cargo.toml', |
| 97 |
cargo_toml, |
| 98 |
'rsvg/src/lib.rs', |
| 99 |
example_toml, |
| 100 |
dependency_name |
| 101 |
) |
| 102 |
|
| 103 |
print("Dependency versions match in rsvg/src/lib.rs. All good!", file=sys.stderr) |
| 104 |
|
| 105 |
if __name__ == '__main__': |
| 106 |
check() |