2020 the huggingface team 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 orglicenseslicense2 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 utilities for working with package versions perform a runtime check of the dependency versions using the exact same syntax used by pip the installed module version comes from the sitepackages dir via importlib metadata args requirement str pip style definition e g tokenizers0 9 4 tqdm4 27 numpy hint str optional what suggestion to print in case of requirements not being met example python requireversionpandas1 1 2 requireversionnumpy1 18 5 this is important to have for whatever reason hint fnhint if hint is not none else nonversioned check if re matchrwd requirement pkg op wantver requirement none none else match re findallr ss 1 2 requirement if not match raise valueerror requirement needs to be in the pip package format e g packagea1 23 or packageb1 23 but f got requirement pkg wantfull match0 wantrange wantfull split there could be multiple requirements wanted for w in wantrange match re findallrs 1 2 w if not match raise valueerror requirement needs to be in the pip package format e g packagea1 23 or packageb1 23 f but got requirement op wantver match0 wantedop wantver if op not in ops raise valueerrorfrequirement need one of listops keys but got op special case if pkg python gotver joinstrx for x in sys versioninfo 3 for op wantver in wanted items compareversionsop gotver wantver requirement pkg hint return check if any version is installed try gotver importlib metadata versionpkg except importlib metadata packagenotfounderror raise importlib metadata packagenotfounderror fthe requirement distribution was not found and is required by this application hint check that the right version is installed if version number or a range was provided if wantver is not none for op wantver in wanted items compareversionsop gotver wantver requirement pkg hint def requireversioncorerequirement 2020 the huggingface team 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 utilities for working with package versions perform a runtime check of the dependency versions using the exact same syntax used by pip the installed module version comes from the site packages dir via importlib metadata args requirement str pip style definition e g tokenizers 0 9 4 tqdm 4 27 numpy hint str optional what suggestion to print in case of requirements not being met example python require_version pandas 1 1 2 require_version numpy 1 18 5 this is important to have for whatever reason non versioned check there could be multiple requirements special case check if any version is installed check that the right version is installed if version number or a range was provided require_version wrapper which emits a core specific hint on failure
import importlib.metadata
import operator
import re
import sys
from typing import Optional

from packaging import version


ops = {
    "<": operator.lt,
    "<=": operator.le,
    "==": operator.eq,
    "!=": operator.ne,
    ">=": operator.ge,
    ">": operator.gt,
}


def _compare_versions(op, got_ver, want_ver, requirement, pkg, hint):
    if got_ver is None or want_ver is None:
        raise ValueError(
            f"Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider"
            f" reinstalling {pkg}."
        )
    if not ops[op](version.parse(got_ver), version.parse(want_ver)):
        raise ImportError(
            f"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}"
        )


def require_version(requirement: str, hint: Optional[str] = None) -> None:
    

    hint = f"\n{hint}" if hint is not None else ""

    
    if re.match(r"^[\w_\-\d]+$", requirement):
        pkg, op, want_ver = requirement, None, None
    else:
        match = re.findall(r"^([^!=<>\s]+)([\s!=<>]{1,2}.+)", requirement)
        if not match:
            raise ValueError(
                "requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but"
                f" got {requirement}"
            )
        pkg, want_full = match[0]
        want_range = want_full.split(",")  
        wanted = {}
        for w in want_range:
            match = re.findall(r"^([\s!=<>]{1,2})(.+)", w)
            if not match:
                raise ValueError(
                    "requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,"
                    f" but got {requirement}"
                )
            op, want_ver = match[0]
            wanted[op] = want_ver
            if op not in ops:
                raise ValueError(f"{requirement}: need one of {list(ops.keys())}, but got {op}")

    
    if pkg == "python":
        got_ver = ".".join([str(x) for x in sys.version_info[:3]])
        for op, want_ver in wanted.items():
            _compare_versions(op, got_ver, want_ver, requirement, pkg, hint)
        return

    
    try:
        got_ver = importlib.metadata.version(pkg)
    except importlib.metadata.PackageNotFoundError:
        raise importlib.metadata.PackageNotFoundError(
            f"The '{requirement}' distribution was not found and is required by this application. {hint}"
        )

    
    if want_ver is not None:
        for op, want_ver in wanted.items():
            _compare_versions(op, got_ver, want_ver, requirement, pkg, hint)


def require_version_core(requirement):
    
    hint = "Try: pip install transformers -U or pip install -e '.[dev]' if you're working with git main"
    return require_version(requirement, hint)