qvm_prefs.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # encoding=utf-8
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. ''' Manipulate VM properties.'''
  21. # TODO list only non-default properties
  22. from __future__ import print_function
  23. import sys
  24. import textwrap
  25. import qubesadmin
  26. import qubesadmin.tools
  27. import qubesadmin.utils
  28. import qubesadmin.vm
  29. def get_parser(vmname_nargs=1):
  30. '''Return argument parser for generic property-related tool'''
  31. parser = qubesadmin.tools.QubesArgumentParser(
  32. vmname_nargs=vmname_nargs)
  33. parser.add_argument('--help-properties',
  34. action='store_true',
  35. help='list all available properties with short descriptions and exit')
  36. parser.add_argument('--get', '-g',
  37. action='store_true',
  38. help='Ignored; for compatibility with older scripts.')
  39. parser.add_argument('--set', '-s',
  40. action='store_true',
  41. help='Ignored; for compatibility with older scripts.')
  42. parser.add_argument('property', metavar='PROPERTY',
  43. nargs='?',
  44. help='name of the property to show or change')
  45. parser_value = parser.add_mutually_exclusive_group()
  46. parser_value.add_argument('value', metavar='VALUE',
  47. nargs='?',
  48. help='new value of the property')
  49. parser.add_argument('--unset', '--default', '--delete', '-D',
  50. dest='delete',
  51. action='store_true',
  52. help='unset the property; '
  53. 'if property has default value, it will be used instead')
  54. return parser
  55. def process_actions(parser, args, target):
  56. '''Handle actions for generic property-related tool
  57. :param parser: argument parser used to produce args
  58. :param args: arguments to handle
  59. :param target: object on which actions should be performed
  60. '''
  61. if args.help_properties:
  62. properties = target.property_list()
  63. width = max(len(prop) for prop in properties)
  64. wrapper = textwrap.TextWrapper(width=80,
  65. initial_indent=' ', subsequent_indent=' ' * (width + 6))
  66. for prop in sorted(properties):
  67. help_text = target.property_help(prop)
  68. print(wrapper.fill('{name:{width}s} {help_text!s}'.format(
  69. name=prop, width=width, help_text=help_text)))
  70. return 0
  71. if args.property is None:
  72. properties = target.property_list()
  73. width = max(len(prop) for prop in properties)
  74. for prop in sorted(properties):
  75. try:
  76. value = getattr(target, prop)
  77. except AttributeError:
  78. print('{name:{width}s} U'.format(
  79. name=prop, width=width))
  80. continue
  81. if target.property_is_default(prop):
  82. print('{name:{width}s} D {value!s}'.format(
  83. name=prop, width=width, value=value))
  84. else:
  85. print('{name:{width}s} - {value!s}'.format(
  86. name=prop, width=width, value=value))
  87. return 0
  88. else:
  89. args.property = args.property.replace('-', '_')
  90. if args.value is not None:
  91. try:
  92. setattr(target, args.property, args.value)
  93. except AttributeError:
  94. parser.error('no such property: {!r}'.format(args.property))
  95. return 0
  96. if args.delete:
  97. try:
  98. delattr(target, args.property)
  99. except AttributeError:
  100. parser.error('no such property: {!r}'.format(args.property))
  101. return 0
  102. try:
  103. print(str(getattr(target, args.property)))
  104. except AttributeError:
  105. parser.error('no such property: {!r}'.format(args.property))
  106. return 0
  107. def main(args=None, app=None): # pylint: disable=missing-docstring
  108. parser = get_parser(1)
  109. args = parser.parse_args(args, app=app)
  110. target = args.domains.pop()
  111. return process_actions(parser, args, target)
  112. if __name__ == '__main__':
  113. sys.exit(main())