Ignore:
Timestamp:
03/29/13 12:40:39 (11 years ago)
Author:
ramonb
Message:
  • cleanup and fixes
  • use psycopg2 now
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/0.4/jobarchived/jobarchived.py

    r772 r773  
    4848def processArgs( args ):
    4949
    50         SHORT_L    = 'p:hvc:'
    51         LONG_L    = [ 'help', 'config=', 'pidfile=', 'version' ]
    52 
    53         config_filename = '/etc/jobarchived.conf'
     50    SHORT_L    = 'p:hvc:'
     51    LONG_L    = [ 'help', 'config=', 'pidfile=', 'version' ]
     52
     53    config_filename = '/etc/jobarchived.conf'
    5454
    5555    global PIDFILE
     
    5757    PIDFILE    = None
    5858
    59         try:
    60 
    61                 opts, args = getopt.getopt( args, SHORT_L, LONG_L )
    62 
    63         except getopt.error, detail:
    64 
    65                 print detail
    66                 sys.exit(1)
    67 
    68         for opt, value in opts:
    69 
    70                 if opt in [ '--config', '-c' ]:
    71 
    72                         config_filename = value
     59    try:
     60
     61        opts, args = getopt.getopt( args, SHORT_L, LONG_L )
     62
     63    except getopt.error, detail:
     64
     65        print detail
     66        sys.exit(1)
     67
     68    for opt, value in opts:
     69
     70        if opt in [ '--config', '-c' ]:
     71
     72            config_filename = value
    7373
    7474        if opt in [ '--pidfile', '-p' ]:
     
    175175    ARCHIVE_XMLSOURCE    = cfg.get( 'DEFAULT', 'ARCHIVE_XMLSOURCE' )
    176176
    177         ARCHIVE_DATASOURCES    = getlist( cfg.get( 'DEFAULT', 'ARCHIVE_DATASOURCES' ) )
     177    ARCHIVE_DATASOURCES    = getlist( cfg.get( 'DEFAULT', 'ARCHIVE_DATASOURCES' ) )
    178178
    179179    ARCHIVE_EXCLUDE_METRICS    = getlist( cfg.get( 'DEFAULT', 'ARCHIVE_EXCLUDE_METRICS' ) )
     
    209209
    210210try:
    211     from pyPgSQL import PgSQL
     211    import psycopg2
    212212
    213213except ImportError, details:
    214214
    215     print "FATAL ERROR: pyPgSQL python module not found"
     215    print "FATAL ERROR: psycopg2 python module not found"
    216216    sys.exit( 1 )
    217 
    218 # Orginal from Andre van der Vlies <andre@vandervlies.xs4all.nl> for MySQL. Changed
    219 # and added some more functions for postgres.
    220 #       
    221 #
    222 # Changed by: Bas van der Vlies <basv@sara.nl>
    223 #       
    224 # SARA API for Postgres Database
    225 #
    226 # Changed by: Ramon Bastiaans for Job Monarch
    227 #
    228217
    229218class InitVars:
     
    274263#
    275264class DB:
    276         def __init__(self, db_vars):
    277 
    278                 self.dict = db_vars
    279 
    280                 if self.dict.has_key('User'):
    281                         self.user = self.dict['User']
    282                 else:
    283                         self.user = 'postgres'
    284 
    285                 if self.dict.has_key('Host'):
    286                         self.host = self.dict['Host']
    287                 else:
    288                         self.host = 'localhost'
    289 
    290                 if self.dict.has_key('Password'):
    291                         self.passwd = self.dict['Password']
    292                 else:
    293                         self.passwd = ''
    294 
    295                 if self.dict.has_key('DataBaseName'):
    296                         self.db = self.dict['DataBaseName']
    297                 else:
    298                         self.db = 'uva_cluster_db'
    299 
    300                 # connect_string = 'host:port:database:user:password:
    301                 dsn = "%s::%s:%s:%s" %(self.host, self.db, self.user, self.passwd)
    302 
    303                 try:
    304                         self.SQL = PgSQL.connect(dsn)
    305                 except PgSQL.Error, details:
    306                         str = "%s" %details
    307                         raise DBError(str)
    308 
    309         def __repr__(self):
    310                 return repr(self.result)
    311 
    312         def __nonzero__(self):
    313                 return not(self.result == None)
    314 
    315         def __len__(self):
    316                 return len(self.result)
    317 
    318         def __getitem__(self,i):
    319                 return self.result[i]
    320 
    321         def __getslice__(self,i,j):
    322                 return self.result[i:j]
    323 
    324         def Get(self, q_str):
    325                 c = self.SQL.cursor()
    326                 try:
    327                         c.execute(q_str)
    328                         result = c.fetchall()
    329                 except PgSQL.Error, details:
    330                         c.close()
    331                         str = "%s" %details
    332                         raise DBError(str)
    333 
    334                 c.close()
    335                 return result
    336 
    337         def Set(self, q_str):
    338                 c = self.SQL.cursor()
    339                 try:
    340                         c.execute(q_str)
    341                         result = c.oidValue
    342 
    343                 except PgSQL.Error, details:
    344                         c.close()
    345                         str = "%s" %details
    346                         raise DBError(str)
    347 
    348                 c.close()
    349                 return result
    350 
    351         def Commit(self):
    352                 self.SQL.commit()
     265    def __init__(self, db_vars):
     266
     267        self.dict = db_vars
     268
     269        if self.dict.has_key('User'):
     270            self.user = self.dict['User']
     271        else:
     272            self.user = 'postgres'
     273
     274        if self.dict.has_key('Host'):
     275            self.host = self.dict['Host']
     276        else:
     277            self.host = 'localhost'
     278
     279        if self.dict.has_key('Password'):
     280            self.passwd = self.dict['Password']
     281        else:
     282            self.passwd = ''
     283
     284        if self.dict.has_key('DataBaseName'):
     285            self.db = self.dict['DataBaseName']
     286        else:
     287            self.db = 'jobarchive'
     288
     289        # connect_string = 'host:port:database:user:password:
     290        dsn = "host='%s' dbname='%s' user='%s' password='%s'" %(self.host, self.db, self.user, self.passwd)
     291
     292        try:
     293            self.SQL = psycopg2.connect(dsn)
     294        except psycopg2.Error, details:
     295            str = "%s" %details
     296            raise DBError(str)
     297
     298    def __repr__(self):
     299        return repr(self.result)
     300
     301    def __nonzero__(self):
     302        return not(self.result == None)
     303
     304    def __len__(self):
     305        return len(self.result)
     306
     307    def __getitem__(self,i):
     308        return self.result[i]
     309
     310    def __getslice__(self,i,j):
     311        return self.result[i:j]
     312
     313    def Get(self, q_str):
     314        c = self.SQL.cursor()
     315        try:
     316            c.execute(q_str)
     317            result = c.fetchall()
     318        except psycopg2.Error, details:
     319            c.close()
     320            str = "%s" %details
     321            raise DBError(str)
     322
     323        c.close()
     324        return result
     325
     326    def Set(self, q_str):
     327        c = self.SQL.cursor()
     328        try:
     329            c.execute(q_str)
     330            result = c.oidValue
     331
     332        except psycopg2.Error, details:
     333            c.close()
     334            str = "%s" %details
     335            raise DBError(str)
     336
     337        c.close()
     338        return result
     339
     340    def Commit(self):
     341        self.SQL.commit()
    353342
    354343class DataSQLStore:
     
    11861175                continue
    11871176
    1188                 try:
     1177            try:
    11891178
    11901179                self.s.connect( sa )
    11911180
    1192                 except ( socket.error, socket.gaierror, socket.herror, socket.timeout ), msg:
     1181            except ( socket.error, socket.gaierror, socket.herror, socket.timeout ), msg:
    11931182
    11941183                self.disconnect()
    11951184                continue
    11961185
    1197                 break
     1186            break
    11981187
    11991188        if self.s is None:
     
    12071196
    12081197            my_fp            = self.s.makefile( 'r' )
    1209             my_data            = my_fp.readlines()
    1210             my_data            = string.join( my_data, '' )
     1198            my_data          = my_fp.readlines()
     1199            my_data          = string.join( my_data, '' )
    12111200
    12121201            self.data        = my_data
    12131202
    1214             self.LAST_UPDATE    = time.time()
     1203            self.LAST_UPDATE = time.time()
    12151204
    12161205        self.slot.release()
Note: See TracChangeset for help on using the changeset viewer.