Skip to content

Commit b089b33

Browse files
Change Paths to use pathlib.Path (#1431)
* Switch to using pathlib * Sort linter list by code * Add ignore for new rule violation * Update ruff in pre-commit * Fix s3 tests * Fix newer test files with pathlib changes * Fix agent unittests * Fix path monkeypatching in docker container tests * Fix additional os.path references * Rename all os.path.pathsep to os.pathsep for brevity --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 777e025 commit b089b33

File tree

71 files changed

+322
-326
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+322
-326
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ default_install_hook_types:
2929
repos:
3030
- repo: https://github.com/astral-sh/ruff-pre-commit
3131
# Ruff version.
32-
rev: v0.11.5
32+
rev: v0.12.4
3333
hooks:
3434
# Run the linter.
3535
- id: ruff

newrelic/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os.path
15+
from pathlib import Path
1616

17-
THIS_DIR = os.path.dirname(__file__)
17+
VERSION_FILE = Path(__file__).parent / "version.txt"
1818

1919
try:
20-
with open(os.path.join(THIS_DIR, "version.txt")) as f:
20+
with VERSION_FILE.open() as f:
2121
version = f.read()
22-
except:
22+
except Exception:
2323
version = "0.0.0"
2424

2525
version_info = list(map(int, version.split(".")))

newrelic/admin/debug_console.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
def debug_console(args):
2727
import sys
28+
from pathlib import Path
2829

2930
if len(args) == 0:
3031
usage("debug-console")
@@ -36,7 +37,7 @@ def debug_console(args):
3637
log_object = None
3738

3839
if len(args) >= 2:
39-
log_object = open(args[1], "w")
40+
log_object = Path(args[1]).open("w")
4041

4142
shell = ClientShell(config_file, log=log_object)
4243
shell.cmdloop()

newrelic/admin/generate_config.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,25 @@
1919
"generate-config", "license_key [output_file]", """Generates a sample agent configuration file for <license_key>."""
2020
)
2121
def generate_config(args):
22-
import os
2322
import sys
23+
from pathlib import Path
2424

2525
if len(args) == 0:
2626
usage("generate-config")
2727
sys.exit(1)
2828

29-
from newrelic import __file__ as package_root
29+
import newrelic
3030

31-
package_root = os.path.dirname(package_root)
31+
config_file = Path(newrelic.__file__).parent / "newrelic.ini"
3232

33-
config_file = os.path.join(package_root, "newrelic.ini")
34-
35-
with open(config_file) as f:
33+
with config_file.open() as f:
3634
content = f.read()
3735

3836
if len(args) >= 1:
3937
content = content.replace("*** REPLACE ME ***", args[0])
4038

4139
if len(args) >= 2 and args[1] != "-":
42-
output_file = open(args[1], "w")
43-
output_file.write(content)
44-
output_file.close()
40+
with Path(args[1]).open("w") as output_file:
41+
output_file.write(content)
4542
else:
4643
print(content)

newrelic/admin/license_key.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def license_key(args):
2626
import logging
2727
import os
2828
import sys
29+
from pathlib import Path
2930

3031
if len(args) == 0:
3132
usage("license-key")
@@ -37,12 +38,12 @@ def license_key(args):
3738
if len(args) >= 2:
3839
log_file = args[1]
3940
else:
40-
log_file = "/tmp/python-agent-test.log"
41+
log_file = Path("/tmp/python-agent-test.log")
4142

4243
log_level = logging.DEBUG
4344

4445
try:
45-
os.unlink(log_file)
46+
log_file.unlink()
4647
except Exception:
4748
pass
4849

newrelic/admin/local_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def local_config(args):
2525
import logging
2626
import os
2727
import sys
28+
from pathlib import Path
2829

2930
if len(args) == 0:
3031
usage("local-config")
@@ -36,12 +37,12 @@ def local_config(args):
3637
if len(args) >= 2:
3738
log_file = args[1]
3839
else:
39-
log_file = "/tmp/python-agent-test.log"
40+
log_file = Path("/tmp/python-agent-test.log")
4041

4142
log_level = logging.DEBUG
4243

4344
try:
44-
os.unlink(log_file)
45+
log_file.unlink()
4546
except Exception:
4647
pass
4748

newrelic/admin/network_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def network_config(args):
2525
import logging
2626
import os
2727
import sys
28+
from pathlib import Path
2829

