Skip to content
Merged
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ default_install_hook_types:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.11.5
rev: v0.12.4
hooks:
# Run the linter.
- id: ruff
Expand Down
8 changes: 4 additions & 4 deletions newrelic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os.path
from pathlib import Path

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

try:
with open(os.path.join(THIS_DIR, "version.txt")) as f:
with VERSION_FILE.open() as f:
version = f.read()
except:
except Exception:
version = "0.0.0"

version_info = list(map(int, version.split(".")))
3 changes: 2 additions & 1 deletion newrelic/admin/debug_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
def debug_console(args):
import sys
from pathlib import Path

if len(args) == 0:
usage("debug-console")
Expand All @@ -36,7 +37,7 @@ def debug_console(args):
log_object = None

if len(args) >= 2:
log_object = open(args[1], "w")
log_object = Path(args[1]).open("w")

shell = ClientShell(config_file, log=log_object)
shell.cmdloop()
15 changes: 6 additions & 9 deletions newrelic/admin/generate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,25 @@
"generate-config", "license_key [output_file]", """Generates a sample agent configuration file for <license_key>."""
)
def generate_config(args):
import os
import sys
from pathlib import Path

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

from newrelic import __file__ as package_root
import newrelic

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

config_file = os.path.join(package_root, "newrelic.ini")

with open(config_file) as f:
with config_file.open() as f:
content = f.read()

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

if len(args) >= 2 and args[1] != "-":
output_file = open(args[1], "w")
output_file.write(content)
output_file.close()
with Path(args[1]).open("w") as output_file:
output_file.write(content)
else:
print(content)
5 changes: 3 additions & 2 deletions newrelic/admin/license_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def license_key(args):
import logging
import os
import sys
from pathlib import Path

if len(args) == 0:
usage("license-key")
Expand All @@ -37,12 +38,12 @@ def license_key(args):
if len(args) >= 2:
log_file = args[1]
else:
log_file = "/tmp/python-agent-test.log"
log_file = Path("/tmp/python-agent-test.log")

log_level = logging.DEBUG

try:
os.unlink(log_file)
log_file.unlink()
except Exception:
pass

Expand Down
5 changes: 3 additions & 2 deletions newrelic/admin/local_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def local_config(args):
import logging
import os
import sys
from pathlib import Path

if len(args) == 0:
usage("local-config")
Expand All @@ -36,12 +37,12 @@ def local_config(args):
if len(args) >= 2:
log_file = args[1]
else:
log_file = "/tmp/python-agent-test.log"
log_file = Path("/tmp/python-agent-test.log")

log_level = logging.DEBUG

try:
os.unlink(log_file)
log_file.unlink()
except Exception:
pass

Expand Down
5 changes: 3 additions & 2 deletions newrelic/admin/network_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def network_config(args):
import logging
import os
import sys
from pathlib import Path

if len(args) == 0:
usage("network-config")
Expand All @@ -36,12 +37,12 @@ def network_config(args):
if len(args) >= 2:
log_file = args[1]
else:
log_file = "/tmp/python-agent-test.log"
log_file = Path("/tmp/python-agent-test.log")

log_level = logging.DEBUG

try:
os.unlink(log_file)
log_file.unlink()
except Exception:
pass

Expand Down
35 changes: 20 additions & 15 deletions newrelic/admin/run_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def run_program(args):
import os
import sys
import time
from pathlib import Path

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

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

log_message("working_directory = %r", os.getcwd())
log_message("working_directory = %r", str(Path.cwd()))
log_message("current_command = %r", sys.argv)

log_message("sys.prefix = %r", os.path.normpath(sys.prefix))
sys_prefix = str(Path(sys.prefix).resolve())
log_message("sys.prefix = %r", sys_prefix)

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

from newrelic import __file__ as root_directory
import newrelic

root_directory = os.path.dirname(root_directory)
boot_directory = os.path.join(root_directory, "bootstrap")
root_directory = Path(newrelic.__file__).parent
boot_directory = root_directory / "bootstrap"

root_directory = str(root_directory)
boot_directory = str(boot_directory)

log_message("root_directory = %r", root_directory)
log_message("boot_directory = %r", boot_directory)

python_path = boot_directory

if "PYTHONPATH" in os.environ:
path = os.environ["PYTHONPATH"].split(os.path.pathsep)
path = os.environ["PYTHONPATH"].split(os.pathsep)
if boot_directory not in path:
python_path = f"{boot_directory}{os.path.pathsep}{os.environ['PYTHONPATH']}"
python_path = f"{boot_directory}{os.pathsep}{os.environ['PYTHONPATH']}"

os.environ["PYTHONPATH"] = python_path

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

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

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

program_exe_path = args[0]
program_exe_path = Path(args[0])

