Skip to content

ayon_common

calculate_file_checksum(filepath, checksum_algorithm, chunk_size=10000)

Calculate file checksum for given algorithm.

Parameters:

Name Type Description Default
filepath str

Path to a file.

required
checksum_algorithm str

Algorithm to use. ('md5', 'sha1', 'sha256')

required
chunk_size Optional[int]

Chunk size to read file. Defaults to 10000.

10000

Returns:

Name Type Description
str

Calculated checksum.

Raises:

Type Description
ValueError

File not found or unknown checksum algorithm.

Source code in common/ayon_common/utils.py
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
def calculate_file_checksum(
    filepath: str, checksum_algorithm: str, chunk_size: Optional[int]=10000
):
    """Calculate file checksum for given algorithm.

    Args:
        filepath (str): Path to a file.
        checksum_algorithm (str): Algorithm to use. ('md5', 'sha1', 'sha256')
        chunk_size (Optional[int]): Chunk size to read file.
            Defaults to 10000.

    Returns:
        str: Calculated checksum.

    Raises:
        ValueError: File not found or unknown checksum algorithm.

    """
    import hashlib

    if not filepath:
        raise ValueError("Filepath is empty.")

    if not os.path.exists(filepath):
        raise ValueError("{} doesn't exist.".format(filepath))

    if not os.path.isfile(filepath):
        raise ValueError("{} is not a file.".format(filepath))

    func = getattr(hashlib, checksum_algorithm, None)
    if func is None:
        raise ValueError(
            "Unknown checksum algorithm '{}'".format(checksum_algorithm))

    hash_obj = func()
    with open(filepath, "rb") as f:
        for chunk in iter(lambda: f.read(chunk_size), b""):
            hash_obj.update(chunk)
    return hash_obj.hexdigest()

extract_archive_file(archive_file, dst_folder=None)

Extract archived file to a directory.

Parameters:

Name Type Description Default
archive_file str

Path to a archive file.

required
dst_folder Optional[str]

Directory where content will be extracted. By default, same folder where archive file is.

None
Source code in common/ayon_common/utils.py
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
def extract_archive_file(archive_file: str, dst_folder: Optional[str] = None):
    """Extract archived file to a directory.

    Args:
        archive_file (str): Path to a archive file.
        dst_folder (Optional[str]): Directory where content will be extracted.
            By default, same folder where archive file is.

    """
    if not dst_folder:
        dst_folder = os.path.dirname(archive_file)

    archive_ext, archive_type = get_archive_ext_and_type(archive_file)

    print("Extracting {} -> {}".format(archive_file, dst_folder))
    if archive_type is None:
        _, ext = os.path.splitext(archive_file)
        raise ValueError((
            f"Invalid file extension \"{ext}\"."
            f" Expected {', '.join(IMPLEMENTED_ARCHIVE_FORMATS)}"
        ))

    if archive_type == "zip":
        zip_file = ZipFileLongPaths(archive_file)
        try:
            zip_file.extractall(dst_folder)
        finally:
            zip_file.close()

    elif archive_type == "tar":
        if archive_ext == ".tar":
            tar_type = "r:"
        elif archive_ext.endswith(".xz"):
            tar_type = "r:xz"
        elif archive_ext.endswith(".gz"):
            tar_type = "r:gz"
        elif archive_ext.endswith(".bz2"):
            tar_type = "r:bz2"
        else:
            tar_type = "r:*"

        try:
            tar_file = tarfile.open(archive_file, tar_type)
        except tarfile.ReadError:
            raise SystemExit("corrupted archive")

        try:
            tar_file.extractall(dst_folder)
        finally:
            tar_file.close()

get_archive_ext_and_type(archive_file)

Get archive extension and type.

Parameters:

Name Type Description Default
archive_file str

Path to archive file.

required

Returns:

Type Description
Tuple[Optional[str], Optional[str]]

Tuple[str, str]: Archive extension and type.

Source code in common/ayon_common/utils.py
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
def get_archive_ext_and_type(
    archive_file: str
) -> Tuple[Optional[str], Optional[str]]:
    """Get archive extension and type.

    Args:
        archive_file (str): Path to archive file.

    Returns:
        Tuple[str, str]: Archive extension and type.

    """
    tmp_name = archive_file.lower()
    if tmp_name.endswith(".zip"):
        return ".zip", "zip"

    for ext in (
        ".tar",
        ".tgz",
        ".tar.gz",
        ".tar.xz",
        ".tar.bz2",
    ):
        if tmp_name.endswith(ext):
            return ext, "tar"

    return None, None

get_ayon_appdirs(*args)

Local app data directory of AYON launcher.

Deprecated

The function was replaced with 'get_launcher_local_dir' or 'get_launcher_storage_dir' based on usage. Deprecated since 1.1.0 .

Parameters:

Name Type Description Default
*args Iterable[str]

Subdirectories/files in local app data dir.

()

Returns:

Name Type Description
str

Path to directory/file in local app data dir.

Source code in common/ayon_common/utils.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def get_ayon_appdirs(*args):
    """Local app data directory of AYON launcher.

    Deprecated:
        The function was replaced with 'get_launcher_local_dir'
            or 'get_launcher_storage_dir' based on usage.
        Deprecated since 1.1.0 .

    Args:
        *args (Iterable[str]): Subdirectories/files in local app data dir.

    Returns:
        str: Path to directory/file in local app data dir.

    """
    warnings.warn(
        (
            "Function 'get_ayon_appdirs' is deprecated. Should be replaced"
            " with 'get_launcher_local_dir' or 'get_launcher_storage_dir'"
            " based on use-case."
        ),
        DeprecationWarning
    )
    return _get_ayon_appdirs(*args)

get_ayon_launch_args(*args)

Launch arguments that can be used to launch ayon process.

Parameters:

Name Type Description Default
*args str

Additional arguments.

()

Returns:

Type Description
List[str]

list[str]: Launch arguments.

Source code in common/ayon_common/utils.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def get_ayon_launch_args(*args: str) -> List[str]:
    """Launch arguments that can be used to launch ayon process.

    Args:
        *args (str): Additional arguments.

    Returns:
        list[str]: Launch arguments.

    """
    output = [sys.executable]
    if not IS_BUILT_APPLICATION:
        output.append(os.path.join(os.environ["AYON_ROOT"], "start.py"))
    output.extend(args)
    return output

get_downloads_dir()

Downloads directory path.

Each platform may use different approach how the downloads directory is received. This function will try to find the directory and return it.

Returns:

Type Description
str

Union[str, None]: Path to downloads directory or None if not found.

Source code in common/ayon_common/utils.py
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
def get_downloads_dir() -> str:
    """Downloads directory path.

    Each platform may use different approach how the downloads directory is
    received. This function will try to find the directory and return it.

    Returns:
        Union[str, None]: Path to downloads directory or None if not found.

    """
    if _Cache.downloads_dir != 0:
        return _Cache.downloads_dir

    path = None
    try:
        platform_name = platform.system().lower()
        if platform_name == "linux":
            path = _get_linux_downloads_dir()
        elif platform_name == "windows":
            path = _get_windows_downloads_dir()
        elif platform_name == "darwin":
            path = _get_macos_downloads_dir()

    except Exception:
        pass

    if path is None:
        default = os.path.expanduser("~/Downloads")
        if os.path.exists(default):
            path = default

    _Cache.downloads_dir = path
    return path

get_launcher_local_dir(*subdirs, create=False)

Get local directory for launcher.

Local directory is used for storing machine or user specific data.

The location is user specific.

Note

This function should be called at least once on bootstrap.

Parameters:

Name Type Description Default
*subdirs str

Subdirectories relative to local dir.

()
create Optional[bool]

Create the folder if it does not exist.

False

Returns:

Name Type Description
str str

Path to local directory.

Source code in common/ayon_common/utils.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def get_launcher_local_dir(
    *subdirs: str,
    create: Optional[bool] = False
) -> str:
    """Get local directory for launcher.

    Local directory is used for storing machine or user specific data.

    The location is user specific.

    Note:
        This function should be called at least once on bootstrap.

    Args:
        *subdirs (str): Subdirectories relative to local dir.
        create (Optional[bool]): Create the folder if it does not exist.

    Returns:
        str: Path to local directory.

    """
    storage_dir = os.getenv("AYON_LAUNCHER_LOCAL_DIR")
    if not storage_dir:
        storage_dir = _get_ayon_appdirs()
        os.environ["AYON_LAUNCHER_LOCAL_DIR"] = storage_dir

    path = os.path.join(storage_dir, *subdirs)
    if create:
        os.makedirs(path, exist_ok=True)
    return path

get_launcher_storage_dir(*subdirs, create=False)

Get storage directory for launcher.

Storage directory is used for storing shims, addons, dependencies, etc.

It is not recommended, but the location can be shared across multiple machines.

Note

This function should be called at least once on bootstrap.

Parameters:

Name Type Description Default
*subdirs str

Subdirectories relative to storage dir.

()
create Optional[bool]

Create the folder if it does not exist.

False

Returns:

Name Type Description
str str

Path to storage directory.

Source code in common/ayon_common/utils.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def get_launcher_storage_dir(
    *subdirs: str,
    create: Optional[bool] = False
) -> str:
    """Get storage directory for launcher.

    Storage directory is used for storing shims, addons, dependencies, etc.

    It is not recommended, but the location can be shared across
        multiple machines.

    Note:
        This function should be called at least once on bootstrap.

    Args:
        *subdirs (str): Subdirectories relative to storage dir.
        create (Optional[bool]): Create the folder if it does not exist.

    Returns:
        str: Path to storage directory.

    """
    storage_dir = os.getenv("AYON_LAUNCHER_STORAGE_DIR")
    if not storage_dir:
        storage_dir = _get_ayon_appdirs()
        os.environ["AYON_LAUNCHER_STORAGE_DIR"] = storage_dir

    path = os.path.join(storage_dir, *subdirs)
    if create and not os.path.exists(path):
        os.makedirs(path, exist_ok=True)
    return path

get_local_site_id()

Get local site identifier.

Site id is created if does not exist yet.

Returns:

Name Type Description
str str

Site id.

Source code in common/ayon_common/utils.py
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def get_local_site_id() -> str:
    """Get local site identifier.

    Site id is created if does not exist yet.

    Returns:
        str: Site id.

    """
    # used for background syncing
    site_id = os.environ.get(SITE_ID_ENV_KEY)
    if site_id:
        return site_id

    site_id_path = get_launcher_local_dir("site_id")
    if os.path.exists(site_id_path):
        with open(site_id_path, "r") as stream:
            site_id = stream.read()

    if not site_id:
        os.makedirs(os.path.dirname(site_id_path), exist_ok=True)
        site_id = _create_local_site_id()
        with open(site_id_path, "w") as stream:
            stream.write(site_id)
    return site_id

is_dev_mode_enabled()

Check if dev is enabled.

A dev bundle is used when dev is enabled.

Returns:

Name Type Description
bool bool

Dev is enabled.

Source code in common/ayon_common/utils.py
160
161
162
163
164
165
166
167
168
169
def is_dev_mode_enabled() -> bool:
    """Check if dev is enabled.

    A dev bundle is used when dev is enabled.

    Returns:
        bool: Dev is enabled.

    """
    return os.getenv("AYON_USE_DEV") == "1"

is_staging_enabled()

Check if staging is enabled.

Returns:

Name Type Description
bool bool

True if staging is enabled.

Source code in common/ayon_common/utils.py
150
151
152
153
154
155
156
157
def is_staging_enabled() -> bool:
    """Check if staging is enabled.

    Returns:
        bool: True if staging is enabled.

    """
    return os.getenv("AYON_USE_STAGING") == "1"

validate_file_checksum(filepath, checksum, checksum_algorithm)

Validate file checksum.

Parameters:

Name Type Description Default
filepath str

Path to file.

required
checksum str

Hash of file.

required
checksum_algorithm str

Type of checksum.

required

Returns:

Name Type Description
bool bool

Hash is valid/invalid.

Raises:

Type Description
ValueError

File not found or unknown checksum algorithm.

Source code in common/ayon_common/utils.py
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
def validate_file_checksum(
    filepath: str, checksum: str, checksum_algorithm: str
) -> bool:
    """Validate file checksum.

    Args:
        filepath (str): Path to file.
        checksum (str): Hash of file.
        checksum_algorithm (str): Type of checksum.

    Returns:
        bool: Hash is valid/invalid.

    Raises:
        ValueError: File not found or unknown checksum algorithm.

    """
    return checksum == calculate_file_checksum(filepath, checksum_algorithm)