PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/dnf/query.py

https://github.com/mscherer/dnf
Python | 125 lines | 81 code | 18 blank | 26 comment | 18 complexity | 7e94d0dc9df280edf182a694a2465ea7 MD5 | raw file
Possible License(s): GPL-2.0
  1. # query.py
  2. # Implements Query.
  3. #
  4. # Copyright (C) 2012-2013 Red Hat, Inc.
  5. #
  6. # This copyrighted material is made available to anyone wishing to use,
  7. # modify, copy, or redistribute it subject to the terms and conditions of
  8. # the GNU General Public License v.2, or (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY expressed or implied, including the implied warranties of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. # Public License for more details. You should have received a copy of the
  13. # GNU General Public License along with this program; if not, write to the
  14. # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. # 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
  16. # source code or documentation are not subject to the GNU General Public
  17. # License and may only be used or replicated with the express permission of
  18. # Red Hat, Inc.
  19. #
  20. from __future__ import absolute_import
  21. from __future__ import unicode_literals
  22. import functools
  23. import hawkey
  24. import dnf.exceptions
  25. import dnf.selector
  26. import dnf.util
  27. from dnf.pycomp import basestring
  28. def is_nevra(pattern):
  29. try:
  30. hawkey.split_nevra(pattern)
  31. except hawkey.ValueException:
  32. return False
  33. return True
  34. class Query(hawkey.Query):
  35. # :api
  36. # :api also includes hawkey.Query.filter
  37. def available(self):
  38. # :api
  39. return self.filter(reponame__neq=hawkey.SYSTEM_REPO_NAME)
  40. def downgrades(self):
  41. # :api
  42. return self.filter(downgrades=True)
  43. def filter_autoglob(self, *args, **kwargs):
  44. nargs = {}
  45. for (key, value) in kwargs.items():
  46. if dnf.util.is_glob_pattern(value):
  47. nargs[key + "__glob"] = value
  48. else:
  49. nargs[key] = value
  50. return self.filter(*args, **nargs)
  51. def installed(self):
  52. # :api
  53. return self.filter(reponame=hawkey.SYSTEM_REPO_NAME)
  54. def latest(self):
  55. # :api
  56. return self.filter(latest_per_arch=True)
  57. def upgrades(self):
  58. # :api
  59. return self.filter(upgrades=True)
  60. def name_dict(self):
  61. d = {}
  62. for pkg in self:
  63. d.setdefault(pkg.name, []).append(pkg)
  64. return d
  65. def na_dict(self):
  66. d = {}
  67. for pkg in self.run():
  68. key = (pkg.name, pkg.arch)
  69. d.setdefault(key, []).append(pkg)
  70. return d
  71. def pkgtup_dict(self):
  72. return per_pkgtup_dict(self.run())
  73. def nevra(self, *args):
  74. args_len = len(args)
  75. if args_len == 3:
  76. return self.filter(name=args[0], evr=args[1], arch=args[2])
  77. if args_len == 1:
  78. nevra = hawkey.split_nevra(args[0])
  79. elif args_len == 5:
  80. nevra = args
  81. else:
  82. raise TypeError("nevra() takes 1, 3 or 5 str params")
  83. return self.filter(
  84. name=nevra.name, epoch=nevra.epoch, version=nevra.version,
  85. release=nevra.release, arch=nevra.arch)
  86. def by_provides(sack, patterns, ignore_case=False, get_query=False):
  87. if isinstance(patterns, basestring):
  88. patterns = [patterns]
  89. try:
  90. reldeps = list(map(functools.partial(hawkey.Reldep, sack), patterns))
  91. except hawkey.ValueException:
  92. return sack.query().filter(empty=True)
  93. q = sack.query()
  94. flags = []
  95. if ignore_case:
  96. flags.append(hawkey.ICASE)
  97. q.filterm(*flags, provides=reldeps)
  98. if get_query:
  99. return q
  100. return q.run()
  101. def per_pkgtup_dict(pkg_list):
  102. d = {}
  103. for pkg in pkg_list:
  104. d.setdefault(pkg.pkgtup, []).append(pkg)
  105. return d
  106. def per_nevra_dict(pkg_list):
  107. return {str(pkg):pkg for pkg in pkg_list}