comparison commons/tools/srptCreateTable.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
children
comparison
equal deleted inserted replaced
17:b0e8584489e6 18:94ab73e8a190
1 #!/usr/bin/env python
2
3 import os
4 import sys
5 import getopt
6 import ConfigParser
7
8 from commons.core.sql.DbMySql import DbMySql
9
10
11 def help():
12 print
13 print "usage: %s [ options ]" % ( sys.argv[0].split("/")[-1] )
14 print "options:"
15 print " -h: this help"
16 print " -f: name of the input file"
17 print " -n: name of the MySQL table"
18 print " -t: table type (fasta|align|path|set|match|map|TEclassif|cluster)"
19 print " -o: overwrite (default=False)"
20 print " -c: configuration file from TEdenovo or TEannot pipeline"
21 print " -H: MySQL host (if no configuration file)"
22 print " -U: MySQL user (if no configuration file)"
23 print " -P: MySQL password (if no configuration file)"
24 print " -D: MySQL database (if no configuration file)"
25 print " -T: MySQL port (if no configuration file, default=3306)"
26 print " -v: verbose (default=0/1)"
27 print
28
29
30 def main():
31 """
32 This program loads data from a file into a MySQL table.
33 """
34 filename = ""
35 tablename = ""
36 filetype = ""
37 overwrite = False
38 configFileName = ""
39 host = ""
40 user = ""
41 passwd = ""
42 dbname = ""
43 port = 0
44 verbose = 0
45
46 try:
47 opts, args = getopt.getopt( sys.argv[1:], "hf:t:n:oc:H:U:P:D:T:v:" )
48 except getopt.GetoptError, err:
49 sys.stderr.write( "%s\n" % str(err) )
50 help()
51 sys.exit(1)
52 for o,a in opts:
53 if o == "-h":
54 help()
55 sys.exit(0)
56 elif o == "-f":
57 filename = a
58 elif o == "-n":
59 tablename = a
60 elif o == "-t":
61 filetype = a
62 elif o == "-o":
63 overwrite = True
64 elif o == "-c":
65 configFileName = a
66 elif o == "-H":
67 host = a
68 elif o == "-U":
69 user = a
70 elif o == "-P":
71 passwd = a
72 elif o == "-D":
73 dbname = a
74 elif o == "-T":
75 port = int(a)
76 elif o == "-v":
77 verbose = int(a)
78
79 if filename == "" or tablename == "" or filetype == "":
80 print "ERROR: missing compulsory options"
81 help()
82 sys.exit(1)
83
84 if configFileName != "":
85 config = ConfigParser.ConfigParser()
86 config.readfp( open(configFileName) )
87 host = config.get("repet_env","repet_host")
88 user = config.get("repet_env","repet_user")
89 passwd = config.get("repet_env","repet_pw")
90 dbname = config.get("repet_env","repet_db")
91 port = config.get("repet_env","repet_port")
92
93 if host == "" and os.environ.get( "REPET_HOST" ) != "":
94 host = os.environ.get( "REPET_HOST" )
95 if user == "" and os.environ.get( "REPET_USER" ) != "":
96 user = os.environ.get( "REPET_USER" )
97 if passwd == "" and os.environ.get( "REPET_PW" ) != "":
98 passwd = os.environ.get( "REPET_PW" )
99 if dbname == "" and os.environ.get( "REPET_DB" ) != "":
100 dbname = os.environ.get( "REPET_DB" )
101 if port == 0 and os.environ.get( "REPET_PORT" ) != "":
102 port = int( os.environ.get( "REPET_PORT" ) )
103
104 if host == "":
105 print "ERROR: missing host"
106 sys.exit(1)
107 if user == "":
108 print "ERROR: missing user"
109 sys.exit(1)
110 if passwd == "":
111 print "ERROR: missing password"
112 sys.exit(1)
113 if dbname == "":
114 print "ERROR: missing db name"
115 sys.exit(1)
116 if port == 0:
117 print "ERROR: missing port"
118 sys.exit(1)
119
120 db = DbMySql(user, host, passwd, dbname, port )
121
122 if not os.path.exists( filename ):
123 print "ERROR: input file '%s' doesn't exist" % ( filename )
124 sys.exit(1)
125
126 db.createTable( tablename, filetype, filename, overwrite)
127
128 db.close()
129
130 return 0
131
132
133 if __name__ == "__main__":
134 main()