Skip to content

Instantly share code, notes, and snippets.

@A6GibKm
Created July 13, 2020 04:20
Show Gist options
  • Save A6GibKm/6c1aa09fc7aeabda927aa60ac7f13327 to your computer and use it in GitHub Desktop.
Save A6GibKm/6c1aa09fc7aeabda927aa60ac7f13327 to your computer and use it in GitHub Desktop.
Export applications out of toolbox
#!/usr/bin/env python3
import os
import argparse
import shutil
from xdg.DesktopEntry import DesktopEntry
from xdg.BaseDirectory import xdg_data_home
import gi
gi.require_version('Gtk', '3.0')
__license__ = 'MIT'
def copy_icons(app):
from gi.repository import Gtk
from gi.repository import Gdk
icon_theme = Gtk.IconTheme.get_default()
if not icon_theme.has_icon(app):
return
# Does not return all sizes
# sizes = icontheme.get_icon_sizes(app)
sizes = [16, 24, 32, 48, 64, 96, 128, 256, 512]
for size in sizes:
pixbuf = icon_theme.load_icon(app, size, 0)
icon_file = icon_theme.lookup_icon(app, size, Gtk.IconLookupFlags.FORCE_SIZE)
if icon_file != None:
icon_path = icon_file.get_filename()
else:
continue
new_icon_path = os.path.join(xdg_data_home, icon_path.replace('/usr/share/', ''))
if os.path.exists(new_icon_path):
print('Icon', new_icon_path, 'already installed')
continue
extension = os.path.splitext(icon_path)[1].replace('.','')
os.makedirs(os.path.dirname(new_icon_path), exist_ok=True)
shutil.copy(icon_path, new_icon_path)
print('Copied: {} to {}'.format(icon_path, new_icon_path))
def main():
# if not os.path.exists('/README.md'):
if os.environ.get('TOOLBOX_PATH') is None:
exit('Not inside a toolbox container')
parser = argparse.ArgumentParser()
parser.add_argument('application',
help='application to export', nargs='?')
parser.add_argument('--list', action='store_true',
help='list desktop files inside the toolbox')
opts = parser.parse_args()
if opts.list:
for f in os.listdir('/usr/share/applications'):
print(f.replace('.desktop', ''))
exit()
if not opts.application:
exit('Specify an application. See --help for additional information')
app = opts.application
app_path = os.path.join('/usr/share/applications', app + '.desktop')
desktop_file = DesktopEntry(app_path)
exec_field = desktop_file.content['Desktop Entry']['Exec']
desktop_file.content['Desktop Entry']['Exec'] = 'toolbox run ' + exec_field
if desktop_file.content['Desktop Entry']['Icon']:
icon_name = desktop_file.content['Desktop Entry']['Icon']
else:
icon_name = app
new_path = os.path.join(xdg_data_home, app_path.replace('/usr/share/', ''))
os.makedirs(os.path.dirname(new_path), exist_ok=True)
desktop_file.write(new_path)
print('Copied: {} to {}'.format(app_path, new_path))
copy_icons(app)
for data_dir in ['appdata', 'metainfo']:
xml_dir = os.path.join('/usr/share', data_dir)
if not os.path.exists(xml_dir):
continue
for appdata in os.listdir(xml_dir):
appdata_name = appdata.replace('.appdata.xml', '').replace('.metainfo.xml', '')
if appdata_name == app:
xml_path = os.path.join(xml_dir, appdata)
new_path = os.path.join(xdg_data_home, xml_path.replace('/usr/share/', ''))
os.makedirs(os.path.dirname(new_path), exist_ok=True)
shutil.copy(xml_path, new_path)
print('Copied: {} to {}'.format(xml_path, new_path))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment