srcformats.py 3.7 KB

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