qvm_tags.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 tags'''
  22. from __future__ import print_function
  23. import sys
  24. import qubes
  25. parser = qubes.tools.QubesArgumentParser(
  26. vmname_nargs=1,
  27. description='manage domain\'s tags')
  28. mode = parser.add_mutually_exclusive_group()
  29. def mode_query(args):
  30. if args.tag is None:
  31. # list
  32. print('\n'.join(sorted(args.vm.tags)))
  33. else:
  34. # real query; logic is inverted, because this is exit code
  35. return int(args.tag not in args.vm.tags)
  36. mode.add_argument('--query',
  37. dest='mode',
  38. action='store_const',
  39. const=mode_query,
  40. help='query for the tag; if no tag specified, list all tags;'
  41. ' this is the default')
  42. def mode_set(args):
  43. if args.tag is None:
  44. parser.error('tag is mandatory for --set')
  45. args.vm.tags.add(args.tag)
  46. args.app.save()
  47. mode.add_argument('--set', '-s',
  48. dest='mode',
  49. action='store_const',
  50. const=mode_set,
  51. help='set the tag; if tag is already set, do nothing')
  52. def mode_unset(args):
  53. if args.tag is None:
  54. parser.error('tag is mandatory for --unset')
  55. args.vm.tags.discard(args.tag)
  56. args.app.save()
  57. mode.add_argument('--unset', '--delete', '-D',
  58. dest='mode',
  59. action='store_const',
  60. const=mode_unset,
  61. help='unset the tag; if tag is not set, do nothing')
  62. parser.add_argument('tag', metavar='TAG',
  63. action='store', nargs='?',
  64. help='name of the tag')
  65. parser.set_defaults(mode=mode_query)
  66. def main(args=None):
  67. '''Main routine of :program:`qvm-tags`.
  68. :param list args: Optional arguments to override those delivered from \
  69. command line.
  70. '''
  71. args = parser.parse_args(args)
  72. return args.mode(args)
  73. if __name__ == '__main__':
  74. sys.exit(main())