Skip to content

Commit e707cc0

Browse files
hmstepanekmergify[bot]lrafeeiTimPansinoumaannamalai
authored
Fix local scoped package reporting (#837)
* Include isort stdlibs for determining stdlib modules * Use isort & sys to eliminate std & builtin modules Previously, the logic would fail to identify third party modules installed within the local user socpe. This fixes that issue by skipping builtin and stdlib modules by name, instead of attempting to identify third party modules based on file paths. * Handle importlib_metadata.version being a callable * Add isort into third party notices * [Mega-Linter] Apply linters fixes * Remove Python 2.7 and pypy2 testing (#835) * Change setup-python to @v2 for py2.7 * Remove py27 and pypy testing * Fix syntax errors * Fix comma related syntax errors * Fix more issues in tox * Remove gearman test * Containerized CI Pipeline (#836) * Revert "Remove Python 2.7 and pypy2 testing (#835)" This reverts commit abb6405. * Containerize CI process * Publish new docker container for CI images * Rename github actions job * Copyright tag scripts * Drop debug line * Swap to new CI image * Move pip install to just main python * Remove libcurl special case from tox * Install special case packages into main image * Remove unused packages * Remove all other triggers besides manual * Add make run command * Cleanup small bugs * Fix CI Image Tagging (#838) * Correct templated CI image name * Pin pypy2.7 in image * Fix up scripting * Temporarily Restore Old CI Pipeline (#841) * Restore old pipelines * Remove python 2 from setup-python * Rework CI Pipeline (#839) Change pypy to pypy27 in tox. Fix checkout logic Pin tox requires * Fix Tests on New CI (#843) * Remove non-root user * Test new CI image * Change pypy to pypy27 in tox. * Fix checkout logic * Fetch git tags properly * Pin tox requires * Adjust default db settings for github actions * Rename elasticsearch services * Reset to new pipelines * [Mega-Linter] Apply linters fixes * Fix timezone * Fix docker networking * Pin dev image to new sha * Standardize gearman DB settings * Fix elasticsearch settings bug * Fix gearman bug * Add missing odbc headers * Add more debug messages * Swap out dev ci image * Fix required virtualenv version * Swap out dev ci image * Swap out dev ci image * Remove aioredis v1 for EOL * Add coverage paths for docker container * Unpin ci container --------- Co-authored-by: TimPansino <[email protected]> * Trigger tests --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: hmstepanek <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]> Co-authored-by: Timothy Pansino <[email protected]> Co-authored-by: TimPansino <[email protected]> Co-authored-by: Uma Annamalai <[email protected]>
1 parent 33aa111 commit e707cc0

19 files changed

+1747
-20
lines changed

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ include newrelic/common/cacert.pem
88
include newrelic/packages/wrapt/LICENSE
99
include newrelic/packages/wrapt/README
1010
include newrelic/packages/urllib3/LICENSE.txt
11+
include newrelic/packages/isort/LICENSE

THIRD_PARTY_NOTICES.md

+14-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ Copyright (c) Django Software Foundation and individual contributors.
1414

1515
Distributed under the following license(s):
1616

17-
* [The BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause)
17+
* [The BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause)
18+
19+
20+
## [isort](https://pypi.org/project/isort)
21+
22+
Copyright (c) 2013 Timothy Edmund Crosley
23+
24+
Distributed under the following license(s):
25+
26+
* [The MIT License](http://opensource.org/licenses/MIT)
1827

1928

2029
## [six](https://pypi.org/project/six)
@@ -23,7 +32,7 @@ Copyright (c) 2010-2013 Benjamin Peterson
2332

2433
Distributed under the following license(s):
2534

26-
* [The MIT License](http://opensource.org/licenses/MIT)
35+
* [The MIT License](http://opensource.org/licenses/MIT)
2736

2837

2938
## [time.monotonic](newrelic/common/_monotonic.c)
@@ -32,7 +41,7 @@ Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
3241

3342
Distributed under the following license(s):
3443

35-
* [Python Software Foundation](https://docs.python.org/3/license.html)
44+
* [Python Software Foundation](https://docs.python.org/3/license.html)
3645

3746

3847
## [urllib3](https://pypi.org/project/urllib3)
@@ -41,7 +50,7 @@ Copyright (c) 2008-2019 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
4150

4251
Distributed under the following license(s):
4352

44-
* [The MIT License](http://opensource.org/licenses/MIT)
53+
* [The MIT License](http://opensource.org/licenses/MIT)
4554

4655

4756
## [wrapt](https://pypi.org/project/wrapt)
@@ -51,5 +60,5 @@ All rights reserved.
5160

5261
Distributed under the following license(s):
5362

54-
* [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause)
63+
* [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause)
5564

newrelic/common/package_version_utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def _get_package_version(name):
7373
for attr in VERSION_ATTRS:
7474
try:
7575
version = getattr(module, attr, None)
76+
# In certain cases like importlib_metadata.version, version is a callable
77+
# function.
78+
if callable(version):
79+
continue
7680
# Cast any version specified as a list into a tuple.
7781
version = tuple(version) if isinstance(version, list) else version
7882
if version not in NULL_VERSIONS:
@@ -95,4 +99,4 @@ def _get_package_version(name):
9599
if version not in NULL_VERSIONS:
96100
return version
97101
except Exception:
98-
pass
102+
pass

newrelic/core/environment.py

+37-14
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
1818
"""
1919

20+
import logging
2021
import os
2122
import platform
2223
import sys
23-
import sysconfig
2424

2525
import newrelic
2626
from newrelic.common.package_version_utils import get_package_version
@@ -29,12 +29,15 @@
2929
physical_processor_count,
3030
total_physical_memory,
3131
)
32+
from newrelic.packages.isort import stdlibs as isort_stdlibs
3233

3334
try:
3435
import newrelic.core._thread_utilization
3536
except ImportError:
3637
pass
3738

39+
_logger = logging.getLogger(__name__)
40+
3841

3942
def environment_settings():
4043
"""Returns an array of arrays of environment settings"""
@@ -195,8 +198,7 @@ def environment_settings():
195198
env.extend(dispatcher)
196199

197200
# Module information.
198-
purelib = sysconfig.get_path("purelib")
199-
platlib = sysconfig.get_path("platlib")
201+
stdlib_builtin_module_names = _get_stdlib_builtin_module_names()
200202

201203
plugins = []
202204

@@ -208,29 +210,50 @@ def environment_settings():
208210
# list
209211
for name, module in sys.modules.copy().items():
210212
# Exclude lib.sub_paths as independent modules except for newrelic.hooks.
211-
if "." in name and not name.startswith("newrelic.hooks."):
213+
nr_hook = name.startswith("newrelic.hooks.")
214+
if "." in name and not nr_hook or name.startswith("_"):
212215
continue
216+
213217
# If the module isn't actually loaded (such as failed relative imports
214218
# in Python 2.7), the module will be None and should not be reported.
215219
if not module:
216220
continue
221+
217222
# Exclude standard library/built-in modules.
218-
# Third-party modules can be installed in either purelib or platlib directories.
219-
# See https://docs.python.org/3/library/sysconfig.html#installation-paths.
220-
if (
221-
not hasattr(module, "__file__")
222-
or not module.__file__
223-
or not module.__file__.startswith(purelib)
224-
or not module.__file__.startswith(platlib)
225-
):
223+
if name in stdlib_builtin_module_names:
226224
continue
227225

228226
try:
229227
version = get_package_version(name)
230-
plugins.append("%s (%s)" % (name, version))
231228
except Exception:
232-
plugins.append(name)
229+
version = None
230+
231+
# If it has no version it's likely not a real package so don't report it unless
232+
# it's a new relic hook.
233+
if version or nr_hook:
234+
plugins.append("%s (%s)" % (name, version))
233235

234236
env.append(("Plugin List", plugins))
235237

236238
return env
239+
240+
241+
def _get_stdlib_builtin_module_names():
242+
builtins = set(sys.builtin_module_names)
243+
# Since sys.stdlib_module_names is not available in versions of python below 3.10,
244+
# use isort's hardcoded stdlibs instead.
245+
python_version = sys.version_info[0:2]
246+
if python_version < (3,):
247+
stdlibs = isort_stdlibs.py27.stdlib
248+
elif (3, 7) <= python_version < (3, 8):
249+
stdlibs = isort_stdlibs.py37.stdlib
250+
elif python_version < (3, 9):
251+
stdlibs = isort_stdlibs.py38.stdlib
252+
elif python_version < (3, 10):
253+
stdlibs = isort_stdlibs.py39.stdlib
254+
elif python_version >= (3, 10):
255+
stdlibs = sys.stdlib_module_names
256+
else:
257+
_logger.warn("Unsupported Python version. Unable to determine stdlibs.")
258+
return builtins
259+
return builtins | stdlibs

newrelic/packages/isort/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Timothy Edmund Crosley
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

newrelic/packages/isort/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import all as _all
2+
from . import py2, py3, py27, py36, py37, py38, py39, py310, py311
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import py2, py3
2+
3+
stdlib = py2.stdlib | py3.stdlib
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import py27
2+
3+
stdlib = py27.stdlib

0 commit comments

Comments
 (0)