|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | +# |
| 5 | +# This file is part of Ramses Composer |
| 6 | +# (see https://github.com/bmwcarit/ramses-composer-docs). |
| 7 | +# |
| 8 | +# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 9 | +# If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | +import shutil |
| 13 | +import os |
| 14 | +import sys |
| 15 | +import subprocess |
| 16 | +import re |
| 17 | + |
| 18 | + |
| 19 | +def get_git_files(path): |
| 20 | + """Get all files known to git""" |
| 21 | + cmd = ['git', 'ls-files', '-z'] |
| 22 | + files = [f.decode('utf-8') for f in subprocess.check_output(cmd, cwd=path).split(b'\0') if f] |
| 23 | + return files |
| 24 | + |
| 25 | + |
| 26 | +def filter_includes_excludes(iterable, *, includes=['.*'], excludes=[]): |
| 27 | + key_fun = (lambda e: e) |
| 28 | + include_re = re.compile('|'.join([f'(?:{i})'for i in includes])) |
| 29 | + exclude_re = re.compile('|'.join([f'(?:{e})'for e in excludes])) |
| 30 | + |
| 31 | + res = [] |
| 32 | + for e in iterable: |
| 33 | + k = key_fun(e) |
| 34 | + if includes and include_re.search(k) and not (excludes and exclude_re.search(k)): |
| 35 | + res.append(e) |
| 36 | + return res |
| 37 | + |
| 38 | + |
| 39 | +def copy_files(source_root, destination_root, files): |
| 40 | + for f in files: |
| 41 | + source_path = Path(source_root) / f |
| 42 | + destination_path = Path(destination_root) / f |
| 43 | + destination_path.parent.mkdir(parents=True, exist_ok=True) |
| 44 | + if source_path.is_symlink(): |
| 45 | + destination_path.unlink(missing_ok=True) |
| 46 | + shutil.copy(source_path, destination_path, follow_symlinks=False) |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + script_dir = Path(os.path.realpath(os.path.dirname(__file__))) |
| 51 | + root_dir = (script_dir / '..').resolve() |
| 52 | + target_dir = root_dir / 'release' |
| 53 | + target_dir.mkdir(parents=True, exist_ok=True) |
| 54 | + |
| 55 | + source = root_dir |
| 56 | + destination = target_dir |
| 57 | + |
| 58 | + tutorials = set() |
| 59 | + |
| 60 | + for root, _dirs, files in os.walk(root_dir): |
| 61 | + for file in files: |
| 62 | + if file.endswith(".rca"): |
| 63 | + tutorial_path = (Path(root) / file).parents[0] |
| 64 | + tutorials.add(tutorial_path.relative_to(root_dir)) |
| 65 | + |
| 66 | + tutorial_paths = [str(t.as_posix()) for t in tutorials] |
| 67 | + include_paths = tutorial_paths + [ |
| 68 | + '^readme.md', |
| 69 | + '^LICENSE.txt', |
| 70 | + ] |
| 71 | + |
| 72 | + exclude_docs = [str((t / 'docs').as_posix()) for t in tutorials] |
| 73 | + exclude_readmes = [str((t / 'README.md').as_posix()) for t in tutorials] |
| 74 | + exclude_extra = [ |
| 75 | + '.git*', |
| 76 | + 'ramses-composer-logo.png', |
| 77 | + ] |
| 78 | + |
| 79 | + exclude_paths = exclude_docs + exclude_readmes + exclude_extra |
| 80 | + # gather source files |
| 81 | + source_files = get_git_files(source) |
| 82 | + source_files = filter_includes_excludes(source_files, includes=include_paths, excludes=exclude_paths) |
| 83 | + |
| 84 | + # copy source -> destination |
| 85 | + copy_files(source, destination, source_files) |
| 86 | + |
| 87 | + # Create zip archive |
| 88 | + archive_name = 'tutorials' |
| 89 | + if len(sys.argv) == 2: |
| 90 | + archive_name = sys.argv[1] |
| 91 | + shutil.make_archive(archive_name, 'zip', destination) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments