-
Notifications
You must be signed in to change notification settings - Fork 55
/
build_kernel.py
70 lines (54 loc) · 2.08 KB
/
build_kernel.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
#!/usr/bin/env python3
# Kaggle script build system template: https://github.com/lopuhin/kaggle-script-template
import os
import base64
import gzip
from pathlib import Path
IGNORE_LIST = ["data", "build"]
PACKAGES = [
('https://github.com/lRomul/argus.git', 'v0.0.8')
]
def encode_file(path: Path) -> str:
compressed = gzip.compress(path.read_bytes(), compresslevel=9)
return base64.b64encode(compressed).decode('utf-8')
def check_ignore(path: Path, ignore_list):
if not path.is_file():
return False
for ignore in ignore_list:
if str(path).startswith(ignore):
return False
return True
def clone_package(git_url, branch="master"):
name = Path(git_url).stem
os.system('mkdir -p tmp')
os.system(f'rm -rf tmp/{name}')
os.system(f'cd tmp && git clone --depth 1 -b {branch} {git_url}')
os.system(f'cp -R tmp/{name}/{name} .')
os.system(f'rm -rf tmp/{name}')
def build_script(ignore_list, packages, template_name='kernel_template.py'):
to_encode = []
for path in Path('.').glob('**/*.py'):
if check_ignore(path, ignore_list + packages):
to_encode.append(path)
for package, branch in packages:
clone_package(package, branch)
package_name = Path(package).stem
for path in Path(package_name).glob('**/*'):
if check_ignore(path, ignore_list):
to_encode.append(path)
file_data = {str(path): encode_file(path) for path in to_encode}
print("Encoded python files:")
for path in file_data:
print(path)
template = Path(template_name).read_text('utf8')
(Path('kernel') / template_name).write_text(
template.replace('{file_data}', str(file_data)),
encoding='utf8')
if __name__ == '__main__':
os.system('rm -rf kernel && mkdir kernel')
build_script(IGNORE_LIST, PACKAGES,
template_name='kernel_template.py')
build_script(IGNORE_LIST, PACKAGES,
template_name='blend_kernel_template.py')
build_script(IGNORE_LIST, PACKAGES,
template_name='stacking_kernel_template.py')