2 # -*- coding: utf-8 -*-
4 # COPYRIGHT NOTICE STARTS HERE
6 # Copyright 2019 © Samsung Electronics Co., Ltd.
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
20 # COPYRIGHT NOTICE ENDS HERE
23 import concurrent.futures
28 from distutils.spawn import find_executable
30 progressbar.streams.wrap_stdout()
31 progressbar.streams.wrap_stderr()
34 def load_list(item_list):
36 Parse list with items to be downloaded.
37 :param item_list: File with list of items (1 line per item)
38 :return: set of items from file
40 with open(item_list, 'r') as f:
41 return {item for item in (line.strip() for line in f) if item}
44 def init_progress(items_name):
45 progress_widgets = ['Downloading {}: '.format(items_name),
46 progressbar.Bar(), ' ',
47 progressbar.Percentage(), ' ',
48 '(', progressbar.SimpleProgress(), ')']
50 progress = progressbar.ProgressBar(widgets=progress_widgets,
56 def start_progress(progress, target_count, skipping, log):
57 log_skipping(skipping, log)
58 log.info("Initializing download. Takes a while.")
60 progress.max_value = target_count
62 progress.update(len(skipping))
65 def log_skipping(skipping_iterable, logger):
66 for skipped in skipping_iterable:
67 logger.info('Skipping: {}'.format(skipped))
70 def run_concurrent(workers, progress, fn, iterable, *args):
71 with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor:
72 futures = [executor.submit(fn, item, *args) for item in iterable]
74 for future in concurrent.futures.as_completed(futures):
75 error = future.exception()
80 progress.update(progress.value +1)
84 def finish_progress(progress, error_count, log):
85 progress.finish(dirty=error_count > 0)
86 log.info('Download ended. Elapsed time {}'.format(progress.data()['time_elapsed']))
89 return find_executable(name)
91 def save_to_file(dst, content):
93 Save downloaded byte content to file
94 :param dst: path to file to save content to
95 :param content: byte content of file
97 dst_dir = os.path.dirname(dst)
98 if not os.path.exists(dst_dir):
100 with open(dst, 'wb') as dst_file:
101 dst_file.write(content)
103 def make_get_request(url):
104 req = requests.get(url)
105 req.raise_for_status()
108 def simple_check_table(target, missing):
109 table = prettytable.PrettyTable(['Name', 'Downloaded'])
110 table.align['Name'] = 'l'
111 for item in sorted(target):
112 table.add_row([item, item not in missing])