Example #1
0
def get_unique_id(filename, digits=4):
    counter = 1
    path = "%s/%s" % (directory, filename)
    try:
        # open for update
        file = posixfile.open(path, "r+")
        file.lock("w|", digits)
        counter = int(file.read(digits)) + 1
    except IOError:
        # create it
        try:
            file = posixfile.open(path, "w")
            file.lock("w|", digits)
        except IOError:
            print("creation of file '%s' failed" % path)
            return -1

    file.seek(0)  # rewind
    format = "%%0%dd" % digits
    file.write(format % counter)

    # Note: close releases lock
    file.close()

    return counter
Example #2
0
    def _savePersistentParameters(self):
        '''
        Writes to the parameter file, according to the ML_JOBNET_NAME environment parameter
        The OPTIONAL environment flag 'THREAD', if set, allows to distinguish several processes that run concurrently in the same jobnet
        '''
        jobname = os.getenv('ML_JOBNET_NAME') + os.getenv('THREAD', '')

        if jobname is not None:
            self._createPersistentParamsDirectory()
            paramsDirectoryBasePath = self.config.get(SECTION_DIRECTORIES, PARAMETERS_DIRECTORY)
            paramsFilePath = os.path.join(paramsDirectoryBasePath, jobname.lower()+".prm")
            paramsFile = posixfile.open(paramsFilePath, 'a')
            try:
                paramsFile.lock('w|')
            except IOError as e:
                logging.warning("Warning! Locking not possible: %s" % e)
            writer = csv.writer(paramsFile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
            for p in self._persistentParams:
                try:
                    #paramsFile.write("\"%s\",\"%s\"\n" % (p, self._persistentParams[p]))
                    writer.writerow([p, self._persistentParams[p]])
                except Exception, e:
                    logging.error("Error writing to %s: %s" % (paramsFilePath, e))
                    paramsFile.lock('u')
                    return 1
            paramsFile.lock('u')
            paramsFile.close()
            return 0
Example #3
0
def transact(conn):
    lock_file = posixfile.open(conn + ".lock", 'w')
    lock_file.lock('w|')
    db_old = _read(conn)
    db_new = Db(db_old.datoms, db_old.t + 1)
    try:
        yield db_new
    finally:
        with open(conn, 'wb') as f:
            pickle.dump(db_new, f)
        lock_file.lock('u')
        lock_file.close()
Example #4
0
def get_lock(lock_fpath):
        '''
        Check if the lock file is locked by another process
        If not, lock it and return the file object
        If yes, return None and wait for the file to unlock
        '''
        f = posixfile.open(lock_fpath, 'w')
        #f = open(lock_fpath, 'w')
        try:
                f.lock('w|')
                #flock(f, flock.LOCK_EX | flock.LOCK_NB)
                return f
        except IOError:
                return None
        except:
                log.error("Error locking file: %s" % lock_fpath)
                return None
Example #5
0
 def save(self):
     lines = open(USERFILE).readlines()
     for x in range(len(lines)):
         line = lines[x]
         name = string.split(line, ":")[0]
         if name == self.username:
             lines[x] = str(self) 
             break
     data = string.join(lines, "")
     fp = posixfile.open(USERFILE, "a+")
     fp.lock("w|")
     try:
         fp.truncate(0)
         fp.write(data)
     finally:
         fp.lock("u")
         fp.close()
Example #6
0
def get_lock(lock_fpath):
    '''
        Check if the lock file is locked by another process
        If not, lock it and return the file object
        If yes, return None and wait for the file to unlock
        '''
    f = posixfile.open(lock_fpath, 'w')
    #f = open(lock_fpath, 'w')
    try:
        f.lock('w|')
        #flock(f, flock.LOCK_EX | flock.LOCK_NB)
        return f
    except IOError:
        return None
    except:
        log.error("Error locking file: %s" % lock_fpath)
        return None
Example #7
0
def wait_for_unlock(lock_fpath):
        '''
        If the file is locked when this process tries to open it, just
        wait for it to unlock before proceeding
        '''
        f = posixfile.open(lock_fpath, 'w')
        #f = open(lock_fpath, 'w')
        got_lock = False
        while not got_lock:
                # try locking it, if lock achieved, file processing is complete
                try:
                        f.lock('w|')
                        #flock(f, flock.LOCK_EX | flock.LOCK_NB)
                        got_lock = True
                # otherwise, continue to try
                except:
                        got_lock = False
        release_lock(f)
Example #8
0
def wait_for_unlock(lock_fpath):
    '''
        If the file is locked when this process tries to open it, just
        wait for it to unlock before proceeding
        '''
    f = posixfile.open(lock_fpath, 'w')
    #f = open(lock_fpath, 'w')
    got_lock = False
    while not got_lock:
        # try locking it, if lock achieved, file processing is complete
        try:
            f.lock('w|')
            #flock(f, flock.LOCK_EX | flock.LOCK_NB)
            got_lock = True
        # otherwise, continue to try
        except:
            got_lock = False
    release_lock(f)
Example #9
0
def newuser(uname, email, systems, pw, task, organiz=""):
    # username:email:password:lang:task:trialts:traints:testts:answerts
    if uname.find(":") != -1:
        raise InvalidUserName(uname, "can't contain a ':'")
    try:
        User(uname)
        raise InvalidUserName(uname, "name in use")
    except KeyError:
        pw_checksum = base64.encodestring(md5.md5(pw).digest())[:-1]
        ustr = "%s:%s:%s:%s:%s:::::%s\n" % (uname,
                                            email,
                                            systems,
                                            pw_checksum,
                                            task,
                                            organiz)
        log("newuser: %s", ustr)
        fp = posixfile.open(USERFILE, "a+")
        fp.lock("w|")
        try:
            fp.write(ustr)
        finally:
            fp.lock("u")
            fp.close()
Example #10
0
import posixfile
import string

filename = "counter.txt"

try:
    # open for update
    file = posixfile.open(filename, "r+")
    counter = int(file.read(6)) + 1
except IOError:
    # create it
    file = posixfile.open(filename, "w")
    counter = 0

file.lock("w|", 6)

file.seek(0)  # rewind
file.write("%06d" % counter)

file.close()  # releases lock
Example #11
0
#!/usr/bin/env python

import string
import re
import posixfile
import time
import glob
import os
import sys

bandwidth_cost = 7.7  # cents per megabyte

global_lock = posixfile.open('/var/acct/global.lock', 'w')
global_lock.lock('w|')

# The way I am keeping atomicity is to write my new
# state into a new directory.
CURRENT_DATA_DIRECTORY = '/var/acct/current/'
NEW_DATA_DIRECTORY = '/var/acct/new/'
# So, when I'm finished,  I rename CURRENT to PAST...
PAST_DATA_DIRECTORY = '/var/acct/past/'
# ... and rename NEW to CURRENT
if not (os.path.exists(CURRENT_DATA_DIRECTORY)):
    # .. then something went wrong.  Let's back out
    if os.path.exists(PAST_DATA_DIRECTORY):
        os.rename(PAST_DATA_DIRECTORY, CURRENT_DATA_DIRECTORY)
        sys.stderr.write('Went back to old state\n')
    else:
        # no current,  no past,  huh???
        sys.exit('No current; no past;  what to do?')
Example #12
0
if money[0] == '$': money = money[1:]
if money[0] == '-' and money[1] == '$': money = '-' + money[2:]
try:
    money = string.atof(money)
except ValueError:
    sys.exit("Does not appear to be a monetary amount " + amount)
    
money = money * 100.0 # we record account balances in cents

# depositnum we just leave as is.

######################################################################


try:
    global_lock = posixfile.open('/var/acct/global.lock','w')
    global_lock.lock('w|')
except IOError,x:
    sys.exit("Can't obtain lock " + `x.strerror`)

now = time.time()

def get_current_account(user):
    if os.path.isfile(user_acct_file(user)):
        current_account_file = open(user_acct_file(user))
        current_account = string.atof(current_account_file.read())
        current_account_file.close()
    else:
        current_account = 0.0
    return current_account
        
Example #13
0
def write(filename, text):
	output=posixfile.open("./{}.csv".format(filename), 'a')
	output.lock('w|')
	output.write(text)
	output.lock('u')
	output.close
Example #14
0
import posixfile
import string

filename = "counter.txt"

try:
    # open for update
    file = posixfile.open(filename, "r+")
    counter = int(file.read(6)) + 1
except IOError:
    # create it
    file = posixfile.open(filename, "w")
    counter = 0

file.lock("w|", 6)

file.seek(0) # rewind
file.write("%06d" % counter)

file.close() # releases lock
Example #15
0
def write(filename, text):
    output = posixfile.open("./{}.csv".format(filename), 'a')
    output.lock('w|')
    output.write(text)
    output.lock('u')
    output.close