qvm_prefs.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. # TODO list properties for all classes
  22. # TODO list only non-default properties
  23. from __future__ import print_function
  24. import sys
  25. import qubes
  26. import qubes.tools
  27. import qubes.utils
  28. import qubes.vm
  29. parser = qubes.tools.QubesArgumentParser(
  30. want_force_root=True,
  31. vmname_nargs=1)
  32. parser.add_argument('--help-properties',
  33. action=qubes.tools.HelpPropertiesAction,
  34. klass=qubes.vm.qubesvm.QubesVM)
  35. parser.add_argument('--get', '-g',
  36. action='store_true',
  37. help='Ignored; for compatibility with older scripts.')
  38. parser.add_argument('--set', '-s',
  39. action='store_true',
  40. help='Ignored; for compatibility with older scripts.')
  41. parser.add_argument('property', metavar='PROPERTY',
  42. nargs='?',
  43. help='name of the property to show or change')
  44. parser_value = parser.add_mutually_exclusive_group()
  45. parser_value.add_argument('value', metavar='VALUE',
  46. nargs='?',
  47. help='new value of the property')
  48. parser.add_argument('--unset', '--default', '--delete', '-D',
  49. dest='delete',
  50. action='store_true',
  51. help='unset the property; if property has default value, it will be used'
  52. ' instead')
  53. def main(args=None):
  54. args = parser.parse_args(args)
  55. args.domain = args.domains.pop()
  56. if args.property is None:
  57. properties = args.domain.property_list()
  58. width = max(len(prop.__name__) for prop in properties)
  59. for prop in sorted(properties):
  60. try:
  61. value = getattr(args.domain, prop.__name__)
  62. except AttributeError:
  63. print('{name:{width}s} U'.format(
  64. name=prop.__name__, width=width))
  65. continue
  66. if args.domain.property_is_default(prop):
  67. print('{name:{width}s} D {value!s}'.format(
  68. name=prop.__name__, width=width, value=value))
  69. else:
  70. print('{name:{width}s} - {value!s}'.format(
  71. name=prop.__name__, width=width, value=value))
  72. return 0
  73. else:
  74. args.property = args.property.replace('-', '_')
  75. if args.property not in [prop.__name__
  76. for prop in args.domain.property_list()]:
  77. parser.error('no such property: {!r}'.format(args.property))
  78. if args.value is not None:
  79. setattr(args.domain, args.property, args.value)
  80. args.app.save()
  81. return 0
  82. if args.delete:
  83. delattr(args.domain, args.property)
  84. args.app.save()
  85. return 0
  86. print(str(getattr(args.domain, args.property)))
  87. return 0
  88. if __name__ == '__main__':
  89. sys.exit(main())