qvm-features-request 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python2
  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. import argparse
  24. import os
  25. import subprocess
  26. import sys
  27. import qubesdb
  28. class FeatureRequestAction(argparse.Action):
  29. '''Action for argument parser that stores a property.'''
  30. # pylint: disable=redefined-builtin,too-few-public-methods
  31. def __init__(self,
  32. option_strings,
  33. dest='features',
  34. metavar='NAME=VALUE',
  35. required=False,
  36. help='request a feature with the value'):
  37. super(FeatureRequestAction, self).__init__(option_strings, dest=dest,
  38. metavar=metavar, nargs='*', required=required, default={},
  39. help=help)
  40. def __call__(self, parser, namespace, values, option_string=None):
  41. for request in values:
  42. try:
  43. feature, value = request.split('=', 1)
  44. except ValueError:
  45. parser.error(
  46. 'invalid feature request token: {!r}'.format(request))
  47. getattr(namespace, self.dest)[feature] = value
  48. parser = argparse.ArgumentParser(
  49. description='submit a feature request to the dom0')
  50. parser.add_argument('--commit',
  51. action='store_true', default=False,
  52. help='actually send the request (without it, only make entries in qubesdb)')
  53. parser.add_argument('features',
  54. action=FeatureRequestAction)
  55. def main(args=None):
  56. args = parser.parse_args(args)
  57. qdb = qubesdb.QubesDB()
  58. for feature, value in args.features.items():
  59. qdb.write('/features-request/' + feature, value)
  60. if args.commit:
  61. devnull = os.open(os.devnull, os.O_RDWR)
  62. subprocess.check_call(
  63. ['qrexec-client-vm', 'dom0', 'qubes.FeaturesRequest'],
  64. stdin=devnull, stdout=devnull)
  65. if __name__ == '__main__':
  66. sys.exit(main())