|
| 1 | +# Copyright 2020 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" |
| 15 | +Utilities for working with package versions |
| 16 | +""" |
| 17 | + |
| 18 | +import importlib.metadata |
| 19 | +import operator |
| 20 | +import re |
| 21 | +import sys |
| 22 | +from typing import Optional |
| 23 | + |
| 24 | +from packaging import version |
| 25 | + |
| 26 | + |
| 27 | +ops = { |
| 28 | + "<": operator.lt, |
| 29 | + "<=": operator.le, |
| 30 | + "==": operator.eq, |
| 31 | + "!=": operator.ne, |
| 32 | + ">=": operator.ge, |
| 33 | + ">": operator.gt, |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +def _compare_versions(op, got_ver, want_ver, requirement, pkg, hint): |
| 38 | + if got_ver is None or want_ver is None: |
| 39 | + raise ValueError( |
| 40 | + f"Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider" |
| 41 | + f" reinstalling {pkg}." |
| 42 | + ) |
| 43 | + if not ops[op](version.parse(got_ver), version.parse(want_ver)): |
| 44 | + raise ImportError( |
| 45 | + f"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}" |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def require_version(requirement: str, hint: Optional[str] = None) -> None: |
| 50 | + """ |
| 51 | + Perform a runtime check of the dependency versions, using the exact same syntax used by pip. |
| 52 | +
|
| 53 | + The installed module version comes from the *site-packages* dir via *importlib.metadata*. |
| 54 | +
|
| 55 | + Args: |
| 56 | + requirement (`str`): pip style definition, e.g., "tokenizers==0.9.4", "tqdm>=4.27", "numpy" |
| 57 | + hint (`str`, *optional*): what suggestion to print in case of requirements not being met |
| 58 | +
|
| 59 | + Example: |
| 60 | +
|
| 61 | + ```python |
| 62 | + require_version("pandas>1.1.2") |
| 63 | + require_version("numpy>1.18.5", "this is important to have for whatever reason") |
| 64 | + ```""" |
| 65 | + |
| 66 | + hint = f"\n{hint}" if hint is not None else "" |
| 67 | + |
| 68 | + # non-versioned check |
| 69 | + if re.match(r"^[\w_\-\d]+$", requirement): |
| 70 | + pkg, op, want_ver = requirement, None, None |
| 71 | + else: |
| 72 | + match = re.findall(r"^([^!=<>\s]+)([\s!=<>]{1,2}.+)", requirement) |
| 73 | + if not match: |
| 74 | + raise ValueError( |
| 75 | + "requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but" |
| 76 | + f" got {requirement}" |
| 77 | + ) |
| 78 | + pkg, want_full = match[0] |
| 79 | + want_range = want_full.split(",") # there could be multiple requirements |
| 80 | + wanted = {} |
| 81 | + for w in want_range: |
| 82 | + match = re.findall(r"^([\s!=<>]{1,2})(.+)", w) |
| 83 | + if not match: |
| 84 | + raise ValueError( |
| 85 | + "requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23," |
| 86 | + f" but got {requirement}" |
| 87 | + ) |
| 88 | + op, want_ver = match[0] |
| 89 | + wanted[op] = want_ver |
| 90 | + if op not in ops: |
| 91 | + raise ValueError(f"{requirement}: need one of {list(ops.keys())}, but got {op}") |
| 92 | + |
| 93 | + # special case |
| 94 | + if pkg == "python": |
| 95 | + got_ver = ".".join([str(x) for x in sys.version_info[:3]]) |
| 96 | + for op, want_ver in wanted.items(): |
| 97 | + _compare_versions(op, got_ver, want_ver, requirement, pkg, hint) |
| 98 | + return |
| 99 | + |
| 100 | + # check if any version is installed |
| 101 | + try: |
| 102 | + got_ver = importlib.metadata.version(pkg) |
| 103 | + except importlib.metadata.PackageNotFoundError: |
| 104 | + raise importlib.metadata.PackageNotFoundError( |
| 105 | + f"The '{requirement}' distribution was not found and is required by this application. {hint}" |
| 106 | + ) |
| 107 | + |
| 108 | + # check that the right version is installed if version number or a range was provided |
| 109 | + if want_ver is not None: |
| 110 | + for op, want_ver in wanted.items(): |
| 111 | + _compare_versions(op, got_ver, want_ver, requirement, pkg, hint) |
| 112 | + |
| 113 | + |
| 114 | +def require_version_core(requirement): |
| 115 | + """require_version wrapper which emits a core-specific hint on failure""" |
| 116 | + hint = "Try: pip install transformers -U or pip install -e '.[dev]' if you're working with git main" |
| 117 | + return require_version(requirement, hint) |
0 commit comments