formats.py 2.6 KB

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