qvm_features.py 3.0 KB

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