diff options
| author | jvoisin | 2019-06-05 13:28:34 -0700 |
|---|---|---|
| committer | jvoisin | 2019-06-05 22:28:57 +0200 |
| commit | 88b95923ab72b4d3ed32438062d19d0699ed95b9 (patch) | |
| tree | 63172cfa8f55b6ed7f71619f161b644a32f7a99d | |
| parent | 13d71a256587c2eb41904480ea9a7bce8e46cd3d (diff) | |
Parallelize the cli
| -rwxr-xr-x | mat2 | 25 |
1 files changed, 18 insertions, 7 deletions
| @@ -1,12 +1,13 @@ | |||
| 1 | #!/usr/bin/env python3 | 1 | #!/usr/bin/env python3 |
| 2 | 2 | ||
| 3 | import os | 3 | import os |
| 4 | from typing import Tuple, Generator, List, Union | 4 | from typing import Tuple, List, Union, Set |
| 5 | import sys | 5 | import sys |
| 6 | import mimetypes | 6 | import mimetypes |
| 7 | import argparse | 7 | import argparse |
| 8 | import logging | 8 | import logging |
| 9 | import unicodedata | 9 | import unicodedata |
| 10 | import concurrent.futures | ||
| 10 | 11 | ||
| 11 | try: | 12 | try: |
| 12 | from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS | 13 | from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS |
| @@ -18,6 +19,7 @@ except ValueError as e: | |||
| 18 | __version__ = '0.9.0' | 19 | __version__ = '0.9.0' |
| 19 | 20 | ||
| 20 | # Make pyflakes happy | 21 | # Make pyflakes happy |
| 22 | assert Set | ||
| 21 | assert Tuple | 23 | assert Tuple |
| 22 | assert Union | 24 | assert Union |
| 23 | 25 | ||
| @@ -142,16 +144,18 @@ def show_parsers(): | |||
| 142 | print('\n'.join(sorted(formats))) | 144 | print('\n'.join(sorted(formats))) |
| 143 | 145 | ||
| 144 | 146 | ||
| 145 | def __get_files_recursively(files: List[str]) -> Generator[str, None, None]: | 147 | def __get_files_recursively(files: List[str]) -> List[str]: |
| 148 | ret = set() # type: Set[str] | ||
| 146 | for f in files: | 149 | for f in files: |
| 147 | if os.path.isdir(f): | 150 | if os.path.isdir(f): |
| 148 | for path, _, _files in os.walk(f): | 151 | for path, _, _files in os.walk(f): |
| 149 | for _f in _files: | 152 | for _f in _files: |
| 150 | fname = os.path.join(path, _f) | 153 | fname = os.path.join(path, _f) |
| 151 | if __check_file(fname): | 154 | if __check_file(fname): |
| 152 | yield fname | 155 | ret.add(fname) |
| 153 | elif __check_file(f): | 156 | elif __check_file(f): |
| 154 | yield f | 157 | ret.add(f) |
| 158 | return list(ret) | ||
| 155 | 159 | ||
| 156 | def main() -> int: | 160 | def main() -> int: |
| 157 | arg_parser = create_arg_parser() | 161 | arg_parser = create_arg_parser() |
| @@ -184,9 +188,16 @@ def main() -> int: | |||
| 184 | logging.warning('Keeping unknown member files may leak metadata in the resulting file!') | 188 | logging.warning('Keeping unknown member files may leak metadata in the resulting file!') |
| 185 | 189 | ||
| 186 | no_failure = True | 190 | no_failure = True |
| 187 | for f in __get_files_recursively(args.files): | 191 | files = __get_files_recursively(args.files) |
| 188 | if clean_meta(f, args.lightweight, policy) is False: | 192 | # We have to use Processes instead of Threads, since |
| 189 | no_failure = False | 193 | # we're using tempfile.mkdtemp, which isn't thread-safe. |
| 194 | with concurrent.futures.ProcessPoolExecutor() as executor: | ||
| 195 | futures = list() | ||
| 196 | for f in files: | ||
| 197 | future = executor.submit(clean_meta, f, args.lightweight, policy) | ||
| 198 | futures.append(future) | ||
| 199 | for future in concurrent.futures.as_completed(futures): | ||
| 200 | no_failure &= future.result() | ||
| 190 | return 0 if no_failure is True else -1 | 201 | return 0 if no_failure is True else -1 |
| 191 | 202 | ||
| 192 | 203 | ||
