Skip to content

Commit e4a56bd

Browse files
committed
[GR-23879] Use subprocess instead of os.system in pip hook and ginstall.
PullRequest: graalpython/1211
2 parents bc2f247 + 2ab2812 commit e4a56bd

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixSubprocessModuleBuiltins.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ private synchronized int forkExec(PList args, @SuppressWarnings("unused") PList
144144
}
145145
}
146146

147-
// TODO: fix this better? sys.executable is often used in subprocess tests, but on Java
148-
// that actually gives you a whole cmdline, which we need to split up for process
149-
// builder
150147
if (!argStrings.isEmpty()) {
151148
if (argStrings.get(0).equals(context.getOption(PythonOptions.Executable))) {
152149
String[] executableList = PythonOptions.getExecutableList(context);

graalpython/lib-graalpython/modules/ginstall.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,13 @@ def wrapper(*args, **kwargs):
9999
return wrapper
100100
return decorator
101101

102-
103-
def system(cmd, msg=""):
104-
print("+", cmd)
105-
status = os.system(cmd)
106-
if status != 0:
107-
xit(msg, status=status)
108-
return status
109-
102+
def run_cmd(args, msg="", failOnError=True, cwd=None, env=None):
103+
cwd_log = "cd " + cwd if cwd else ""
104+
print("+", cwd_log, ' '.join(args))
105+
result = subprocess.run(args, cwd=cwd, env=env)
106+
if failOnError and result.returncode != 0:
107+
xit(msg, status=result.returncode)
108+
return result.returncode
110109

111110
def known_packages():
112111
@pip_package()
@@ -377,32 +376,30 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
377376
elif url.startswith("https://") and "HTTPS_PROXY" in os_env:
378377
curl_opts += ["--proxy", os_env["HTTPS_PROXY"]]
379378

380-
# honor env var 'CFLAGS' and 'CPPFLAGS'
381-
cppflags = os_env.get("CPPFLAGS", "")
379+
# honor env var 'CFLAGS' and the explicitly passed env
380+
setup_env = os_env.copy()
381+
setup_env.update(env)
382382
cflags = os_env.get("CFLAGS", "") + ((" " + add_cflags) if add_cflags else "")
383+
setup_env['CFLAGS'] = cflags if cflags else ""
383384

384-
env_str = ('CFLAGS="%s" ' % cflags if cflags else "") + ('CPPFLAGS="%s" ' % cppflags if cppflags else "")
385-
for key in env.keys():
386-
env_str = env_str + ('%s="%s" ' % (key, env[key]))
387-
388-
if os.system("curl -L -o %s/%s %s" % (tempdir, name, url)) != 0:
385+
if run_cmd(["curl", "-L", "-o", os.path.join(tempdir, name), url], failOnError=False) != 0:
389386
# honor env var 'HTTP_PROXY' and 'HTTPS_PROXY'
390387
env = os.environ
391388
curl_opts = []
392389
if url.startswith("http://") and "HTTP_PROXY" in env:
393390
curl_opts += ["--proxy", env["HTTP_PROXY"]]
394391
elif url.startswith("https://") and "HTTPS_PROXY" in env:
395392
curl_opts += ["--proxy", env["HTTPS_PROXY"]]
396-
system("curl -L %s -o %s/%s %s" % (" ".join(curl_opts), tempdir, name, url), msg="Download error")
393+
run_cmd(["curl", "-L"] + curl_opts + ["-o", os.path.join(tempdir, name), url], msg="Download error")
397394

398395
if name.endswith(".tar.gz"):
399-
system("tar xzf %s/%s -C %s" % (tempdir, name, tempdir), msg="Error extracting tar.gz")
396+
run_cmd(["tar", "xzf", os.path.join(tempdir, name), "-C", tempdir], msg="Error extracting tar.gz")
400397
bare_name = name[:-len(".tar.gz")]
401398
elif name.endswith(".tar.bz2"):
402-
system("tar xjf %s/%s -C %s" % (tempdir, name, tempdir), msg="Error extracting tar.bz2")
399+
run_cmd(["tar", "xjf", os.path.join(tempdir, name), "-C", tempdir], msg="Error extracting tar.bz2")
403400
bare_name = name[:-len(".tar.bz2")]
404401
elif name.endswith(".zip"):
405-
system("unzip -u %s/%s -d %s" % (tempdir, name, tempdir), msg="Error extracting zip")
402+
run_cmd(["unzip", "-u", os.path.join(tempdir, name), "-d", tempdir], msg="Error extracting zip")
406403
bare_name = name[:-len(".zip")]
407404
else:
408405
xit("Unknown file type: %s" % name)
@@ -415,22 +412,22 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
415412

416413
patch_file_path = first_existing(package, versions, os.path.join(patches_dir, "sdist"), ".patch")
417414
if patch_file_path:
418-
system("patch -d %s/%s/ -p1 < %s" %
419-
(tempdir, bare_name, patch_file_path))
415+
run_cmd(["patch", "-d", os.path.join(tempdir, bare_name, ""), "-p1", "-i", patch_file_path])
420416

421417
whl_patches_dir = os.path.join(patches_dir, "whl")
422418
patch_file_path = first_existing(package, versions, whl_patches_dir, ".patch")
423419
subdir = read_first_existing(package, versions, whl_patches_dir, ".dir")
424-
subdir = "" if subdir is None else "/" + subdir
420+
subdir = "" if subdir is None else subdir
425421
if patch_file_path:
426-
system("patch -d %s/%s%s -p1 < %s" %
427-
(tempdir, bare_name, subdir, patch_file_path))
422+
os.path.join(tempdir, bare_name, subdir)
423+
run_cmd(["patch", "-d", os.path.join(tempdir, bare_name, subdir), "-p1", "-i", patch_file_path])
428424

429425
if "--prefix" not in extra_opts and site.ENABLE_USER_SITE:
430-
user_arg = "--user"
426+
user_arg = ["--user"]
431427
else:
432-
user_arg = ""
433-
status = system("cd %s/%s; %s %s setup.py install %s %s" % (tempdir, bare_name, env_str, sys.executable, user_arg, " ".join(extra_opts)))
428+
user_arg = []
429+
status = run_cmd([sys.executable, "setup.py", "install"] + user_arg + extra_opts, env=setup_env,
430+
cwd=os.path.join(tempdir, bare_name))
434431
if status != 0 and not ignore_errors:
435432
xit("An error occurred trying to run `setup.py install %s %s'" % (user_arg, " ".join(extra_opts)))
436433

graalpython/lib-graalpython/pip_hook.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
39-
import sys
4039
import _frozen_importlib
40+
import sys
4141

4242

4343
class PipLoader:
@@ -59,6 +59,7 @@ def unpack_file(filename, location, *args, **kwargs):
5959

6060
import os
6161
import re
62+
import subprocess
6263
# we expect filename to be something like "pytest-5.4.2-py3-none-any.whl"
6364
# some packages may have only major.minor or just major version
6465
archive_name = os.path.basename(filename)
@@ -106,9 +107,8 @@ def apply_first_existing(dir, suffix, wd=None):
106107
filename = first_existing(package_name, name_ver_match, dir, suffix)
107108
if filename:
108109
print("Patching package " + package_name + " using " + filename)
109-
cwd = "" if wd is None else "cd " + wd + ";"
110-
patch_res = os.system(cwd + "patch -f -d %s -p1 < %s" % (location, filename))
111-
if patch_res != 0:
110+
patch_res = subprocess.run(["patch", "-f", "-d", location, "-p1", "-i", filename], cwd=wd)
111+
if patch_res.returncode != 0:
112112
print("Applying Graal Python patch failed for %s. The package may still work." % package_name)
113113

114114
print("Looking for Graal Python patches for " + package_name)

0 commit comments

Comments
 (0)