qvm_features.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2016 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. '''qvm-features - Manage domain's features'''
  24. import argparse
  25. import qubes
  26. parser = qubes.tools.QubesArgumentParser(
  27. want_vm=True,
  28. description='manage domain\'s features')
  29. parser.add_argument('--request',
  30. action='store_true', default=False,
  31. help=argparse.SUPPRESS)
  32. parser.add_argument('feature', metavar='FEATURE',
  33. action='store', nargs='?',
  34. help='name of the feature')
  35. parser.add_argument('value', metavar='VALUE',
  36. action='store', nargs='?',
  37. help='new value of the feature')
  38. parser.add_argument('--unset', '--default', '--delete', '-D',
  39. dest='delete',
  40. action='store_true',
  41. help='unset the feature')
  42. def main(args=None):
  43. '''Main routine of :program:`qvm-features`.
  44. :param list args: Optional arguments to override those delivered from \
  45. command line.
  46. '''
  47. args = parser.parse_args(args)
  48. if args.request:
  49. # Request mode: instead of setting the features directly,
  50. # let the extensions handle them first.
  51. args.vm.fire_event('feature-request', untrusted_features=args.features)
  52. return 0
  53. if args.feature is None:
  54. if args.delete:
  55. parser.error('--unset requires a feature')
  56. width = max(len(feature) for feature in args.vm.features)
  57. for feature in sorted(args.vm.features):
  58. print('{name:{width}s} {value}'.format(
  59. name=feature, value=args.vm.features[feature], width=width))
  60. return 0
  61. if args.delete:
  62. if args.value is not None:
  63. parser.error('cannot both set and unset a value')
  64. try:
  65. del args.vm.features[args.feature]
  66. args.app.save()
  67. except KeyError:
  68. pass
  69. return 0
  70. if args.value is None:
  71. try:
  72. print(args.vm.features[args.feature])
  73. return 0
  74. except KeyError:
  75. return 1
  76. args.vm.features[args.feature] = args.value
  77. args.app.save()
  78. return 0
  79. if __name__ == '__main__':
  80. sys.exit(main())