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
24 import concurrent.futures
25 from distutils.spawn import find_executable
27 progressbar.streams.wrap_stdout()
28 progressbar.streams.wrap_stderr()
31 def load_list(item_list):
33 Parse list with items to be downloaded.
34 :param item_list: File with list of items (1 line per item)
35 :return: set of items from file
37 with open(item_list, 'r') as f:
38 return {item for item in (line.strip() for line in f) if item}
41 def init_progress(items_name):
42 progress_widgets = ['Downloading {}: '.format(items_name),
43 progressbar.Bar(), ' ',
44 progressbar.Percentage(), ' ',
45 '(', progressbar.SimpleProgress(), ')']
47 progress = progressbar.ProgressBar(widgets=progress_widgets,
53 def start_progress(progress, target_count, skipping, log):
54 log_skipping(skipping, log)
55 log.info("Initializing download. Takes a while.")
57 progress.max_value = target_count
59 progress.update(len(skipping))
62 def log_skipping(skipping_iterable, logger):
63 for skipped in skipping_iterable:
64 logger.info('Skipping: {}'.format(skipped))
67 def run_concurrent(workers, progress, fn, iterable, *args):
68 with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor:
69 futures = [executor.submit(fn, item, *args) for item in iterable]
71 for future in concurrent.futures.as_completed(futures):
72 error = future.exception()
77 progress.update(progress.value +1)
81 def finish_progress(progress, error_count, log):
82 progress.finish(dirty=error_count > 0)
83 log.info('Download ended. Elapsed time {}'.format(progress.data()['time_elapsed']))
86 return find_executable(name)