srcformats.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/python
  2. """ Helper functions for the various source formats
  3. @contact: Debian FTPMaster <ftpmaster@debian.org>
  4. @copyright: 2009, 2010 Joerg Jaspert <joerg@debian.org>
  5. @copyright: 2009 Chris Lamb <lamby@debian.org>
  6. @license: GNU General Public License version 2 or later
  7. """
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. ################################################################################
  20. # <sgran> hey, I think something's wrong with your git repo
  21. # <sgran> when I git pulled this last time, I got something that looked almost
  22. # like python instead of dak
  23. # <mhy> sgran: slander
  24. # <sgran> sorry, I take it back, I've had a better look now
  25. ################################################################################
  26. import re
  27. from dak_exceptions import UnknownFormatError
  28. srcformats = []
  29. def get_format_from_string(txt):
  30. """
  31. Returns the SourceFormat class that corresponds to the specified .changes
  32. Format value. If the string does not match any class, UnknownFormatError
  33. is raised.
  34. """
  35. for format in srcformats:
  36. if format.re_format.match(txt):
  37. return format
  38. raise UnknownFormatError("Unknown format %r" % txt)
  39. class SourceFormat(type):
  40. def __new__(cls, name, bases, attrs):
  41. klass = super(SourceFormat, cls).__new__(cls, name, bases, attrs)
  42. srcformats.append(klass)
  43. assert str(klass.name)
  44. assert iter(klass.requires)
  45. assert iter(klass.disallowed)
  46. klass.re_format = re.compile(klass.format)
  47. return klass
  48. @classmethod
  49. def reject_msgs(cls, has):
  50. if len(cls.requires) != len([x for x in cls.requires if has[x]]):
  51. yield "lack of required files for format %s" % cls.name
  52. for key in cls.disallowed:
  53. if has[key]:
  54. yield "contains source files not allowed in format %s" % cls.name
  55. class FormatOne(SourceFormat):
  56. __metaclass__ = SourceFormat
  57. name = '1.0'
  58. format = r'1\.0'
  59. requires = ()
  60. disallowed = ('debian_tar', 'more_orig_tar')
  61. @classmethod
  62. def reject_msgs(cls, has):
  63. if not (has['native_tar_gz'] or (has['orig_tar_gz'] and has['debian_diff'])):
  64. yield "no .tar.gz or .orig.tar.gz+.diff.gz in 'Files' field."
  65. if has['native_tar_gz'] and has['debian_diff']:
  66. yield "native package with diff makes no sense"
  67. if (has['orig_tar_gz'] != has['orig_tar']) or \
  68. (has['native_tar_gz'] != has['native_tar']):
  69. yield "contains source files not allowed in format %s" % cls.name
  70. for msg in super(FormatOne, cls).reject_msgs(has):
  71. yield msg
  72. class FormatThree(SourceFormat):
  73. __metaclass__ = SourceFormat
  74. name = '3.x (native)'
  75. format = r'3\.\d+ \(native\)'
  76. requires = ('native_tar',)
  77. disallowed = ('orig_tar', 'debian_diff', 'debian_tar', 'more_orig_tar')
  78. class FormatThreeQuilt(SourceFormat):
  79. __metaclass__ = SourceFormat
  80. name = '3.x (quilt)'
  81. format = r'3\.\d+ \(quilt\)'
  82. requires = ('orig_tar', 'debian_tar')
  83. disallowed = ('debian_diff', 'native_tar')