Skip to content

Instantly share code, notes, and snippets.

@rusty-snake
Last active July 20, 2021 08:46
Show Gist options
  • Select an option

  • Save rusty-snake/3e4b8f8555e942d2964a181d4a5f64a0 to your computer and use it in GitHub Desktop.

Select an option

Save rusty-snake/3e4b8f8555e942d2964a181d4a5f64a0 to your computer and use it in GitHub Desktop.
Alternative firecfg .desktop cleaning | Use >> https://github.com/rusty-snake/firecfg.py <<
--- a/src/firecfg/main.c
+++ b/src/firecfg/main.c
@@ -486,8 +486,5 @@ int main(int argc, char **argv) {
if (arg_debug)
printf("%s %d %d %d %d\n", user, getuid(), getgid(), geteuid(), getegid());
- // fix .desktop files in ~/.local/share/applications directory
- fix_desktop_files(home);
-
return 0;
}
#!/usr/bin/python3
# Copyright © 2020 rusty-snake
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from configparser import ConfigParser, DuplicateOptionError
from os import listdir, readlink, SEEK_END
from os.path import basename, exists, expanduser, islink
TRACE = False
def trace(msg):
if TRACE:
print(msg)
DEBUG = False
def debug(msg):
if DEBUG:
print(msg)
INFO = True
def info(msg):
if INFO:
print(msg)
WARN = True
def warn(msg):
if WARN:
print(msg)
KEEP_EXISTING = False
# SOURCE_DIR and TARGET_DIR *must* end with a '/'.
# For SOURCE_DIR you may want to set also other paths
# you found in $XDG_DATA_DIRS like /usr/local/share/applications/.
SOURCE_DIR = "/usr/share/applications/"
TARGET_DIR = expanduser("~/.local/share/applications/")
debug("SOURCE_DIR: " + SOURCE_DIR)
debug("TARGET_DIR: " + TARGET_DIR)
kept_files = []
fixed_files = []
for dot_desktop_file in listdir(SOURCE_DIR):
trace("TRACE: reading: " + dot_desktop_file)
write = False
if not dot_desktop_file[-8:] == ".desktop":
debug("DEBUG: skipping (suffix): " + dot_desktop_file)
continue
try:
c = ConfigParser(interpolation=None)
c.optionxform = str
c.read(SOURCE_DIR + dot_desktop_file)
except DuplicateOptionError:
warn("WARN: DuplicateOptionError occured in " + dot_desktop_file)
c = ConfigParser(interpolation=None, strict=False)
c.optionxform = str
c.read(SOURCE_DIR + dot_desktop_file)
rexeckey = c.get("Desktop Entry", "Exec", fallback=None)
if rexeckey is None:
warn("WARN: no 'Exec' key found in " + dot_desktop_file)
continue
execkey = rexeckey.split()
exec_cmd = execkey[0]
exec_args = " ".join(execkey[1:])
if exec_cmd[0] == "/":
prg = basename(exec_cmd)
trace("TRACE: fixing (Exec): " + dot_desktop_file)
c["Desktop Entry"]["Exec"] = prg + " " + exec_args
write = True
else:
prg = exec_cmd
firelink = "/usr/local/bin/" + prg
if not (islink(firelink) and readlink(firelink) == "/usr/bin/firejail"):
trace("TRACE: skipping (symlink): " + dot_desktop_file)
continue
if c.getboolean("Desktop Entry", "DBusActivatable", fallback=False):
trace("TRACE: fixing (DBusActivatable): " + dot_desktop_file)
c["Desktop Entry"]["DBusActivatable"] = "false"
write = True
if write:
if KEEP_EXISTING and exists(TARGET_DIR + dot_desktop_file):
kept_files.append(dot_desktop_file)
continue
fixed_files.append(dot_desktop_file)
with open(TARGET_DIR + dot_desktop_file, "w") as fd:
trace("TRACE: writing: " + dot_desktop_file)
c.write(fd, space_around_delimiters=False)
fd.seek(0, SEEK_END)
fd.truncate(fd.tell() - 1)
info("INFO: Fixed .desktop files: \n " + "\n ".join(fixed_files))
info("INFO: Kept .desktop files: \n " + "\n ".join(kept_files))
from os import system
system(f"""bash -c '[ -e {SOURCE_DIR}/org.gnome.Maps.desktop ] && sed -e "s/Exec=gapplication launch org.gnome.Maps %U/Exec=gnome-maps %U/" -e "s/DBusActivatable=true/DBusActivatable=false/" "{SOURCE_DIR}/org.gnome.Maps.desktop" > "{TARGET_DIR}/org.gnome.Maps.desktop"'""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment