forked from openmaptiles/openmaptiles-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-sql
executable file
·46 lines (41 loc) · 1.52 KB
/
generate-sql
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
#!/usr/bin/env python
"""
Usage:
generate-sql <tileset> [--dir <dir>]
generate-sql --help
generate-sql --version
Options:
-d --dir=<dir> Output multiple sql files into this directory. Prints to STDOUT by default.
--help Show this screen.
--version Show version.
"""
import re
from pathlib import Path
from docopt import docopt
import openmaptiles
from openmaptiles.sql import collect_sql
if __name__ == '__main__':
args = docopt(__doc__, version=openmaptiles.__version__)
if not args['--dir']:
print(collect_sql(args['<tileset>']))
else:
run_first, parallel_sql, run_last = collect_sql(
args['<tileset>'], parallel=True)
path = Path(args['--dir'])
path.mkdir(parents=True, exist_ok=True)
parallel_dir = path / 'parallel'
parallel_dir.mkdir(exist_ok=True)
deleted_files = 0
for file in parallel_dir.glob('*.sql'):
if re.match(r'^\d+\.sql$', file.name):
file.unlink()
deleted_files += 1
else:
print(f"Unexpected file {file} is ignored")
if deleted_files:
print(f"Removed {deleted_files} existing files in dir {parallel_dir}")
(path / 'run_first.sql').write_text(run_first)
(path / 'run_last.sql').write_text(run_last)
for idx, sql in enumerate(parallel_sql):
(parallel_dir / f'{idx:03}.sql').write_text(sql)
print(f'Created {len(parallel_sql)} sql files for parallel execution at {path}')