formats.py 2.6 KB

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