Browse Source

Start working on cache directory handleing

Nathaniel van Diepen 3 years ago
parent
commit
5ac936eeec
3 changed files with 43 additions and 5 deletions
  1. 1 0
      etc/pacman-repo.d/pacman-repo.yml
  2. 7 0
      pacman_repo/config.py
  3. 35 5
      pacman_repo/repo.py

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

@@ -2,6 +2,7 @@
 verbosity: 3
 maxthreads: 4
 logdir: /var/log/pacman-repo.d
+cachedir: ~/.cache/pacman-repo.d
 truncatelogs: true
 repos:
   - repos.d

+ 7 - 0
pacman_repo/config.py

@@ -47,6 +47,13 @@ class Config(BaseConfig):
         with open(self.path) as f:
             self._data = yaml.load(f, Loader=yaml.SafeLoader) or {}
 
+        if not self._data.get("cachedir"):
+            self._data["cachedir"] = "/var/cache/pacman-repo.d"
+
+        if not os.path.exists(self["cachedir"]):
+            os.makedirs(self["cachedir"])
+
+        print("Creating cachedir {}".format(os.path.realpath(self["cachedir"])))
         self.repos = []
         self.servers = []
         with pushd(path):

+ 35 - 5
pacman_repo/repo.py

@@ -1,7 +1,37 @@
+import os
+import subprocess
+
+from .pkgbuild import PKGBUILD
+
+
 class Package(object):
-    def __init__(self, name, url=None):
-        self.name = name
-        self.url = url or "https://aur.archlinux.org/{}.git".format(name)
+    def __init__(self, config):
+        self.config = config
+
+    @property
+    def name(self):
+        return self.config["name"]
+
+    @property
+    def url(self):
+        return self.config.get("url") or "https://aur.archlinux.org/{}.git".format(
+                self.name)
+
+    @property
+    def path(self):
+        return os.path.join(self.config.config.cachedir, self.name)
+
+    @property
+    def PKGBUILD(self):
+        if not os.path.exists(self.path):
+            subprocess.check_call(["git", "clone", self.url],
+                    cwd=self.config.config.cachedir)
+
+        if not hasattr(self, '_PKGBUILD'):
+            path = os.path.join(self.path, 'PKGBUILD')
+            self._PKGBUILD = PKGBUILD(path)
+
+        return self._PKGBUILD
 
 
 class Repo(object):
@@ -10,10 +40,10 @@ class Repo(object):
         self.packages = []
         for package in self.config["packages"]:
             if isinstance(package, str):
-                self.packages.append(Package(package))
+                self.packages.append(Package(dict(name=package)))
 
             else:
-                self.packages.append(Package(package["name"], package.get("url")))
+                self.packages.append(Package(package))
 
     @property
     def name(self):