dscfile.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2006,2007,2011,2013 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 re
  20. from gbp.errors import GbpError
  21. from gbp.deb.upstreamsource import DebianUpstreamSource
  22. from gbp.deb.policy import DebianPkgPolicy
  23. class DscFile(object):
  24. """Keeps all needed data read from a dscfile"""
  25. compressions = r"(%s)" % '|'.join(DebianUpstreamSource.known_compressions())
  26. pkg_re = re.compile(r'Source:\s+(?P<pkg>.+)\s*')
  27. version_re = re.compile(r'Version:\s((?P<epoch>\d+)\:)?'
  28. '(?P<version>[%s]+)\s*$'
  29. % DebianPkgPolicy.debianversion_chars)
  30. tar_re = re.compile(r'^\s\w+\s\d+\s+(?P<tar>[^_]+_[^_]+'
  31. '(\.orig)?\.tar\.%s)' % compressions)
  32. add_tar_re = re.compile(r'^\s\w+\s\d+\s+(?P<tar>[^_]+_[^_]+'
  33. '(?P<add_tar>\.orig-[a-z0-9-]+)\.tar\.%s)' % compressions)
  34. diff_re = re.compile(r'^\s\w+\s\d+\s+(?P<diff>[^_]+_[^_]+'
  35. '\.diff.(gz|bz2))')
  36. deb_tgz_re = re.compile(r'^\s\w+\s\d+\s+(?P<deb_tgz>[^_]+_[^_]+'
  37. '\.debian.tar.%s)' % compressions)
  38. format_re = re.compile(r'Format:\s+(?P<format>[0-9.]+)\s*')
  39. def __init__(self, dscfile):
  40. self.pkg = ""
  41. self.tgz = ""
  42. self.diff = ""
  43. self.deb_tgz = ""
  44. self.pkgformat = "1.0"
  45. self.debian_version = ""
  46. self.upstream_version = ""
  47. self.native = False
  48. self.dscfile = os.path.abspath(dscfile)
  49. add_tars = []
  50. f = open(self.dscfile)
  51. fromdir = os.path.dirname(os.path.abspath(dscfile))
  52. for line in f:
  53. m = self.version_re.match(line)
  54. if m and not self.upstream_version:
  55. if '-' in m.group('version'):
  56. self.debian_version = m.group('version').split("-")[-1]
  57. self.upstream_version = "-".join(m.group('version').split("-")[0:-1])
  58. self.native = False
  59. else:
  60. self.native = True # Debian native package
  61. self.upstream_version = m.group('version')
  62. if m.group('epoch'):
  63. self.epoch = m.group('epoch')
  64. else:
  65. self.epoch = ""
  66. continue
  67. m = self.pkg_re.match(line)
  68. if m:
  69. self.pkg = m.group('pkg')
  70. continue
  71. m = self.deb_tgz_re.match(line)
  72. if m:
  73. self.deb_tgz = os.path.join(fromdir, m.group('deb_tgz'))
  74. continue
  75. m = self.add_tar_re.match(line)
  76. if m:
  77. add_tars.append(os.path.join(fromdir, m.group('tar')))
  78. continue
  79. m = self.tar_re.match(line)
  80. if m:
  81. self.tgz = os.path.join(fromdir, m.group('tar'))
  82. continue
  83. m = self.diff_re.match(line)
  84. if m:
  85. self.diff = os.path.join(fromdir, m.group('diff'))
  86. continue
  87. m = self.format_re.match(line)
  88. if m:
  89. self.pkgformat = m.group('format')
  90. continue
  91. f.close()
  92. if not self.pkg:
  93. raise GbpError("Cannot parse package name from '%s'" % self.dscfile)
  94. elif not self.tgz:
  95. raise GbpError("Cannot parse archive name from '%s'" % self.dscfile)
  96. if not self.upstream_version:
  97. raise GbpError("Cannot parse version number from '%s'" % self.dscfile)
  98. if not self.native and not self.debian_version:
  99. raise GbpError("Cannot parse Debian version number from '%s'" % self.dscfile)
  100. self.additional_tarballs = list(set(add_tars))
  101. def _get_version(self):
  102. version = [ "", self.epoch + ":" ][len(self.epoch) > 0]
  103. if self.native:
  104. version += self.upstream_version
  105. else:
  106. version += "%s-%s" % (self.upstream_version, self.debian_version)
  107. return version
  108. version = property(_get_version)
  109. def __str__(self):
  110. return "<%s object %s>" % (self.__class__.__name__, self.dscfile)
  111. @classmethod
  112. def parse(cls, filename):
  113. try:
  114. dsc = cls(filename)
  115. except IOError as err:
  116. raise GbpError("Error reading dsc file: %s" % err)
  117. return dsc
  118. # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: