qvm_tags.py 3.1 KB

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