Advertisement
Guest User

Скрипт на питоне

a guest
Apr 21st, 2011
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import kinterbasdb
  4. import subprocess
  5. import time
  6. import os.path
  7. import settings
  8.  
  9. class fb:
  10.     "Робота з FB"
  11.     def __init__(self, dbhost, dbfile, dbuser, dbpass):
  12.         self.con = kinterbasdb.connect(
  13.             host = dbhost, database = dbfile,
  14.             user = dbuser, password = dbpass, charset = 'WIN1251'
  15.         )
  16.  
  17.     def selectSQL(self, query, params=([])):
  18.         "Select data from db"
  19.         customTPB = (kinterbasdb.isc_tpb_write + kinterbasdb.isc_tpb_read_committed + kinterbasdb.isc_tpb_rec_version)
  20.         self.con.begin(tpb = customTPB)
  21.         cur = self.con.cursor()
  22.         cur.execute(query, params)
  23.         result = cur.fetchall()
  24.         cur.close()
  25.         self.con.commit()
  26.         return result
  27.  
  28.     def selectSQLmap(self, query, params=([])):
  29.         customTPB = (kinterbasdb.isc_tpb_write + kinterbasdb.isc_tpb_read_committed + kinterbasdb.isc_tpb_rec_version)
  30.         self.con.begin(tpb = customTPB)
  31.         cur = self.con.cursor()
  32.         cur.execute(query, params)
  33.         result = cur.fetchallmap()
  34.         cur.close()
  35.         self.con.commit()
  36.         return result
  37.  
  38.     def selectBlobToFile(self, filename, query, params=([])):
  39.         customTPB = (kinterbasdb.isc_tpb_write + kinterbasdb.isc_tpb_read_committed + kinterbasdb.isc_tpb_rec_version)
  40.         self.con.begin(tpb = customTPB)
  41.         cur = self.con.cursor()
  42.         cur.set_type_trans_in ({'BLOB': {'mode': 'stream'}})
  43.         cur.set_type_trans_out({'BLOB': {'mode': 'stream'}})
  44.         try:
  45.             blobfile = open(filename, 'w')
  46.             cur.execute(query, params)
  47.             blobreader = cur.fetchone()[0]
  48.             for chunkNo, chunk in enumerate(blobreader.chunks(50)):
  49.                 blobfile.write(chunk)
  50.         except kinterbasdb.ProgrammingError, (errNumb, errText):
  51.             cur.close()
  52.             self.con.rollback()
  53.             result = (errNumb, errText, query)
  54.         except:
  55.             cur.close()
  56.             self.con.rollback()
  57.             result = (-1, 'Unknown error', query)
  58.         else:
  59.             cur.close()
  60.             self.con.commit()
  61.             result = (0, 'Success', query)
  62.         finally:
  63.             return result
  64.  
  65.     def execSQL(self, query, params=([])):
  66.         customTPB = (kinterbasdb.isc_tpb_write + kinterbasdb.isc_tpb_read_committed + kinterbasdb.isc_tpb_rec_version)
  67.         self.con.begin(tpb = customTPB)
  68.         cur = self.con.cursor()
  69.         cur.set_type_trans_in ({'BLOB': {'mode': 'stream'}})
  70.         cur.set_type_trans_out({'BLOB': {'mode': 'stream'}})
  71.         try:
  72.             cur.execute(query, params)
  73.         except kinterbasdb.ProgrammingError, (errNumb, errText):
  74.             cur.close()
  75.             self.con.rollback()
  76.             result = (errNumb, errText, query)
  77.         except:
  78.             cur.close()
  79.             self.con.rollback()
  80.             result = (-1, 'Unknown error', query)
  81.         else:
  82.             cur.close()
  83.             self.con.commit()
  84.             result = (0, 'Success', query)
  85.         finally:
  86.             return result
  87.  
  88. if __name__ == '__main__':
  89.     fbclient = fb(settings.host, settings.path, settings.user, settings.pasw)
  90.     session_id = fbclient.selectSQL("insert into rpl_sessions default values returning id")[0]
  91.     sender_id = fbclient.selectSQL("select id from rpl_databases where its_me = 1")[0][0]
  92.     fbclient.execSQL("update rpl_sessions set end_ts = current_timestamp where id = %d" % (session_id[0]-1))
  93.     rplfname = "replication_tmp.sql"
  94.     resfile = open(rplfname, 'w')
  95.     resfile.write("select rdb$set_context('USER_SESSION','replicating_now','true') from rdb$database;\n")
  96.     for statement in fbclient.selectSQL("select rpl_sql from rpl_log where session_id = %d order by id" % (session_id[0]-1)):
  97.         resfile.write(statement[0].encode("cp1251") + '\n')
  98.     resfile.close()
  99.     srvparams = fbclient.selectSQL("select dbhost, dbpath, dbuser, dbpass from rpl_databases where its_server = 1")[0]
  100.     fbserver = fb(srvparams[0].encode("cp1251"), srvparams[1].encode("cp1251"), srvparams[2].encode("cp1251"), srvparams[3].encode("cp1251"))
  101.     resblob = file(os.path.abspath(rplfname), 'rb')
  102.     r = fbserver.execSQL("insert into rpl_blobs(sender_id, session_id, rpl_blob)values(?, ?, ?)", (sender_id, session_id[0]-1, resblob))
  103.     if r[0] == -1:
  104.         print r[1] + ' happened during ' + r[2]
  105.  
  106.     for dbitem in fbclient.selectSQL("select id, last_session from rpl_databases d where d.its_me = 0"):
  107.         for rplitem in fbserver.selectSQL("select id,session_id from rpl_blobs where sender_id = ? and session_id > ?", (dbitem[0], dbitem[1])):
  108.             rplfilename = "replication_" + str(rplitem[1]) + ".sql"
  109.             fbserver.selectBlobToFile(rplfilename, "select rpl_blob from rpl_blobs where id = ?", ([rplitem[0]]))
  110.             isqlcmd = "isql-fb -user sysdba -password masterkey -input " + rplfilename + " /usr/database/localtest1.fdb > replication_" + str(rplitem[1]) + ".log 2>&1"
  111.             res = subprocess.call(isqlcmd, shell=True)
  112.             fbclient.execSQL("insert into rpl_received(db_id,blob_id,session_id,received_ts)values(?, ?, ?, current_timestamp)", (dbitem[0], rplitem[0], rplitem[1]))
  113.             fbclient.execSQL("update rpl_databases set last_session = ? where id = ?", (rplitem[1], dbitem[0]))
  114.  
  115. ####################################################################################
  116. ### А это файл настроек settings.py
  117. ####################################################################################
  118.  
  119. # -*- coding: utf-8 -*-
  120. host='localhost'
  121. user='SYSDBA'
  122. pasw='masterkey'
  123. path='/var/database/localtest1.fdb'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement