qvm_prefs.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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('--default', '-D',
  50. dest='delete',
  51. action='store_true',
  52. help='reset property to its default value')
  53. return parser
  54. def process_actions(parser, args, target):
  55. '''Handle actions for generic property-related tool
  56. :param parser: argument parser used to produce args
  57. :param args: arguments to handle
  58. :param target: object on which actions should be performed
  59. '''
  60. # pylint: disable=no-else-return
  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. if str(args.value).lower() == "none":
  92. if args.property in ["default_dispvm", "netvm", "template"]:
  93. args.value = ''
  94. try:
  95. setattr(target, args.property, args.value)
  96. except AttributeError:
  97. parser.error('no such property: {!r}'.format(args.property))
  98. except qubesadmin.exc.QubesException as e:
  99. parser.error_runtime(e)
  100. return 0
  101. if args.delete:
  102. try:
  103. delattr(target, args.property)
  104. except AttributeError:
  105. parser.error('no such property: {!r}'.format(args.property))
  106. except qubesadmin.exc.QubesException as e:
  107. parser.error_runtime(e)
  108. return 0
  109. try:
  110. value = getattr(target, args.property)
  111. if value is not None:
  112. print(str(value))
  113. except AttributeError:
  114. parser.error('no such property: {!r}'.format(args.property))
  115. except qubesadmin.exc.QubesException as e:
  116. parser.error_runtime(e)
  117. return 0
  118. def main(args=None, app=None): # pylint: disable=missing-docstring
  119. parser = get_parser(1)
  120. args = parser.parse_args(args, app=app)
  121. target = args.domains.pop()
  122. return process_actions(parser, args, target)
  123. if __name__ == '__main__':
  124. sys.exit(main())