format.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2012 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. """Parse debian/source/format"""
  18. class DebianSourceFormatError(Exception):
  19. pass
  20. class DebianSourceFormat(object):
  21. """
  22. Contents of debian/source/format
  23. >>> d = DebianSourceFormat("3.0 (quilt)")
  24. >>> d.type
  25. 'quilt'
  26. >>> d.version
  27. '3.0'
  28. >>> d = DebianSourceFormat("3.0 (native)")
  29. >>> d.type
  30. 'native'
  31. >>> d = DebianSourceFormat("1.0")
  32. >>> d.type
  33. >>> d.version
  34. '1.0'
  35. >>> d = DebianSourceFormat("1.0 broken")
  36. Traceback (most recent call last):
  37. ...
  38. DebianSourceFormatError: Cannot get source format from '1.0 broken'
  39. """
  40. format_file = 'debian/source/format'
  41. def _parse(self, content):
  42. parts = content.split()
  43. self._version = parts[0]
  44. if len(parts) == 2:
  45. if (parts[1][0] == '(' and
  46. parts[1][-1] == ')'):
  47. self._type = parts[1][1:-1]
  48. else:
  49. raise DebianSourceFormatError("Cannot get source format from "
  50. "'%s'" % content)
  51. def __init__(self, content):
  52. self._version = None
  53. self._type = None
  54. self._parse(content)
  55. @property
  56. def version(self):
  57. """The source format version number"""
  58. return self._version
  59. @property
  60. def type(self):
  61. """The 'type' (e.g. git, native)"""
  62. return self._type
  63. def __str__(self):
  64. return "%s (%s)" % (self._version, self._type)
  65. @classmethod
  66. def parse_file(klass, filename):
  67. """
  68. Parse debian/source/format file
  69. @param filename: the file to parse
  70. @type filename: C{str}
  71. @returns: a debisn/source/format object
  72. @rtype: L{DebianSourceFormat}
  73. >>> from six import b
  74. >>> import tempfile, os
  75. >>> with tempfile.NamedTemporaryFile(delete=False) as t:
  76. ... ret = t.write(b("3.0 (quilt)"))
  77. >>> d = DebianSourceFormat.parse_file(t.name)
  78. >>> d.version
  79. '3.0'
  80. >>> d.type
  81. 'quilt'
  82. >>> os.unlink(t.name)
  83. """
  84. with open(filename) as f:
  85. return klass(f.read())
  86. @classmethod
  87. def from_content(klass, version, type, format_file=None):
  88. """
  89. Write a format file from I{type} and I{format} at
  90. I{format_file}
  91. @param version: the source package format version
  92. @param type: the format type
  93. @param format_file: the format file to create with
  94. the above parameters
  95. """
  96. format_file = format_file or klass.format_file
  97. with open(klass.format_file, 'w') as f:
  98. f.write("%s (%s)" % (version, type))
  99. return klass.parse_file(klass.format_file)
  100. if __name__ == "__main__":
  101. import doctest
  102. doctest.testmod()