qvm_features.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # coding=utf-8
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. # Copyright (C) 2017 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU Lesser General Public License as published by
  12. # the Free Software Foundation; either version 2.1 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Lesser General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. '''qvm-features - Manage domain's features'''
  25. from __future__ import print_function
  26. import sys
  27. import qubesadmin
  28. import qubesadmin.tools
  29. parser = qubesadmin.tools.QubesArgumentParser(
  30. vmname_nargs=1,
  31. description='manage domain\'s features')
  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, app=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, app=app)
  48. vm = args.domains[0]
  49. if args.feature is None:
  50. if args.delete:
  51. parser.error('--unset requires a feature')
  52. features = [(feat, vm.features[feat]) for feat in vm.features]
  53. qubesadmin.tools.print_table(features)
  54. elif args.delete:
  55. if args.value is not None:
  56. parser.error('cannot both set and unset a value')
  57. try:
  58. del vm.features[args.feature]
  59. except KeyError:
  60. pass
  61. elif args.value is None:
  62. try:
  63. print(vm.features[args.feature])
  64. return 0
  65. except KeyError:
  66. return 1
  67. else:
  68. vm.features[args.feature] = args.value
  69. return 0
  70. if __name__ == '__main__':
  71. sys.exit(main())