Skip to content

Commit

Permalink
Remove portage._shell_quote function
Browse files Browse the repository at this point in the history
Replaced by shlex.quote, shlex.join, or subprocess.run as appropriate.

Signed-off-by: Mike Gilbert <[email protected]>
  • Loading branch information
floppym committed Jan 11, 2025
1 parent af80dd3 commit a4f8802
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 218 deletions.
15 changes: 7 additions & 8 deletions bin/binhost-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import argparse
import os
import subprocess
import sys
import textwrap

Expand Down Expand Up @@ -107,20 +108,18 @@ def main(argv):
if not os.path.isdir(binhost_dir):
parser.error(f"binhost_dir could not be created: '{binhost_dir}'")

cp_opts = "RP"
cp_opts = "-RP"
if options.hardlinks == "n":
cp_opts += "p"
else:
cp_opts += "l"

cp_cmd = "cp -{} {} {}".format(
cp_opts,
portage._shell_quote(src_pkg_dir),
portage._shell_quote(snapshot_dir),
)
try:
result = subprocess.run(["cp", cp_opts, src_pkg_dir, snapshot_dir])
except OSError:
result = None

ret = os.system(cp_cmd)
if not (os.WIFEXITED(ret) and os.WEXITSTATUS(ret) == os.EX_OK):
if result is None or result.returncode != 0:
return 1

