control.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2012 Daniel Dehennin <daniel.dehennin@baby-gnu.org>
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, please see
  16. # <http://www.gnu.org/licenses/>
  17. """A Debian Control file"""
  18. import email
  19. import os
  20. class NoControlError(Exception):
  21. """No control found"""
  22. pass
  23. class ParseControlError(Exception):
  24. """Problem parsing control"""
  25. pass
  26. class Control(object):
  27. """A Debian control"""
  28. def __init__(self, contents=None, filename="debian/control"):
  29. """
  30. Parse an existing control file.
  31. @param contents: content of a control file
  32. @type contents: C{str}
  33. @param filename: name of the control file
  34. @type filename: C{str}
  35. @return: Control object
  36. @rtype: C{gbp.deb.conrol.Control} object
  37. """
  38. if contents:
  39. control = email.message_from_string(contents)
  40. else:
  41. if not os.access(filename, os.F_OK):
  42. raise NoControlError("Control file %s does not exist" % filename)
  43. with open(filename) as f:
  44. control = email.message_from_file(f)
  45. if not control.items():
  46. raise ParseControlError("Empty or invalid control file or contents")
  47. self._control = control
  48. self.filename = filename
  49. def __getitem__(self, item):
  50. return self._control[item]
  51. def __setitem__(self, item, value):
  52. self._control[item] = value
  53. @property
  54. def name(self):
  55. """The name of the package"""
  56. return self._control['Source']
  57. @property
  58. def section(self):
  59. """The section of the package"""
  60. return self._control['Section']
  61. @property
  62. def priority(self):
  63. """The priority of the package"""
  64. return self._control['Priority']