srcformats.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. from __future__ import absolute_import, print_function
  27. import re
  28. import six
  29. from .dak_exceptions import UnknownFormatError
  30. srcformats = []
  31. def get_format_from_string(txt):
  32. """
  33. Returns the SourceFormat class that corresponds to the specified .changes
  34. Format value. If the string does not match any class, UnknownFormatError
  35. is raised.
  36. """
  37. for format in srcformats:
  38. if format.re_format.match(txt):
  39. return format
  40. raise UnknownFormatError("Unknown format %r" % txt)
  41. class SourceFormat(type):
  42. def __new__(cls, name, bases, attrs):
  43. klass = super(SourceFormat, cls).__new__(cls, name, bases, attrs)
  44. srcformats.append(klass)
  45. assert str(klass.name)
  46. assert iter(klass.requires)
  47. assert iter(klass.disallowed)
  48. klass.re_format = re.compile(klass.format)
  49. return klass
  50. @classmethod
  51. def reject_msgs(cls, has):
  52. if len(cls.requires) != len([x for x in cls.requires if has[x]]):
  53. yield "lack of required files for format %s" % cls.name
  54. for key in cls.disallowed:
  55. if has[key]:
  56. yield "contains source files not allowed in format %s" % cls.name
  57. class FormatOne(six.with_metaclass(SourceFormat, SourceFormat)):
  58. name = '1.0'
  59. format = r'1\.0'
  60. requires = ()
  61. disallowed = ('debian_tar', 'more_orig_tar')
  62. @classmethod
  63. def reject_msgs(cls, has):
  64. if not (has['native_tar_gz'] or (has['orig_tar_gz'] and has['debian_diff'])):
  65. yield "no .tar.gz or .orig.tar.gz+.diff.gz in 'Files' field."
  66. if has['native_tar_gz'] and has['debian_diff']:
  67. yield "native package with diff makes no sense"
  68. if (has['orig_tar_gz'] != has['orig_tar']) or \
  69. (has['native_tar_gz'] != has['native_tar']):
  70. yield "contains source files not allowed in format %s" % cls.name
  71. for msg in super(FormatOne, cls).reject_msgs(has):
  72. yield msg
  73. class FormatThree(six.with_metaclass(SourceFormat, 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(six.with_metaclass(SourceFormat, SourceFormat)):
  79. name = '3.x (quilt)'
  80. format = r'3\.\d+ \(quilt\)'
  81. requires = ('orig_tar', 'debian_tar')
  82. disallowed = ('debian_diff', 'native_tar')