Skip to content

Commit

Permalink
Use pkexec to open a root editor for root-owned files (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesabre committed Mar 25, 2021
1 parent 7fede20 commit eee6d43
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
55 changes: 53 additions & 2 deletions menulibre/MenulibreLog.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from gi.repository import Gdk, Gio, Gtk

import menulibre_lib
from . util import find_program


class LogDialog:
Expand Down Expand Up @@ -63,6 +64,49 @@ def get_editor_executable(self):
if info is not None:
return info.get_executable()
return None

def get_pkexecs(self):
try:
output = subprocess.check_output(["pkaction"], stderr=subprocess.STDOUT)
except Exception as e:
output = e.output
try:
return output.decode('utf-8').split("\n")
except:
return []

def get_editor_executables(self):
execs = {}
infos = Gio.AppInfo.get_all_for_type("text/plain")
for info in infos:
executable = info.get_executable()
appid = info.get_id()
appid = appid[:-8]
execs[appid] = executable
return execs

def get_root_editor_executable(self):
pkexecs = self.get_pkexecs()
default = self.get_editor_executable()
preferred = None
for appid, executable in self.get_editor_executables().items():
for pkexec in pkexecs:
if pkexec == appid or pkexec == "org.freedesktop.policykit.pkexec.%s" % appid:
if executable == default:
return executable
if preferred is None:
preferred = executable
if preferred is not None:
return preferred
return default

def file_is_writable(self, path):
try:
gfile = Gio.File.new_for_path(path)
info = gfile.query_info(Gio.FILE_ATTRIBUTE_ACCESS_CAN_WRITE, Gio.FileQueryInfoFlags.NONE, None)
return info.get_attribute_boolean(Gio.FILE_ATTRIBUTE_ACCESS_CAN_WRITE)
except:
return False

def view_path(self, path):
if os.path.isdir(path):
Expand All @@ -72,8 +116,15 @@ def view_path(self, path):
else:
Gtk.show_uri(None, uri, 0)
else:
binary = self.get_editor_executable()
subprocess.Popen([binary, path])
if not find_program("pkexec"):
binary = self.get_editor_executable()
subprocess.Popen([binary, path])
elif self.file_is_writable(path):
binary = self.get_editor_executable()
subprocess.Popen([binary, path])
else:
binary = self.get_root_editor_executable()
subprocess.Popen(["pkexec", binary, path])

def get_path_details_at_pos(self, x, y):
pos = self._log_treeview.get_path_at_pos(x, y)
Expand Down
2 changes: 1 addition & 1 deletion po/menulibre.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-03-24 21:24-0400\n"
"POT-Creation-Date: 2021-03-24 23:18-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down

0 comments on commit eee6d43

Please sign in to comment.