|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from dataclasses import dataclass |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +@dataclass |
| 8 | +class Package: |
| 9 | + """Class for tracking a package.""" |
| 10 | + name: str |
| 11 | + version: Optional[str] = None |
| 12 | + local_path: Optional[str] = None |
| 13 | + |
| 14 | + def get_dependency_line(self): |
| 15 | + if self.local_path: |
| 16 | + return f'{self.name} = {{ path = "{self.local_path}" }}' |
| 17 | + elif self.version: |
| 18 | + return f'{self.name} = "{self.version}"' |
| 19 | + else: |
| 20 | + return f'{self.name} = "*"' |
| 21 | + |
| 22 | +def install(package: Package): |
| 23 | + """Cargo build.""" |
| 24 | + try: |
| 25 | + with open("Cargo.toml", 'a') as handle: |
| 26 | + handle.write(package.get_dependency_line() + '\n') |
| 27 | + handle.flush() |
| 28 | + |
| 29 | + output = subprocess.check_output(['cargo', 'build'], stderr=subprocess.STDOUT) |
| 30 | + |
| 31 | + print('Install succeeded:') |
| 32 | + print(output.decode()) |
| 33 | + except subprocess.CalledProcessError as e: |
| 34 | + print('Failed to install:') |
| 35 | + print(e.output.decode()) |
| 36 | + # Always raise. |
| 37 | + # Install failing is either an interesting issue, or an opportunity to |
| 38 | + # improve the analysis. |
| 39 | + raise |
| 40 | + |
| 41 | + |
| 42 | +PHASES = { |
| 43 | + "all": [install], |
| 44 | + "install": [install], |
| 45 | +} |
| 46 | + |
| 47 | +def main(): |
| 48 | + args = list(sys.argv) |
| 49 | + script = args.pop(0) |
| 50 | + |
| 51 | + if len(args) < 2 or len(args) > 4: |
| 52 | + raise ValueError(f'Usage: {script} [--local file | --version version] phase package_name') |
| 53 | + |
| 54 | + # Parse the arguments manually to avoid introducing unnecessary dependencies |
| 55 | + # and side effects that add noise to the strace output. |
| 56 | + local_path = None |
| 57 | + version = None |
| 58 | + if args[0] == '--local': |
| 59 | + args.pop(0) |
| 60 | + local_path = args.pop(0) |
| 61 | + elif args[0] == '--version': |
| 62 | + args.pop(0) |
| 63 | + version = args.pop(0) |
| 64 | + |
| 65 | + phase = args.pop(0) |
| 66 | + package_name = args.pop(0) |
| 67 | + |
| 68 | + if not phase in PHASES: |
| 69 | + print(f'Unknown phase {phase} specified.') |
| 70 | + exit(1) |
| 71 | + |
| 72 | + package = Package(name=package_name, version=version, local_path=local_path) |
| 73 | + |
| 74 | + # Execute for the specified phase. |
| 75 | + for phase in PHASES[phase]: |
| 76 | + phase(package) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + main() |
0 commit comments