__init__.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import sys
  2. import asyncio
  3. import argparse
  4. from .config import Config
  5. from .util import term
  6. from .repo import Repo
  7. from . import server
  8. async def _update(package, args):
  9. print(" {0}: {1}".format(package.name, package.url))
  10. await package.update()
  11. await package.makepkg(args.force)
  12. async def update(args, config):
  13. for repo in config.repos:
  14. repo = Repo(repo)
  15. print("Repo: {}".format(repo.name))
  16. tasks = []
  17. for package in repo.packages:
  18. tasks.append(_update(package, args))
  19. await asyncio.gather(*tasks)
  20. async def webserver(args, config):
  21. server.start()
  22. def main(argv):
  23. # Setup argument parser
  24. parser = argparse.ArgumentParser(
  25. prog="pacman-repo",
  26. description='A tool for automating the creation and maintenance of pacman repos')
  27. parser.add_argument(
  28. '--config', '-c',
  29. default='/etc/pacman-repo.d',
  30. help="Configuration folder path"
  31. )
  32. subparsers = parser.add_subparsers(
  33. required=True, dest='command', metavar='command')
  34. # Update command
  35. update_parser = subparsers.add_parser('update', help='Update repository')
  36. update_parser.add_argument(
  37. '--force', '-f',
  38. action="store_true",
  39. help="Force rebuild of all packages, even if they already exist"
  40. )
  41. update_parser.set_defaults(func=update)
  42. # Daemon command
  43. webserver_parser = subparsers.add_parser('webserver', help='Run as a webserver')
  44. webserver_parser.add_argument(
  45. '--weserver',
  46. action='store_true',
  47. help='Run webserver '
  48. )
  49. webserver_parser.set_defaults(func=webserver)
  50. # Handle arguments
  51. args = parser.parse_args(argv)
  52. try:
  53. config = Config(args.config)
  54. asyncio.run(args.func(args, config))
  55. except Exception:
  56. from traceback import format_exc
  57. msg = "Error encountered:\n" + format_exc().strip()
  58. print(term().red(msg))
  59. sys.exit(1)
  60. if __name__ == '__main__':
  61. main(sys.argv[1:])