Skip to content

Commit 801227f

Browse files
committed
Counter added lock compatibility
1 parent 7e4a12a commit 801227f

File tree

4 files changed

+76
-20
lines changed

4 files changed

+76
-20
lines changed

stats.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# vim: ai ts=4 sts=4 et sw=4
4+
5+
6+
"""
7+
Exctract usefull infos from web server logs
8+
"""
9+
10+
import re
11+
12+
13+
# define your web server logs path
14+
LOGS_PATH = "/var/log/nginx/access_0bin.log"
15+
16+
17+
rexp = re.compile('(\d+\.\d+\.\d+\.\d+) - - \[([^\[\]:]+):(\d+:\d+:\d+) -(\d\d\d\d\)] ("[^"]*")(\d+) (-|\d+) ("[^"]*") (".*")\s*\Z')
18+
19+
20+
f = open(LOGS_PATH, 'r')
21+
22+
for line in f:
23+
a = rexp.match(line)
24+
25+
if not a is None:
26+
# a.group(1) #IP address
27+
# a.group(2) #day/month/year
28+
# a.group(3) #time of day
29+
# a.group(4) #timezone
30+
# a.group(5) #request
31+
# a.group(6) #code 200 for success, 404 for not found, etc.
32+
# a.group(7) #bytes transferred
33+
# a.group(8) #referrer
34+
# a.group(9) #browser
35+
print a.group(8) #referrer
36+
37+
f.close()

zerobin/default_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
# Be carreful if your site have to many pastes this can hurt your hard drive performances.
5858
# Refresh counter interval. Default to every minute after a paste.
5959
DISPLAY_COUNTER = True
60-
REFRESH_COUNTER = 60 * 1
60+
REFRESH_COUNTER = 1 * 1
6161

6262
# Names/links to insert in the menu bar.
6363
# Any link with "mailto:" will be escaped to prevent spam

zerobin/paste.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# -*- coding: utf-8 -*-
22

33
import os
4-
import fcntl
5-
import sys
64
import hashlib
75

8-
96
from datetime import datetime, timedelta
107

118
from utils import settings
@@ -127,22 +124,44 @@ def increment_counter(self):
127124

128125

129126
path = settings.PASTE_FILES_ROOT
130-
counter_file = os.path.join(path, 'counter')
127+
counter_file = os.path.join(path, 'counter')
128+
lock_file = os.path.join(path, 'counter.lock')
129+
130+
if not os.path.isfile(lock_file):
131+
try:
132+
#make lock file
133+
flock = open(lock_file, "w")
134+
flock.write('lock')
135+
flock.close()
136+
137+
# init counter (first time)
138+
if not os.path.isfile(counter_file):
139+
fcounter = open(counter_file, "w")
140+
fcounter.write('1')
141+
fcounter.close()
142+
143+
# get counter value
144+
fcounter = open(counter_file, "r")
145+
counter_value = fcounter.read(50)
146+
fcounter.close()
147+
148+
try:
149+
counter_value = long(counter_value) + 1
150+
except ValueError:
151+
counter_value = 1
152+
153+
# write new value to counter
154+
fcounter = open(counter_file, "w")
155+
fcounter.write(str(counter_value))
156+
fcounter.close()
157+
158+
#remove lock file
159+
os.remove(lock_file)
160+
except (IOError, OSError):
161+
if os.path.isfile(lock_file):
162+
#remove lock file
163+
os.remove(lock_file)
131164

132-
fd = os.open(counter_file, os.O_RDWR | os.O_CREAT)
133-
fcntl.lockf(fd, fcntl.LOCK_EX)
134-
s = os.read(fd, 4096)
135-
try:
136-
n = long(float(s))
137-
except ValueError:
138-
raise ValueError(u"Couldn't read value from counter file " + counter_file + ", assuming 0")
139-
n = 0
140-
fnn = counter_file + ".new"
141-
f = open(fnn, "w")
142-
f.write(str(n + 1))
143-
f.close()
144-
os.rename(fnn, counter_file)
145-
os.close(fd)
146165

147166

148167
def save(self):

zerobin/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_pastes_count():
5959
Return the number of pastes created (must have option DISPLAY_COUNTER enabled)
6060
"""
6161
locale.setlocale(locale.LC_ALL, 'en_US')
62-
counter_path = default_settings.PASTE_FILES_ROOT
62+
counter_path = settings.PASTE_FILES_ROOT
6363
counter_file = os.path.join(counter_path, 'counter')
6464
try:
6565
with open(counter_file, "r") as f:

0 commit comments

Comments
 (0)