Browse Source

Add argument parsing and start working on webserver

Nathaniel van Diepen 3 years ago
parent
commit
5eac7dc776
4 changed files with 97 additions and 30 deletions
  1. 63 28
      pacman_repo/__init__.py
  2. 5 2
      pacman_repo/repo.py
  3. 25 0
      pacman_repo/server.py
  4. 4 0
      requirements.txt

+ 63 - 28
pacman_repo/__init__.py

@@ -1,41 +1,76 @@
 import sys
+import argparse
 
 from .config import Config
 from .util import term
 from .repo import Repo
+from . import server
 
+def update(args, config):
+    print(config.path)
+    print('---')
+    print()
+    print(config)
+    print()
+    for repo in config.repos:
+        print(repo.path)
+        print('---')
+        print()
+        print(repo)
+        print()
 
-def main(args):
-    try:
-        config = Config(args[0] if len(args) else '/etc/pacman-repo.d')
-        print(config.path)
+    for server in config.servers:
+        print(server.path)
         print('---')
         print()
-        print(config)
+        print(server)
         print()
-        for repo in config.repos:
-            print(repo.path)
-            print('---')
-            print()
-            print(repo)
-            print()
-
-        for server in config.servers:
-            print(server.path)
-            print('---')
-            print()
-            print(server)
-            print()
-
-        repos = []
-        for repo in config.repos:
-            repo = Repo(repo)
-            repos.append(repo)
-            print("Repo: {}".format(repo.name))
-            for package in repo.packages:
-                package.update()
-                print("  {0}: {1}".format(package.name, package.url))
-                package.makepkg()
+
+    repos = []
+    for repo in config.repos:
+        repo = Repo(repo)
+        repos.append(repo)
+        print("Repo: {}".format(repo.name))
+        for package in repo.packages:
+            package.update()
+            print("  {0}: {1}".format(package.name, package.url))
+            package.makepkg(args.force)
+
+def webserver(args, config):
+    server.start()
+
+def main(argv):
+    # Setup argument parser
+    parser = argparse.ArgumentParser(
+        prog="pacman-repo",
+        description='A tool for automating the creation and maintenance of pacman repos')
+    parser.add_argument(
+        '--config', '-c',
+        default='/etc/pacman-repo.d',
+        help="Configuration folder path"
+    )
+    subparsers = parser.add_subparsers()
+    # Update command
+    update_parser = subparsers.add_parser('update', help='Update repository')
+    update_parser.add_argument(
+        '--force', '-f',
+        action="store_true",
+        help="Force rebuild of all packages, even if they already exist"
+    )
+    update_parser.set_defaults(func=update)
+    # Daemon command
+    webserver_parser = subparsers.add_parser('webserver', help='Run as a webserver')
+    webserver_parser.add_argument(
+        '--weserver',
+        action='store_true',
+        help='Run webserver '
+    )
+    webserver_parser.set_defaults(func=webserver)
+    # Handle arguments
+    args = parser.parse_args(argv)
+    try:
+        config = Config(args.config)
+        args.func(args, config)
 
     except Exception:
         from traceback import format_exc

+ 5 - 2
pacman_repo/repo.py

@@ -65,8 +65,11 @@ class Package(object):
             self._git("fetch", "--prune", "origin")
             self._git("checkout", "-B", "master", "origin/master")
 
-    def makepkg(self):
-        self._exec("makepkg")
+    def makepkg(self, force=False):
+        if force:
+            self._exec("makepkg", "-f")
+        else:
+            self._exec("makepkg")
 
 
 class Repo(object):

+ 25 - 0
pacman_repo/server.py

@@ -0,0 +1,25 @@
+from aiohttp import web
+from aiohttp.web import middleware
+from aiohttp.web import View
+
+
+async def handle(request):
+    name = request.match_info.get('name', "Anonymous")
+    return web.json_response(dict(text="Hello, {}".format(name)))
+
+@middleware
+async def auth(request, handler):
+    # TODO - check auth cookie
+    return await handler(request)
+
+
+def start():
+    app = web.Application(middlewares=[auth])
+    app.add_routes([
+        web.get('/', handle),
+        web.get('/{name}', handle)
+    ])
+    web.run_app(app)
+
+if __name__ == '__main__':
+    start()

+ 4 - 0
requirements.txt

@@ -3,3 +3,7 @@ intercessions==1.1.6
 PyYAML==3.13
 psutil==5.7.0
 python-slugify==4.0.0
+aiohttp==3.6.2
+cchardet==2.1.6
+aiodns==2.0.0
+brotlipy==0.7.0