if not os.path.dirname(program_exe_path):
program_search_path = os.environ.get("PATH", "").split(os.path.pathsep)
if not program_exe_path.parent:
program_search_path = os.environ.get("PATH", "").split(os.pathsep)
for path in program_search_path:
path = os.path.join(path, program_exe_path)
if os.path.exists(path) and os.access(path, os.X_OK):
path = Path(path) / program_exe_path
if path.exists() and os.access(path, os.X_OK):
program_exe_path = path
break

log_message("program_exe_path = %r", program_exe_path)
log_message("execl_arguments = %r", [program_exe_path] + args)
log_message("program_exe_path = %r", str(program_exe_path))
log_message("execl_arguments = %r", [program_exe_path, *args])

os.execl(program_exe_path, *args) # noqa: S606
33 changes: 19 additions & 14 deletions newrelic/admin/run_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def run_python(args):
import os
import sys
import time
from pathlib import Path

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

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

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

log_message("working_directory = %r", os.getcwd())
log_message("working_directory = %r", str(Path.cwd()))
log_message("current_command = %r", sys.argv)

log_message("sys.prefix = %r", os.path.normpath(sys.prefix))
sys_prefix = str(Path(sys.prefix).resolve())
log_message("sys.prefix = %r", sys_prefix)

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

from newrelic import __file__ as root_directory
import newrelic

root_directory = os.path.dirname(root_directory)
boot_directory = os.path.join(root_directory, "bootstrap")
root_directory = Path(newrelic.__file__).parent
boot_directory = root_directory / "bootstrap"

root_directory = str(root_directory)
boot_directory = str(boot_directory)

log_message("root_directory = %r", root_directory)
log_message("boot_directory = %r", boot_directory)

python_path = boot_directory

if "PYTHONPATH" in os.environ:
path = os.environ["PYTHONPATH"].split(os.path.pathsep)
path = os.environ["PYTHONPATH"].split(os.pathsep)
if boot_directory not in path:
python_path = f"{boot_directory}{os.path.pathsep}{os.environ['PYTHONPATH']}"
python_path = f"{boot_directory}{os.pathsep}{os.environ['PYTHONPATH']}"

os.environ["PYTHONPATH"] = python_path

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

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

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

bin_directory = os.path.dirname(sys.argv[0])
bin_directory = Path(sys.argv[0]).parent

if bin_directory:
python_exe = os.path.basename(sys.executable)
python_exe_path = os.path.join(bin_directory, python_exe)
if not os.path.exists(python_exe_path) or not os.access(python_exe_path, os.X_OK):
python_exe = Path(sys.executable).name
python_exe_path = bin_directory / python_exe
if not python_exe_path.exists() or not os.access(python_exe_path, os.X_OK):
python_exe_path = sys.executable
else:
python_exe_path = sys.executable

log_message("python_exe_path = %r", python_exe_path)
log_message("execl_arguments = %r", [python_exe_path, python_exe_path] + args)
log_message("python_exe_path = %r", str(python_exe_path))
log_message("execl_arguments = %r", [python_exe_path, python_exe_path, *args])

os.execl(python_exe_path, python_exe_path, *args) # noqa: S606
5 changes: 3 additions & 2 deletions newrelic/admin/server_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def server_config(args):
import os
import sys
import time
from pathlib import Path

if len(args) == 0:
usage("server-config")
Expand All @@ -39,12 +40,12 @@ def server_config(args):
if len(args) >= 2:
log_file = args[1]
else:
log_file = "/tmp/python-agent-test.log"
log_file = Path("/tmp/python-agent-test.log")

log_level = logging.DEBUG

try:
os.unlink(log_file)
log_file.unlink()
except Exception:
pass

Expand Down
5 changes: 3 additions & 2 deletions newrelic/admin/validate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def validate_config(args):
import os
import sys
import time
from pathlib import Path

if len(args) == 0:
usage("validate-config")
Expand All @@ -154,12 +155,12 @@ def validate_config(args):
if len(args) >= 2:
log_file = args[1]
else:
log_file = "/tmp/python-agent-test.log"
log_file = Path("/tmp/python-agent-test.log")

log_level = logging.DEBUG

try:
os.unlink(log_file)
log_file.unlink()
except Exception:
pass

Expand Down
5 changes: 3 additions & 2 deletions newrelic/api/profile_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
import functools
import os
import sys
from pathlib import Path

from newrelic import __file__ as AGENT_PACKAGE_FILE
import newrelic
from newrelic.api.function_trace import FunctionTrace
from newrelic.api.time_trace import current_trace
from newrelic.common.object_names import callable_name
from newrelic.common.object_wrapper import FunctionWrapper, wrap_object

AGENT_PACKAGE_DIRECTORY = f"{os.path.dirname(AGENT_PACKAGE_FILE)}/"
AGENT_PACKAGE_DIRECTORY = str(Path(newrelic.__file__).parent) + os.sep


class ProfileTrace:
Expand Down
Loading
Loading