-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
098296d
commit 27c99de
Showing
6 changed files
with
432 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import requests | ||
import os | ||
import shutil | ||
import subprocess | ||
import tempfile | ||
|
||
|
||
AGENT_VERSION = "9.9.0" | ||
FILE_DIR = os.path.dirname(__file__) | ||
WORKSPACE_DIR = os.path.join(FILE_DIR, "workspace") | ||
|
||
|
||
def main(): | ||
with requests.session() as session: | ||
# Fetch JSON list of all agent package artifacts | ||
resp = session.get("https://pypi.org/pypi/newrelic/json") | ||
resp.raise_for_status() | ||
resp_dict = resp.json() | ||
|
||
# Grab the correct release | ||
release = resp_dict["releases"][AGENT_VERSION] | ||
|
||
# Filter artifacts for wheels and tarballs | ||
wheel_urls = [ | ||
artifact["url"] | ||
for artifact in release | ||
if artifact["url"].endswith(".whl") | ||
] | ||
|
||
tarball_url = [ | ||
artifact["url"] | ||
for artifact in release | ||
if artifact["url"].endswith(".tar.gz") | ||
][0] | ||
|
||
# Make workspace directory | ||
if not os.path.exists(WORKSPACE_DIR): | ||
os.mkdir(WORKSPACE_DIR) | ||
|
||
# Download tarball | ||
resp = session.get(tarball_url) | ||
resp.raise_for_status() | ||
|
||
tarball_filepath = os.path.join(WORKSPACE_DIR, "newrelic.tar.gz") | ||
with open(tarball_filepath, "wb") as file: | ||
file.write(resp.content) | ||
|
||
# Download and extract all wheels | ||
for wheel_url in wheel_urls: | ||
# Download wheel | ||
resp = session.get(wheel_url) | ||
resp.raise_for_status() | ||
|
||
# Write wheel to file under a folder of the same name | ||
with tempfile.TemporaryDirectory() as wheel_temp_dir: | ||
# Compute paths | ||
wheel_filename = wheel_url.split("/")[-1] | ||
wheel_filepath = os.path.join(wheel_temp_dir, wheel_filename) | ||
|
||
# Write wheel file to tempdir | ||
with open(wheel_filepath, "wb") as file: | ||
file.write(resp.content) | ||
|
||
# Unpack wheel using wheel command | ||
subprocess.run( | ||
["wheel", "unpack", str(wheel_filename)], | ||
cwd=wheel_temp_dir, | ||
) | ||
|
||
# Delete wheel file after unpacking | ||
os.remove(wheel_filepath) | ||
|
||
# Move unpacked module from tempdir to final destination | ||
unpacked_module_folder = os.path.join( | ||
wheel_temp_dir, list(os.listdir(wheel_temp_dir))[0] | ||
) | ||
|
||
# Put wheel contents into folders named after the original wheel types | ||
final_wheel_dest = os.path.join( | ||
WORKSPACE_DIR, wheel_filename.rstrip(".whl") | ||
) | ||
shutil.move(str(unpacked_module_folder), str(final_wheel_dest)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import sys | ||
|
||
INSTRUMENTATION_PATH = os.path.dirname(__file__) | ||
INSTRUMENTATION_VENDOR_PATH = os.path.join(INSTRUMENTATION_PATH, "vendor") | ||
SDIST_PATH = str(os.path.join(INSTRUMENTATION_PATH, "newrelic")) | ||
|
||
|
||
def get_supported_tags(): | ||
""" | ||
Load pip and run compatibility_tags.get_supported(). If pip is not present | ||
or does not contain this function, load our vendored version of pip instead. | ||
""" | ||
try: | ||
# Attempt to use any existing installs of pip to find supported tags | ||
from pip._internal.utils.compatibility_tags import get_supported | ||
|
||
return get_supported() | ||
except Exception: | ||
pass | ||
|
||
# Unable to find pip, or pip version does not have compatibility tags | ||
if "pip" in sys.modules: | ||
# Save original pip module and load our own copy | ||
original_pip = sys.modules["pip"] | ||
del sys.modules["pip"] | ||
else: | ||
original_pip = None | ||
|
||
try: | ||
# Insert our vendored version of pip into the path, and try to return | ||
# the supported tags again. | ||
sys.path.insert(0, INSTRUMENTATION_VENDOR_PATH) | ||
from pip._internal.utils.compatibility_tags import get_supported | ||
|
||
return get_supported() | ||
finally: | ||
# Remove our vendored version of pip from the path | ||
if INSTRUMENTATION_VENDOR_PATH in sys.path: | ||
del sys.path[sys.path.index(INSTRUMENTATION_VENDOR_PATH)] | ||
|
||
# Replace original pip module for compatibility | ||
if original_pip: | ||
sys.modules["pip"] = original_pip | ||
elif "pip" in sys.modules: | ||
del sys.modules["pip"] | ||
|
||
|
||
def find_supported_newrelic_distribution(): | ||
wheels = list(os.listdir(INSTRUMENTATION_PATH)) | ||
for tag in get_supported_tags(): | ||
tag = str(tag) | ||
for wheel in wheels: | ||
if tag in wheel: | ||
return str(os.path.join(INSTRUMENTATION_PATH, wheel)) | ||
else: | ||
return SDIST_PATH | ||
|
||
|
||
def insert_newrelic_distribution(): | ||
# Find path of supported distribution | ||
try: | ||
new_relic_path = find_supported_newrelic_distribution() | ||
except Exception: | ||
new_relic_path = SDIST_PATH | ||
|
||
# Add path to sys.path and import | ||
sys.path.insert(0, new_relic_path) | ||
import newrelic.config | ||
|
||
# Returned path will be logged and then removed from sys.path by sitecustomize | ||
return new_relic_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests | ||
setuptools>=40.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pip |
Oops, something went wrong.