-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpullucd.py
executable file
·153 lines (140 loc) · 3.97 KB
/
pullucd.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
from __future__ import print_function
import os
import re
import subprocess
import sys
import zipfile
def download(url, path):
args = ['curl', '-L', '-s', url, '-o', path]
subprocess.check_call(args)
def read_index(url):
args = ['curl', '-L', '-s', url]
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = proc.communicate()
return re.findall(r'<a href="([^"]+)">\1</a>', out.decode('utf-8'))
def read_index_recursive(url):
for name in read_index(url):
yield name
if name.endswith('/'):
for child in read_index_recursive(url + name):
yield name + child
def read_versions():
url = 'https://www.unicode.org/Public/'
for v in read_index(url):
if re.match(r'^[0-9]+([.][0-9]+)+/$', v):
yield v[0:-1]
def latest_version():
return '.'.join(str(p) for p in max(tuple(int(p) for p in v.split('.')) for v in read_versions()))
def read_ucd_index(v):
url = 'https://www.unicode.org/Public/%s/ucd/' % v
for name in read_index_recursive(url):
if name.endswith('.txt'):
yield name
def download_ucd(v, path):
url = 'https://www.unicode.org/Public/%s/ucd/' % v
for name in read_index_recursive(url):
if name == 'Unihan.zip' or name.endswith('.txt'):
basename = name.split('/')[-1]
if basename != 'ReadMe.txt':
dest = os.path.join(path, basename)
if not os.path.exists(dest):
print('Downloading version %s of %s...' % (v, basename))
download(url + name, dest)
unihan = os.path.join(path, 'Unihan.zip')
if os.path.exists(unihan):
with zipfile.ZipFile(unihan) as zip:
for info in zip.infolist():
if info.filename.endswith('.txt'):
dest = os.path.join(path, info.filename)
if not os.path.exists(dest):
print('Extracting version %s of %s...' % (v, info.filename))
zip.extract(info, path)
def download_ucd_all(path):
for v in read_versions():
dest = os.path.join(path, v)
if not os.path.exists(dest):
os.mkdir(dest)
download_ucd(v, dest)
def main(args):
def printHelp():
print()
print('pullucd - Download UCD data files from unicode.org.')
print()
print(' -d <path> Specify destination directory.')
print(' -v <maj>.<min>.<pt> Download version by number.')
print(' --latest Download latest version.')
print(' --all Download all versions.')
print(' --version Print latest version number.')
print(' --versions Print all version numbers.')
print()
if not args:
printHelp()
return
parsingOptions = True
multiple = False
versions = []
path = 'ucd'
argi = 0
while argi < len(args):
arg = args[argi]
argi += 1
if parsingOptions and arg.startswith('-'):
if arg == '--':
parsingOptions = False
elif arg == '-d' and argi < len(args):
path = args[argi]
argi += 1
elif arg == '-v' and argi < len(args):
if versions:
multiple = True
if args[argi] not in versions:
versions.append(args[argi])
argi += 1
elif arg == '--latest':
if versions:
multiple = True
v = latest_version()
if v not in versions:
versions.append(v)
elif arg == '--all':
multiple = True
for v in read_versions():
if v not in versions:
versions.append(v)
elif arg == '--multiple':
multiple = True
elif arg == '--version':
print(latest_version())
return
elif arg == '--versions':
for v in read_versions():
print(v)
return
elif arg == '--help':
printHelp()
return
else:
print('Unknown option: %s' % arg)
return
elif re.match(r'^[0-9]+([.][0-9]+)+$', arg):
if versions:
multiple = True
if arg not in versions:
versions.append(arg)
else:
path = arg
if not os.path.exists(path):
os.mkdir(path)
if len(versions) == 0:
versions.append(latest_version())
for v in versions:
if multiple:
dest = os.path.join(path, v)
if not os.path.exists(dest):
os.mkdir(dest)
download_ucd(v, dest)
else:
download_ucd(v, path)
if __name__ == '__main__':
main(sys.argv[1:])