run.py 861 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import os
  2. import sys
  3. from pathlib import Path
  4. from typing import NoReturn
  5. from . import find_requirements
  6. from .exceptions import RequirementsNotFound
  7. from .formatters import FORMATTERS
  8. def _die(message) -> NoReturn:
  9. sys.stderr.write("%s\n" % message)
  10. sys.exit(1)
  11. def run() -> NoReturn:
  12. if len(sys.argv) > 1:
  13. path = Path(sys.argv[1])
  14. else:
  15. path = Path.cwd()
  16. if not path.exists():
  17. _die("%s does not exist" % path)
  18. if not path.is_dir():
  19. _die("%s is not a directory" % path)
  20. try:
  21. requirements = find_requirements(path)
  22. except RequirementsNotFound:
  23. _die("Unable to find requirements at %s" % path)
  24. format_name = "requirements_file" # TODO: other output formats such as JSON
  25. FORMATTERS[format_name](requirements)
  26. sys.exit(0)
  27. if __name__ == "__main__":
  28. run()