infile = open(
Expand Down
3 changes: 1 addition & 2 deletions bin/ebuild
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import portage
portage._internal_caller = True
from portage import os
from portage import _encodings
from portage import _shell_quote
from portage import _unicode_encode
from portage.const import VDB_PATH
from portage.exception import (
Expand Down Expand Up @@ -165,7 +164,7 @@ def main():
if ebuild_portdir != vdb_path and ebuild_portdir not in portage.portdb.porttrees:
portdir_overlay = portage.settings.get("PORTDIR_OVERLAY", "")
os.environ["PORTDIR_OVERLAY"] = (
portdir_overlay + " " + _shell_quote(ebuild_portdir)
portdir_overlay + " " + shlex.quote(ebuild_portdir)
)

print(f"Appending {ebuild_portdir} to PORTDIR_OVERLAY...")
Expand Down
2 changes: 1 addition & 1 deletion bin/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def main(args):
if returncode != os.EX_OK:
portage.util.writemsg(
"!!! install: copy_xattrs failed with the "
f"following arguments: {' '.join(portage._shell_quote(x) for x in args)}\n",
f"following arguments: {shlex.join(args)}\n",
noiselevel=-1,
)
return returncode
Expand Down
5 changes: 3 additions & 2 deletions bin/portageq
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ try:
signal.signal(signal.SIGUSR1, debug_signal)

import argparse
import shlex
import sys
import types

Expand Down Expand Up @@ -882,7 +883,7 @@ try:
exit_status = 1

if verbose:
print(arg + "=" + portage._shell_quote(value))
print(arg + "=" + shlex.quote(value))
else:
print(value)

Expand Down Expand Up @@ -1441,7 +1442,7 @@ try:
def elog(elog_funcname, lines):
cmd = f"source '{os.environ['PORTAGE_BIN_PATH']}/isolated-functions.sh' ; "
for line in lines:
cmd += f"{elog_funcname} {portage._shell_quote(line)} ; "
cmd += f"{elog_funcname} {shlex.quote(line)} ; "
subprocess.call([portage.const.BASH_BINARY, "-c", cmd])

else:
Expand Down
5 changes: 3 additions & 2 deletions lib/_emerge/BinpkgEnvExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# Distributed under the terms of the GNU General Public License v2

import errno
import shlex

from _emerge.CompositeTask import CompositeTask
from _emerge.SpawnProcess import SpawnProcess
from portage import os, _shell_quote, _unicode_encode
from portage import os, _unicode_encode
from portage.const import BASH_BINARY


Expand All @@ -31,7 +32,7 @@ def _get_dest_env_path(self):
def _start(self):
saved_env_path = self._get_saved_env_path()
dest_env_path = self._get_dest_env_path()
shell_cmd = f"${{PORTAGE_BUNZIP2_COMMAND:-${{PORTAGE_BZIP2_COMMAND}} -d}} -c -- {_shell_quote(saved_env_path)} > {_shell_quote(dest_env_path)}"
shell_cmd = f"${{PORTAGE_BUNZIP2_COMMAND:-${{PORTAGE_BZIP2_COMMAND}} -d}} -c -- {shlex.quote(saved_env_path)} > {shlex.quote(dest_env_path)}"

logfile = None
if self.settings.get("PORTAGE_BACKGROUND") != "subprocess":
Expand Down
6 changes: 3 additions & 3 deletions lib/_emerge/BinpkgExtractorAsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _xpak_start(self):
if b"--xattrs" in output:
tar_options = ["--xattrs", "--xattrs-include='*'"]
for x in shlex.split(self.env.get("PORTAGE_XATTR_EXCLUDE", "")):
tar_options.append(portage._shell_quote(f"--xattrs-exclude={x}"))
tar_options.append(shlex.quote(f"--xattrs-exclude={x}"))
tar_options = " ".join(tar_options)

decomp = _compressors.get(compression_probe(self.pkg_path))
Expand Down Expand Up @@ -119,9 +119,9 @@ def _xpak_start(self):
"-c",
textwrap.dedent(
f"""
cmd0=(head -c {pkg_xpak.filestat.st_size - pkg_xpak.xpaksize} -- {portage._shell_quote(self.pkg_path)})
cmd0=(head -c {pkg_xpak.filestat.st_size - pkg_xpak.xpaksize} -- {shlex.quote(self.pkg_path)})
cmd1=({decomp_cmd})
cmd2=(tar -xp {tar_options} -C {portage._shell_quote(self.image_dir)} -f -);
cmd2=(tar -xp {tar_options} -C {shlex.quote(self.image_dir)} -f -);
"""
"""
"${cmd0[@]}" | "${cmd1[@]}" | "${cmd2[@]}";
Expand Down
4 changes: 3 additions & 1 deletion lib/_emerge/MiscFunctionsProcess.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import shlex

from _emerge.AbstractEbuildProcess import AbstractEbuildProcess
import portage

Expand All @@ -22,7 +24,7 @@ def _start(self):
portage_bin_path, os.path.basename(portage.const.MISC_SH_BINARY)
)

self.args = [portage._shell_quote(misc_sh_binary)] + self.commands
self.args = [shlex.quote(misc_sh_binary)] + self.commands
if (
self.logfile is None
and self.settings.get("PORTAGE_BACKGROUND") != "subprocess"
Expand Down
3 changes: 2 additions & 1 deletion lib/_emerge/PackagePhase.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2018 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import shlex

from _emerge.CompositeTask import CompositeTask
from _emerge.EbuildProcess import EbuildProcess
Expand Down Expand Up @@ -60,7 +61,7 @@ def _start(self):
"rm -rf {PROOT}; "
'cp -pPR $(cp --help | grep -q -- "^[[:space:]]*-l," && echo -l)'
' "${{D}}" {PROOT}'
).format(PROOT=portage._shell_quote(self._proot)),
).format(PROOT=shlex.quote(self._proot)),
],
background=self.background,
env=self.settings.environ(),
Expand Down
17 changes: 0 additions & 17 deletions lib/portage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,23 +445,6 @@ def _get_stdin():
return sys.stdin


_shell_quote_re = re.compile(r"[\s><=*\\\"'$`;&|(){}\[\]#!~?]")


def _shell_quote(s):
"""
Quote a string in double-quotes and use backslashes to
escape any backslashes, double-quotes, dollar signs, or
backquotes in the string.
"""
if _shell_quote_re.search(s) is None:
return s
for letter in r"\"$`":
if letter in s:
s = s.replace(letter, rf"\{letter}")
return f'"{s}"'


bsd_chflags = None

if platform.system() in ("FreeBSD",):
Expand Down
6 changes: 2 additions & 4 deletions lib/portage/eclass_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

__all__ = ["cache"]

import shlex
import stat
import operator
import warnings
Expand All @@ -12,7 +13,6 @@
from portage.exception import FileNotFound, PermissionDenied
from portage import os
from portage import checksum
from portage import _shell_quote


class hashed_path:
Expand Down Expand Up @@ -175,7 +175,5 @@ def get_eclass_data(self, inherits):
@property
def eclass_locations_string(self):
if self._eclass_locations_str is None:
self._eclass_locations_str = " ".join(
_shell_quote(x) for x in reversed(self.porttrees)
)
self._eclass_locations_str = shlex.join(reversed(self.porttrees))
return self._eclass_locations_str
2 changes: 1 addition & 1 deletion lib/portage/package/ebuild/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ def __init__(
for ov in portdir_overlay:
ov = normalize_path(ov)
if isdir_raise_eaccess(ov) or portage._sync_mode:
new_ov.append(portage._shell_quote(ov))
new_ov.append(shlex.quote(ov))
else:
writemsg(
_("!!! Invalid PORTDIR_OVERLAY" " (not a dir): '%s'\n")
Expand Down
63 changes: 33 additions & 30 deletions lib/portage/package/ebuild/doebuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import shlex
import signal
import stat
import subprocess
import sys
import tempfile
from textwrap import wrap
Expand Down Expand Up @@ -58,7 +59,6 @@
unmerge,
_encodings,
_os_merge,
_shell_quote,
_unicode_decode,
_unicode_encode,
)
Expand Down Expand Up @@ -233,7 +233,7 @@ def _doebuild_spawn(phase, settings, actionmap=None, **kwargs):
ebuild_sh_arg = phase

cmd = "{} {}".format(
_shell_quote(
shlex.quote(
os.path.join(
settings["PORTAGE_BIN_PATH"], os.path.basename(EBUILD_SH_BINARY)
)
Expand Down Expand Up @@ -1722,8 +1722,8 @@ def _spawn_actionmap(settings):
portage_bin_path, os.path.basename(EBUILD_SH_BINARY)
)
misc_sh_binary = os.path.join(portage_bin_path, os.path.basename(MISC_SH_BINARY))
ebuild_sh = _shell_quote(ebuild_sh_binary) + " %s"
misc_sh = _shell_quote(misc_sh_binary) + " __dyn_%s"
ebuild_sh = shlex.quote(ebuild_sh_binary) + " %s"
misc_sh = shlex.quote(misc_sh_binary) + " __dyn_%s"

# args are for the to spawn function
actionmap = {
Expand Down Expand Up @@ -2613,35 +2613,38 @@ def _post_src_install_write_metadata(settings):

def _preinst_bsdflags(mysettings):
if bsd_chflags:
# Save all the file flags for restoration later.
os.system(
"mtree -c -p %s -k flags > %s"
% (
_shell_quote(mysettings["D"]),
_shell_quote(os.path.join(mysettings["T"], "bsdflags.mtree")),
)
)
try:
# Save all the file flags for restoration later.
with open(os.path.join(mysettings["T"], "bsdflags.mtree"), "wb") as outfile:
subprocess.run(
["mtree", "-c", "-p", mysettings["D"], "-k", "flags"],
stdout=outfile,
)

# Remove all the file flags to avoid EPERM errors.
os.system(
"chflags -R noschg,nouchg,nosappnd,nouappnd %s"
% (_shell_quote(mysettings["D"]),)
)
os.system(
f"chflags -R nosunlnk,nouunlnk {_shell_quote(mysettings['D'])} 2>/dev/null"
)
# Remove all the file flags to avoid EPERM errors.
subprocess.run(
["chflags", "-R", "noschg,nouchg,nosappnd,nouappnd", mysettings["D"]]
)
subprocess.run(
["chflags", "-R", "nosunlnk,nouunlnk", mysettings["D"]],
stderr=subprocess.DEVNULL,
)
except OSError:
pass


def _postinst_bsdflags(mysettings):
if bsd_chflags:
# Restore all of the flags saved above.
os.system(
"mtree -e -p %s -U -k flags < %s > /dev/null"
% (
_shell_quote(mysettings["ROOT"]),
_shell_quote(os.path.join(mysettings["T"], "bsdflags.mtree")),
)
)
try:
# Restore all of the flags saved above.
with open(os.path.join(mysettings["T"], "bsdflags.mtree"), "rb") as infile:
subprocess.run(
["mtree", "-e", "-p", mysettings["ROOT"], "-U", "-k", "flags"],
stdin=infile,
stdout=subprocess.DEVNULL,
)
except OSError:
pass


def _post_src_install_uid_fix(mysettings, out):
Expand Down Expand Up @@ -2886,8 +2889,8 @@ def _reapply_bsdflags_to_image(mysettings):
os.system(
"mtree -e -p %s -U -k flags < %s > /dev/null"
% (
_shell_quote(mysettings["D"]),
_shell_quote(os.path.join(mysettings["T"], "bsdflags.mtree")),
shlex.quote(mysettings["D"]),
shlex.quote(os.path.join(mysettings["T"], "bsdflags.mtree")),
)
)

Expand Down
3 changes: 1 addition & 2 deletions lib/portage/package/ebuild/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
shutil,
_encodings,
_movefile,
_shell_quote,
_unicode_encode,
)
from portage.checksum import (
Expand Down Expand Up @@ -235,7 +234,7 @@ async def _userpriv_test_write_file(settings, file_path):
args = [
BASH_BINARY,
"-c",
_userpriv_test_write_cmd_script % {"file_path": _shell_quote(file_path)},
_userpriv_test_write_cmd_script % {"file_path": shlex.quote(file_path)},
]

returncode = await _async_spawn_fetch(settings, args)
Expand Down
14 changes: 6 additions & 8 deletions lib/portage/sync/modules/cvs/cvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Distributed under the terms of the GNU General Public License v2

import logging
import shlex

import portage
from portage import os
Expand Down Expand Up @@ -34,12 +35,10 @@ def new(self, **kwargs):
portage.process.spawn_bash(
"cd %s; exec cvs -z0 -d %s co -P -d %s %s"
% (
portage._shell_quote(os.path.dirname(self.repo.location)),
portage._shell_quote(cvs_root),
portage._shell_quote(os.path.basename(self.repo.location)),
portage._shell_quote(
self.repo.module_specific_options["sync-cvs-repo"]
),
shlex.quote(os.path.dirname(self.repo.location)),
shlex.quote(cvs_root),
shlex.quote(os.path.basename(self.repo.location)),
shlex.quote(self.repo.module_specific_options["sync-cvs-repo"]),
),
**self.spawn_kwargs,
)
Expand All @@ -62,8 +61,7 @@ def update(self):

# cvs update
exitcode = portage.process.spawn_bash(
"cd %s; exec cvs -z0 -q update -dP"
% (portage._shell_quote(self.repo.location),),
f"cd {shlex.quote(self.repo.location)}; exec cvs -z0 -q update -dP",
**self.spawn_kwargs,
)
if exitcode != os.EX_OK:
Expand Down
Loading

0 comments on commit a4f8802

Please sign in to comment.