Milan Verespej | 8786632 | 2019-04-18 14:37:51 +0200 | [diff] [blame^] | 1 | #! /usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | # COPYRIGHT NOTICE STARTS HERE |
| 5 | |
| 6 | # Copyright 2019 © Samsung Electronics Co., Ltd. |
| 7 | # |
| 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 |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 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. |
| 19 | |
| 20 | # COPYRIGHT NOTICE ENDS HERE |
| 21 | |
| 22 | |
| 23 | import progressbar |
| 24 | import concurrent.futures |
| 25 | |
| 26 | progressbar.streams.wrap_stdout() |
| 27 | progressbar.streams.wrap_stderr() |
| 28 | |
| 29 | |
| 30 | def load_list(item_list): |
| 31 | """ |
| 32 | Parse list with items to be downloaded. |
| 33 | :param item_list: File with list of items (1 line per item) |
| 34 | :return: set of items from file |
| 35 | """ |
| 36 | with open(item_list, 'r') as f: |
| 37 | return {item for item in (line.strip() for line in f) if item} |
| 38 | |
| 39 | |
| 40 | def init_progress(items_name): |
| 41 | progress_widgets = ['Downloading {}: '.format(items_name), |
| 42 | progressbar.Bar(), ' ', |
| 43 | progressbar.Percentage(), ' ', |
| 44 | '(', progressbar.SimpleProgress(), ')'] |
| 45 | |
| 46 | progress = progressbar.ProgressBar(widgets=progress_widgets, |
| 47 | poll_rate=1.0, |
| 48 | redirect_stdout=True) |
| 49 | return progress |
| 50 | |
| 51 | |
| 52 | def start_progress(progress, target_count, skipping, log): |
| 53 | log_skipping(skipping, log) |
| 54 | log.info("Initializing download. Takes a while.") |
| 55 | |
| 56 | progress.max_value = target_count |
| 57 | progress.start() |
| 58 | progress.update(len(skipping)) |
| 59 | |
| 60 | |
| 61 | def log_skipping(skipping_iterable, logger): |
| 62 | for skipped in skipping_iterable: |
| 63 | logger.info('Skipping: {}'.format(skipped)) |
| 64 | |
| 65 | |
| 66 | def run_concurrent(workers, progress, fn, iterable, *args): |
| 67 | with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor: |
| 68 | futures = [executor.submit(fn, item, *args) for item in iterable] |
| 69 | error_count = 0 |
| 70 | for future in concurrent.futures.as_completed(futures): |
| 71 | error = future.exception() |
| 72 | if error: |
| 73 | error_count += 1 |
| 74 | progress.update() |
| 75 | else: |
| 76 | progress.update(progress.value +1) |
| 77 | return error_count |
| 78 | |
| 79 | |
| 80 | def finish_progress(progress, error_count, log): |
| 81 | progress.finish(dirty=error_count > 0) |
| 82 | log.info('Download ended. Elapsed time {}'.format(progress.data()['time_elapsed'])) |
| 83 | |