qvm_features.py 2.9 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. 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())