import os import toml from glob import glob abspath = os.path.abspath(__file__) dname = os.path.dirname(abspath) os.chdir(dname) # ======= load test list tests = {} for f in sorted(glob('./src/bin/*.rs')): name = os.path.splitext(os.path.basename(f))[0] features = [] with open(f, 'r') as f: for line in f: if line.startswith('// required-features:'): features = line.split(':', 2)[1].strip().split(',') tests[name] = features # ========= Update Cargo.toml things = { 'bin': [ { 'name': f'{name}', 'path': f'src/bin/{name}.rs', 'required-features': features, } for name, features in tests.items() ] } SEPARATOR_START = '# BEGIN TESTS\n' SEPARATOR_END = '# END TESTS\n' HELP = '# Generated by gen_test.py. DO NOT EDIT.\n' with open('Cargo.toml', 'r') as f: data = f.read() before, data = data.split(SEPARATOR_START, maxsplit=1) _, after = data.split(SEPARATOR_END, maxsplit=1) data = before + SEPARATOR_START + HELP + \ toml.dumps(things) + SEPARATOR_END + after with open('Cargo.toml', 'w') as f: f.write(data)