Software repository API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

62 lines
2.1 KiB

  1. import ZODB
  2. import ZODB.FileStorage
  3. import persistent
  4. import BTrees.OOBTree
  5. from repobot.provider import providers
  6. import os
  7. from repobot.common import plist, pmap
  8. class Repo(persistent.Persistent):
  9. def __init__(self, name, provider):
  10. self.name = name
  11. self.provider = provider
  12. self.packages = pmap()
  13. self.data = pmap()
  14. def get_package(self, name, version):
  15. if name not in self.packages:
  16. self.packages[name] = pmap()
  17. if version not in self.packages[name]:
  18. self.packages[name][version] = RepoPackage(name, version)
  19. return self.packages[name][version]
  20. class RepoPackage(persistent.Persistent):
  21. def __init__(self, name, version):
  22. self.name = name
  23. self.version = version
  24. self.data = pmap()
  25. class RepoDb(object):
  26. def __init__(self, db_path, data_root):
  27. self.storage = ZODB.FileStorage.FileStorage(db_path)
  28. self.db = ZODB.DB(self.storage)
  29. self.data_root = data_root
  30. with self.db.transaction() as c:
  31. if "repos" not in c.root():
  32. c.root.repos = BTrees.OOBTree.BTree()
  33. def add_package(self, provider, reponame, pkgname, pkgversion, fname, fobj, params):
  34. with self.db.transaction() as c:
  35. repo = self._get_repo(c, provider, reponame)
  36. datadir = os.path.join(self.data_root, provider, reponame)
  37. provider = providers[repo.provider](self.db, repo, datadir)
  38. provider.add_package(repo.get_package(pkgname, pkgversion), fname, fobj, params)
  39. def _get_repo(self, c, provider, name):
  40. if provider not in c.root.repos:
  41. c.root.repos[provider] = pmap()
  42. if name not in c.root.repos[provider]:
  43. c.root.repos[provider][name] = Repo(name, provider)
  44. return c.root.repos[provider][name]
  45. def browse_repo(self, provider, reponame, args):
  46. with self.db.transaction() as c:
  47. repo = c.root.repos[provider][reponame]
  48. datadir = os.path.join(self.data_root, provider, reponame)
  49. provider = providers[repo.provider](self.db, repo, datadir)
  50. return provider.browse(args)