mirror of
https://github.com/Tygs/0bin.git
synced 2023-08-10 21:13:00 +03:00
counter first version
This commit is contained in:
@ -1,8 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import fcntl
|
||||
import sys
|
||||
import hashlib
|
||||
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from utils import settings
|
||||
@ -112,6 +115,36 @@ class Paste(object):
|
||||
return cls.load_from_file(cls.get_path(uuid))
|
||||
|
||||
|
||||
|
||||
def increment_counter(self):
|
||||
"""
|
||||
Increment pastes counter
|
||||
"""
|
||||
|
||||
# simple counter incrementation
|
||||
# using lock file to prevent multi access to the file
|
||||
# could be improved.
|
||||
|
||||
|
||||
path = settings.PASTE_FILES_ROOT
|
||||
counter_file = os.path.join(path, 'counter')
|
||||
|
||||
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):
|
||||
"""
|
||||
Save the content of this paste to a file.
|
||||
|
Reference in New Issue
Block a user