#!/usr/pkg/bin/python """ simple implementation of Bernard Lietaer's ROCS system probably not useful due to lack of secure transactions""" Copyright = """ rocs -- simple implementation of Bernard Lietaer's ROCS system Copyright (C) 2005 John Comeau This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path: " try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re, pwd sys.path.append(os.path.join(pwd.getpwuid(os.geteuid())[5], 'lib', 'python')) errormessage = errormessage + repr(sys.path) from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # get name this program was called as self = os.path.split(sys.argv[0])[1] command = os.path.splitext(self)[0] # chop any suffix (extension) # now get name we gave it when we wrote it originalself = re.compile('[0-9A-Za-z]+').search(Copyright).group() # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) def DebugPrint(*whatever): return False # defined instead by pytest module, use that for debugging def join(*args): "for pythons without str.join" string, array = args if hasattr(str, 'join'): return string.join(array) else: joined = '' for index in range(0, len(array)): joined = joined + array[index] if index != (len(array) - 1): joined = joined + string return joined def split(*args): "for pythons without str.split" string, string_to_split = args if not len(string): string = None if hasattr('str', 'split'): return string_to_split.split(string) else: raise Exception, 'tell programmer to split with regular expression' # other globals, specific to this program import time ROCSDIR = pwd.getpwuid(os.geteuid())[5] # program must own directories zero = 0 # HOURS; fictitious "positive balance" in each account at start # only set it positive if needed to spark acceptance of the system hoarding = { # accumulating too many HOURS without spending 'threshold': zero + 80, # HOURS; accounts over this will suffer demurrage 'demurrage': .02, # amount deducted every 30 days from (balance - zero) } freeloading = { # spending too many HOURS without reciprocal effort to earn 'threshold': zero - 100, # HOURS; credit limit, so to speak 'demurrage': .04, # charged every 30 days on (zero - balance) } suspension = { 'threshold': zero - 150, # member cannot spend after reaching this } commands = { 'transact': ['claim', 'withdraw', 'acknowledge', 'challenge'], 'register': [], 'list': [], 'report': [], 'arbitrate': ['accept', 'deny'], 'appoint': [], } def transact(*args): print 'transacting %s' % repr(args) def register(*args): print 'attempting to register %s' % repr(args) if len(args): sys.stderr.write('warning: ignoring args %s\n' % repr(args)) username, ignore, ignore, ignore, realname = pwd.getpwuid(os.getuid())[0:5] if username == 'root' or username == 'rocs' or username == 'guest': sys.stderr.write('you must be logged in as a normal user to register\n') sys.exit(1) name = re.compile('^[A-Za-z][^,]+').match(realname) if not name or not len(name): sys.stderr.write('we must know your real name first; use chfn\n') sys.exit(1) member_dir = os.path.join(ROCSDIR, 'members', username) os.mkdir(member_dir) open(os.path.join(member_dir, 'name'), 'w').write(name) intro = open(os.path.join(member_dir, 'intro'), 'w') intro.write('no intro yet') intro.close() os.chmod(os.path.join(member_dir, 'intro'), os.getuid(), os.getgid()) open(os.path.join(member_dir, 'date'), 'w').write( time.asctime(time.localtime(time.time()))) sys.stderr.write("""please introduce yourself to the trading community by editing %s when you get a chance""" % os.path.join(member_dir, 'intro')) def usage(*args): errorlevel = int(args[0]) output = sys.stdout if errorlevel != 0: output = sys.stderr output.write('Usage: %s %s\n' % (command, join(' | ', commands.keys()))) if errorlevel != 0: sys.exit(errorlevel) def snapshot(*args): """snapshot the database for faster loading avoid verbose output, or a cron job will generate emails""" pass def rocs(*args): """implement the ROCS commands""" os.chdir(os.path.split(sys.argv[0])[0]) try: os.mkdir(os.path.join('transactions', '__LOCK__')) except: sys.stderr.write('program locked, or not suid rocs\n') sys.exit(1) if pwd.getpwuid(os.getuid())[0] == 'rocs': # only thing doable by 'rocs' himself is to snapshot the database snapshot() os.rmdir(os.path.join('transactions', '__LOCK__')) if len(args) and args[0] in commands.keys(): print eval('%s%s' % (args[0], tuple(args[1:]))) os.rmdir(os.path.join('transactions', '__LOCK__')) else: os.rmdir(os.path.join('transactions', '__LOCK__')) usage(1) if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... function = command; args = sys.argv[1:] # default case eval('%s%s' % (function, repr(tuple(args)))) else: # if you want something to be done on import, do it here; otherwise pass pass