forked from opensearch-project/opensearch-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependency_installer.py
63 lines (52 loc) · 2.33 KB
/
dependency_installer.py
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
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
import abc
import concurrent.futures
import logging
import os
import shutil
import urllib
from typing import List, Tuple
import validators # type:ignore
from manifests.build_manifest import BuildManifest
from manifests.bundle_manifest import BundleManifest
class DependencyInstaller(abc.ABC):
root_url: str
build_manifest: BuildManifest
bundle_manifest: BundleManifest
"""
Provides a dependency installer for the test suites.
"""
def __init__(self, root_url: str, build_manifest: BuildManifest, bundle_manifest: BundleManifest) -> None:
self.root_url = root_url
self.build_manifest = build_manifest
self.bundle_manifest = bundle_manifest
def download_dist(self, dest: str) -> str:
local_path = os.path.realpath(os.path.join(dest, os.path.basename(self.bundle_manifest.build.location)))
return self.download_or_copy(self.bundle_manifest.build.location, local_path)
def __source_dest(self, path: str, category: str, dest: str) -> Tuple[str, str]:
source = "/".join([self.root_url, category, self.build_manifest.build.filename, path])
dest = os.path.realpath(os.path.join(dest, "/".join(path.split("/")[1:])))
return (source, dest)
def download(self, paths: List[str], category: str, dest: str) -> None:
logging.info(f"Downloading to {dest} ...")
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
for result in executor.map(
lambda args: self.download_or_copy(*args),
map(lambda path: self.__source_dest(path, category, dest),
paths)
):
logging.debug(f"Written {result}")
def download_or_copy(self, source: str, dest: str) -> str:
os.makedirs(os.path.dirname(dest), exist_ok=True)
if validators.url(source):
logging.info(f"Downloading {source} into {dest} ...")
urllib.request.urlretrieve(source, dest)
else:
logging.info(f"Copying {source} into {dest} ...")
source = os.path.realpath(source)
shutil.copyfile(os.path.realpath(source), dest)
return dest