1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00

Counter added lock compatibility

This commit is contained in:
max 2012-05-22 02:24:39 +07:00
parent 7e4a12a263
commit 801227fde1
4 changed files with 76 additions and 20 deletions

37
stats.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
"""
Exctract usefull infos from web server logs
"""
import re
# define your web server logs path
LOGS_PATH = "/var/log/nginx/access_0bin.log"
rexp = re.compile('(\d+\.\d+\.\d+\.\d+) - - \[([^\[\]:]+):(\d+:\d+:\d+) -(\d\d\d\d\)] ("[^"]*")(\d+) (-|\d+) ("[^"]*") (".*")\s*\Z')
f = open(LOGS_PATH, 'r')
for line in f:
a = rexp.match(line)
if not a is None:
# a.group(1) #IP address
# a.group(2) #day/month/year
# a.group(3) #time of day
# a.group(4) #timezone
# a.group(5) #request
# a.group(6) #code 200 for success, 404 for not found, etc.
# a.group(7) #bytes transferred
# a.group(8) #referrer
# a.group(9) #browser
print a.group(8) #referrer
f.close()

View File

@ -57,7 +57,7 @@ GROUP = None
# Be carreful if your site have to many pastes this can hurt your hard drive performances.
# Refresh counter interval. Default to every minute after a paste.
DISPLAY_COUNTER = True
REFRESH_COUNTER = 60 * 1
REFRESH_COUNTER = 1 * 1
# Names/links to insert in the menu bar.
# Any link with "mailto:" will be escaped to prevent spam

View File

@ -1,11 +1,8 @@
# -*- coding: utf-8 -*-
import os
import fcntl
import sys
import hashlib
from datetime import datetime, timedelta
from utils import settings
@ -127,22 +124,44 @@ class Paste(object):
path = settings.PASTE_FILES_ROOT
counter_file = os.path.join(path, 'counter')
counter_file = os.path.join(path, 'counter')
lock_file = os.path.join(path, 'counter.lock')
if not os.path.isfile(lock_file):
try:
#make lock file
flock = open(lock_file, "w")
flock.write('lock')
flock.close()
# init counter (first time)
if not os.path.isfile(counter_file):
fcounter = open(counter_file, "w")
fcounter.write('1')
fcounter.close()
# get counter value
fcounter = open(counter_file, "r")
counter_value = fcounter.read(50)
fcounter.close()
try:
counter_value = long(counter_value) + 1
except ValueError:
counter_value = 1
# write new value to counter
fcounter = open(counter_file, "w")
fcounter.write(str(counter_value))
fcounter.close()
#remove lock file
os.remove(lock_file)
except (IOError, OSError):
if os.path.isfile(lock_file):
#remove lock file
os.remove(lock_file)
fd = os.open(counter_file, os.O_RDWR | os.O_CREAT)
fcntl.lockf(fd, fcntl.LOCK_EX)
s = os.read(fd, 4096)
try:
n = long(float(s))
except ValueError:
raise ValueError(u"Couldn't read value from counter file " + counter_file + ", assuming 0")
n = 0
fnn = counter_file + ".new"
f = open(fnn, "w")
f.write(str(n + 1))
f.close()
os.rename(fnn, counter_file)
os.close(fd)
def save(self):

View File

@ -59,7 +59,7 @@ def get_pastes_count():
Return the number of pastes created (must have option DISPLAY_COUNTER enabled)
"""
locale.setlocale(locale.LC_ALL, 'en_US')
counter_path = default_settings.PASTE_FILES_ROOT
counter_path = settings.PASTE_FILES_ROOT
counter_file = os.path.join(counter_path, 'counter')
try:
with open(counter_file, "r") as f: