manager.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2005-2009 TUBITAK/UEKAE
  5. #
  6. # This program is free software; you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free
  8. # Software Foundation; either version 2 of the License, or (at your option)
  9. # any later version.
  10. #
  11. # Please read the COPYING file.
  12. #
  13. # Disable cyclic garbage collector of Python to avoid comar segmentation
  14. # faults under high load (#11110)
  15. import gc
  16. gc.disable()
  17. import os
  18. import locale
  19. import string
  20. # FIXME: later this will be Comar's job
  21. systemlocale = open("/etc/mudur/locale", "r").readline().strip()
  22. # for pisi
  23. os.environ["LC_ALL"] = systemlocale
  24. # for system error messages
  25. locale.setlocale(locale.LC_ALL, systemlocale)
  26. try:
  27. import pisi.api
  28. import pisi.db
  29. import pisi.ui
  30. import pisi.util as util
  31. import pisi.configfile
  32. from pisi.version import Version
  33. except KeyboardInterrupt:
  34. notify("System.Manager", "cancelled", "")
  35. class UI(pisi.ui.UI):
  36. def error(self, msg):
  37. notify("System.Manager", "error", str(msg))
  38. def warning(self, msg):
  39. notify("System.Manager", "warning", str(msg))
  40. def notify(self, event, **keywords):
  41. if event == pisi.ui.installing:
  42. pkgname = keywords["package"].name
  43. notify("System.Manager", "status", ("installing", pkgname, "", ""))
  44. elif event == pisi.ui.configuring:
  45. pkgname = keywords["package"].name
  46. notify("System.Manager", "status", ("configuring", pkgname, "", ""))
  47. elif event == pisi.ui.extracting:
  48. pkgname = keywords["package"].name
  49. notify("System.Manager", "status", ("extracting", pkgname, "", ""))
  50. elif event == pisi.ui.updatingrepo:
  51. reponame = keywords["name"]
  52. notify("System.Manager", "status", ("updatingrepo", reponame, "", ""))
  53. elif event == pisi.ui.removing:
  54. pkgname = keywords["package"].name
  55. notify("System.Manager", "status", ("removing", pkgname, "", ""))
  56. elif event == pisi.ui.cached:
  57. total = str(keywords["total"])
  58. cached = str(keywords["cached"])
  59. notify("System.Manager", "status", ("cached", total, cached, ""))
  60. elif event == pisi.ui.installed:
  61. notify("System.Manager", "status", ("installed", "", "", ""))
  62. elif event == pisi.ui.removed:
  63. notify("System.Manager", "status", ("removed", "", "", ""))
  64. elif event == pisi.ui.upgraded:
  65. notify("System.Manager", "status", ("upgraded", "", "", ""))
  66. elif event == pisi.ui.packagestogo:
  67. notify("System.Manager", "status", ("order", "", "", ""))
  68. elif event == pisi.ui.desktopfile:
  69. filepath = keywords["desktopfile"]
  70. notify("System.Manager", "status", ("desktopfile", filepath, "", ""))
  71. else:
  72. return
  73. def ack(self, msg):
  74. return True
  75. def confirm(self, msg):
  76. return True
  77. def display_progress(self, operation, percent, info="", **kw):
  78. if operation == "fetching":
  79. file_name = kw["filename"]
  80. if not file_name.startswith("pisi-index.xml"):
  81. file_name = pisi.util.parse_package_name(file_name)[0]
  82. out = (operation, file_name, str(percent), int(kw["rate"]), kw["symbol"], int(kw["downloaded_size"]), int(kw["total_size"]))
  83. else:
  84. out = (operation, str(percent), info, 0, 0, 0, 0)
  85. notify("System.Manager", "progress", out)
  86. def _init_pisi():
  87. ui = UI()
  88. try:
  89. pisi.api.set_userinterface(ui)
  90. except KeyboardInterrupt:
  91. cancelled()
  92. def cancelled():
  93. notify("System.Manager", "cancelled", None)
  94. def started(operation=""):
  95. notify("System.Manager", "started", operation)
  96. def finished(operation=""):
  97. if operation in ["System.Manager.setCache", "System.Manager.installPackage", "System.Manager.removePackage", "System.Manager.updatePackage"]:
  98. __checkCacheLimits()
  99. notify("System.Manager", "finished", operation)
  100. def privileged(func):
  101. """
  102. Decorator for synchronizing privileged functions
  103. """
  104. def wrapper(*__args,**__kw):
  105. operation = "System.Manager.%s" % func.func_name
  106. started(operation)
  107. _init_pisi()
  108. try:
  109. func(*__args,**__kw)
  110. except KeyboardInterrupt:
  111. cancelled()
  112. return
  113. except Exception, e:
  114. notify("System.Manager", "error", str(e))
  115. return
  116. finished(operation)
  117. return wrapper
  118. @privileged
  119. def installPackage(package=None):
  120. if package:
  121. package = package.split(",")
  122. reinstall = package[0].endswith(".pisi")
  123. pisi.api.install(package, ignore_file_conflicts=True, reinstall=reinstall)
  124. @privileged
  125. def reinstallPackage(package=None):
  126. if package:
  127. package = package.split(",")
  128. pisi.api.install(package, ignore_file_conflicts=True, reinstall=True)
  129. @privileged
  130. def updatePackage(package=None):
  131. if package is None:
  132. package = []
  133. else:
  134. package = package.split(",")
  135. pisi.api.upgrade(package)
  136. @privileged
  137. def removePackage(package=None):
  138. if package:
  139. package = package.split(",")
  140. pisi.api.remove(package)
  141. @privileged
  142. def updateRepository(repository=None):
  143. if repository:
  144. pisi.api.update_repo(repository)
  145. @privileged
  146. def updateAllRepositories():
  147. repos = pisi.db.repodb.RepoDB().list_repos()
  148. for repo in repos:
  149. try:
  150. pisi.api.update_repo(repo)
  151. except pisi.db.repodb.RepoError, e:
  152. notify("System.Manager", "error", str(e))
  153. @privileged
  154. def addRepository(name=None,uri=None):
  155. if name and uri:
  156. pisi.api.add_repo(name,uri)
  157. @privileged
  158. def removeRepository(repo=None):
  159. if repo:
  160. pisi.api.remove_repo(repo)
  161. @privileged
  162. def setRepoActivities(repos=None):
  163. if repos:
  164. for repo, active in repos.items():
  165. pisi.api.set_repo_activity(repo, active)
  166. @privileged
  167. def setRepositories(repos):
  168. oldRepos = pisi.db.repodb.RepoDB().list_repos(only_active=False)
  169. for repo in oldRepos:
  170. pisi.api.remove_repo(repo)
  171. for repo in repos:
  172. pisi.api.add_repo(repo[0], repo[1])
  173. @privileged
  174. # ex: setConfig("general", "bandwidth_limit", "30")
  175. def setConfig(category, name, value):
  176. config = pisi.configfile.ConfigurationFile("/etc/pisi/pisi.conf")
  177. config.set(category, name, value)
  178. config.write_config()
  179. @privileged
  180. def setCache(enabled, limit):
  181. config = pisi.configfile.ConfigurationFile("/etc/pisi/pisi.conf")
  182. config.set("general", "package_cache", str(enabled))
  183. config.set("general", "package_cache_limit", str(limit))
  184. config.write_config()
  185. @privileged
  186. def takeSnapshot():
  187. pisi.api.snapshot()
  188. @privileged
  189. def takeBack(operation):
  190. pisi.api.takeback(operation)
  191. @privileged
  192. def clearCache(cacheDir, limit):
  193. pisi.api.clearCache(int(limit) == 0)
  194. def __checkCacheLimits():
  195. cached_pkgs_dir = "/var/cache/pisi/packages"
  196. config = pisi.configfile.ConfigurationFile("/etc/pisi/pisi.conf")
  197. cache = config.get("general", "package_cache")
  198. if cache == "True":
  199. limit = config.get("general", "package_cache_limit")
  200. # If PackageCache is used and limit is 0. It means limitless.
  201. if limit and int(limit) != 0:
  202. clearCache(cached_pkgs_dir, int(limit) * 1024 * 1024)
  203. elif cache == "False":
  204. clearCache(cached_pkgs_dir, 0)