qvm_features.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2016 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. '''qvm-features - Manage domain's features'''
  22. from __future__ import print_function
  23. import argparse
  24. import sys
  25. import qubes
  26. parser = qubes.tools.QubesArgumentParser(
  27. vmname_nargs=1,
  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. vm = args.domains[0]
  49. if args.request:
  50. # Request mode: instead of setting the features directly,
  51. # let the extensions handle them first.
  52. vm.fire_event('feature-request', untrusted_features=args.features)
  53. elif args.feature is None:
  54. if args.delete:
  55. parser.error('--unset requires a feature')
  56. # max doesn't like empty list
  57. if vm.features:
  58. width = max(len(feature) for feature in vm.features)
  59. for feature in sorted(vm.features):
  60. print('{name:{width}s} {value}'.format(
  61. name=feature, value=vm.features[feature], width=width))
  62. elif args.delete:
  63. if args.value is not None:
  64. parser.error('cannot both set and unset a value')
  65. try:
  66. del vm.features[args.feature]
  67. args.app.save()
  68. except KeyError:
  69. pass
  70. elif args.value is None:
  71. try:
  72. print(vm.features[args.feature])
  73. return 0
  74. except KeyError:
  75. return 1
  76. else:
  77. vm.features[args.feature] = args.value
  78. args.app.save()
  79. return 0
  80. if __name__ == '__main__':
  81. sys.exit(main())