qvm_prefs.py 3.1 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 sys
  27. import qubes
  28. import qubes.tools
  29. import qubes.utils
  30. import qubes.vm
  31. parser = qubes.tools.QubesArgumentParser(
  32. want_force_root=True,
  33. want_vm=True)
  34. parser.add_argument('--help-properties',
  35. action=qubes.tools.HelpPropertiesAction,
  36. klass=qubes.vm.qubesvm.QubesVM)
  37. parser.add_argument('property', metavar='PROPERTY',
  38. nargs='?',
  39. help='name of the property to show or change')
  40. parser_value = parser.add_mutually_exclusive_group()
  41. parser_value.add_argument('value', metavar='VALUE',
  42. nargs='?',
  43. help='new value of the property')
  44. parser.add_argument('--unset', '--default', '--delete', '-D',
  45. dest='delete',
  46. action='store_true',
  47. help='unset the property; if property has default value, it will be used'
  48. ' instead')
  49. def main(args=None):
  50. args = parser.parse_args(args)
  51. if args.property is None:
  52. properties = args.vm.property_list()
  53. width = max(len(prop.__name__) for prop in properties)
  54. for prop in sorted(properties):
  55. try:
  56. value = getattr(args.vm, prop.__name__)
  57. except AttributeError:
  58. print('{name:{width}s} U'.format(
  59. name=prop.__name__, width=width))
  60. continue
  61. if args.vm.property_is_default(prop):
  62. print('{name:{width}s} D {value!s}'.format(
  63. name=prop.__name__, width=width, value=value))
  64. else:
  65. print('{name:{width}s} - {value!s}'.format(
  66. name=prop.__name__, width=width, value=value))
  67. return 0
  68. if args.property not in [prop.__name__ for prop in args.vm.property_list()]:
  69. parser.error('no such property: {!r}'.format(args.property))
  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())