repo.py 2.6 KB

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