__init__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2006,2007,2011 Guido Günther <agx@sigxcpu.org>
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, please see
  16. # <http://www.gnu.org/licenses/>
  17. """provides some debian source package related helpers"""
  18. import os
  19. import subprocess
  20. import gbp.command_wrappers as gbpc
  21. from gbp.errors import GbpError
  22. from gbp.git import GitRepositoryError
  23. # Make sure these are available with 'import gbp.deb'
  24. from gbp.deb.changelog import ChangeLog, NoChangeLogError
  25. from gbp.deb.policy import DebianPkgPolicy
  26. class DpkgCompareVersions(gbpc.Command):
  27. dpkg = '/usr/bin/dpkg'
  28. def __init__(self):
  29. if not os.access(self.dpkg, os.X_OK):
  30. raise GbpError("%s not found - cannot use compare versions" % self.dpkg)
  31. gbpc.Command.__init__(self, self.dpkg, ['--compare-versions'], capture_stderr=True)
  32. def __call__(self, version1, version2):
  33. """
  34. Compare two package versions. Return 0 if the versions are equal, -1 1 if version1 < version2,
  35. and 1 oterwise.
  36. @raises CommandExecFailed: if the version comparison fails
  37. """
  38. self.run_error = "Couldn't compare %s with %s" % (version1, version2)
  39. res = self.call([ version1, 'lt', version2 ])
  40. if res not in [ 0, 1 ]:
  41. if self.stderr:
  42. self.run_error += ' (%s)' % self.stderr
  43. raise gbpc.CommandExecFailed("%s: bad return code %d" % (self.run_error, res))
  44. if res == 0:
  45. return -1
  46. elif res == 1:
  47. res = self.call([ version1, 'gt', version2 ])
  48. if res not in [ 0, 1 ]:
  49. if self.stderr:
  50. self.run_error += ' (%s)' % self.stderr
  51. raise gbpc.CommandExecFailed("%s: bad return code %d" % (self.run_error, res))
  52. if res == 0:
  53. return 1
  54. return 0
  55. def parse_changelog_repo(repo, branch, filename):
  56. """
  57. Parse the changelog file from given branch in the git
  58. repository.
  59. FIXME: this should use *Vfs methods
  60. """
  61. try:
  62. # Note that we could just pass in the branch:filename notation
  63. # to show as well, but we want to check if the branch / filename
  64. # exists first, so we can give a separate error from other
  65. # repository errors.
  66. sha = repo.rev_parse("%s:%s" % (branch, filename))
  67. except GitRepositoryError:
  68. raise NoChangeLogError("Changelog %s not found in branch %s" % (filename, branch))
  69. return ChangeLog(repo.show(sha))
  70. def orig_file(cp, compression):
  71. """
  72. The name of the orig file belonging to changelog cp
  73. >>> orig_file({'Source': 'foo', 'Upstream-Version': '1.0'}, "bzip2")
  74. 'foo_1.0.orig.tar.bz2'
  75. >>> orig_file({'Source': 'bar', 'Upstream-Version': '0.0~git1234'}, "xz")
  76. 'bar_0.0~git1234.orig.tar.xz'
  77. """
  78. return DebianPkgPolicy.build_tarball_name(cp['Source'],
  79. cp['Upstream-Version'],
  80. compression)
  81. def get_arch():
  82. pipe = subprocess.Popen(["dpkg", "--print-architecture"], shell=False, stdout=subprocess.PIPE)
  83. arch = pipe.stdout.readline().strip()
  84. return arch
  85. def compare_versions(version1, version2):
  86. """compares to Debian versionnumbers suitable for sort()"""
  87. return DpkgCompareVersions()(version1, version2)
  88. def get_vendor():
  89. pipe = subprocess.Popen(["dpkg-vendor", "--query", "Vendor"], shell=False, stdout=subprocess.PIPE)
  90. return pipe.stdout.readline().strip()
  91. # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: