Show
Ignore:
Timestamp:
09/27/08 22:02:37 (16 years ago)
Author:
jerome
Message:

Removed unnecessary spaces at EOL.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/initscripts/postgresql/upgrade-from-before-1.03.py

    r3259 r3413  
    88# the Free Software Foundation, either version 3 of the License, or 
    99# (at your option) any later version. 
    10 #  
     10# 
    1111# This program is distributed in the hope that it will be useful, 
    1212# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1313# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1414# GNU General Public License for more details. 
    15 #  
     15# 
    1616# You should have received a copy of the GNU General Public License 
    1717# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
     
    2626try : 
    2727    import pg 
    28 except ImportError :     
     28except ImportError : 
    2929    sys.stderr.write("The PyGreSQL Python module doesn't seem to be available. ABORTED.\n") 
    3030    sys.exit(-1) 
     
    3232def dump_old_database() : 
    3333    """Dumps the existing PyKota database to a file, to avoir loosing data. 
    34      
     34 
    3535       Returns 1 if dump is successfull, 0 if it isn't. 
    36     """    
     36    """ 
    3737    pipeinput = os.popen("pg_dump -C -D -N -U postgres -f pykota-dump.sql pykota") 
    3838    dummy = pipeinput.read() 
     
    4444    dummy = pipeinput.read() 
    4545    return (pipeinput.close() is None) or 0 
    46      
     46 
    4747def restore_original_database() : 
    4848    """Creates the empty database.""" 
     
    5050    dummy = pipeinput.read() 
    5151    return (pipeinput.close() is None) or 0 
    52      
     52 
    5353def open_database(dbname="pykota") : 
    5454    """Returns the database object or None if we can't connect to it.""" 
    5555    try : 
    5656        pykotadb = pg.connect(host="localhost", port=5432, dbname=dbname, user="postgres") 
    57     except pg.error, msg :      
     57    except pg.error, msg : 
    5858        sys.stderr.write("%s\n" % msg) 
    5959        sys.stderr.write("Unable to connect to the local PostgreSQL server.\nPlease modify the open_database() method in %s\nto connect to the correct PostgreSQL server\nand relaunch the script.\n" % sys.argv[0]) 
    60         return  
    61     else :     
     60        return 
     61    else : 
    6262        return pykotadb 
    63          
    64          
     63 
     64 
    6565def doQuote(field) : 
    6666    """Quotes a field for use as a string in SQL queries.""" 
    6767    if type(field) == type(0) : # TODO : do something safer 
    6868        typ = "decimal" 
    69     else :     
     69    else : 
    7070        typ = "text" 
    7171    return pg._quote(field, typ) 
    72          
     72 
    7373def main() : 
    7474    """Does the work.""" 
    75      
     75 
    7676    # First we make a dump of the old database 
    7777    print "Dumping old database for safety...", 
     
    8080        return -1 
    8181    print "Done." 
    82          
    83     # Second we try to connect to it     
    84     print "Extracting datas from old database...",  
     82 
     83    # Second we try to connect to it 
     84    print "Extracting datas from old database...", 
    8585    db = open_database() 
    8686    if db is None : 
    8787        sys.stderr.write("Impossible to connect to old PyKota database. ABORTED.\nAre you sure you are upgrading an existing installation ?\n") 
    8888        return -1 
    89      
     89 
    9090    # Third we extract datas 
    9191    oldprinters = db.query("SELECT * FROM printers ORDER BY id;") 
    9292    oldusers = db.query("SELECT * FROM users ORDER BY id;") 
    9393    oldquotas = db.query("SELECT * FROM userpquota ORDER BY printerid, userid;") 
    94      
     94 
    9595    # Fourth close the database 
    9696    db.close() 
    9797    print "Done." 
    98      
     98 
    9999    # Fifth we delete the old database ! 
    100100    answer = raw_input("The old database will be deleted for the upgrade to take place.\nAre you sure you want to continue (y/N) ? ") 
     
    112112        db.close() 
    113113        return -1 
    114     else :     
     114    else : 
    115115        db.close() 
    116          
     116 
    117117    # Sixth we create the new database 
    118118    print "Creating the new database...", 
     
    121121        return -1 
    122122    print "Done." 
    123      
     123 
    124124    # Seventh we restore old data 
    125125    print "Restoring old datas..." 
     
    131131            sys.stderr.write("Shit ! A double-error occured !!!\nPlease report problem to your database administrator.\n") 
    132132            sys.stderr.write("And file a bug report to alet@librelogiciel.com\n") 
    133         else :     
     133        else : 
    134134            print "Done." 
    135135        return -1 
    136     db.query("BEGIN;")     
     136    db.query("BEGIN;") 
    137137    try : 
    138138        newprinters = {} 
     
    141141            newid = db.query("SELECT id FROM printers WHERE printername='%s';" % oldprinter["printername"]).dictresult()[0]["id"] 
    142142            newprinters[oldprinter["id"]] = newid 
    143         newusers = {}     
    144         for olduser in oldusers.dictresult() :     
     143        newusers = {} 
     144        for olduser in oldusers.dictresult() : 
    145145            db.query("INSERT INTO users (username) VALUES (%s);" % doQuote(olduser["username"])) 
    146146            newid = db.query("SELECT id FROM users WHERE username='%s';" % olduser["username"]).dictresult()[0]["id"] 
    147147            newusers[olduser["id"]] = newid 
    148         for oldquota in oldquotas.dictresult() :    
    149             db.query("INSERT INTO userpquota (userid, printerid, pagecounter, lifepagecounter, softlimit, hardlimit, datelimit) VALUES (%s, %s, %s, %s, %s, %s, %s);" %  
     148        for oldquota in oldquotas.dictresult() : 
     149            db.query("INSERT INTO userpquota (userid, printerid, pagecounter, lifepagecounter, softlimit, hardlimit, datelimit) VALUES (%s, %s, %s, %s, %s, %s, %s);" % 
    150150                                              (doQuote(newusers[oldquota["userid"]]) , doQuote(newprinters[oldquota["printerid"]]), doQuote(oldquota["pagecounter"]), doQuote(oldquota["lifepagecounter"]), doQuote(oldquota["softlimit"]), doQuote(oldquota["hardlimit"]), doQuote(oldquota["datelimit"]))) 
    151     except pg.error, msg :     
     151    except pg.error, msg : 
    152152        sys.stderr.write("ERROR : %s\nABORTED.\n" % msg) 
    153153        db.query("ROLLBACK;") 
    154         db.close()     
     154        db.close() 
    155155        return -1 
    156     except :     
     156    except : 
    157157        sys.stderr.write("Unknown error ! ABORTED.\n") 
    158158        db.query("ROLLBACK;") 
    159         db.close()     
     159        db.close() 
    160160        return -1 
    161     else :     
     161    else : 
    162162        db.query("COMMIT;") 
    163163        db.close() 
     
    165165    print "NB : Last job on each printer was lost. This is normal !" 
    166166    return 0 
    167      
     167 
    168168if __name__ == "__main__" : 
    169169    sys.exit(main())