File size: 2,090 Bytes
17dfad1
5c7ac99
17dfad1
 
390d3bd
5c7ac99
 
17dfad1
de6938d
 
 
17dfad1
5c7ac99
17dfad1
 
 
5c7ac99
de6938d
 
 
390d3bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from pathlib import Path

from datasets.utils.py_utils import get_imports

from .deprecation_utils import compare_versions
from .file_utils import get_all_files_in_dir


def get_missing_imports(file, exclude=None):
    if exclude is None:
        exclude = []
    src_dir = Path(__file__).parent
    python_files = get_all_files_in_dir(src_dir, file_extension=".py")
    # get only the file without the path and extension
    required_modules = [Path(p).stem for p in python_files]
    imports = get_imports(file)
    imported_modules = [i[1] for i in imports if i[0] == "internal"]
    return [
        i for i in required_modules if i not in imported_modules and i not in exclude
    ]


class UnitxtVersionsConflictError(ValueError):
    def __init__(self, error_in: str, hf_unitxt_version, installed_unitxt_version):
        assert hf_unitxt_version != installed_unitxt_version
        if compare_versions(hf_unitxt_version, installed_unitxt_version) == 1:
            msg = f"Located installed unitxt version {installed_unitxt_version} that is older then unitxt {error_in} version {hf_unitxt_version}. Please update unitxt package or uninstall it to avoid conflicts."
        if compare_versions(hf_unitxt_version, installed_unitxt_version) == -1:
            msg = f"Located installed unitxt version {installed_unitxt_version} that is newer then unitxt {error_in} version {hf_unitxt_version}. Please force-reload the {error_in} or downgrade unitxt to {error_in} version or uninstall unitxt to avoid conflicts."
        super().__init__(msg)


def get_installed_version():
    from unitxt.settings_utils import get_constants as installed_get_constants

    return installed_get_constants().version


def verify_versions_compatibility(hf_asset_type, hf_asset_version):
    _verify_versions(hf_asset_type, get_installed_version(), hf_asset_version)


def _verify_versions(hf_asset_type, installed_version, hf_asset_version):
    if installed_version != hf_asset_version:
        raise UnitxtVersionsConflictError(
            hf_asset_type, hf_asset_version, installed_version
        )