qvm_prefs.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. if args.help_properties:
  61. properties = target.property_list()
  62. width = max(len(prop) for prop in properties)
  63. wrapper = textwrap.TextWrapper(width=80,
  64. initial_indent=' ', subsequent_indent=' ' * (width + 6))
  65. for prop in sorted(properties):
  66. help_text = target.property_help(prop)
  67. print(wrapper.fill('{name:{width}s} {help_text!s}'.format(
  68. name=prop, width=width, help_text=help_text)))
  69. return 0
  70. if args.property is None:
  71. properties = target.property_list()
  72. width = max(len(prop) for prop in properties)
  73. for prop in sorted(properties):
  74. try:
  75. value = getattr(target, prop)
  76. except AttributeError:
  77. print('{name:{width}s} U'.format(
  78. name=prop, width=width))
  79. continue
  80. if target.property_is_default(prop):
  81. print('{name:{width}s} D {value!s}'.format(
  82. name=prop, width=width, value=value))
  83. else:
  84. print('{name:{width}s} - {value!s}'.format(
  85. name=prop, width=width, value=value))
  86. return 0
  87. else:
  88. args.property = args.property.replace('-', '_')
  89. if args.value is not None:
  90. try:
  91. setattr(target, args.property, args.value)
  92. except AttributeError:
  93. parser.error('no such property: {!r}'.format(args.property))
  94. return 0
  95. if args.delete:
  96. try:
  97. delattr(target, args.property)
  98. except AttributeError:
  99. parser.error('no such property: {!r}'.format(args.property))
  100. return 0
  101. try:
  102. print(str(getattr(target, args.property)))
  103. except AttributeError:
  104. parser.error('no such property: {!r}'.format(args.property))
  105. return 0
  106. def main(args=None, app=None): # pylint: disable=missing-docstring
  107. parser = get_parser(1)
  108. args = parser.parse_args(args, app=app)
  109. target = args.domains.pop()
  110. return process_actions(parser, args, target)
  111. if __name__ == '__main__':
  112. sys.exit(main())