2930
if len(args) == 0:
3031
usage("network-config")
@@ -36,12 +37,12 @@ def network_config(args):
3637
if len(args) >= 2:
3738
log_file = args[1]
3839
else:
39-
log_file = "/tmp/python-agent-test.log"
40+
log_file = Path("/tmp/python-agent-test.log")
4041

4142
log_level = logging.DEBUG
4243

4344
try:
44-
os.unlink(log_file)
45+
log_file.unlink()
4546
except Exception:
4647
pass
4748

newrelic/admin/run_program.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def run_program(args):
3131
import os
3232
import sys
3333
import time
34+
from pathlib import Path
3435

3536
if len(args) == 0:
3637
usage("run-program")
@@ -46,10 +47,11 @@ def log_message(text, *args):
4647

4748
log_message("New Relic Admin Script (%s)", __file__)
4849

49-
log_message("working_directory = %r", os.getcwd())
50+
log_message("working_directory = %r", str(Path.cwd()))
5051
log_message("current_command = %r", sys.argv)
5152

52-
log_message("sys.prefix = %r", os.path.normpath(sys.prefix))
53+
sys_prefix = str(Path(sys.prefix).resolve())
54+
log_message("sys.prefix = %r", sys_prefix)
5355

5456
try:
5557
log_message("sys.real_prefix = %r", sys.real_prefix)
@@ -67,44 +69,47 @@ def log_message(text, *args):
6769
continue
6870
log_message("%s = %r", name, os.environ.get(name))
6971

70-
from newrelic import __file__ as root_directory
72+
import newrelic
7173

72-
root_directory = os.path.dirname(root_directory)
73-
boot_directory = os.path.join(root_directory, "bootstrap")
74+
root_directory = Path(newrelic.__file__).parent
75+
boot_directory = root_directory / "bootstrap"
76+
77+
root_directory = str(root_directory)
78+
boot_directory = str(boot_directory)
7479

7580
log_message("root_directory = %r", root_directory)
7681
log_message("boot_directory = %r", boot_directory)
7782

7883
python_path = boot_directory
7984

8085
if "PYTHONPATH" in os.environ:
81-
path = os.environ["PYTHONPATH"].split(os.path.pathsep)
86+
path = os.environ["PYTHONPATH"].split(os.pathsep)
8287
if boot_directory not in path:
83-
python_path = f"{boot_directory}{os.path.pathsep}{os.environ['PYTHONPATH']}"
88+
python_path = f"{boot_directory}{os.pathsep}{os.environ['PYTHONPATH']}"
8489

8590
os.environ["PYTHONPATH"] = python_path
8691

8792
os.environ["NEW_RELIC_ADMIN_COMMAND"] = repr(sys.argv)
8893

89-
os.environ["NEW_RELIC_PYTHON_PREFIX"] = os.path.realpath(os.path.normpath(sys.prefix))
94+
os.environ["NEW_RELIC_PYTHON_PREFIX"] = sys_prefix
9095
os.environ["NEW_RELIC_PYTHON_VERSION"] = ".".join(map(str, sys.version_info[:2]))
9196

9297
# If not an absolute or relative path, then we need to
9398
# see if program can be found in PATH. Note that can
9499
# be found in current working directory even though '.'
95100
# not in PATH.
96101

97-
program_exe_path = args[0]
102+
program_exe_path = Path(args[0])
98103

99-
if not os.path.dirname(program_exe_path):
100-
program_search_path = os.environ.get("PATH", "").split(os.path.pathsep)
104+
if not program_exe_path.parent:
105+
program_search_path = os.environ.get("PATH", "").split(os.pathsep)
101106
for path in program_search_path:
102-
path = os.path.join(path, program_exe_path)
103-
if os.path.exists(path) and os.access(path, os.X_OK):
107+
path = Path(path) / program_exe_path
108+
if path.exists() and os.access(path, os.X_OK):
104109
program_exe_path = path
105110
break
106111

107-
log_message("program_exe_path = %r", program_exe_path)
108-
log_message("execl_arguments = %r", [program_exe_path] + args)
112+
log_message("program_exe_path = %r", str(program_exe_path))
113+
log_message("execl_arguments = %r", [program_exe_path, *args])
109114

110115
os.execl(program_exe_path, *args) # noqa: S606

newrelic/admin/run_python.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def run_python(args):
3131
import os
3232
import sys
3333
import time
34+
from pathlib import Path
3435

3536
startup_debug = os.environ.get("NEW_RELIC_STARTUP_DEBUG", "off").lower() in ("on", "true", "1")
3637

