|
9 | 9 | from os.path import normpath |
10 | 10 | from os.path import relpath |
11 | 11 |
|
| 12 | + |
| 13 | +class _Distribution(object): |
| 14 | + def __init__(self, project_name, version): |
| 15 | + self.project_name = project_name |
| 16 | + self.version = version |
| 17 | + |
| 18 | + |
12 | 19 | try: |
13 | | - from pkg_resources import working_set |
14 | | - from pkg_resources import Requirement |
15 | | - ply_dist = working_set.find(Requirement.parse('ply')) |
16 | | - # note that for **extremely** ancient versions of setuptools, e.g. |
17 | | - # setuptools<0.6c11, or some very non-standard environment that does |
18 | | - # not include the required metadata (e.g. pyinstaller without the |
19 | | - # required metadata), will require the following workaround... |
20 | | - if ply_dist is None: # pragma: no cover |
21 | | - from pkg_resources import Distribution |
22 | | - import ply |
23 | | - ply_dist = Distribution(project_name='ply', version=ply.__version__) |
| 20 | + from importlib import metadata |
| 21 | + try: |
| 22 | + ply_version = metadata.version('ply') |
| 23 | + except Exception: # pragma: no cover |
| 24 | + ply_dist = None |
| 25 | + else: |
| 26 | + ply_dist = _Distribution(project_name='ply', version=ply_version) |
24 | 27 | except ImportError: # pragma: no cover |
25 | | - ply_dist = None |
| 28 | + try: |
| 29 | + from pkg_resources import working_set |
| 30 | + from pkg_resources import Requirement |
| 31 | + ply_dist = working_set.find(Requirement.parse('ply')) |
| 32 | + # note that for **extremely** ancient versions of setuptools, e.g. |
| 33 | + # setuptools<0.6c11, or some very non-standard environment that does |
| 34 | + # not include the required metadata (e.g. pyinstaller without the |
| 35 | + # required metadata), will require the following workaround... |
| 36 | + if ply_dist: |
| 37 | + # convert to our private version class |
| 38 | + ply_dist = _Distribution( |
| 39 | + project_name='ply', |
| 40 | + version=ply_dist.version, |
| 41 | + ) |
| 42 | + else: |
| 43 | + try: |
| 44 | + import ply |
| 45 | + ply_dist = _Distribution( |
| 46 | + project_name='ply', |
| 47 | + version=ply.__version__, |
| 48 | + ) |
| 49 | + except ImportError: |
| 50 | + ply_dist = None |
| 51 | + except ImportError: # pragma: no cover |
| 52 | + ply_dist = None |
26 | 53 |
|
27 | 54 | py_major = sys.version_info.major |
28 | 55 | unicode = unicode if py_major < 3 else None # noqa: F821 |
|
0 commit comments