From c1dffc5c64377cfcda9f2befd357e4791903bf39 Mon Sep 17 00:00:00 2001
From: Ben Beasley <code@musicinmybrain.net>
Date: Tue, 28 May 2024 14:12:55 -0400
Subject: [PATCH] On Python 3.3+, replace pipes.quote with shlex.quote (#342)

* On Python 3.3+, replace pipes.quote with shlex.quote

The pipes.quote() function was undocumented, and the pipes module was
deprecated in Python 3.11 and will be removed in Python 3.13.

Fixes #341.

* Choose shlex or pipes based on Python version

It was pointed out that pyupgrade can handle this better than
try/except.
---
 nodeenv.py            | 13 ++++++++-----
 tests/nodeenv_test.py |  9 ++++++---
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/nodeenv.py b/nodeenv.py
index 7d68237..88c849f 100644
--- a/nodeenv.py
+++ b/nodeenv.py
@@ -23,7 +23,10 @@
 import argparse
 import subprocess
 import tarfile
-import pipes
+if sys.version_info < (3, 3):
+    from pipes import quote as _quote
+else:
+    from shlex import quote as _quote
 import platform
 import zipfile
 import shutil
@@ -733,7 +736,7 @@ def build_node_from_src(env_dir, src_dir, node_src_dir, args):
 
     conf_cmd = [
         './configure',
-        '--prefix=%s' % pipes.quote(env_dir)
+        '--prefix=%s' % _quote(env_dir)
     ]
     if args.without_ssl:
         conf_cmd.append('--without-ssl')
@@ -815,7 +818,7 @@ def install_npm(env_dir, _src_dir, args):
         (
             'bash', '-c',
             '. {0} && npm install -g npm@{1}'.format(
-                pipes.quote(join(env_dir, 'bin', 'activate')),
+                _quote(join(env_dir, 'bin', 'activate')),
                 args.npm,
             )
         ),
@@ -883,10 +886,10 @@ def install_packages(env_dir, args):
     activate_path = join(env_dir, 'bin', 'activate')
     real_npm_ver = args.npm if args.npm.count(".") == 2 else args.npm + ".0"
     if args.npm == "latest" or real_npm_ver >= "1.0.0":
-        cmd = '. ' + pipes.quote(activate_path) + \
+        cmd = '. ' + _quote(activate_path) + \
               ' && npm install -g %(pack)s'
     else:
-        cmd = '. ' + pipes.quote(activate_path) + \
+        cmd = '. ' + _quote(activate_path) + \
               ' && npm install %(pack)s' + \
               ' && npm activate %(pack)s'
 
diff --git a/tests/nodeenv_test.py b/tests/nodeenv_test.py
index 09bcbb0..22dd5eb 100644
--- a/tests/nodeenv_test.py
+++ b/tests/nodeenv_test.py
@@ -2,7 +2,10 @@
 from __future__ import unicode_literals
 
 import os.path
-import pipes
+if sys.version_info < (3, 3):
+    from pipes import quote as _quote
+else:
+    from shlex import quote as _quote
 import subprocess
 import sys
 import sysconfig
@@ -29,7 +32,7 @@ def test_smoke(tmpdir):
         '-m', 'nodeenv', '--prebuilt', nenv_path,
     ])
     assert os.path.exists(nenv_path)
-    activate = pipes.quote(os.path.join(nenv_path, 'bin', 'activate'))
+    activate = _quote(os.path.join(nenv_path, 'bin', 'activate'))
     subprocess.check_call([
         'sh', '-c', '. {} && node --version'.format(activate),
     ])
@@ -44,7 +47,7 @@ def test_smoke_n_system_special_chars(tmpdir):
         '-m', 'nodeenv', '-n', 'system', nenv_path,
     ))
     assert os.path.exists(nenv_path)
-    activate = pipes.quote(os.path.join(nenv_path, 'bin', 'activate'))
+    activate = _quote(os.path.join(nenv_path, 'bin', 'activate'))
     subprocess.check_call([
         'sh', '-c', '. {} && node --version'.format(activate),
     ])