@@ -42,10 +43,11 @@ def log_message(text, *args):
4243

4344
log_message("New Relic Admin Script (%s)", __file__)
4445

45-
log_message("working_directory = %r", os.getcwd())
46+
log_message("working_directory = %r", str(Path.cwd()))
4647
log_message("current_command = %r", sys.argv)
4748

48-
log_message("sys.prefix = %r", os.path.normpath(sys.prefix))
49+
sys_prefix = str(Path(sys.prefix).resolve())
50+
log_message("sys.prefix = %r", sys_prefix)
4951

5052
try:
5153
log_message("sys.real_prefix = %r", sys.real_prefix)
@@ -61,26 +63,29 @@ def log_message(text, *args):
6163
if name.startswith("NEW_RELIC_") or name.startswith("PYTHON"):
6264
log_message("%s = %r", name, os.environ.get(name))
6365

64-
from newrelic import __file__ as root_directory
66+
import newrelic
6567

66-
root_directory = os.path.dirname(root_directory)
67-
boot_directory = os.path.join(root_directory, "bootstrap")
68+
root_directory = Path(newrelic.__file__).parent
69+
boot_directory = root_directory / "bootstrap"
70+
71+
root_directory = str(root_directory)
72+
boot_directory = str(boot_directory)
6873

6974
log_message("root_directory = %r", root_directory)
7075
log_message("boot_directory = %r", boot_directory)
7176

7277
python_path = boot_directory
7378

7479
if "PYTHONPATH" in os.environ:
75-
path = os.environ["PYTHONPATH"].split(os.path.pathsep)
80+
path = os.environ["PYTHONPATH"].split(os.pathsep)
7681
if boot_directory not in path:
77-
python_path = f"{boot_directory}{os.path.pathsep}{os.environ['PYTHONPATH']}"
82+
python_path = f"{boot_directory}{os.pathsep}{os.environ['PYTHONPATH']}"
7883

7984
os.environ["PYTHONPATH"] = python_path
8085

8186
os.environ["NEW_RELIC_ADMIN_COMMAND"] = repr(sys.argv)
8287

83-
os.environ["NEW_RELIC_PYTHON_PREFIX"] = os.path.realpath(os.path.normpath(sys.prefix))
88+
os.environ["NEW_RELIC_PYTHON_PREFIX"] = sys_prefix
8489
os.environ["NEW_RELIC_PYTHON_VERSION"] = ".".join(map(str, sys.version_info[:2]))
8590

8691
# Heroku does not set #! line on installed Python scripts
@@ -91,17 +96,17 @@ def log_message(text, *args):
9196
# this script in preference to that used to execute this
9297
# script.
9398

94-
bin_directory = os.path.dirname(sys.argv[0])
99+
bin_directory = Path(sys.argv[0]).parent
95100

96101
if bin_directory:
97-
python_exe = os.path.basename(sys.executable)
98-
python_exe_path = os.path.join(bin_directory, python_exe)
99-
if not os.path.exists(python_exe_path) or not os.access(python_exe_path, os.X_OK):
102+
python_exe = Path(sys.executable).name
103+
python_exe_path = bin_directory / python_exe
104+
if not python_exe_path.exists() or not os.access(python_exe_path, os.X_OK):
100105
python_exe_path = sys.executable
101106
else:
102107
python_exe_path = sys.executable
103108

104-
log_message("python_exe_path = %r", python_exe_path)
105-
log_message("execl_arguments = %r", [python_exe_path, python_exe_path] + args)
109+
log_message("python_exe_path = %r", str(python_exe_path))
110+
log_message("execl_arguments = %r", [python_exe_path, python_exe_path, *args])
106111

107112
os.execl(python_exe_path, python_exe_path, *args) # noqa: S606

newrelic/admin/server_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def server_config(args):
2828
import os
2929
import sys
3030
import time
31+
from pathlib import Path
3132

3233
if len(args) == 0:
3334
usage("server-config")
@@ -39,12 +40,12 @@ def server_config(args):
3940
if len(args) >= 2:
4041
log_file = args[1]
4142
else:
42-
log_file = "/tmp/python-agent-test.log"
43+
log_file = Path("/tmp/python-agent-test.log")
4344

4445
log_level = logging.DEBUG
4546

4647
try:
47-
os.unlink(log_file)
48+
log_file.unlink()
4849
except Exception:
4950
pass
5051

0 commit comments

Comments
 (0)