forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore_artifacts.py
More file actions
89 lines (72 loc) · 3.72 KB
/
restore_artifacts.py
File metadata and controls
89 lines (72 loc) · 3.72 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import argparse
import logging
import os
import shutil
import sys
import tarfile
import zipfile
from pathlib import Path, PureWindowsPath
sys.path.append(str(Path(__file__).parents[1]))
from common import artifact_utils, action_utils
from common.constants import PlatformMapping, PlatformKey
def parse_args():
parser = argparse.ArgumentParser(description='Returns a path to artifacts for a given revision on a shared drive')
artifact_utils.add_common_args(parser)
parser.add_argument('-t', '--target_dir', type=str, help='Path to a dir in a workspace to download artifacts into',
required=True)
parser.add_argument('-r', '--to_restore', type=str, required=False,
help='Comma-separated list of packages to restore, all available by default')
parser.add_argument('-u', '--unpack_archives', action='store_true', required=False,
help='Whether to unpack all artifact archives once retrieved')
args = parser.parse_args()
return args
def include_filter(include_list: set | list):
"""
Returns input for shutil.copytree ignore - to copy only files from include list
"""
def _filter(root, files: list):
if not include_list:
return []
return [f for f in files if f not in include_list and Path(root).name not in include_list]
return _filter
def main():
action_utils.init_logger()
logger = logging.getLogger(__name__)
args = parse_args()
storage_dir = args.storage_dir or PlatformMapping[PlatformKey[args.platform.upper()]].value
if args.commit_sha == 'latest_available_commit':
latest_artifacts_link = artifact_utils.get_latest_artifacts_link(storage_dir, args.storage_root,
args.branch_name, args.event_name,
args.product_name)
latest_artifacts_path = PureWindowsPath(latest_artifacts_link.read_text())
normalized_path = latest_artifacts_path.as_posix() if os.name == 'posix' else latest_artifacts_path
storage = Path(args.storage_root) / normalized_path
else:
storage = artifact_utils.get_storage_dir(storage_dir, args.commit_sha, args.storage_root, args.branch_name,
args.event_name, args.product_name)
action_utils.set_github_output("artifacts_storage_path", str(storage))
logger.info(f"Artifacts are taken from here: {storage}")
main_package_extension = 'zip' if 'windows' in storage_dir else 'tar.gz'
main_package_name = f'openvino_package.{main_package_extension}'
defaults = [main_package_name, 'manifest.yml']
to_restore = set(args.to_restore.split(',')).union(defaults) if args.to_restore else defaults
if args.to_restore == 'all':
to_restore = None
shutil.copytree(storage, args.target_dir, dirs_exist_ok=True,
ignore=include_filter(to_restore))
logger.info(f"Artifacts are copied here: {args.target_dir}")
if args.unpack_archives:
for file in Path(args.target_dir).iterdir():
logger.info(f"file: {file}")
if not file.is_file() or file.is_file() and not (tarfile.is_tarfile(file) or zipfile.is_zipfile(file)):
continue
output_dir = Path(args.target_dir) / Path(str(file).removesuffix('.tar.gz')).stem
logger.info(f"Unpacking: {file} to {output_dir}")
shutil.unpack_archive(file, output_dir)
file.unlink()
action_utils.set_github_output("artifacts_path", args.target_dir)
if __name__ == '__main__':
main()