source.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 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. from gbp.deb.format import DebianSourceFormat
  20. from gbp.deb.changelog import ChangeLog
  21. import six
  22. class FileVfs(object):
  23. def __init__(self, dir):
  24. """
  25. Access files in a unpaced Debian source package.
  26. @param dir: the toplevel of the source tree
  27. @type dir: C{str}
  28. """
  29. self._dir = dir
  30. def open(self, path, flags=None):
  31. flags = flags or 'r'
  32. return open(os.path.join(self._dir, path), flags)
  33. class DebianSourceError(Exception):
  34. pass
  35. class DebianSource(object):
  36. """
  37. A debianized source tree
  38. Querying/setting information in a debianized source tree
  39. involves several files. This class provides a common interface.
  40. """
  41. def __init__(self, vfs):
  42. """
  43. @param vfs: a class that implemented GbpVFS interfacce or
  44. a directory (which will used the DirGbpVFS class.
  45. """
  46. self._changelog = None
  47. if isinstance(vfs, six.string_types):
  48. self._vfs = FileVfs(vfs)
  49. else:
  50. self._vfs = vfs
  51. def is_native(self):
  52. """
  53. Whether this is a native Debian package
  54. """
  55. try:
  56. ff = self._vfs.open('debian/source/format')
  57. f = DebianSourceFormat(ff.read())
  58. if f.type:
  59. return f.type == 'native'
  60. except IOError as e:
  61. pass # Fall back to changelog parsing
  62. try:
  63. return not '-' in self.changelog.version
  64. except IOError as e:
  65. raise DebianSourceError("Failed to determine source format: %s" % e)
  66. @property
  67. def changelog(self):
  68. """
  69. Return the L{gbp.deb.ChangeLog}
  70. """
  71. if not self._changelog:
  72. try:
  73. clf = self._vfs.open('debian/changelog')
  74. self._changelog = ChangeLog(clf.read())
  75. except IOError as err:
  76. raise DebianSourceError('Failed to read changelog: %s' % err)
  77. return self._changelog
  78. @property
  79. def sourcepkg(self):
  80. """
  81. The source package's name
  82. """
  83. return self.changelog['Source']