forked from osmandapp/OsmAnd-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify-embed-resources-list.py
executable file
·192 lines (164 loc) · 9.01 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
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, workroot, rules, outputFilename):
# Open output file
try:
outputFile = open(outputFilename, "w")
except IOError:
print("Failed to open '%s' for writing" % (outputFilename))
return False
# List all files
print("Looking for files...")
filenames = []
for path, dirs, files in os.walk(workroot):
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, workroot))
print("Found %d files to test" % (len(filenames)))
# Apply each rule to each file entry
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)
outputFile.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)))
outputFile.flush()
outputFile.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))
# 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 + "/.embed-resources.stamp"):
with open(resourcesPath + "/.embed-resources.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 + "/embed-resources.list"):
shouldRegenerate = True
print("Resources list missing, will regenerate...")
if not shouldRegenerate:
if os.path.getmtime(os.path.realpath(__file__)) > os.path.getmtime(resourcesPath + "/embed-resources.list"):
shouldRegenerate = True
print("List generation script is newer than generated list, will regenerate resources list...")
if not shouldRegenerate and os.path.exists(resourcesPath + "/embed-resources.list"):
currentResourcesList = list()
with open(resourcesPath + "/embed-resources.list") 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 + "/embed-resources.list"):
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)
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'],
]
resourcesListFilename = resourcesPath + "/embed-resources.list"
generator = OsmAndCoreResourcesListGenerator()
ok = generator.generate(resourcesPath, rules, resourcesListFilename)
if not ok:
sys.exit(-1)
# Update stamp (if development build)
if currentHeadCommitHash:
with open(resourcesPath + "/.embed-resources.stamp", "w") as stampFile:
stampFile.write(currentHeadCommitHash)
sys.exit(0)