appmenus: convert shell scripts to python

Fixes QubesOS/qubes-issues#1897
This commit is contained in:
Marek Marczykowski-Górecki 2016-04-19 23:08:57 +02:00
parent 9690f52dc5
commit 7dccbd1ead
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
12 changed files with 181 additions and 228 deletions

View File

@ -26,6 +26,7 @@ import os
import os.path
import shutil
import dbus
import pkg_resources
import qubes.ext
@ -44,16 +45,13 @@ class AppmenusSubdirs:
class AppmenusPaths:
appmenu_start_hvm_template = \
'/usr/share/qubes-appmenus/qubes-start.desktop'
appmenu_create_cmd = \
'/usr/libexec/qubes-appmenus/create-apps-for-appvm.sh'
appmenu_remove_cmd = \
'/usr/libexec/qubes-appmenus/remove-appvm-appmenus.sh'
class AppmenusExtension(qubes.ext.Extension):
def __init__(self, *args):
super(AppmenusExtension, self).__init__(*args)
import qubes.vm.qubesvm
import qubes.vm.templatevm
def templates_dir(self, vm):
"""
@ -83,60 +81,171 @@ class AppmenusExtension(qubes.ext.Extension):
def icons_dir(self, vm):
return os.path.join(vm.dir_path, AppmenusSubdirs.icons_subdir)
def appmenus_create(self, vm, source_template=None):
if source_template is None and hasattr(vm, 'template'):
source_template = vm.template
def whitelist_path(self, vm):
return os.path.join(vm.dir_path, AppmenusSubdirs.whitelist)
def directory_template_name(self, vm):
if isinstance(vm, qubes.vm.templatevm.TemplateVM):
return 'qubes-templatevm.directory.template'
elif vm.provides_network:
return 'qubes-servicevm.directory.template'
else:
return 'qubes-vm.directory.template'
def write_desktop_file(self, vm, source, destination_path):
"""Format .desktop/.directory file
:param vm: QubesVM object for which write desktop file
:param source: desktop file template (path or template itself)
:param destination_path: where to write the desktop file
:return: True if target file was changed, otherwise False
"""
if source.startswith('/'):
source = open(source).read()
data = source.\
replace("%VMNAME%", vm.name).\
replace("%VMDIR%", vm.dir_path).\
replace("%XDGICON%", vm.label.icon)
if os.path.exists(destination_path):
current_dest = open(destination_path).read()
if current_dest == data:
return False
with open(destination_path, "w") as f:
f.write(data)
return True
def appmenus_create(self, vm, refresh_cache=True):
"""Create/update .desktop files
:param vm: QubesVM object for which create entries
:param refresh_cache: refresh desktop environment cache; if false,
must be refreshed manually later
:return: None
"""
if vm.internal:
return
if vm.is_disposablevm():
return
vmsubdir = vm.dir_path.split(os.path.sep)[-2]
vm.log.info("Creating appmenus")
appmenus_dir = self.appmenus_dir(vm)
if not os.path.exists(appmenus_dir):
os.makedirs(appmenus_dir)
try:
#TODO msgoutput = None if verbose else open(os.devnull, 'w')
msgoutput = None
if source_template is not None:
subprocess.check_call([AppmenusPaths.appmenu_create_cmd,
self.templates_dir(source_template),
vm.name, vmsubdir, vm.label.icon],
stdout=msgoutput, stderr=msgoutput)
elif self.templates_dir(vm) is not None:
subprocess.check_call([AppmenusPaths.appmenu_create_cmd,
self.templates_dir(vm), vm.name,
vmsubdir, vm.label.icon],
stdout=msgoutput, stderr=msgoutput)
else:
# Only add apps to menu
subprocess.check_call([AppmenusPaths.appmenu_create_cmd,
"none", vm.name, vmsubdir,
vm.label.icon],
stdout=msgoutput, stderr=msgoutput)
except subprocess.CalledProcessError:
vm.log.warning("Ooops, there was a problem creating appmenus "
"for {0} VM!")
anything_changed = False
directory_file = os.path.join(appmenus_dir, vm.name + '-vm.directory')
if self.write_desktop_file(vm,
pkg_resources.resource_string(__name__,
self.directory_template_name(vm)), directory_file):
anything_changed = True
templates_dir = self.templates_dir(vm)
appmenus = os.listdir(templates_dir)
changed_appmenus = []
if os.path.exists(self.whitelist_path(vm)):
whitelist = [x.rstrip() for x in open(self.whitelist_path(vm))]
appmenus = [x for x in appmenus if x in whitelist]
def appmenus_remove(self, vm):
vmsubdir = vm.dir_path.split(os.path.sep)[-2]
subprocess.check_call([AppmenusPaths.appmenu_remove_cmd, vm.name,
vmsubdir], stderr=open(os.devnull, 'w'))
for appmenu in appmenus:
if self.write_desktop_file(vm,
os.path.join(templates_dir, appmenu),
os.path.join(appmenus_dir,
'-'.join((vm.name, appmenu)))):
changed_appmenus.append(appmenu)
if self.write_desktop_file(vm,
pkg_resources.resource_string(
__name__, 'qubes-appmenu-select.desktop.template'
),
os.path.join(appmenus_dir,
'-'.join((vm.name, 'qubes-appmenu-select.desktop')))):
changed_appmenus.append('qubes-appmenu-select.desktop')
def appmenus_cleanup(self, vm):
srcdir = self.templates_dir(vm)
if srcdir is None:
return
if not os.path.exists(srcdir):
return
if not os.path.exists(self.appmenus_dir(vm)):
return
if changed_appmenus:
anything_changed = True
for appmenu in os.listdir(self.appmenus_dir(vm)):
if not os.path.exists(os.path.join(srcdir, appmenu)):
os.unlink(os.path.join(self.appmenus_dir(vm), appmenu))
target_appmenus = map(
lambda x: '-'.join((vm.name, x)),
appmenus + ['qubes-appmenu-select.desktop']
)
# remove old entries
installed_appmenus = os.listdir(appmenus_dir)
installed_appmenus.remove(os.path.basename(directory_file))
appmenus_to_remove = set(installed_appmenus).difference(set(
target_appmenus))
if len(appmenus_to_remove):
appmenus_to_remove_fnames = map(
lambda x: os.path.join(appmenus_dir, x), appmenus_to_remove)
try:
desktop_menu_cmd = ['xdg-desktop-menu', 'uninstall']
if not refresh_cache:
desktop_menu_cmd.append('--noupdate')
desktop_menu_cmd.append(directory_file)
desktop_menu_cmd.extend(appmenus_to_remove_fnames)
desktop_menu_env = os.environ.copy()
desktop_menu_env['LC_COLLATE'] = 'C'
subprocess.check_call(desktop_menu_cmd, env=desktop_menu_env)
except subprocess.CalledProcessError:
vm.log.warning("Problem removing old appmenus")
for appmenu in appmenus_to_remove_fnames:
os.unlink(appmenu)
# add new entries
if anything_changed:
try:
desktop_menu_cmd = ['xdg-desktop-menu', 'install']
if not refresh_cache:
desktop_menu_cmd.append('--noupdate')
desktop_menu_cmd.append(directory_file)
desktop_menu_cmd.extend(map(
lambda x: os.path.join(
appmenus_dir, '-'.join((vm.name, x))),
changed_appmenus))
desktop_menu_env = os.environ.copy()
desktop_menu_env['LC_COLLATE'] = 'C'
subprocess.check_call(desktop_menu_cmd, env=desktop_menu_env)
except subprocess.CalledProcessError:
vm.log.warning("Problem creating appmenus")
if refresh_cache:
if 'KDE_SESSION_UID' in os.environ:
subprocess.call(['kbuildsycoca' +
os.environ.get('KDE_SESSION_VERSION', '4')])
def appmenus_remove(self, vm, refresh_cache=True):
appmenus_dir = self.appmenus_dir(vm)
if os.path.exists(appmenus_dir):
vm.log.info("Removing appmenus")
installed_appmenus = os.listdir(appmenus_dir)
directory_file = os.path.join(self.appmenus_dir(vm),
vm.name + '-vm.directory')
installed_appmenus.remove(os.path.basename(directory_file))
if installed_appmenus:
appmenus_to_remove_fnames = map(
lambda x: os.path.join(appmenus_dir, x), installed_appmenus)
try:
desktop_menu_cmd = ['xdg-desktop-menu', 'uninstall']
if not refresh_cache:
desktop_menu_cmd.append('--noupdate')
desktop_menu_cmd.append(directory_file)
desktop_menu_cmd.extend(appmenus_to_remove_fnames)
desktop_menu_env = os.environ.copy()
desktop_menu_env['LC_COLLATE'] = 'C'
subprocess.check_call(desktop_menu_cmd,
env=desktop_menu_env)
except subprocess.CalledProcessError:
vm.log.warning("Problem removing appmenus")
shutil.rmtree(appmenus_dir)
if refresh_cache:
if 'KDE_SESSION_UID' in os.environ:
subprocess.call(['kbuildsycoca' +
os.environ.get('KDE_SESSION_VERSION', '4')])
def appicons_create(self, vm, srcdir=None, force=False):
"""Create/update applications icons"""
if srcdir is None:
srcdir = self.template_icons_dir(vm)
if srcdir is None:
@ -149,48 +258,43 @@ class AppmenusExtension(qubes.ext.Extension):
if vm.is_disposablevm():
return
whitelist = os.path.join(vm.dir_path, AppmenusSubdirs.whitelist)
whitelist = self.whitelist_path(vm)
if os.path.exists(whitelist):
whitelist = [line.strip() for line in open(whitelist)]
else:
whitelist = None
if not os.path.exists(self.icons_dir(vm)):
os.mkdir(self.icons_dir(vm))
elif not os.path.isdir(self.icons_dir(vm)):
os.unlink(self.icons_dir(vm))
os.mkdir(self.icons_dir(vm))
dstdir = self.icons_dir(vm)
if not os.path.exists(dstdir):
os.mkdir(dstdir)
elif not os.path.isdir(dstdir):
os.unlink(dstdir)
os.mkdir(dstdir)
if whitelist:
expected_icons = \
map(lambda x: os.path.splitext(x)[0] + '.png', whitelist)
else:
expected_icons = os.listdir(srcdir)
for icon in os.listdir(srcdir):
desktop = os.path.splitext(icon)[0] + '.desktop'
if whitelist and desktop not in whitelist:
if icon not in expected_icons:
continue
src_icon = os.path.join(srcdir, icon)
dst_icon = os.path.join(self.icons_dir(vm), icon)
dst_icon = os.path.join(dstdir, icon)
if not os.path.exists(dst_icon) or force or \
os.path.getmtime(src_icon) > os.path.getmtime(dst_icon):
qubesimgconverter.tint(src_icon, dst_icon, vm.label.color)
for icon in os.listdir(dstdir):
if icon not in expected_icons:
os.unlink(os.path.join(dstdir, icon))
def appicons_remove(self, vm):
if not os.path.exists(self.icons_dir(vm)):
return
for icon in os.listdir(self.icons_dir(vm)):
os.unlink(os.path.join(self.icons_dir(vm), icon))
def appicons_cleanup(self, vm):
srcdir = self.template_icons_dir(vm)
if srcdir is None:
return
if not os.path.exists(srcdir):
return
if not os.path.exists(self.icons_dir(vm)):
return
for icon in os.listdir(self.icons_dir(vm)):
if not os.path.exists(os.path.join(srcdir, icon)):
os.unlink(os.path.join(self.icons_dir(vm), icon))
shutil.rmtree(self.icons_dir(vm))
@qubes.ext.handler('property-pre-set:name')
def pre_rename(self, vm, event, prop, *args):
@ -309,29 +413,8 @@ class AppmenusExtension(qubes.ext.Extension):
self.appmenus_remove(vm)
self.appmenus_create(vm)
def appmenus_recreate(self, vm):
"""
Force recreation of all appmenus and icons. For example when VM label
color was changed
"""
self.appmenus_remove(vm)
self.appmenus_cleanup(vm)
self.appicons_remove(vm)
self.appicons_create(vm)
self.appmenus_create(vm)
def appmenus_update(self, vm):
"""
Similar to appmenus_recreate, but do not touch unchanged files
"""
self.appmenus_remove(vm)
self.appmenus_cleanup(vm)
self.appicons_create(vm)
self.appicons_cleanup(vm)
self.appmenus_create(vm)
@qubes.ext.handler('property-set:internal')
def set_attr(self, vm, event, prop, newvalue, *args):
def on_property_set_internal(self, vm, event, prop, newvalue, *args):
if len(args):
oldvalue = args[0]
else:

View File

@ -1,20 +0,0 @@
#!/bin/sh
SRC=$1
DSTDIR=$2
VMNAME=$3
VMDIR=$4
XDGICON=$5
DST=$DSTDIR/$VMNAME-$(basename $SRC)
if ! [ -r "$SRC" ]; then
exit 0
fi
sed \
-e "s/%VMNAME%/$VMNAME/" \
-e "s %VMDIR% $VMDIR " \
-e "s/%XDGICON%/$XDGICON/" \
<$SRC >$DST

View File

@ -1,14 +0,0 @@
#!/bin/sh
SRC=$1
DST=$2
VMNAME=$3
VMDIR=$4
XDGICON=$5
sed \
-e "s/%VMNAME%/$VMNAME/" \
-e "s %VMDIR% $VMDIR " \
-e "s/%XDGICON%/$XDGICON/" \
<$SRC >$DST

View File

@ -1,64 +0,0 @@
#!/bin/sh
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2010 Joanna Rutkowska <joanna@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.
#
#
SRCDIR=$1
VMNAME=$2
VMTYPE=$3
if [ -z "$VMTYPE" ]; then
VMTYPE=appvms
fi
XDGICON=$4
VMDIR=/var/lib/qubes/$VMTYPE/$VMNAME
APPSDIR=$VMDIR/apps
if [ $# -lt 2 ]; then
echo "usage: $0 <apps_templates_dir> <vmname> [appvms|vm-templates|servicevms]"
exit
fi
mkdir -p $APPSDIR
if [ "$SRCDIR" != "none" ]; then
echo "--> Converting Appmenu Templates..."
if [ -r "$VMDIR/whitelisted-appmenus.list" ]; then
cat $VMDIR/whitelisted-appmenus.list | xargs -I{} /usr/libexec/qubes-appmenus/convert-apptemplate2vm.sh $SRCDIR/{} $APPSDIR $VMNAME $VMDIR $XDGICON
else
find $SRCDIR -name "*.desktop" $CHECK_WHITELISTED -exec /usr/libexec/qubes-appmenus/convert-apptemplate2vm.sh {} $APPSDIR $VMNAME $VMDIR $XDGICON \;
fi
/usr/libexec/qubes-appmenus/convert-apptemplate2vm.sh /usr/share/qubes-appmenus/qubes-appmenu-select.desktop $APPSDIR $VMNAME $VMDIR $XDGICON
if [ "$VMTYPE" = "vm-templates" ]; then
DIR_TEMPLATE=/usr/share/qubes-appmenus/qubes-templatevm.directory.template
elif [ "$VMTYPE" = "servicevms" ]; then
DIR_TEMPLATE=/usr/share/qubes-appmenus/qubes-servicevm.directory.template
else
DIR_TEMPLATE=/usr/share/qubes-appmenus/qubes-vm.directory.template
fi
/usr/libexec/qubes-appmenus/convert-dirtemplate2vm.sh $DIR_TEMPLATE $APPSDIR/$VMNAME-vm.directory $VMNAME $VMDIR $XDGICON
fi
echo "--> Adding Apps to the Menu..."
LC_COLLATE=C xdg-desktop-menu install --noupdate $APPSDIR/*.directory $APPSDIR/*.desktop
if [ -n "$KDE_SESSION_UID" -a -z "$SKIP_CACHE_REBUILD" ]; then
xdg-desktop-menu forceupdate
kbuildsycoca$KDE_SESSION_VERSION
fi

View File

@ -344,14 +344,12 @@ def main(args=None):
appmenusext.appmenus_update(vm)
if hasattr(vm, 'appvms'):
os.putenv('SKIP_CACHE_REBUILD', '1')
for child_vm in vm.appvms:
try:
appmenusext.appmenus_update(child_vm)
appmenusext.appmenus_create(child_vm, refresh_cache=False)
except Exception, e:
child_vm.log.error("Failed to recreate appmenus for "
"'{0}': {1}".format(child_vm.name, str(e)))
os.unsetenv('SKIP_CACHE_REBUILD')
subprocess.call(['xdg-desktop-menu', 'forceupdate'])
if 'KDE_SESSION_UID' in os.environ:
subprocess.call(['kbuildsycoca' + os.environ.get('KDE_SESSION_VERSION', '4')])

View File

@ -1,23 +0,0 @@
#!/bin/sh
VMNAME=$1
VMTYPE=$2
if [ -z "$VMTYPE" ]; then
VMTYPE=appvms
fi
VMDIR=/var/lib/qubes/$VMTYPE/$VMNAME
APPSDIR=$VMDIR/apps
if [ $# -lt 1 ]; then
echo "usage: $0 <vmname> [appvms|vm-templates|servicevms]"
exit
fi
if ls $APPSDIR/*.directory $APPSDIR/*.desktop > /dev/null 2>&1; then
LC_COLLATE=C xdg-desktop-menu uninstall $APPSDIR/*.directory $APPSDIR/*.desktop
rm -f $APPSDIR/*.desktop $APPSDIR/*.directory
rm -f $HOME/.config/menus/applications-merged/user-$VMNAME-vm.menu
fi
if [ -n "$KDE_SESSION_UID" -a -z "$SKIP_CACHE_REBUILD" ]; then
kbuildsycoca$KDE_SESSION_VERSION
fi

View File

@ -86,9 +86,6 @@ python setup.py build
### Appmenus
python setup.py install -O1 --skip-build --root $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/usr/libexec/qubes-appmenus
cp qubesappmenus/*.sh $RPM_BUILD_ROOT/usr/libexec/qubes-appmenus/
mkdir -p $RPM_BUILD_ROOT/etc/qubes-rpc/policy
cp qubesappmenus/qubes.SyncAppMenus $RPM_BUILD_ROOT/etc/qubes-rpc/
cp qubesappmenus/qubes.SyncAppMenus.policy $RPM_BUILD_ROOT/etc/qubes-rpc/policy/qubes.SyncAppMenus
@ -215,22 +212,18 @@ chmod -x /etc/grub.d/10_linux
%{python_sitelib}/qubeslinux-*.egg-info/*
/usr/lib/python2.7/site-packages/qubesappmenus/__init__.py*
/usr/lib/python2.7/site-packages/qubesappmenus/receive.py*
/usr/lib/python2.7/site-packages/qubesappmenus/qubes-appmenu-select.desktop.template
/usr/lib/python2.7/site-packages/qubesappmenus/qubes-servicevm.directory.template
/usr/lib/python2.7/site-packages/qubesappmenus/qubes-templatevm.directory.template
/usr/lib/python2.7/site-packages/qubesappmenus/qubes-vm.directory.template
/usr/lib/python2.7/site-packages/qubesappmenus/tests.py*
/usr/lib/python2.7/site-packages/qubesappmenus/test-data
/etc/qubes-rpc/policy/qubes.SyncAppMenus
/etc/qubes-rpc/qubes.SyncAppMenus
/usr/libexec/qubes-appmenus/convert-apptemplate2vm.sh
/usr/libexec/qubes-appmenus/convert-dirtemplate2vm.sh
/usr/libexec/qubes-appmenus/create-apps-for-appvm.sh
/usr/libexec/qubes-appmenus/remove-appvm-appmenus.sh
/usr/share/qubes-appmenus/qubes-appmenu-select.desktop
/usr/share/qubes-appmenus/qubes-dispvm-firefox.desktop
/usr/share/qubes-appmenus/qubes-dispvm-xterm.desktop
/usr/share/qubes-appmenus/qubes-dispvm.directory
/usr/share/qubes-appmenus/qubes-servicevm.directory.template
/usr/share/qubes-appmenus/qubes-start.desktop
/usr/share/qubes-appmenus/qubes-templatevm.directory.template
/usr/share/qubes-appmenus/qubes-vm.directory.template
/usr/share/qubes-appmenus/hvm
/usr/share/qubes/icons/*.png
/usr/bin/qvm-sync-appmenus

View File

@ -15,7 +15,7 @@ if __name__ == '__main__':
packages=('qubesappmenus',),
package_data = {
'qubesappmenus': ['test-data/*'],
'qubesappmenus': ['test-data/*', '*.template'],
},
entry_points={