Mercurial > repos > yufei-luo > s_mart
comparison commons/tools/srptExportTable.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 user, os, sys, getopt, exceptions, ConfigParser | |
| 4 | |
| 5 #----------------------------------------------------------------------------- | |
| 6 | |
| 7 def help(): | |
| 8 | |
| 9 """ | |
| 10 Give the list of the command-line options. | |
| 11 """ | |
| 12 | |
| 13 print | |
| 14 print "usage:",sys.argv[0]," [ options ]" | |
| 15 print "options:" | |
| 16 print " -h: this help" | |
| 17 print " -i: name of the table to export" | |
| 18 print " -o: name of the output file (default=inTable)" | |
| 19 print " -p: extra parameters to add to the SQL query (e.g. 'ORDER BY path')" | |
| 20 print " -k: keep the first line" | |
| 21 print " -C: configuration file from TEdenovo or TEannot pipeline" | |
| 22 print " -H: MySQL host (if no configuration file)" | |
| 23 print " -U: MySQL user (if no configuration file)" | |
| 24 print " -P: MySQL password (if no configuration file)" | |
| 25 print " -D: MySQL database (if no configuration file)" | |
| 26 print " -v: verbose (default=0/1)" | |
| 27 print | |
| 28 | |
| 29 #----------------------------------------------------------------------------- | |
| 30 | |
| 31 def main(): | |
| 32 | |
| 33 """ | |
| 34 This program exports all the data contained in a MySQL table into a flat file in the current directory. | |
| 35 """ | |
| 36 | |
| 37 inTable = "" | |
| 38 outFileName = "" | |
| 39 param = "" | |
| 40 keepFirstLine = False | |
| 41 configFileName = "" | |
| 42 host = "" | |
| 43 user = "" | |
| 44 passwd = "" | |
| 45 dbname = "" | |
| 46 verbose = 0 | |
| 47 | |
| 48 try: | |
| 49 opts, args = getopt.getopt(sys.argv[1:],"hi:o:p:kC:H:U:P:D:v:") | |
| 50 except getopt.GetoptError, err: | |
| 51 print str(err) | |
| 52 help() | |
| 53 sys.exit(1) | |
| 54 for o,a in opts: | |
| 55 if o == "-h": | |
| 56 help() | |
| 57 sys.exit(0) | |
| 58 elif o == "-i": | |
| 59 inTable = a | |
| 60 elif o == "-o": | |
| 61 outFileName = a | |
| 62 elif o == "-p": | |
| 63 param = a | |
| 64 elif o == "-k": | |
| 65 keepFirstLine = True | |
| 66 elif o == "-C": | |
| 67 configFileName = a | |
| 68 elif o == "-H": | |
| 69 host = a | |
| 70 elif o == "-U": | |
| 71 user = a | |
| 72 elif o == "-P": | |
| 73 passwd = a | |
| 74 elif o == "-D": | |
| 75 dbname = a | |
| 76 elif o == "-v": | |
| 77 verbose = int(a) | |
| 78 | |
| 79 if inTable == "": | |
| 80 print "*** Error: missing input table name" | |
| 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 if host == "" or user == "" or passwd == "" or dbname == "": | |
| 92 if os.environ.get( "REPET_HOST" ) not in [ "", None ]: | |
| 93 host = os.environ.get( "REPET_HOST" ) | |
| 94 if os.environ.get( "REPET_USER" ) not in [ "", None ]: | |
| 95 user = os.environ.get( "REPET_USER" ) | |
| 96 if os.environ.get( "REPET_PW" ) not in [ "", None ]: | |
| 97 passwd = os.environ.get( "REPET_PW" ) | |
| 98 if os.environ.get( "REPET_DB" ) not in [ "", None ]: | |
| 99 dbname = os.environ.get( "REPET_DB" ) | |
| 100 if host == "" or user == "" or passwd == "" or dbname == "": | |
| 101 print "*** Error: missing information about MySQL connection" | |
| 102 sys.exit(1) | |
| 103 | |
| 104 if outFileName == "": | |
| 105 outFileName = inTable | |
| 106 | |
| 107 prg = "mysql" | |
| 108 cmd = prg | |
| 109 cmd += " -h %s" % ( host ) | |
| 110 cmd += " -u %s" % ( user ) | |
| 111 cmd += " -p\"%s\"" % ( passwd ) | |
| 112 cmd += " --database=%s" % ( dbname ) | |
| 113 cmd += " -e\"SELECT * FROM %s" % ( inTable ) | |
| 114 if param != "": | |
| 115 cmd += " %s" % ( param ) | |
| 116 cmd += ";\"" | |
| 117 cmd += " > " | |
| 118 if keepFirstLine == False: | |
| 119 cmd += "%s.tmp" % ( outFileName ) | |
| 120 else: | |
| 121 cmd += "%s" % ( outFileName ) | |
| 122 if verbose > 0: print cmd; sys.stdout.flush() | |
| 123 log = os.system( cmd ) | |
| 124 if log != 0: | |
| 125 print "*** Error: %s returned %i" % ( prg, log ) | |
| 126 sys.exit(1) | |
| 127 | |
| 128 if keepFirstLine == False: | |
| 129 tmpFileName = "%s.tmp" % ( outFileName ) | |
| 130 tmpFile = open( tmpFileName, "r" ) | |
| 131 outFile = open( outFileName, "w" ) | |
| 132 i = 0 | |
| 133 for line in tmpFile: | |
| 134 if i > 0: | |
| 135 outFile.write( line ) | |
| 136 i += 1 | |
| 137 tmpFile.close() | |
| 138 outFile.close() | |
| 139 os.remove( tmpFileName ) | |
| 140 | |
| 141 return 0 | |
| 142 | |
| 143 #---------------------------------------------------------------------------- | |
| 144 | |
| 145 if __name__ == '__main__': | |
| 146 main() |
