qvm_prefs.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. # TODO list properties for all classes
  24. # TODO list only non-default properties
  25. from __future__ import print_function
  26. import argparse
  27. import sys
  28. import textwrap
  29. import qubes
  30. import qubes.tools
  31. import qubes.utils
  32. import qubes.vm
  33. class _HelpPropertiesAction(argparse.Action):
  34. '''Action for argument parser that displays all properties and exits.'''
  35. # pylint: disable=redefined-builtin,too-few-public-methods
  36. def __init__(self,
  37. option_strings,
  38. dest=argparse.SUPPRESS,
  39. default=argparse.SUPPRESS,
  40. help='list all available properties with short descriptions'
  41. ' and exit'):
  42. super(_HelpPropertiesAction, self).__init__(
  43. option_strings=option_strings,
  44. dest=dest,
  45. default=default,
  46. nargs=0,
  47. help=help)
  48. def __call__(self, parser, namespace, values, option_string=None):
  49. # pylint: disable=redefined-outer-name
  50. properties = qubes.vm.qubesvm.QubesVM.property_list()
  51. width = max(len(prop.__name__) for prop in properties)
  52. wrapper = textwrap.TextWrapper(width=80,
  53. initial_indent=' ', subsequent_indent=' ' * (width + 6))
  54. text = 'Common properties:\n' + '\n'.join(
  55. wrapper.fill('{name:{width}s} {doc}'.format(
  56. name=prop.__name__,
  57. doc=qubes.utils.format_doc(prop.__doc__) if prop.__doc__ else'',
  58. width=width))
  59. for prop in sorted(properties))
  60. parser.exit(message=text
  61. + '\n\nThere may be more properties in specific domain classes.\n')
  62. parser = qubes.tools.QubesArgumentParser(
  63. want_force_root=True,
  64. want_vm=True)
  65. parser.add_argument('--help-properties', action=_HelpPropertiesAction)
  66. parser.add_argument('property', metavar='PROPERTY',
  67. nargs='?',
  68. help='name of the property to show or change')
  69. parser_value = parser.add_mutually_exclusive_group()
  70. parser_value.add_argument('value', metavar='VALUE',
  71. nargs='?',
  72. help='new value of the property')
  73. parser.add_argument('--unset', '--default', '--delete', '-D',
  74. dest='delete',
  75. action='store_true',
  76. help='unset the property; if property has default value, it will be used'
  77. ' instead')
  78. def main():
  79. args = parser.parse_args()
  80. if args.property is None:
  81. properties = args.vm.property_list()
  82. width = max(len(prop.__name__) for prop in properties)
  83. for prop in sorted(properties):
  84. try:
  85. value = getattr(args.vm, prop.__name__)
  86. except AttributeError:
  87. print('{name:{width}s} U'.format(
  88. name=prop.__name__, width=width))
  89. continue
  90. if args.vm.property_is_default(prop):
  91. print('{name:{width}s} D {value!r}'.format(
  92. name=prop.__name__, width=width, value=value))
  93. else:
  94. print('{name:{width}s} - {value!r}'.format(
  95. name=prop.__name__, width=width, value=value))
  96. return True
  97. if args.value is not None:
  98. setattr(args.vm, args.property, args.value)
  99. args.app.save()
  100. return True
  101. if args.delete:
  102. delattr(args.vm, args.property)
  103. args.app.save()
  104. return True
  105. print(str(getattr(args.vm, args.property)))
  106. return True
  107. if __name__ == '__main__':
  108. sys.exit(not main())