Skip to content

Instantly share code, notes, and snippets.

@perillo
Last active December 8, 2023 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perillo/0e18a52f02976477d2339e42217e3382 to your computer and use it in GitHub Desktop.
Save perillo/0e18a52f02976477d2339e42217e3382 to your computer and use it in GitHub Desktop.
Detect the Python distribution specification/format
def _get_dist_spec(dist):
"""Returns the specification used by the distribution.
The result is "dist-info", "egg-info", "egg" or "distutils-legacy".
"""
def is_dir():
return dist.path is not None and os.path.isdir(dist.path)
def is_file():
return dist.path is not None and os.path.isfile(dist.path)
# Comments and detection has been adapted from
# https://github.com/pypa/pip/blob/a15dd75/src/pip/_internal/metadata/base.py#L219
spec = "unknown"
if is_dir() and dist.path.endswith(".dist-info"):
# This distribution was installed with the "modern format".
#
# This applies to installations made by setuptools
# (but through pip, not directly), or anything using the
# standardized build backend interface (PEP 517).
spec = "dist-info"
elif is_dir() and dist.path.endswith(".egg-info"):
# This distribution was installed with the .egg-info format.
#
# This usually indicates the distribution was installed with
# setuptools with an old pip version or with
# single-version-externally-managed.
spec = "egg-info"
elif dist.path.endswith(".egg"):
# This distribution was installed as an egg.
#
# This usually indicates the distribution was installed by
# (older versions of) easy_install.
#
# Deprecated, but pip still support it.
spec = "egg"
elif is_file():
# This distribution was installed with legacy distutils format.
#
# A distribution installed with "raw" distutils not patched by setuptools
# uses one single file.
#
# Deprecated, but pip still supports it.
spec = "distutils-legacy"
return spec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment