generalize script to fix all paths

This commit is contained in:
bunnie 2022-10-14 05:01:52 +08:00
parent eaac18070e
commit c2a78333c8

View file

@ -1,6 +1,6 @@
#! /usr/bin/env python3
import argparse
from pathlib import Path
from pathlib import Path, PurePath
import re
# format is {crate : path}
@ -20,11 +20,10 @@ CRATES = {
VERSIONS = {}
class PatchInfo:
def __init__(self, dirpath, cratename):
filename = dirpath + "/Cargo.toml"
def __init__(self, filename, cratename=None):
self.filepath = Path(filename)
if not self.filepath.is_file():
print("Bad crate path: {}".format(dirpath))
print("Bad crate path: {}".format(filename))
self.cratename = cratename
def get_version(self):
@ -42,15 +41,15 @@ class PatchInfo:
elif in_package:
if line.strip().startswith('version'):
version = line.split('=')[1].replace('"', '').strip()
VERSIONS[self.cratename] = version
if line.strip().startswith('name'):
name = line.split('=')[1].replace('"', '').strip()
if self.cratename == name:
if name in CRATES:
name_check = True
if not name_check:
return False
else:
return True
if name_check:
assert(version is not None, "Target name found but no version was extracted!")
VERSIONS[name] = version
return name_check
# assumes that VERSIONS has been initialized.
def increment_versions(self):
@ -73,13 +72,21 @@ class PatchInfo:
else:
in_dependencies = False
file.write(line)
continue
elif line.strip().startswith('#'): # skip comments
file.write(line)
elif in_package:
# increment my own version
if line.strip().startswith('version'):
file.write('version = "{}"\n'.format(bump_version(VERSIONS[self.cratename])))
# increment my own version, if I'm in the listed crates
if self.cratename is not None:
if line.strip().startswith('version'):
file.write('version = "{}"\n'.format(bump_version(VERSIONS[self.cratename])))
else:
file.write(line)
else:
file.write(line)
if line.strip().startswith('name'):
this_crate = line.split('=')[1].replace('"', '').strip()
print("Patching {}...".format(this_crate))
elif in_dependencies:
# now increment dependency versions
@ -91,7 +98,7 @@ class PatchInfo:
# we have to rewrite this semi-parser to be smarter about looking inside the curly braces only,
# instead of just stupidly splitting on = and ,
print("Warning: crate name has the word 'package' in it, and we don't parse this correctly!")
print("Searching in {}, found {}".format(self.cratename, semiparse[0]))
print("Searching in {}, found {}".format(this_crate, semiparse[0]))
else:
# extract the first index where 'package' is found
index = [idx for idx, s in enumerate(semiparse) if 'package' in s][0]
@ -101,12 +108,12 @@ class PatchInfo:
depcrate = line.strip().split('=')[0].strip()
# print("simple package name: {}".format(depcrate))
# print("{}:{}".format(self.cratename, depcrate))
# print("{}:{}".format(this_crate, depcrate))
if depcrate in VERSIONS:
oldver = VERSIONS[depcrate]
(newline, numsubs) = re.subn(oldver, bump_version(oldver), line)
if numsubs != 1:
print("Warning! Version substitution failed for {}:{} in crate {}".format(depcrate, oldver, self.cratename))
if numsubs != 1 and not "\"*\"" in newline:
print("Warning! Version substitution failed for {}:{} in crate {} ({})".format(depcrate, oldver, this_crate, numsubs))
# print("orig: {}\nnew: {}".format(line, newline))
file.write(newline)
@ -136,14 +143,36 @@ def main():
args = parser.parse_args()
if args.bump_versions:
cargo_toml_paths = []
for path in Path('.').rglob('Cargo.toml'):
if 'target' not in str(path):
not_core_path = True
for editpath in CRATES.values():
if editpath in str(Path(path)).replace('\\', '/'): # fix windows paths
not_core_path = False
if not_core_path:
cargo_toml_paths += [path]
#import pprint
#pp = pprint.PrettyPrinter(indent=2)
#pp.pprint(cargo_toml_paths)
patches = []
# extract the versions of crates to patch
for (crate, path) in CRATES.items():
patchinfo = PatchInfo(path, crate)
#print("extracting {}".format(path))
patchinfo = PatchInfo(path + '/Cargo.toml', crate)
if not patchinfo.get_version():
print("Couldn't extract version info from {} crate".format(crate))
exit(1)
patches += [patchinfo]
# now extract all the *other* crates
for path in cargo_toml_paths:
#print("{}".format(str(path)))
patchinfo = PatchInfo(path)
patches += [patchinfo]
print(VERSIONS)
for (name, ver) in VERSIONS.items():
print("{}: {} -> {}".format(name, ver, bump_version(ver)))