qvm_prefs.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 os
  28. import subprocess
  29. import sys
  30. import textwrap
  31. import qubes
  32. import qubes.tools
  33. import qubes.utils
  34. import qubes.vm
  35. class _HelpPropertiesAction(argparse.Action):
  36. '''Action for argument parser that displays all properties and exits.'''
  37. # pylint: disable=redefined-builtin
  38. def __init__(self,
  39. option_strings,
  40. dest=argparse.SUPPRESS,
  41. default=argparse.SUPPRESS,
  42. help='list all available properties with short descriptions'
  43. ' and exit'):
  44. super(_HelpPropertiesAction, self).__init__(
  45. option_strings=option_strings,
  46. dest=dest,
  47. default=default,
  48. nargs=0,
  49. help=help)
  50. def __call__(self, parser, namespace, values, option_string=None):
  51. properties = qubes.vm.qubesvm.QubesVM.property_list()
  52. width = max(len(prop.__name__) for prop in properties)
  53. wrapper = textwrap.TextWrapper(width=80,
  54. initial_indent=' ', subsequent_indent=' ' * (width + 6))
  55. text = 'Common properties:\n' + '\n'.join(
  56. wrapper.fill('{name:{width}s} {doc}'.format(
  57. name=prop.__name__,
  58. doc=qubes.utils.format_doc(prop.__doc__) if prop.__doc__ else'',
  59. width=width))
  60. for prop in sorted(properties))
  61. parser.exit(message=text
  62. + '\n\nThere may be more properties in specific domain classes.\n')
  63. parser = qubes.tools.get_parser_base(want_force_root=True)
  64. parser.add_argument('--help-properties', action=_HelpPropertiesAction)
  65. parser.add_argument('name', metavar='VMNAME',
  66. help='name of the domain to change')
  67. parser.add_argument('property', metavar='PROPERTY',
  68. nargs='?',
  69. help='name of the property to show or change')
  70. parser_value = parser.add_mutually_exclusive_group()
  71. parser_value.add_argument('value', metavar='VALUE',
  72. nargs='?',
  73. help='new value of the property')
  74. parser.add_argument('--unset', '--default', '--delete', '-D',
  75. dest='delete',
  76. action='store_true',
  77. help='unset the property; if property has default value, it will be used'
  78. ' instead')
  79. def main():
  80. args = parser.parse_args()
  81. qubes.tools.set_verbosity(parser, args)
  82. qubes.tools.dont_run_as_root(parser, args)
  83. app = qubes.Qubes(args.xml)
  84. try:
  85. domain = app.domains[args.name]
  86. except KeyError:
  87. parser.error('no such domain: {!r}'.format(args.name))
  88. if args.property is None:
  89. properties = domain.property_list()
  90. width = max(len(prop.__name__) for prop in properties)
  91. for prop in sorted(properties):
  92. try:
  93. value = getattr(domain, prop.__name__)
  94. except AttributeError:
  95. print('{name:{width}s} U'.format(
  96. name=prop.__name__, width=width))
  97. continue
  98. if domain.property_is_default(prop):
  99. print('{name:{width}s} D {value!r}'.format(
  100. name=prop.__name__, width=width, value=value))
  101. else:
  102. print('{name:{width}s} - {value!r}'.format(
  103. name=prop.__name__, width=width, value=value))
  104. return True
  105. if args.value is not None:
  106. setattr(domain, args.property, args.value)
  107. app.save()
  108. return True
  109. if args.delete:
  110. delattr(domain, args.property)
  111. app.save()
  112. return True
  113. print(str(getattr(domain, args.property)))
  114. return True
  115. if __name__ == '__main__':
  116. sys.exit(not main())