qvm_tags.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 qubesadmin
  25. import qubesadmin.exc
  26. import qubesadmin.tools
  27. def mode_query(args):
  28. '''Query/list tags'''
  29. if not hasattr(args, 'tag') or args.tag is None:
  30. # list
  31. tags = list(sorted(args.domains[0].tags))
  32. if tags:
  33. print('\n'.join(tags))
  34. else:
  35. # real query
  36. if args.tag not in args.domains[0].tags:
  37. return 1
  38. print(args.tag)
  39. return 0
  40. def mode_add(args):
  41. '''Add tag'''
  42. for tag in args.tag:
  43. args.domains[0].tags.add(tag)
  44. return 0
  45. def mode_del(args):
  46. '''Delete tag'''
  47. for tag in args.tag:
  48. args.domains[0].tags.discard(tag)
  49. return 0
  50. def get_parser():
  51. ''' Return qvm-tags tool command line parser '''
  52. parser = qubesadmin.tools.QubesArgumentParser(
  53. vmname_nargs=1,
  54. description='manage domain\'s tags')
  55. parser.register('action', 'parsers',
  56. qubesadmin.tools.AliasedSubParsersAction)
  57. sub_parsers = parser.add_subparsers(
  58. title='commands',
  59. description="For more information see qvm-tags command -h",
  60. dest='command')
  61. list_parser = sub_parsers.add_parser('list', aliases=('ls', 'l'),
  62. help='list tags')
  63. list_parser.add_argument('tag', nargs='?',
  64. action='store', default=None)
  65. list_parser.set_defaults(func=mode_query)
  66. add_parser = sub_parsers.add_parser('add', aliases=('a', 'set'),
  67. help='add tag')
  68. add_parser.add_argument('tag', nargs='+',
  69. action='store')
  70. add_parser.set_defaults(func=mode_add)
  71. del_parser = sub_parsers.add_parser('del', aliases=('d', 'unset', 'u'),
  72. help='add tag')
  73. del_parser.add_argument('tag', nargs=1,
  74. action='store')
  75. del_parser.set_defaults(func=mode_del)
  76. parser.set_defaults(func=mode_query)
  77. return parser
  78. def main(args=None, app=None):
  79. '''Main routine of :program:`qvm-tags`.
  80. :param list args: Optional arguments to override those delivered from \
  81. command line.
  82. '''
  83. parser = get_parser()
  84. args = parser.parse_args(args, app=app)
  85. try:
  86. return args.func(args)
  87. except qubesadmin.exc.QubesException as e:
  88. parser.error_runtime(e)
  89. if __name__ == '__main__':
  90. sys.exit(main())