RAPPEL : Opération de maintenance > ForgeMIA indisponible le 20 Janvier entre 7h et 12h

ng6_update_storage_size.py 2.41 KB
Newer Older
Penom Nom's avatar
Penom Nom committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# Copyright (C) 2012 INRA
# 
# 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 3 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, see <http://www.gnu.org/licenses/>.
#

18
19
import argparse, os, sys
from ConfigParser import ConfigParser
Penom Nom's avatar
Penom Nom committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from MySQLdb import *

def get_storage_size(start_path):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size

def update_storage_size(savedir, host, user, passwd, db):
    conn = connect(host, user, passwd, db)
    with conn :
        curs = conn.cursor()
        
        for tablename in ["tx_nG6_run", "tx_nG6_analyze"] :
            curs.execute("SELECT uid, directory FROM " + tablename)
            res = curs.fetchall()
            update_list = []
            for data in res :
                update_list.append((get_storage_size( savedir + data[1] ), data[0]) )
            
            curs.executemany( "UPDATE  " + tablename + "  SET storage_size='%s' WHERE uid= %s ", update_list )
            conn.commit()
        
        curs.close()
46
47
    print "Updated database " + db 
    
Penom Nom's avatar
Penom Nom committed
48
49
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description = "update analysis and run storage size in ng6 database")
50
51
52
53
54
55
56
57
58
    parser.parse_args()
    
    ng6reader = ConfigParser()
    ng6reader.read(os.path.join(os.path.dirname(__file__), "..", "application.properties")) 
    db_host = ng6reader.get('database', 'host')
    db_user = ng6reader.get('database', 'user')
    db_password =  ng6reader.get('database', 'passwd')
    db_name = ng6reader.get('database', 'dbname')
    savedir = ng6reader.get('storage', 'save_directory')
Penom Nom's avatar
Penom Nom committed
59
    
60
61
62
63
    if not os.path.isdir(savedir):
        sys.exit("Directory '" + savedir + "' does not exists. Pleache change your application.properties file")
    update_storage_size(savedir, db_host, db_user, db_password, db_name)