Browse Source

Start working on config

Nathaniel van Diepen 3 years ago
parent
commit
498511726e

+ 4 - 2
etc/pacman-repo.yml → etc/pacman-repo.d/pacman-repo.yml

@@ -3,5 +3,7 @@ verbosity: 3
 maxthreads: 4
 logdir: /var/log/pacman-repo.d
 truncatelogs: true
-sources:
-  - pacman-repo.d
+repos:
+  - repos.d
+servers:
+  - servers.d

+ 0 - 0
etc/pacman-repo.d/repo.yml


+ 1 - 0
etc/pacman-repo.d/repos.d/repo.yml

@@ -0,0 +1 @@
+---

+ 1 - 0
etc/pacman-repo.d/servers.d/server.yml

@@ -0,0 +1 @@
+---

+ 19 - 2
pacman_repo/__init__.py

@@ -1,13 +1,30 @@
 import sys
 
-from .config import BaseConfig
+from .config import Config
 from .util import term
 
 
 def main(args):
     try:
-        config = BaseConfig(args[0] if len(args) else '/etc/pacman-repo.yml')
+        config = Config(args[0] if len(args) else '/etc/pacman-repo.d')
+        print(config.path)
+        print('---')
+        print()
         print(config)
+        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()
 
     except Exception:
         from traceback import format_exc

+ 1 - 3
pacman_repo/__main__.py

@@ -3,6 +3,4 @@ import sys
 
 from . import main as pacman_backup
 
-
-def main():
-    pacman_backup(sys.argv[1:])
+pacman_backup(sys.argv[1:])

+ 50 - 19
pacman_repo/config.py

@@ -1,6 +1,8 @@
 import yaml
 import os
 
+from glob import glob
+from slugify import slugify
 from .util import pushd
 
 
@@ -9,39 +11,68 @@ class ConfigException(Exception):
 
 
 class BaseConfig(object):
-    def __init__(self, path):
-        self.path = path
-        if not os.path.exists(path):
-            raise ConfigException("Base config file not found: {}".format(path))
-
-        with open(path) as f:
-            self._data = yaml.load(f, Loader=yaml.SafeLoader)
-
-        self.sources = []
-        with pushd(os.path.dirname(path)):
-            for source in self["sources"]:
-                self.sources.append(RepoConfig(self, source))
-
     def __getitem__(self, name):
         return self._data[name]
 
     def __contains__(self, name):
         return name in self._data
 
+    def __str__(self):
+        return yaml.dump(self._data, default_flow_style=False)
+
+
+class Config(BaseConfig):
+    def __init__(self, path):
+        self.path = os.path.join(path, 'pacman-repo.yml')
+        if not os.path.exists(self.path):
+            self.path = os.path.join(path, 'pacman-repo.yaml')
+            if not os.path.exists(self.path):
+                raise ConfigException("Base config file not found: {}".format(path))
+
+        with open(self.path) as f:
+            self._data = yaml.load(f, Loader=yaml.SafeLoader) or {}
+
+        self.repos = []
+        self.servers = []
+        with pushd(path):
+            for source in self["repos"]:
+                for path in glob('{}/*.yml'.format(source)) + \
+                        glob('{}/*.yaml'.format(source)):
+                    self.repos.append(RepoConfig(self, path))
+
+            for server in self["servers"]:
+                for path in glob('{}/*.yml'.format(server)) + \
+                        glob('{}/*.yaml'.format(server)):
+                    self.servers.append(ServerConfig(self, path))
+
 
-class RepoConfig(object):
+class RepoConfig(BaseConfig):
     def __init__(self, config, path):
         self.path = path
         self.config = config
+        self.id = slugify(path, max_length=255)
+        self.name = os.path.splitext(os.path.basename(path))[0]
 
         if not os.path.exists(path):
             raise ConfigException("Repo config file not found: {}".format(path))
 
         with open(path) as f:
-            self._data = yaml.load(f, Loader=yaml.SafeLoader)
+            self._data = yaml.load(f, Loader=yaml.SafeLoader) or {}
 
-    def __getitem__(self, name):
-        return self._data[name]
+        self._data['name'] = self.name
 
-    def __contains__(self, name):
-        return name in self._data
+
+class ServerConfig(BaseConfig):
+    def __init__(self, config, path):
+        self.path = path
+        self.config = config
+        self.id = slugify(path, max_length=255)
+        self.name = os.path.splitext(os.path.basename(path))[0]
+
+        if not os.path.exists(path):
+            raise ConfigException("Repo config file not found: {}".format(path))
+
+        with open(path) as f:
+            self._data = yaml.load(f, Loader=yaml.SafeLoader) or {}
+
+        self._data['name'] = self.name