Refactor every call to subprocess to improve compatibility with python 3.6

This commit is contained in:
Lionel Dricot 2022-03-30 19:23:44 +02:00
parent b71dd77f7d
commit 97f31c0762
2 changed files with 25 additions and 22 deletions

View File

@ -14,6 +14,7 @@
- Fixed a crash when the cache is already a dir inside a dir. - Fixed a crash when the cache is already a dir inside a dir.
- Fixed a crash when manually entering an unknown gopher URL while offline - Fixed a crash when manually entering an unknown gopher URL while offline
- Fixed an error with older less version - Fixed an error with older less version
- Call to shell commands has been refactorised to improve compatibility with python 3.6
## 1.2 - March 24th 2022 ## 1.2 - March 24th 2022
Very experimental release: Very experimental release:

View File

@ -37,7 +37,6 @@ import socket
import sqlite3 import sqlite3
import ssl import ssl
from ssl import CertificateError from ssl import CertificateError
import subprocess
import sys import sys
import tempfile import tempfile
import time import time
@ -45,6 +44,13 @@ import urllib.parse
import uuid import uuid
import webbrowser import webbrowser
import html import html
import subprocess
def run(cmd,direct_output=False):
if not direct_output:
result = subprocess.check_output(cmd,shell=True)
return result.decode()
else:
subprocess.run(cmd,shell=True)
try: try:
import setproctitle import setproctitle
@ -78,8 +84,7 @@ _NEW_CHAFA = False
if _HAS_CHAFA: if _HAS_CHAFA:
# starting with 1.10, chafa can return only one frame # starting with 1.10, chafa can return only one frame
# which allows us to drop dependancy for PIL # which allows us to drop dependancy for PIL
return_code = subprocess.run("chafa --version",shell=True, capture_output=True) output = run("chafa --version")
output = return_code.stdout.decode()
# with chafa < 1.10, --version was returned to stderr instead of stdout. # with chafa < 1.10, --version was returned to stderr instead of stdout.
if output != '': if output != '':
_NEW_CHAFA = True _NEW_CHAFA = True
@ -115,8 +120,7 @@ def inline_image(img_file,width):
if inline: if inline:
cmd = inline%width+ " \"%s\""%img_file cmd = inline%width+ " \"%s\""%img_file
try: try:
return_code = subprocess.run(cmd,shell=True, capture_output=True) ansi_img = run(cmd)
ansi_img = return_code.stdout.decode()
except Exception as err: except Exception as err:
ansi_img = "***image failed : %s***\n" %err ansi_img = "***image failed : %s***\n" %err
return ansi_img return ansi_img
@ -131,7 +135,7 @@ def terminal_image(img_file):
cmd = "chafa -d 0 --bg white -w 1" cmd = "chafa -d 0 --bg white -w 1"
if cmd: if cmd:
cmd = cmd + " \"%s\""%img_file cmd = cmd + " \"%s\""%img_file
subprocess.run(cmd,shell=True) run(cmd,direct_output=True)
_HAS_XSEL = shutil.which('xsel') _HAS_XSEL = shutil.which('xsel')
@ -208,8 +212,7 @@ if not shutil.which("less"):
print("If you wish to use another pager, send your request to offpunk@ploum.eu.") print("If you wish to use another pager, send your request to offpunk@ploum.eu.")
print("(Im really curious to hear about people not having \"less\" on their system.)") print("(Im really curious to hear about people not having \"less\" on their system.)")
sys.exit() sys.exit()
return_code = subprocess.run("less --version",shell=True, capture_output=True) output = run("less --version")
output = return_code.stdout.decode()
# We get less Version (which is the only integer on the first line) # We get less Version (which is the only integer on the first line)
words = output.split("\n")[0].split() words = output.split("\n")[0].split()
less_version = 0 less_version = 0
@ -256,7 +259,7 @@ def less_cmd(file, histfile=None,cat=False,grep=None):
cmd_str = prefix + _DEFAULT_CAT % file + "|" + grep_cmd + " %s"%grep cmd_str = prefix + _DEFAULT_CAT % file + "|" + grep_cmd + " %s"%grep
else: else:
cmd_str = prefix + _DEFAULT_LESS % file cmd_str = prefix + _DEFAULT_LESS % file
subprocess.run(cmd_str,shell=True) run(cmd_str,direct_output=True)
# Command abbreviations # Command abbreviations
@ -1644,8 +1647,7 @@ class GeminiItem():
mime = "text/gemini" mime = "text/gemini"
elif shutil.which("file") : elif shutil.which("file") :
#mime = magic.from_file(path,mime=True) #mime = magic.from_file(path,mime=True)
output = subprocess.run("file -b --mime-type %s"%path,shell=True,capture_output=True) mime = run("file -b --mime-type %s"%path).strip()
mime = output.stdout.decode().strip()
mime2,encoding = mimetypes.guess_type(path,strict=False) mime2,encoding = mimetypes.guess_type(path,strict=False)
#If we hesitate between html and xml, takes the xml one #If we hesitate between html and xml, takes the xml one
#because the FeedRendered fallback to HtmlRenderer #because the FeedRendered fallback to HtmlRenderer
@ -1910,7 +1912,7 @@ class GeminiClient(cmd.Cmd):
if resp.strip().lower() in ("y", "yes"): if resp.strip().lower() in ("y", "yes"):
if _HAS_XDGOPEN : if _HAS_XDGOPEN :
cmd = "xdg-open mailto:%s" %gi.path cmd = "xdg-open mailto:%s" %gi.path
subprocess.run(cmd,shell=True) run(cmd,direct_output=True)
else: else:
print("Cannot find a mail client to send mail to %s" %gi.path) print("Cannot find a mail client to send mail to %s" %gi.path)
print("Please install xdg-open (usually from xdg-util package)") print("Please install xdg-open (usually from xdg-util package)")
@ -2030,7 +2032,7 @@ class GeminiClient(cmd.Cmd):
try: try:
# get tmpfile from gi ! # get tmpfile from gi !
tmpfile = gi.get_body(as_file=True) tmpfile = gi.get_body(as_file=True)
subprocess.run(cmd_str % tmpfile,shell=True) run(cmd_str%tmpfile,direct_output=True)
except FileNotFoundError: except FileNotFoundError:
print("Handler program %s not found!" % shlex.split(cmd_str)[0]) print("Handler program %s not found!" % shlex.split(cmd_str)[0])
print("You can use the ! command to specify another handler program or pipeline.") print("You can use the ! command to specify another handler program or pipeline.")
@ -2996,13 +2998,13 @@ Use with "cache" to copy the path of the cached content."""
url = gi.url url = gi.url
else: else:
url = self.gi.url url = self.gi.url
subprocess.run(("echo %s |xsel -b -i" % url), shell=True) run("echo %s |xsel -b -i" % url,direct_output=True)
elif args and args[0] == "raw": elif args and args[0] == "raw":
subprocess.run(("cat %s |xsel -b -i" % self.gi.get_temp_filename()), shell=True) run("cat %s |xsel -b -i" % self.gi.get_temp_filename(),direct_output=True)
elif args and args[0] == "cache": elif args and args[0] == "cache":
subprocess.run(("echo %s |xsel -b -i" % self.gi.get_cache_path()), shell=True) run("echo %s |xsel -b -i" % self.gi.get_cache_path(), direct_output=True)
else: else:
subprocess.run(("cat %s |xsel -b -i" % self.gi.get_body(as_file=True)), shell=True) run("cat %s |xsel -b -i" % self.gi.get_body(as_file=True), direct_output=True)
else: else:
print("Please install xsel to use copy") print("Please install xsel to use copy")
else: else:
@ -3018,7 +3020,7 @@ Use with "cache" to copy the path of the cached content."""
urls = [] urls = []
for selec in ["-p","-s","-b"]: for selec in ["-p","-s","-b"]:
try: try:
clipboards.append(subprocess.check_output(['xsel',selec],text=True)) clipboards.append(run("xsel "+selec))
except Exception as err: except Exception as err:
#print("Skippink clipboard %s because %s"%(selec,err)) #print("Skippink clipboard %s because %s"%(selec,err))
pass pass
@ -3300,7 +3302,7 @@ Use 'ls -l' to see URLs."""
@needs_gi @needs_gi
def do_cat(self, *args): def do_cat(self, *args):
"""Run most recently visited item through "cat" command.""" """Run most recently visited item through "cat" command."""
subprocess.run("cat %s" % self.gi.get_temp_filename(),shell=True) run("cat %s" % self.gi.get_temp_filename(),direct_output=True)
@needs_gi @needs_gi
def do_view(self, *args): def do_view(self, *args):
@ -3347,14 +3349,14 @@ see "handler" command to set your own."""
cmd_str = self._get_handler_cmd(self.gi.get_mime()) cmd_str = self._get_handler_cmd(self.gi.get_mime())
file_path = "\"%s\"" %self.gi.get_body(as_file=True) file_path = "\"%s\"" %self.gi.get_body(as_file=True)
cmd_str = cmd_str % file_path cmd_str = cmd_str % file_path
subprocess.run(cmd_str,shell=True) run(cmd_str,direct_output=True)
@restricted @restricted
@needs_gi @needs_gi
def do_shell(self, line): def do_shell(self, line):
"""'cat' most recently visited item through a shell pipeline. """'cat' most recently visited item through a shell pipeline.
'!' is an useful shortcut.""" '!' is an useful shortcut."""
subprocess.run(("cat %s |" % self.gi.get_temp_filename()) + line, shell=True) run("cat %s |" % self.gi.get_temp_filename() + line,direct_output=True)
@restricted @restricted
@needs_gi @needs_gi
@ -3820,7 +3822,7 @@ Note: Theres no "delete" on purpose. The use of "archive" is recommended."""
if len(args) > 1 and args[1] in self.list_lists(): if len(args) > 1 and args[1] in self.list_lists():
path = os.path.join(listdir,args[1]+".gmi") path = os.path.join(listdir,args[1]+".gmi")
try: try:
subprocess.run("%s %s"%(editor,path),shell=True) run("%s %s"%(editor,path),direct_output=True)
except Exception as err: except Exception as err:
print(err) print(err)
print("Please set a valid editor with \"set editor\"") print("Please set a valid editor with \"set editor\"")