-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathload.py
More file actions
executable file
·318 lines (289 loc) · 8.65 KB
/
Copy pathload.py
File metadata and controls
executable file
·318 lines (289 loc) · 8.65 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/python3
from lib import db
from lib import xmlline
import sys
import re
import subprocess
import requests
def string2Bool(s):
return s.lower() in ['true', '1', 't', 'y', 'yes']
def mungeBadges(data, context):
addSiteId(data, context)
data['TagBased'] = string2Bool(data['TagBased'])
def mungeTags(data, context):
addSiteId(data, context)
if 'IsModeratorOnly' in data:
data['IsModeratorOnly'] = string2Bool(data['IsModeratorOnly'])
if 'IsRequired' in data:
data['IsRequired'] = string2Bool(data['IsRequired'])
def addSiteId(data, context):
data['SiteId'] = context['site']['Id']
tables={
"sites":{
"name": "Sites"
},
"badges":{
"name": "Badges",
"munge": mungeBadges
},
"comments":{
"name": "Comments",
"munge": addSiteId
},
"posthistory":{
"name": "PostHistory",
"munge": addSiteId
},
"postlinks":{
"name": "PostLinks",
"munge": addSiteId
},
"posts":{
"name": "Posts",
"munge": addSiteId
},
"tags":{
"name": "Tags",
"munge": mungeTags
},
"users":{
"name": "Users",
"munge": addSiteId
},
"votes":{
"name": "Votes",
"munge": addSiteId
}
}
def fileNameToUrl(file):
# Extract site name from file path
basename = re.sub(r'.*/','',file)
# Handle sites.xml file
if re.match(r'sites\.', basename, re.IGNORECASE):
return 'https://stackoverflow.com' # Default main site
# Handle dba.stackexchange.com.7z format
site_match = re.match(r'^([a-zA-Z0-9-]+)\.(stackexchange\.com)(\.(xml|7z))?$', basename)
if site_match:
site_name = site_match.group(1)
return f'https://{site_name}.stackexchange.com'
# Handle standalone Badges.xml format - check if there's a corresponding site
# This assumes if we're processing a file without a site name, it should have been loaded with site context
if '.' in basename and basename.split('.')[0] not in ['sites']:
# For generic files like Badges.xml, we need to rely on the file context
# This case should be handled by the setSiteContext function
pass
# Fallback - try to extract any alphanumeric prefix
match = re.match(r'^([a-zA-Z0-9-]+)', basename)
if match:
return f'https://{match.group(1)}.stackexchange.com'
# Ultimate fallback
return 'https://stackoverflow.com'
def lastSummary(context, last):
lastSummary = "Last:"
if context["site"] and 'TinyName' in context['site']:
lastSummary = lastSummary + " " + context['site']['TinyName']
if context[last]:
lastSummary = lastSummary + " " + context[last]['table']['name']
data = context[last]['data']
if 'Id' in data:
lastSummary = lastSummary + " " + data['Id']
if 'CreationDate' in data:
lastSummary = lastSummary + " " + data['CreationDate']
elif 'Date' in data:
lastSummary = lastSummary + " " + data['Date']
return lastSummary
def logSkipped(context):
print("Skipped " + str(context['skipped']) + " records. " + lastSummary(context, "lastSkipped"))
context['skipped'] = 0
def logAndCommit(context):
print("Committing " + str(context['count']) + " records. " + lastSummary(context, "lastDone"))
db.cnx.commit()
context['count'] = 0
def processResume(context, data):
if 'Id' in data:
resume = str(data['Id'])
if context['resume'] == resume:
context['resume'] = None
if context['table']:
resume = context['table']['name'].lower() + " " + resume
if context['resume'] == resume:
context['resume'] = None
return True
def loadLine(line, context):
line = line.strip()
if (not line):
pass
elif (re.search(r'^.?<\?xml', line, re.IGNORECASE)):
pass
elif (re.match(r'^\s*<!--', line)):
pass
elif (re.match(r'^\s*-->', line)):
pass
elif (re.match(r'^\s*[a-zA-Z]+$', line)):
pass
elif (re.search(r'^\s*https?://', line)):
pass
elif (re.match(r'^\s*CC BY-SA\s*-\s*(Url:|Version:)?', line)): # Only match CC BY-SA license lines at start
pass
elif (re.search(r'Url:', line)):
pass
elif (not re.match(r'^\s*<', line)):
pass
elif (re.search(r'We also provide data for non-beta sites', line)):
pass
elif (re.search(r'These files are available for download', line)):
pass
elif (re.search(r'Data dumps are available', line)):
pass
elif (re.search(r'\.beta\.', line, re.IGNORECASE)):
pass
else:
if context['verbose']:
print(f"Processing line: {line[:50]}...")
if (re.match(r'^\s*</', line)):
if "onLoad" in context['table']:
context['table']["onLoad"]()
context['table'] = None
elif (m := re.match(r'^\s*<([a-z]+)>', line)):
# Skip HTML formatting tags
if m[1] in ['sub', 'sup', 'code', 'pre', 'em', 'strong', 'p', 'br', 'div', 'span', 'ul', 'ol', 'li']:
pass
elif m[1] in tables:
if context['verbose']:
print(f"Found table tag: {m[1]}")
context['table']=tables[m[1]]
if context['verbose']:
print(f"Set table to: {context['table']}")
else:
raise Exception ("Unknown table: " + m[1])
elif (re.match(r'^\s*<row ', line)):
if (not context['table']):
raise Exception ("Row found with no table")
data = xmlline.getAttributes(line)
if context['verbose']:
print(f"Row data: {len(data)} attributes")
if ('munge' in context['table']):
context['table']['munge'](data, context)
if (len(context['tableSet']) != 0 and context['table']['name'].lower() not in context['tableSet']):
context['skipped'] = context['skipped'] + 1
else:
db.upsert(context['table']['name'], data)
context['count'] = context['count'] + 1
if (context['count'] >= 2048):
logAndCommit(context)
elif (context['skipped'] >= 10000):
logSkipped(context)
else:
raise Exception ("Unknown line: `" + line + "`")
def setSiteContext(context, fileName):
# If we already have a site context (e.g., from processing a 7z file), reuse it
if context.get('site') and context['site'].get('Id'):
return
if (re.match(r'^(.*/)?sites\.[a-zA-Z0-9]+$', fileName, re.IGNORECASE)):
context['site'] = None
else:
context['site'] = db.querySite(fileNameToUrl(fileName))
assert context['site']['Id'], "Could not find site id for " + fileName + "(Load Sites.xml first.)"
def loadXml(context, fileName):
print("Loading XML: " + fileName)
setSiteContext(context, fileName)
fh = open(fileName, mode="r", encoding="utf-8")
line_count = 0
try:
while line := fh.readline():
line_count += 1
if line_count <= 10: # Debug first 10 lines
print(f"Line {line_count}: {line.strip()}")
try:
loadLine(line.strip(), context)
except Exception as e:
print(f"Error on line {line_count}: {e}")
print(f"Line content: {line}")
raise
finally:
fh.close()
print(f"Total lines processed: {line_count}")
def loadXmlUrl(context, url):
print("Loading XML: " + url)
setSiteContext(context, url)
req = requests.get(url, stream=True)
if req.encoding is None:
req.encoding = 'utf-8'
try:
for line in req.iter_lines(decode_unicode=True):
try:
loadLine(line.strip(), context)
except:
print(line)
raise
finally:
req.close()
def loadXml7z(context, fileName):
print("Loading zipped XML: " + fileName)
setSiteContext(context, fileName)
process = subprocess.Popen(["7z", "e", "-so", "-bd", fileName], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
try:
while line := process.stdout.readline():
try:
loadLine(line.decode('utf-8').strip(), context)
except:
print(line)
raise
finally:
process.stdout.close()
def loadXml7zUrl(context, url):
print("Loading zipped XML: " + url)
setSiteContext(context, url)
process = subprocess.Popen(["7z", "e", "-so", "-bd", url], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
try:
while line := process.stdout.readline():
try:
loadLine(line.decode('utf-8').strip(), context)
except:
print(line)
raise
finally:
process.stdout.close()
def getDefaultContext():
return {
"count": 0,
"table": None,
"site": None,
"lastDone": None,
"lastSkipped": None,
"tableSet": {},
"skipped": 0,
"resume": None
}
context = getDefaultContext()
context['verbose'] = False # Default to non-verbose mode
try:
db.createSchema()
files = []
for arg in sys.argv[1:]:
if (re.match(r'^.*\.(xml|7z)$', arg)):
files.append(arg)
elif (arg.lower() in tables):
context['tableSet'][arg.lower()] = 1
elif (re.match(r'^-v|--verbose$', arg, re.IGNORECASE)):
context['verbose'] = True
elif (re.match(r'^-q|--quiet$', arg, re.IGNORECASE)):
context['verbose'] = False
elif (re.match(r'^(([a-zA-Z]+ )?([a-zA-Z]+ )?[0-9]+)$', arg)):
context['resume'] = arg.lower()
else:
raise Exception("Unknown argument: " + arg)
for file in files:
if (re.match(r'^.*\.xml$', file)):
if (re.match(r'https?\:\/\/', file)):
loadXmlUrl(context, file)
else:
loadXml(context, file)
elif (re.match(r'^.*\.7z$', file)):
loadXml7z(context, file)
finally:
if (context['count'] > 0):
logAndCommit(context)
if (context['skipped'] > 0):
logSkipped(context)
db.cnx.close()