|
| 1 | +import sys |
| 2 | +import typing |
| 3 | + |
| 4 | +from plumbum import cli |
| 5 | + |
| 6 | +from . import * |
| 7 | +from .BackendsDiscoverer import discoveredBackends |
| 8 | + |
| 9 | + |
| 10 | +class File2PackageCLI(cli.Application): |
| 11 | + pass |
| 12 | + |
| 13 | + |
| 14 | +@File2PackageCLI.subcommand("backends") |
| 15 | +class File2PackageCLIListBackends(cli.Application): |
| 16 | + def main(self): |
| 17 | + from pprint import pprint |
| 18 | + |
| 19 | + pprint(discoveredBackends) |
| 20 | + |
| 21 | + |
| 22 | +cacheDBAttr = cli.SwitchAttr(["-D", "--db"], str, help="Either a path to SQLite db file or just a name. In the case of a name the file will be created in cache dir.") |
| 23 | + |
| 24 | + |
| 25 | +def file2PackageWithStringFilePath(backend: str, cacheDB: str): |
| 26 | + try: |
| 27 | + cacheDBPath = Path(cacheDB).absolute() |
| 28 | + if cacheDBPath.is_file() or cacheDBPath.suffix.lower in {".sqlite", ".db"}: |
| 29 | + return File2Package(backend, cacheDB=cacheDBPath) |
| 30 | + except BaseException: |
| 31 | + pass |
| 32 | + |
| 33 | + return File2Package(backend, cacheDB=cacheDB) |
| 34 | + |
| 35 | + |
| 36 | +@File2PackageCLI.subcommand("refresh") |
| 37 | +class File2PackageCLIRefresh(cli.Application): |
| 38 | + """Recreates the database for the backend""" |
| 39 | + |
| 40 | + def main(self, backend: str, cacheDB: str = None): |
| 41 | + with file2PackageWithStringFilePath(backend, cacheDB=cacheDB) as f2p: |
| 42 | + f2p.createDB() |
| 43 | + |
| 44 | + |
| 45 | +@File2PackageCLI.subcommand("lookup") |
| 46 | +class File2PackageCLILookup(cli.Application): |
| 47 | + """Prints list of packages to which files belong""" |
| 48 | + |
| 49 | + cacheDb = cacheDBAttr |
| 50 | + json = cli.Flag(["-J", "--json"], help="print JSON") |
| 51 | + |
| 52 | + def main(self, backend: str, *files): |
| 53 | + from collections import OrderedDict |
| 54 | + |
| 55 | + res = OrderedDict() |
| 56 | + import json |
| 57 | + |
| 58 | + with file2PackageWithStringFilePath(backend, cacheDB=self.cacheDb) as f2p: |
| 59 | + for f in files: |
| 60 | + fp = Path(f).absolute() |
| 61 | + pkg = f2p[fp] |
| 62 | + res[str(fp)] = (pkg.name, pkg.arch) |
| 63 | + |
| 64 | + if self.json: |
| 65 | + print(json.dumps(res, indent="\t")) |
| 66 | + else: |
| 67 | + print("\n".join(":".join(v) for v in res.values())) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + File2PackageCLI.run() |
0 commit comments