-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_tools.py
139 lines (114 loc) · 4 KB
/
get_tools.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import http.client
import re
import tarfile
import configparser
import urllib.request
import shutil
# WORK_DIR = os.path.expanduser('~') + "/map_build/"
def info(msg):
print("II: " + msg)
config = configparser.ConfigParser()
# config writer
def write_config():
with open('pygmap3.cfg', 'w') as configfile:
config.write(configfile)
def list_test_version():
config.read('pygmap3.cfg')
print()
for i in ['splitter', 'mkgmap']:
try:
target = http.client.HTTPSConnection("www.mkgmap.org.uk")
target.request("GET", "/download/" + i + ".html")
htmlcontent = target.getresponse()
data = htmlcontent.read()
data = data.decode('utf8')
target.close()
except http.client.NotConnected:
print()
print(" can't connect to " + target)
print()
break
data = re.findall(i + r"\S*.zip", data)
list_new = []
for x in data:
x = os.path.splitext(os.path.basename(x))[0]
x = x.replace("-src", "")
x = x.replace(i + "-", "")
x = re.sub(r"\d+", "", x)
if x.endswith("-r"):
x = x[:-2]
elif x.endswith("r"):
x = x[:-1]
if x != '':
if x not in list_new:
list_new.append(x)
info("Testversions of " + i)
print()
if list_new:
for x in list_new:
print(" " + x)
else:
print(" No test versions found for " + i + "!")
print()
# get splitter and mkgmap
def get_tools():
config.read('pygmap3.cfg')
for i in ['splitter', 'mkgmap']:
if config.has_option(i, 'old_version'):
i_rev = config[i]['rev']
else:
try:
target = http.client.HTTPSConnection("www.mkgmap.org.uk")
target.request("GET", "/download/" + i + ".html")
htmlcontent = target.getresponse()
data = htmlcontent.read()
data = data.decode('utf8')
if i == "splitter":
file = r"-r\d{3}.zip"
elif i == "mkgmap":
file = r"-r\d{4}.zip"
if config.has_option(i, 'test'):
test = config[i]['test']
pattern = re.compile(i + "-" + test + file)
else:
pattern = re.compile(i + file)
target.close()
except http.client.NotConnected:
print()
print(" can't connect to " + target)
print()
break
i_rev = sorted(pattern.findall(data), reverse=True)[0]
i_rev = os.path.splitext(os.path.basename(i_rev))[0]
if not os.path.exists(i_rev):
if os.path.isfile(i_rev + ".tar.gz"):
tar = tarfile.open(i_rev + ".tar.gz")
tar.extractall()
tar.close()
else:
url = "https://www.mkgmap.org.uk/download/" + i_rev + ".tar.gz"
file = i_rev + ".tar.gz"
print()
info("download " + url)
# Download the file and save it
try:
uo = urllib.request.urlopen
with uo(url) as response, open(file, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
except urllib.error.URLError:
print()
print(" can't download " + file)
print()
break
tar = tarfile.open(i_rev + ".tar.gz")
tar.extractall()
tar.close()
shutil.copy2(i_rev + ".tar.gz","MKGMAP_backups/")
if config.has_option(i, 'test'):
print()
info("using " + i_rev)
config.set(i, 'rev', i_rev)
write_config()