formats.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """ Helper functions for the various changes 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. # <mhy> !!!!11111iiiiiioneoneoneone
  20. # <dak> mhy: Error: "!!!11111iiiiiioneoneoneone" is not a valid command.
  21. # <mhy> dak: oh shut up
  22. # <dak> mhy: Error: "oh" is not a valid command.
  23. ################################################################################
  24. from .regexes import re_verwithext
  25. from .dak_exceptions import UnknownFormatError
  26. def parse_format(txt):
  27. """
  28. Parse a .changes Format string into a tuple representation for easy
  29. comparison.
  30. >>> parse_format('1.0')
  31. (1, 0)
  32. >>> parse_format('8.4 (hardy)')
  33. (8, 4, 'hardy')
  34. If the format doesn't match these forms, raises UnknownFormatError.
  35. @type txt: string
  36. @param txt: Format string to parse
  37. @rtype: tuple
  38. @return: Parsed format
  39. @raise UnknownFormatError: Unknown Format: line
  40. """
  41. format = re_verwithext.search(txt)
  42. if format is None:
  43. raise UnknownFormatError(txt)
  44. format = format.groups()
  45. if format[1] is None:
  46. format = int(float(format[0])), 0, format[2]
  47. else:
  48. format = int(format[0]), int(format[1]), format[2]
  49. if format[2] is None:
  50. format = format[:2]
  51. return format
  52. def validate_changes_format(format, field):
  53. """
  54. Validate a tuple-representation of a .changes Format: field. Raises
  55. UnknownFormatError if the field is invalid, otherwise return type is
  56. undefined.
  57. """
  58. if (format < (1, 5) or format > (1, 8)):
  59. raise UnknownFormatError(repr(format))
  60. if field != 'files' and format < (1, 8):
  61. raise UnknownFormatError(repr(format))