Christoffer Quist Adamsen | 3f7e428 | 2022-04-21 13:12:31 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
Christoffer Quist Adamsen | eb29bfe | 2023-02-21 14:50:22 +0100 | [diff] [blame] | 6 | import os |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame] | 7 | import shutil |
Christoffer Quist Adamsen | eb29bfe | 2023-02-21 14:50:22 +0100 | [diff] [blame] | 8 | import subprocess |
Christoffer Quist Adamsen | 3f7e428 | 2022-04-21 13:12:31 +0200 | [diff] [blame] | 9 | import zipfile |
| 10 | |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame] | 11 | import utils |
| 12 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 13 | |
Christoffer Quist Adamsen | 3f7e428 | 2022-04-21 13:12:31 +0200 | [diff] [blame] | 14 | def add_file_to_zip(file, destination, zip_file): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 15 | with zipfile.ZipFile(zip_file, 'a') as zip: |
| 16 | zip.write(file, destination) |
| 17 | |
Christoffer Quist Adamsen | 4c6d15d | 2022-09-08 11:30:04 +0200 | [diff] [blame] | 18 | |
| 19 | def extract_all_that_matches(zip_file, destination, predicate): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 20 | with zipfile.ZipFile(zip_file) as zip: |
| 21 | names_to_extract = [name for name in zip.namelist() if predicate(name)] |
| 22 | zip.extractall(path=destination, members=names_to_extract) |
| 23 | return names_to_extract |
| 24 | |
Christoffer Quist Adamsen | 4c6d15d | 2022-09-08 11:30:04 +0200 | [diff] [blame] | 25 | |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame] | 26 | def extract_member(zip_file, member, destination): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 27 | with zipfile.ZipFile(zip_file) as zip: |
| 28 | with utils.TempDir() as temp: |
| 29 | zip.extract(member, path=temp) |
| 30 | shutil.move(os.path.join(temp, member), destination) |
| 31 | |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame] | 32 | |
Christoffer Quist Adamsen | 4c6d15d | 2022-09-08 11:30:04 +0200 | [diff] [blame] | 33 | def get_names_that_matches(zip_file, predicate): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 34 | with zipfile.ZipFile(zip_file) as zip: |
| 35 | return [name for name in zip.namelist() if predicate(name)] |
| 36 | |
Christoffer Quist Adamsen | eb29bfe | 2023-02-21 14:50:22 +0100 | [diff] [blame] | 37 | |
| 38 | def remove_files_from_zip(files, zip_file): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 39 | assert os.path.exists(zip_file) |
| 40 | cmd = ['zip', '-d', zip_file] + files |
| 41 | subprocess.run(cmd) |