/
copy_orchid_manual_examples.py
93 lines (74 loc) · 3.38 KB
/
copy_orchid_manual_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/env python
#
# This file is part of Orchid and related technologies.
#
# Copyright (c) 2017-2024 KAPPA. All Rights Reserved.
#
# LEGAL NOTICE:
# Orchid contains trade secrets and otherwise confidential information
# owned by KAPPA. Access to and use of this information is
# strictly limited and controlled by the Company. This file may not be copied,
# distributed, or otherwise disclosed outside of the Company's facilities
# except under appropriate precautions to maintain the confidentiality hereof,
# and may not be used in any way not expressly authorized by the Company.
#
import argparse
import glob
import pathlib
import shutil
import site
import sys
from typing import Optional, Sequence
def site_packages_path() -> pathlib.Path:
candidate_site_packages = [pn for pn in site.getsitepackages() if pn.find('site-packages') != -1]
assert len(candidate_site_packages) == 1
result = pathlib.Path(candidate_site_packages[0])
return result
def copy_examples_to(target_dir: str, overwrite: bool) -> None:
"""
Copy the Orchid Python API manual examples into `target_dir`.
Args:
target_dir: The target for the examples.
overwrite: Flag: true if script will overwrite existing files with the same name in the target_dir.
"""
def example_candidates(filename_glob):
manual_dirname = pathlib.Path('examples').joinpath('manual')
example_glob = str(site_packages_path().joinpath('orchid_python_api',
manual_dirname,
filename_glob))
return glob.glob(example_glob), example_glob
example_notebook_candidates, example_notebook_glob = example_candidates('*.ipynb')
if not example_notebook_candidates:
print(f'No example notebooks matching "{example_notebook_glob}"')
return
example_script_candidates, example_script_glob = example_candidates('*.py')
if not example_script_candidates:
print(f'No example scripts matching "{example_script_glob}"')
return
candidates = example_notebook_candidates + example_script_candidates
for src in candidates:
target_path = pathlib.Path(target_dir).joinpath(pathlib.Path(src).name)
duplicate_in_target = target_path.exists()
if duplicate_in_target and not overwrite:
print(f'Skipping "{src}". Already exists.')
else:
shutil.copy2(src, target_dir)
print(f'Copied "{src}" to "{target_dir}"')
def main(cli_args: Optional[Sequence[str]] = None):
"""
Entry point for copy Orchid examples utility.
Args:
cli_args: The command line arguments.
"""
cli_args = cli_args if cli_args else sys.argv[1:]
parser = argparse.ArgumentParser(
description='Copy Orchid Python API manual examples from installed package to specified directory')
parser.add_argument('-t', '--target-dir', default='.',
help='Directory into which to copy the Orchid Python API manual examples '
'(default: current directory)')
parser.add_argument('-o', '--overwrite', action='store_true', default=False,
help='Overwrite existing files in target directory',)
args = parser.parse_args(cli_args)
copy_examples_to(args.target_dir, args.overwrite)
if __name__ == '__main__':
main(sys.argv[1:])