run.py 1001 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. """
  2. Run a Python script. The script name should come from a script name in 'scripts'.
  3. """
  4. import os
  5. import sys
  6. import runpy
  7. from pathlib import Path
  8. import logging
  9. import subprocess as sp
  10. _log = logging.getLogger('run.py')
  11. src_dir = Path(__file__).parent
  12. sys.path.insert(0, src_dir)
  13. from bookdata import setup, bin_dir
  14. from bookdata.db import db_url
  15. setup()
  16. if sys.argv[1] == '--rust':
  17. # this is a rust command
  18. del sys.argv[1]
  19. # build the Rust tools
  20. # TODO support alternate working directories
  21. _log.info('compiling Rust tools')
  22. sp.run(['cargo', 'build', '--release'], check=True)
  23. tool = bin_dir / 'bookdata'
  24. tool = os.fspath(tool)
  25. if 'DB_URL' not in os.environ:
  26. os.environ['DB_URL'] = db_url()
  27. _log.info('running tool %s', sys.argv[1:])
  28. sp.run([tool] + sys.argv[1:], check=True)
  29. else:
  30. script = sys.argv[1]
  31. _log.info('preparing to run scripts.%s', script)
  32. del sys.argv[1]
  33. runpy.run_module(f'scripts.{script}', alter_sys=True)