|
|
|
@@ -24,7 +24,7 @@
|
|
24
|
24
|
python manage.py backfill_lottery_names --parse-only /tmp/kodotamron.logit.log
|
|
25
|
25
|
"""
|
|
26
|
26
|
|
|
27
|
|
-from __future__ import print_function
|
|
|
27
|
+from __future__ import print_function, unicode_literals
|
|
28
|
28
|
|
|
29
|
29
|
import ast
|
|
30
|
30
|
import glob
|
|
|
|
@@ -145,6 +145,22 @@ def collect(log_glob):
|
|
145
|
145
|
return found, len(files), scanned
|
|
146
|
146
|
|
|
147
|
147
|
|
|
|
148
|
+PY2 = sys.version_info[0] == 2
|
|
|
149
|
+
|
|
|
150
|
+
|
|
|
151
|
+def emit(stream, text):
|
|
|
152
|
+ """
|
|
|
153
|
+ 统一输出。
|
|
|
154
|
+
|
|
|
155
|
+ py2 的 sys.stdout 吃字节串,直接写 unicode 会按 ascii 编码报错;
|
|
|
156
|
+ py3 的 sys.stdout 吃 str,写 bytes 又会 TypeError。这里按版本区分。
|
|
|
157
|
+ """
|
|
|
158
|
+ if PY2:
|
|
|
159
|
+ stream.write(text.encode('utf-8'))
|
|
|
160
|
+ else:
|
|
|
161
|
+ stream.write(text)
|
|
|
162
|
+
|
|
|
163
|
+
|
|
148
|
164
|
def setup_stdio_encoding():
|
|
149
|
165
|
"""服务器 locale 可能是 POSIX,stdout 编码为 ascii,输出中文会直接抛异常"""
|
|
150
|
166
|
for stream in ('stdout', 'stderr'):
|
|
|
|
@@ -181,11 +197,11 @@ class Command(BaseCommand):
|
|
181
|
197
|
|
|
182
|
198
|
found, file_count, scanned = collect(log_glob)
|
|
183
|
199
|
|
|
184
|
|
- self.stdout.write('日志文件: %d 个,解析到 join 请求: %d 条,可回填组合: %d 个' % (
|
|
|
200
|
+ emit(self.stdout, '日志文件: %d 个,解析到 join 请求: %d 条,可回填组合: %d 个' % (
|
|
185
|
201
|
file_count, scanned, len(found)))
|
|
186
|
202
|
|
|
187
|
203
|
if not file_count:
|
|
188
|
|
- self.stdout.write(self.style.WARNING('没有匹配到日志文件:%s' % log_glob))
|
|
|
204
|
+ emit(self.stdout, self.style.WARNING('没有匹配到日志文件:%s' % log_glob))
|
|
189
|
205
|
|
|
190
|
206
|
entries = LotteryEntryInfo.objects.filter(status=True)
|
|
191
|
207
|
|
|
|
|
@@ -195,7 +211,7 @@ class Command(BaseCommand):
|
|
195
|
211
|
missing = [entry for entry in entries.only('id', 'campaign_id', 'serial_no', 'name', 'phone')
|
|
196
|
212
|
if not (entry.name or '').strip()]
|
|
197
|
213
|
|
|
198
|
|
- self.stdout.write('待检查名额: %d 条,其中姓名为空: %d 条' % (entries.count(), len(missing)))
|
|
|
214
|
+ emit(self.stdout, '待检查名额: %d 条,其中姓名为空: %d 条' % (entries.count(), len(missing)))
|
|
199
|
215
|
|
|
200
|
216
|
todo = []
|
|
201
|
217
|
|
|
|
|
@@ -207,12 +223,12 @@ class Command(BaseCommand):
|
|
207
|
223
|
|
|
208
|
224
|
todo.append((entry, record))
|
|
209
|
225
|
|
|
210
|
|
- self.stdout.write('日志里能匹配到且带姓名的: %d 条' % len(todo))
|
|
211
|
|
- self.stdout.write('补不上的: %d 条(对应日志已被清理)' % (len(missing) - len(todo)))
|
|
|
226
|
+ emit(self.stdout, '日志里能匹配到且带姓名的: %d 条' % len(todo))
|
|
|
227
|
+ emit(self.stdout, '补不上的: %d 条(对应日志已被清理)' % (len(missing) - len(todo)))
|
|
212
|
228
|
|
|
213
|
229
|
for entry, record in todo[:10]:
|
|
214
|
230
|
will_fill_phone = record['phone'] and not (entry.phone or '').strip()
|
|
215
|
|
- self.stdout.write(' %s %s → %s%s' % (
|
|
|
231
|
+ emit(self.stdout, ' %s %s → %s%s' % (
|
|
216
|
232
|
entry.campaign_id[:8],
|
|
217
|
233
|
entry.serial_no,
|
|
218
|
234
|
record['name'],
|
|
|
|
@@ -220,10 +236,10 @@ class Command(BaseCommand):
|
|
220
|
236
|
))
|
|
221
|
237
|
|
|
222
|
238
|
if len(todo) > 10:
|
|
223
|
|
- self.stdout.write(' ... 其余 %d 条省略' % (len(todo) - 10))
|
|
|
239
|
+ emit(self.stdout, ' ... 其余 %d 条省略' % (len(todo) - 10))
|
|
224
|
240
|
|
|
225
|
241
|
if options['dry_run']:
|
|
226
|
|
- self.stdout.write(self.style.SUCCESS('\n[dry-run] 未写库。确认后加 --apply 执行。'))
|
|
|
242
|
+ emit(self.stdout, self.style.SUCCESS('\n[dry-run] 未写库。确认后加 --apply 执行。'))
|
|
227
|
243
|
return
|
|
228
|
244
|
|
|
229
|
245
|
updated = 0
|
|
|
|
@@ -244,7 +260,7 @@ class Command(BaseCommand):
|
|
244
|
260
|
entry.save(update_fields=update_fields)
|
|
245
|
261
|
updated += 1
|
|
246
|
262
|
|
|
247
|
|
- self.stdout.write(self.style.SUCCESS('\n已更新 %d 条名额' % updated))
|
|
|
263
|
+ emit(self.stdout, self.style.SUCCESS('\n已更新 %d 条名额' % updated))
|
|
248
|
264
|
|
|
249
|
265
|
def handle_parse_only(self, path):
|
|
250
|
266
|
if not os.path.exists(path):
|
|
|
|
@@ -252,8 +268,8 @@ class Command(BaseCommand):
|
|
252
|
268
|
|
|
253
|
269
|
found, file_count, scanned = collect(path)
|
|
254
|
270
|
|
|
255
|
|
- self.stdout.write('文件: %d 个,join 请求: %d 条,可回填组合: %d 个' % (file_count, scanned, len(found)))
|
|
|
271
|
+ emit(self.stdout, '文件: %d 个,join 请求: %d 条,可回填组合: %d 个' % (file_count, scanned, len(found)))
|
|
256
|
272
|
|
|
257
|
273
|
for (campaign_id, serial_no), record in list(found.items())[:10]:
|
|
258
|
|
- self.stdout.write(' %s / %s → %s %s' % (
|
|
|
274
|
+ emit(self.stdout, ' %s / %s → %s %s' % (
|
|
259
|
275
|
campaign_id[:8], serial_no, record['name'] or '(空)', record['phone']))
|