-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawl_utils.py
More file actions
130 lines (105 loc) · 3.11 KB
/
crawl_utils.py
File metadata and controls
130 lines (105 loc) · 3.11 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
import os
import os.path
import logging
import fcntl
import sys
import locale
import re
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
# Scoring scripts running on greensnark's machines are in debug mode.
SNARK_USER = 'tecumseh'
DEBUG_SCORES = (SNARK_USER in os.getcwd() or
(os.getenv('PWD') and SNARK_USER in os.getenv('PWD')))
# Update every so often (seconds)
UPDATE_INTERVAL = 21 * 60
LOGFORMAT = "%(asctime)s [%(levelname)s] %(message)s"
LOCK = None
if DEBUG_SCORES:
BASEDIR = os.getenv('HOME')
else:
BASEDIR = '/home/snark'
SCORING_KEY = 'scoring'
LOCKFILE = BASEDIR + ('/%s.lock' % SCORING_KEY)
LOGFILE = (BASEDIR + '/%s.log') % SCORING_KEY
SCORE_FILE_DIR = SCORING_KEY
PLAYER_BASE = 'players'
PLAYER_FILE_DIR = SCORE_FILE_DIR + '/' + PLAYER_BASE
# Use file URLs when testing on greensnark's machines.
CAO_BASE = (DEBUG_SCORES
and 'file:///var/www/crawl'
or 'http://crawl.akrasiac.org')
CAO_SCORING_BASE = '%s/%s' % (CAO_BASE, SCORING_KEY)
CAO_IMAGE_BASE = CAO_SCORING_BASE + '/images'
CAO_PLAYER_BASE = '%s/players' % CAO_SCORING_BASE
CAO_OVERVIEW = '''<a href="%s/overview.html">Overview</a>''' % CAO_SCORING_BASE
RAWDATA_PATH = '/var/www/crawl/rawdata'
SCORESD_STOP_REQUEST_FILE = os.path.join(BASEDIR, 'scoresd.stop')
MKDIRS = [ SCORE_FILE_DIR, PLAYER_FILE_DIR ]
for d in MKDIRS:
if not os.path.exists(d):
os.makedirs(d)
def write_scoresd_stop_request():
f = open(SCORESD_STOP_REQUEST_FILE, 'w')
f.write("\n")
f.close()
def clear_scoresd_stop_request():
if os.path.exists(SCORESD_STOP_REQUEST_FILE):
os.unlink(SCORESD_STOP_REQUEST_FILE)
def scoresd_stop_requested():
return os.path.exists(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 = LOCKFILE):
global LOCK
LOCK = open(lockfile, 'w')
lock_handle()
def lock_or_die(lockfile = 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 = 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 "%s/%s.html" % (CAO_PLAYER_BASE, player.lower())
def banner_link(banner):
return CAO_IMAGE_BASE + '/' + 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)