From ff02e0da909cca914e301b8f0c62285bdf9def4c Mon Sep 17 00:00:00 2001 From: Matt McCutchen Date: Sat, 5 Dec 2015 12:25:29 -0500 Subject: [PATCH] Add qvm-appmenu-replace tool. This can be used to replace the Terminal shortcut (QubesOS/qubes-issues#1428). --- appmenus-scripts/qubes-core-appmenus.py | 38 ++++++++++++----- appmenus-scripts/qvm-appmenu-replace | 56 +++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 appmenus-scripts/qvm-appmenu-replace diff --git a/appmenus-scripts/qubes-core-appmenus.py b/appmenus-scripts/qubes-core-appmenus.py index 0ce58e5..0f75de9 100644 --- a/appmenus-scripts/qubes-core-appmenus.py +++ b/appmenus-scripts/qubes-core-appmenus.py @@ -84,6 +84,14 @@ def QubesVm_get_appmenus_icons_dir(self): QubesVm.appmenus_icons_dir = property(QubesVm_get_appmenus_icons_dir) +def get_whitelist_names(vm): + return (whitelist for whitelist in ( + vm_files["appmenus_whitelist"], + 'vm-' + vm_files["appmenus_whitelist"], + 'netvm-' + vm_files["appmenus_whitelist"]) + if os.path.exists(os.path.join(vm.dir_path, whitelist))) + + def QubesVm_appmenus_create(self, verbose=False, source_template=None): if source_template is None: source_template = self.template @@ -148,6 +156,19 @@ def QubesVm_appmenus_cleanup(self): os.unlink(os.path.join(self.appmenus_dir, appmenu)) +def QubesVm_appmenus_replace_entry(self, old_name, new_name): + for whitelist in get_whitelist_names(self): + whitelist_path = os.path.join(self.dir_path, whitelist) + with open(whitelist_path) as f: + old_lines = f.readlines() + new_lines = [ + (new_name + '\n' if l == old_name + '\n' else l) + for l in old_lines] + if new_lines != old_lines: + with open(whitelist_path, 'w') as f: + f.write(''.join(new_lines)) + + def QubesVm_appicons_create(self, srcdir=None): if srcdir is None: srcdir = self.appmenus_template_icons_dir @@ -277,16 +298,12 @@ def QubesVm_clone_disk_files(self, src_vm, verbose): shutil.copytree(src_vm.appmenus_template_icons_dir, self.appmenus_template_icons_dir) - for whitelist in ( - vm_files["appmenus_whitelist"], - 'vm-' + vm_files["appmenus_whitelist"], - 'netvm-' + vm_files["appmenus_whitelist"]): - if os.path.exists(os.path.join(src_vm.dir_path, whitelist)): - if verbose: - print >> sys.stderr, "--> Copying whitelisted apps list: {0}". \ - format(whitelist) - shutil.copy(os.path.join(src_vm.dir_path, whitelist), - os.path.join(self.dir_path, whitelist)) + for whitelist in get_whitelist_names(src_vm): + if verbose: + print >> sys.stderr, "--> Copying whitelisted apps list: {0}". \ + format(whitelist) + shutil.copy(os.path.join(src_vm.dir_path, whitelist), + os.path.join(self.dir_path, whitelist)) # Create appmenus self.appicons_create() @@ -362,6 +379,7 @@ QubesVm.appmenus_remove = QubesVm_appmenus_remove QubesVm.appmenus_cleanup = QubesVm_appmenus_cleanup QubesVm.appmenus_recreate = QubesVm_appmenus_recreate QubesVm.appmenus_update = QubesVm_appmenus_update +QubesVm.appmenus_replace_entry = QubesVm_appmenus_replace_entry QubesVm.appicons_create = QubesVm_appicons_create QubesVm.appicons_cleanup = QubesVm_appicons_cleanup QubesVm.appicons_remove = QubesVm_appicons_remove diff --git a/appmenus-scripts/qvm-appmenu-replace b/appmenus-scripts/qvm-appmenu-replace new file mode 100644 index 0000000..c5cf8dd --- /dev/null +++ b/appmenus-scripts/qvm-appmenu-replace @@ -0,0 +1,56 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# The Qubes OS Project, http://www.qubes-os.org +# +# 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. +# +# +import sys +from qubes.qubes import QubesVmCollection + +def main(): + if len(sys.argv) != 4: + print >> sys.stderr, \ + 'Usage: qvm-appmenu-replace VM_NAME OLD_NAME.desktop NEW_NAME.desktop' + sys.exit(1) + vm_name = sys.argv[1] + old_name = sys.argv[2] + new_name = sys.argv[3] + + qvm_collection = QubesVmCollection() + qvm_collection.lock_db_for_reading() + qvm_collection.load() + qvm_collection.unlock_db() + + vm = qvm_collection.get_vm_by_name(vm_name) + + if vm is None: + print >> sys.stderr, "ERROR: A VM with the name '{0}' " \ + "does not exist in the system.".format( + vm_name) + exit(1) + + if vm.template is not None: + print >> sys.stderr, "ERROR: To replace appmenu for template based VM, " \ + "do it on template instead" + exit(1) + + vm.appmenus_replace_entry(old_name, new_name) + if hasattr(vm, 'appvms'): + for child_vm in vm.appvms.values(): + child_vm.appmenus_replace_entry(old_name, new_name) + +main()