qvm_prefs.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. parser = qubes.tools.QubesArgumentParser(
  34. want_force_root=True,
  35. want_vm=True)
  36. parser.add_argument('--help-properties',
  37. action=qubes.tools.HelpPropertiesAction,
  38. klass=qubes.vm.qubesvm.QubesVM)
  39. parser.add_argument('property', metavar='PROPERTY',
  40. nargs='?',
  41. help='name of the property to show or change')
  42. parser_value = parser.add_mutually_exclusive_group()
  43. parser_value.add_argument('value', metavar='VALUE',
  44. nargs='?',
  45. help='new value of the property')
  46. parser.add_argument('--unset', '--default', '--delete', '-D',
  47. dest='delete',
  48. action='store_true',
  49. help='unset the property; if property has default value, it will be used'
  50. ' instead')
  51. def main(args=None):
  52. args = parser.parse_args(args)
  53. if args.property is None:
  54. properties = args.vm.property_list()
  55. width = max(len(prop.__name__) for prop in properties)
  56. for prop in sorted(properties):
  57. try:
  58. value = getattr(args.vm, prop.__name__)
  59. except AttributeError:
  60. print('{name:{width}s} U'.format(
  61. name=prop.__name__, width=width))
  62. continue
  63. if args.vm.property_is_default(prop):
  64. print('{name:{width}s} D {value!r}'.format(
  65. name=prop.__name__, width=width, value=value))
  66. else:
  67. print('{name:{width}s} - {value!r}'.format(
  68. name=prop.__name__, width=width, value=value))
  69. return 0
  70. if args.value is not None:
  71. setattr(args.vm, args.property, args.value)
  72. args.app.save()
  73. return 0
  74. if args.delete:
  75. delattr(args.vm, args.property)
  76. args.app.save()
  77. return 0
  78. print(str(getattr(args.vm, args.property)))
  79. return 0
  80. if __name__ == '__main__':
  81. sys.exit(main())