Revision 4446
Added by Aaron Marcuse-Kubitza about 12 years ago
csv2db | ||
---|---|---|
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
# Loads a command's CSV output stream into a PostgreSQL table. |
3 |
# When no command is specified, just cleans up the specified table. |
|
3 | 4 |
# The command may be run more than once. |
4 | 5 |
|
5 | 6 |
import csv |
... | ... | |
25 | 26 |
env_names = [] |
26 | 27 |
def usage_err(): |
27 | 28 |
raise SystemExit('Usage: '+opts.env_usage(env_names)+' '+sys.argv[0] |
28 |
+' input_cmd [args...]')
|
|
29 |
+' [input_cmd args...]')
|
|
29 | 30 |
|
30 | 31 |
# Parse args |
31 | 32 |
input_cmd = sys.argv[1:] |
... | ... | |
37 | 38 |
db_config = opts.get_env_vars(sql.db_config_names, None, env_names) |
38 | 39 |
verbosity = util.cast(float, opts.get_env_var('verbosity', 3, env_names)) |
39 | 40 |
|
40 |
if not (input_cmd != [] and table != None and 'engine' in db_config): |
|
41 |
usage_err() |
|
41 |
if not (table != None and 'engine' in db_config): usage_err() |
|
42 | 42 |
|
43 | 43 |
# Connect to DB |
44 | 44 |
def log(msg, level=1): |
... | ... | |
114 | 114 |
line_in.close() # also closes proc.stdout |
115 | 115 |
proc.wait() |
116 | 116 |
sql.with_savepoint(db, load_) |
117 |
|
|
118 |
log('Cleaning up table') |
|
119 |
sql_io.cleanup_table(db, table, col_names) |
|
120 |
|
|
121 |
log('Vacuuming and reanalyzing table') |
|
122 |
sql.vacuum(db, table) |
|
123 | 117 |
|
124 |
try: load() |
|
125 |
except sql.DatabaseErrors, e: |
|
126 |
if use_copy_from[0]: # first try |
|
127 |
exc.print_ex(e, plain=True) |
|
128 |
use_copy_from[0] = False |
|
129 |
load() # try again with different approach |
|
130 |
else: raise |
|
118 |
if input_cmd != []: |
|
119 |
try: load() |
|
120 |
except sql.DatabaseErrors, e: |
|
121 |
if use_copy_from[0]: # first try |
|
122 |
exc.print_ex(e, plain=True) |
|
123 |
use_copy_from[0] = False |
|
124 |
load() # try again with different approach |
|
125 |
else: raise |
|
126 |
|
|
127 |
log('Cleaning up table') |
|
128 |
sql_io.cleanup_table(db, table) |
|
129 |
|
|
130 |
log('Vacuuming and reanalyzing table') |
|
131 |
sql.vacuum(db, table) |
|
131 | 132 |
|
132 | 133 |
main() |
Also available in: Unified diff
csv2db: When no command is specified, just clean up the specified table