Created
July 30, 2022 17:47
-
-
Save serephus/670d8d601a1ae1350fe87e20ea474d15 to your computer and use it in GitHub Desktop.
Generate ebuild CRATES var from Cargo.lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/python3 | |
| import sys | |
| from collections import namedtuple | |
| Package = namedtuple('Package', ['name', 'version']) | |
| name = '' | |
| version = '' | |
| packages = [] | |
| _started = False | |
| _path = sys.argv[1] if len(sys.argv) > 1 else 'Cargo.lock' | |
| with open(_path) as fp: | |
| for line in fp.readlines(): | |
| l = line.strip() | |
| if l.startswith('#') or l == '': | |
| continue | |
| if l.startswith('[[package]]'): | |
| _started = True | |
| continue | |
| if not _started: | |
| continue | |
| if l.startswith('name'): | |
| name = l.split('=')[1].split('"')[1] | |
| if l.startswith('version'): | |
| version = l.split('=')[1].split('"')[1] | |
| if l.startswith('source'): | |
| packages.append(Package(name=name, version=version)) | |
| name, version = '', '' | |
| _started = False | |
| if _started == True and name != '' and version != '': | |
| packages.append(Package(name=name, version=version)) | |
| packages.sort(key = lambda a: a.name) | |
| with open('crates', 'wt') as fp: | |
| fp.write('CRATES="\n') | |
| for pack in packages: | |
| fp.write(f'\t{pack.name}-{pack.version}\n') | |
| fp.write('"\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment