core-admin/qubes/tools/qvm_prefs.py

105 lines
2.9 KiB
Python
Raw Normal View History

2015-09-21 23:20:49 +02:00
#!/usr/bin/python2
# -*- encoding: utf8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
# Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# TODO list properties for all classes
2015-09-23 15:31:08 +02:00
# TODO list only non-default properties
2015-09-21 23:20:49 +02:00
from __future__ import print_function
import sys
import qubes
import qubes.tools
import qubes.utils
import qubes.vm
parser = qubes.tools.QubesArgumentParser(
want_force_root=True,
want_vm=True)
2015-09-21 23:20:49 +02:00
parser.add_argument('--help-properties',
action=qubes.tools.HelpPropertiesAction,
klass=qubes.vm.qubesvm.QubesVM)
2015-09-21 23:20:49 +02:00
parser.add_argument('property', metavar='PROPERTY',
nargs='?',
help='name of the property to show or change')
2015-09-23 15:31:08 +02:00
parser_value = parser.add_mutually_exclusive_group()
parser_value.add_argument('value', metavar='VALUE',
2015-09-21 23:20:49 +02:00
nargs='?',
help='new value of the property')
2015-09-23 15:31:08 +02:00
parser.add_argument('--unset', '--default', '--delete', '-D',
dest='delete',
action='store_true',
help='unset the property; if property has default value, it will be used'
' instead')
2015-09-21 23:20:49 +02:00
def main(args=None):
args = parser.parse_args(args)
2015-09-21 23:20:49 +02:00
if args.property is None:
properties = args.vm.property_list()
2015-09-21 23:20:49 +02:00
width = max(len(prop.__name__) for prop in properties)
for prop in sorted(properties):
try:
value = getattr(args.vm, prop.__name__)
2015-09-21 23:20:49 +02:00
except AttributeError:
print('{name:{width}s} U'.format(
name=prop.__name__, width=width))
continue
if args.vm.property_is_default(prop):
print('{name:{width}s} D {value!s}'.format(
2015-09-21 23:20:49 +02:00
name=prop.__name__, width=width, value=value))
else:
print('{name:{width}s} - {value!s}'.format(
2015-09-21 23:20:49 +02:00
name=prop.__name__, width=width, value=value))
return 0
2015-09-21 23:20:49 +02:00
2015-09-23 15:31:08 +02:00
if args.value is not None:
setattr(args.vm, args.property, args.value)
args.app.save()
return 0
2015-09-21 23:20:49 +02:00
2015-09-23 15:31:08 +02:00
if args.delete:
delattr(args.vm, args.property)
args.app.save()
return 0
2015-09-23 15:31:08 +02:00
print(str(getattr(args.vm, args.property)))
2015-09-21 23:20:49 +02:00
return 0
2015-09-21 23:20:49 +02:00
if __name__ == '__main__':
sys.exit(main())