repo.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import os
  2. import subprocess
  3. from .pkgbuild import PKGBUILD
  4. class Package(object):
  5. def __init__(self, repo, config):
  6. self.repo = repo
  7. self.config = config
  8. def _exec(self, *args):
  9. try:
  10. subprocess.run(args, cwd=self.path, capture_output=True, check=True,
  11. text=True)
  12. except subprocess.CalledProcessError as e:
  13. print("\n".join([e.stdout,e.stderr]))
  14. def _git(self, *args):
  15. self._exec('git', *args)
  16. def _clone(self):
  17. if not os.path.exists(self.path):
  18. os.makedirs(self.path)
  19. self._git("init")
  20. self._git("remote", "add", "origin", self.url)
  21. self._git("fetch", "origin")
  22. self._git("checkout", "master")
  23. @property
  24. def name(self):
  25. return self.config["name"]
  26. @property
  27. def url(self):
  28. return self.config.get("url") or "https://aur.archlinux.org/{}.git".format(
  29. self.name)
  30. @property
  31. def path(self):
  32. return os.path.join(self.repo.config.config['cachedir'], self.name)
  33. @property
  34. def PKGBUILD(self):
  35. if not os.path.exists(self.path):
  36. self._clone()
  37. if not os.path.exists(os.path.join(self.path, "PKGBUILD")):
  38. self.update()
  39. if not hasattr(self, '_PKGBUILD'):
  40. self._PKGBUILD = PKGBUILD(os.path.join(self.path, 'PKGBUILD'))
  41. return self._PKGBUILD
  42. def update(self):
  43. if not os.path.exists(os.path.join(self.path, '.git')):
  44. self._clone()
  45. else:
  46. self._git("remote", "set-url", "origin", self.url)
  47. self._git("checkout", "-f")
  48. self._git("fetch", "--prune", "origin")
  49. self._git("checkout", "-B", "master", "origin/master")
  50. def makepkg(self):
  51. self._exec("makepkg")
  52. class Repo(object):
  53. def __init__(self, config):
  54. self.config = config
  55. self.packages = []
  56. for package in self.config["packages"]:
  57. if isinstance(package, str):
  58. self.packages.append(Package(self, dict(name=package)))
  59. else:
  60. self.packages.append(Package(self, package))
  61. @property
  62. def name(self):
  63. return self.config.name
  64. @property
  65. def id(self):
  66. return self.config.id