-
Notifications
You must be signed in to change notification settings - Fork 2
/
cyclean.py
executable file
·71 lines (63 loc) · 2.02 KB
/
cyclean.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
#! /usr/bin/env python3
import typing as tp
import sys
from pathlib import Path
import shutil
import argparse
BASE_PATH = Path(__file__).resolve().parent
SRC_PATH = BASE_PATH / 'src'
TEST_PATH = BASE_PATH / 'tests'
DEFAULT_EXTS = ['c', 'cpp', 'html']
if sys.platform == 'win32':
DEFAULT_EXTS.append('pyd')
elif sys.platform == 'darwin':
DEFAULT_EXTS.extend(['dylib', 'so'])
else:
DEFAULT_EXTS.append('so')
def get_filenames(root_dir: Path, rm_exts:tp.Iterable[str]) -> tp.Iterator[Path]:
for ext in rm_exts:
for p in root_dir.glob(f'**/*.{ext}'):
pyx_path = None
if ext in ['pyd', 'dylib', 'so']:
pyx_path = p.with_name('.'.join([p.name.split('.')[0], 'pyx']))
else:
pyx_path = p.with_suffix('.pyx')
if pyx_path is not None and pyx_path.exists():
yield p
def clean(filenames: tp.Iterable[Path]):
for fn in filenames:
if fn.is_dir():
shutil.rmtree(fn)
else:
fn.unlink()
def main():
p = argparse.ArgumentParser()
p.add_argument('-y', dest='force_yes', action='store_true', help='Disable prompt')
p.add_argument(
'--ext', dest='extensions', nargs='*',
default=DEFAULT_EXTS, choices=DEFAULT_EXTS,
)
args = p.parse_args()
build_dirs = [BASE_PATH / 'build', TEST_PATH / 'build']
print('Removing Cython build files...')
filenames = []
for p in [SRC_PATH, TEST_PATH]:
filenames.extend([fn for fn in get_filenames(p, args.extensions)])
filenames.extend([p for p in build_dirs if p.exists()])
if not len(filenames):
print('No files found')
return
print(*filenames, sep='\n')
assert len(set(filenames)) == len(filenames)
if args.force_yes:
clean(filenames)
print('complete')
else:
s = input('proceed (y/n) --> ')
if s.lower() == 'y':
clean(filenames)
print('complete')
else:
print('aborted')
if __name__ == '__main__':
main()