srcformats.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. from .dak_exceptions import UnknownFormatError
  29. srcformats = []
  30. def get_format_from_string(txt):
  31. """
  32. Returns the SourceFormat class that corresponds to the specified .changes
  33. Format value. If the string does not match any class, UnknownFormatError
  34. is raised.
  35. """
  36. for format in srcformats:
  37. if format.re_format.match(txt):
  38. return format
  39. raise UnknownFormatError("Unknown format %r" % txt)
  40. class SourceFormat(type):
  41. def __new__(cls, name, bases, attrs):
  42. klass = super(SourceFormat, cls).__new__(cls, name, bases, attrs)
  43. srcformats.append(klass)
  44. assert str(klass.name)
  45. assert iter(klass.requires)
  46. assert iter(klass.disallowed)
  47. klass.re_format = re.compile(klass.format)
  48. return klass
  49. @classmethod
  50. def reject_msgs(cls, has):
  51. if len(cls.requires) != len([x for x in cls.requires if has[x]]):
  52. yield "lack of required files for format %s" % cls.name
  53. for key in cls.disallowed:
  54. if has[key]:
  55. yield "contains source files not allowed in format %s" % cls.name
  56. class FormatOne(SourceFormat):
  57. __metaclass__ = 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(SourceFormat):
  74. __metaclass__ = SourceFormat
  75. name = '3.x (native)'
  76. format = r'3\.\d+ \(native\)'
  77. requires = ('native_tar',)
  78. disallowed = ('orig_tar', 'debian_diff', 'debian_tar', 'more_orig_tar')
  79. class FormatThreeQuilt(SourceFormat):
  80. __metaclass__ = SourceFormat
  81. name = '3.x (quilt)'
  82. format = r'3\.\d+ \(quilt\)'
  83. requires = ('orig_tar', 'debian_tar')
  84. disallowed = ('debian_diff', 'native_tar')