forked from osmandapp/OsmAnd-resources
-
Notifications
You must be signed in to change notification settings - Fork 1
/
verify-embed-resources-list.py
executable file
·262 lines (224 loc) · 11.9 KB
/
verify-embed-resources-list.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import glob
import os
import zlib
import re
import subprocess
# =============================================================================
# =============================================================================
# =============================================================================
class OsmAndCoreResourcesListGenerator(object):
FILENAME_REGEXP = r"[A–Za–z0–9._-]*?"
# -------------------------------------------------------------------------
def __init__(self):
return
# -------------------------------------------------------------------------
def generate(self, resourcesPath, rules, listName, listExt, indexFilepath):
generalOutputFilename = resourcesPath + "/" + listName + "." + listExt
# Open general output file
try:
generalOutputFile = open(generalOutputFilename, "w")
except IOError:
print("Failed to open '%s' for writing" % (generalOutputFilename))
return False
# Open index file
try:
indexOutputFile = open(indexFilepath, "w")
except IOError:
print("Failed to open '%s' for writing" % (indexFilepath))
return False
# Open cpp index file
cppIndexFilepath = resourcesPath + "/embed-resources-cpp.index"
try:
cppIndexOutputFile = open(cppIndexFilepath, "w")
except IOError:
print("Failed to open '%s' for writing" % (cppIndexFilepath))
return False
# List all files
print("Looking for files...")
filenames = []
for path, dirs, files in os.walk(resourcesPath):
if path.startswith('.') or os.path.basename(path).startswith('.'):
print("Ignoring '%s'" % (path))
continue
print("Listing '%s' with %d files" % (path, len(files)))
for filename in files:
filepath = os.path.join(path, filename)
filenames.append(os.path.relpath(filepath, resourcesPath))
print("Found %d files to test" % (len(filenames)))
# Apply each rule to each file entry
pngLimit = 200 # max files in single list
listIndex = 0
pngCounter = 0
outputFilename = None
outputFile = None
for rule in rules:
print("Processing rule '%s' => '%s' rule:" % (rule[0], rule[1]))
processed = []
for filename in filenames:
if re.match(rule[0], filename) == None:
continue
listname = re.sub(rule[0], rule[1], filename)
processed.append(filename)
createNewFile = False
if (filename.endswith('.png')):
if (pngCounter == 0) or (pngCounter > pngLimit):
pngCounter = 1
createNewFile = True
else:
pngCounter += 1
else:
pngCounter = 0
createNewFile = True
if (createNewFile):
listIndex += 1
if (outputFile):
outputFile.flush()
outputFile.close()
outputFilename = resourcesPath + "/" + listName + "_" + str(listIndex) + "." + listExt
indexOutputFile.write("%s\n" % (outputFilename))
outputCppFilename = "/gen/EmbeddedResourcesBundle_" + str(listIndex) + ".cpp"
cppIndexOutputFile.write("%s\n" % (outputCppFilename))
# Open output file
try:
outputFile = open(outputFilename, "w")
except IOError:
print("Failed to open '%s' for writing" % (outputFilename))
return False
outputFile.write("/%s:%s\n" % (filename, listname))
generalOutputFile.write("/%s:%s\n" % (filename, listname))
print("\t '%s' => '%s'" % (filename, listname))
print("\t%d processed" % (len(processed)))
filenames[:] = [filename for filename in filenames if not filename in processed]
print("%d unmatched" % (len(filenames)))
if (outputFile):
outputFile.flush()
outputFile.close()
indexOutputFile.flush()
indexOutputFile.close()
outputCppFilename = "/gen/EmbeddedResourcesBundle_total.cpp"
cppIndexOutputFile.write("%s" % (outputCppFilename))
cppIndexOutputFile.flush()
cppIndexOutputFile.close()
generalOutputFile.flush()
generalOutputFile.close()
return True
# =============================================================================
# =============================================================================
# =============================================================================
if __name__=='__main__':
# Get root directory of entire project
rootPath = os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
print("OsmAnd root path: %s" % (rootPath))
resourcesPath = rootPath + "/resources"
print("OsmAnd resources path: %s" % (resourcesPath))
resourcesListName = "embed-resources"
resourcesListExt = "list"
resourcesListFilename = resourcesListName + "." + resourcesListExt
# Check if should regenerate. Regeneration is needed in following cases:
# - stamp (git commit hash of the HEAD) differs from current HEAD
# - '.git' directory missing
# - this script is newer than list itself
# - previous list is present and any of the file is newer or missing
shouldRegenerate = False
currentHeadCommitHash = ""
if not shouldRegenerate and not os.path.isdir(resourcesPath + "/.git"):
shouldRegenerate = True
print("Non-development build, will regenerate resources list...")
if not shouldRegenerate:
oldcwd = os.getcwd()
os.chdir(resourcesPath)
currentHeadCommitHash = "#" + subprocess.check_output(
["git", "rev-parse", "HEAD"],
universal_newlines=True).strip()
os.chdir(oldcwd)
lastProcessedCommitHash = ""
if os.path.exists(resourcesPath + "/." + resourcesListName + ".stamp"):
with open(resourcesPath + "/." + resourcesListName + ".stamp", "r") as stampFile:
lastProcessedCommitHash = stampFile.read().strip()
if currentHeadCommitHash != lastProcessedCommitHash:
shouldRegenerate = True
print("Last processed commit: " + lastProcessedCommitHash)
print("Current HEAD commit: " + currentHeadCommitHash)
print("New commits present, will regenerate resources list...")
else:
shouldRegenerate = True
print("Last processed commit: none")
print("Current HEAD commit: " + currentHeadCommitHash)
print("Will regenerate resources list...")
if not shouldRegenerate:
if not os.path.exists(resourcesPath + "/" + resourcesListFilename):
shouldRegenerate = True
print("Resources list missing, will regenerate...")
if not shouldRegenerate:
if os.path.getmtime(os.path.realpath(__file__)) > os.path.getmtime(resourcesPath + "/" + resourcesListFilename):
shouldRegenerate = True
print("List generation script is newer than generated list, will regenerate resources list...")
if not shouldRegenerate and os.path.exists(resourcesPath + "/" + resourcesListFilename):
currentResourcesList = list()
with open(resourcesPath + "/" + resourcesListFilename) as currentResourcesListFile:
currentResourcesList = currentResourcesListFile.readlines()
for resourceListLine in currentResourcesList:
resourceFileName = resourceListLine.split(':')[0]
if not os.path.exists(resourcesPath + resourceFileName):
shouldRegenerate = True
print("Missing resource file '%s', will regenerate resources list..." % (resourceFileName))
break
elif os.path.getmtime(resourcesPath + resourceFileName) > os.path.getmtime(resourcesPath + "/" + resourcesListFilename):
shouldRegenerate = True
print("Resource file '%s' is newer than list, will regenerate resources list..." % (resourceFileName))
break
if not shouldRegenerate:
print("Resources list is up-to-date")
sys.exit(0)
for f in glob.glob(resourcesPath + "/" + resourcesListName + "_*"):
os.remove(f)
rules = [
# Map styles and related:
[r'rendering_styles/default\.render\.xml', 'map/styles/default.render.xml'],
# POI
[r'poi/poi_types\.xml', 'poi/poi_types.xml'],
# Map icons (Android mdpi == 1.0 ddf):
[r'rendering_styles/style-icons/drawable-mdpi/h_((?:[^/]*?shield[^/]*?)|(?:[^/]*?osmc[^/]*?))\.png', r'[ddf=1.0]map/shields/\1.png'],
[r'rendering_styles/style-icons/drawable-mdpi/h_([^/]*?)\.png', r'[ddf=1.0]map/shaders/\1.png'],
[r'rendering_styles/style-icons/drawable-mdpi/mm_([^/]*?)\.png', r'[ddf=1.0]map/icons/\1.png'],
#[r'rendering_styles/style-icons/drawable-mdpi/mx_([^/]*?)\.png', r'[ddf=1.0]map/largeIcons/\1.png'],
# Map icons (Android hdpi == 1.5 ddf):
# [r'rendering_styles/style-icons/drawable-hdpi/h_((?:[^/]*?shield[^/]*?)|(?:[^/]*?osmc[^/]*?))\.png', r'[ddf=1.5]map/shields/\1.png'],
# [r'rendering_styles/style-icons/drawable-hdpi/h_([^/]*?)\.png', r'[ddf=1.5]map/shaders/\1.png'],
# [r'rendering_styles/style-icons/drawable-hdpi/mm_([^/]*?)\.png', r'[ddf=1.5]map/icons/\1.png'],
# [r'rendering_styles/style-icons/drawable-hdpi/mx_([^/]*?)\.png', r'[ddf=1.5]map/largeIcons/\1.png'],
# Map icons (Android xhdpi == 2.0 ddf):
# [r'rendering_styles/style-icons/drawable-xhdpi/h_((?:[^/]*?shield[^/]*?)|(?:[^/]*?osmc[^/]*?))\.png', r'[ddf=2.0]map/shields/\1.png'],
# [r'rendering_styles/style-icons/drawable-xhdpi/h_([^/]*?)\.png', r'[ddf=2.0]map/shaders/\1.png'],
# [r'rendering_styles/style-icons/drawable-xhdpi/mm_([^/]*?)\.png', r'[ddf=2.0]map/icons/\1.png'],
# [r'rendering_styles/style-icons/drawable-xhdpi/mx_([^/]*?)\.png', r'[ddf=2.0]map/largeIcons/\1.png'],
# Map icons (Android xxhdpi == 3.0 ddf):
# [r'rendering_styles/style-icons/drawable-xxhdpi/h_((?:[^/]*?shield[^/]*?)|(?:[^/]*?osmc[^/]*?))\.png', r'[ddf=3.0]map/shields/\1.png'],
# [r'rendering_styles/style-icons/drawable-xxhdpi/h_([^/]*?)\.png', r'[ddf=3.0]map/shaders/\1.png'],
# [r'rendering_styles/style-icons/drawable-xxhdpi/mm_([^/]*?)\.png', r'[ddf=3.0]map/icons/\1.png'],
# [r'rendering_styles/style-icons/drawable-xxhdpi/mx_([^/]*?)\.png', r'[ddf=3.0]map/largeIcons/\1.png'],
# Misc map resources:
[r'rendering_styles/stubs/([^/]*?)\.png', r'map/stubs/\1.png'],
[r'rendering_styles/stubs/\[([^/]*?)\]/([^/]*?)\.png', r'[\1]map/stubs/\2.png'],
# Routing:
[r'routing/routing\.xml', r'routing/routing.xml'],
# Fonts:
# [r'rendering_styles/fonts/([^/]*?)/([^/]*?)\.(ttf)', r'map/fonts/\1/\2.\3'],
[r'rendering_styles/fonts/([^/]*?)\.(ttf)', r'map/fonts/\1.\2'],
# Misc resources
[r'misc/icu4c/icudt\d+([lb])\.dat', r'misc/icu4c/icu-data-\1.dat'],
[r'misc/([^/]*?)', r'misc/\1'],
]
resourcesIndexFilepath = resourcesPath + "/" + resourcesListName + ".index"
generator = OsmAndCoreResourcesListGenerator()
ok = generator.generate(resourcesPath, rules, resourcesListName, resourcesListExt, resourcesIndexFilepath)
if not ok:
sys.exit(-1)
# Update stamp (if development build)
if currentHeadCommitHash:
with open(resourcesPath + "/." + resourcesListName + ".stamp", "w") as stampFile:
stampFile.write(currentHeadCommitHash)
sys.exit(0)