-
Notifications
You must be signed in to change notification settings - Fork 7
/
crawl_utils.py
91 lines (76 loc) · 1.96 KB
/
crawl_utils.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
import os
import os.path
import locale
import logging
import fcntl
import sys
import re
import config
LOCK = None
class ScoringException(Exception):
pass
def write_scoresd_stop_request():
f = open(config.SCORESD_STOP_REQUEST_FILE, 'w')
f.write("\n")
f.close()
def clear_scoresd_stop_request():
if os.path.exists(config.SCORESD_STOP_REQUEST_FILE):
os.unlink(config.SCORESD_STOP_REQUEST_FILE)
def scoresd_stop_requested():
return os.path.exists(config.SCORESD_STOP_REQUEST_FILE)
def unlock_handle():
fcntl.flock(LOCK, fcntl.LOCK_UN)
def lock_handle(check_only=True):
if check_only:
fcntl.flock(LOCK, fcntl.LOCK_EX | fcntl.LOCK_NB)
else:
fcntl.flock(LOCK, fcntl.LOCK_EX)
def lock_or_throw(lockfile = config.LOCKFILE):
global LOCK
LOCK = open(lockfile, 'w')
lock_handle()
def lock_or_die(lockfile = config.LOCKFILE):
global LOCK
LOCK = open(lockfile, 'w')
try:
lock_handle()
except IOError:
sys.stderr.write("%s is locked, perhaps there's someone else running?\n" %
lockfile)
sys.exit(1)
def daemonize(lockfile = config.LOCKFILE):
global LOCK
# Lock, then fork.
LOCK = open(lockfile, 'w')
try:
lock_handle()
except IOError:
sys.stderr.write(("Unable to lock %s - check if another " +
"process is running.\n")
% lockfile)
sys.exit(1)
print("Starting daemon...")
pid = os.fork()
if pid is None:
raise "Unable to fork."
if pid == 0:
# Child
os.setsid()
lock_handle(False)
else:
sys.exit(0)
def player_link(player):
return config.PLAYER_BASE_URL + "/%s.html" % (player.lower())
def banner_link(banner):
return config.IMAGE_BASE_URL + "/" + banner
def linked_text(key, link_fn, text=None):
link = link_fn(key)
if text is None:
text = key
ltext = str(text).replace('_', ' ')
if link:
return '<a href="%s">%s</a>' % (link, ltext)
else:
return ltext
def human_number(n):
return locale.format('%d', n, True)