Browse Source

Clean up repo execution code

Nathaniel van Diepen 3 years ago
parent
commit
0261a36491
1 changed files with 43 additions and 16 deletions
  1. 43 16
      pacman_repo/repo.py

+ 43 - 16
pacman_repo/repo.py

@@ -6,31 +6,56 @@ from asyncio import subprocess
 from async_property import async_cached_property
 
 
+class PackageException(Exception):
+    pass
+
+
 class Package(object):
     def __init__(self, repo, config):
         self.repo = repo
         self.config = config
 
-    async def _exec(self, *args):
+    async def _exec(self, *args, check=True):
         p = await asyncio.create_subprocess_exec(*args,
                 cwd=self.path,
                 stdout=subprocess.PIPE,
                 stderr=subprocess.STDOUT)
         stdout, stderr = await p.communicate()
-        if p.returncode:
-            print(stdout.decode("utf-8"))
+        if check and p.returncode:
+            raise PackageException(stdout.decode("utf-8"))
+
+        return p.returncode, stdout
+
+    async def _execAll(self, *tasks, check=True):
+        output = ""
+        for args in tasks:
+            code, stdout = await self._exec(*args, check=False)
+            output += stdout.decode('utf-8')
+            if code:
+                if check:
+                    print(output)
+                break
 
-    async def _git(self, *args):
-        await self._exec('git', *args)
+        return code, output
+
+    async def _git(self, *args, check=True):
+        return await self._exec('git', *args, check=check)
+
+    async def _gitAll(self, *tasks, check=True):
+        for args in tasks:
+            args.insert(0, 'git')
+
+        return await self._execAll(*tasks, check=check)
 
     async def _clone(self):
         if not os.path.exists(self.path):
             os.makedirs(self.path)
 
-        await self._git("init")
-        await self._git("remote", "add", "origin", self.url)
-        await self._git("fetch", "origin")
-        await self._git("checkout", "master")
+        return await self._gitAll(
+                ["init"],
+                ["remote", "add", "origin", self.url],
+                ["fetch", "origin"],
+                ["checkout", "master"])
 
     @property
     def name(self):
@@ -60,19 +85,21 @@ class Package(object):
 
     async def update(self):
         if not os.path.exists(os.path.join(self.path, '.git')):
-            await self._clone()
+            return await self._clone(check=False)
 
         else:
-            await self._git("remote", "set-url", "origin", self.url)
-            await self._git("checkout", "-f")
-            await self._git("fetch", "--prune", "origin")
-            await self._git("checkout", "-B", "master", "origin/master")
+            return await self._gitAll(
+                    ["remote", "set-url", "origin", self.url],
+                    ["checkout", "-f"],
+                    ["fetch", "--prune", "origin"],
+                    ["checkout", "-B", "master", "origin/master"],
+                    check=False)
 
     async def makepkg(self, force=False):
         if force:
-            await self._exec("makepkg", "-f")
+            return await self._exec("makepkg", "-f", check=False)
         else:
-            await self._exec("makepkg")
+            return await self._exec("makepkg", check=False)
 
 
 class Repo(object):