{0} -> %s in scripts/cpdb.py

This commit is contained in:
mdipierro
2014-08-14 21:41:09 -05:00
parent 0cc5280f0d
commit 4811d6c45e
2 changed files with 62 additions and 72 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.10.0-trunk+timestamp.2014.08.14.19.47.45 Version 2.10.0-trunk+timestamp.2014.08.14.21.40.41
+61 -71
View File
@@ -1,7 +1,6 @@
import os import os
import sys import sys
from collections import deque from collections import deque
import string
import argparse import argparse
import cStringIO import cStringIO
import operator import operator
@@ -26,8 +25,7 @@ class refTable(object):
self.rows = None self.rows = None
def getcolHeader(self, colHeader): def getcolHeader(self, colHeader):
return "{0}".format(' | '.join([string.join(string.strip('**{0}**'.format(item)), return ' | '.join('**%s**' % item for item in colHeader)
'') for item in colHeader]))
def wrapTable( def wrapTable(
self, rows, hasHeader=False, headerChar='-', delim=' | ', justify='left', self, rows, hasHeader=False, headerChar='-', delim=' | ', justify='left',
@@ -61,18 +59,13 @@ class refTable(object):
) for column ) for column
in columns] in columns]
rowSeparator = headerChar * (len(prefix) + len(postfix) + sum(maxWidths) + rowSeparator = headerChar * (len(prefix) +len(postfix) + sum(maxWidths) +
len(delim) * (len(maxWidths) - 1)) len(delim) * (len(maxWidths) - 1))
justify = {'center': str justify = {'center': str.center,
.center, 'right': str.rjust,
'right': str 'left': str.ljust
.rjust, }[justify.lower()]
'left': str.
ljust
}[justify
.lower(
)]
output = cStringIO.StringIO() output = cStringIO.StringIO()
@@ -92,13 +85,12 @@ class refTable(object):
hasHeader = False hasHeader = False
return output.getvalue() return output.getvalue()
def wrap_onspace(self, text, width): def wrap_onspace(self, text, width):
return reduce(lambda line, word, width=width: '{0}{1}{2}' return reduce(lambda line, word, width=width: '%s%s%s' % (
.format(line, ' \n'[(len( line,
line[line.rfind('\n' ' ' if len(line.rsplit('\n')[-1]+word.split('\n')[0])>=width else '\n',
) + 1:]) + len( word), text.split(' '))
word.split('\n', 1)[0]) >=
width)], word), text.split(' '))
def wrap_onspace_strict(self, text, width): def wrap_onspace_strict(self, text, width):
wordRegex = re.compile(r'\S{' + str(width) + r',}') wordRegex = re.compile(r'\S{' + str(width) + r',}')
@@ -208,49 +200,47 @@ class console:
sys.exit(0) sys.exit(0)
def printError(self, err): def printError(self, err):
sys.stderr.write("Error: {0}\r\n".format(str(err),)) sys.stderr.write("Error: %s\r\n" % err)
if self.var_DEBUG: if self.var_DEBUG:
pass pass
def execute(self, cmd): def execute(self, cmd):
try: try:
if not '-table ' in cmd: if not '-table ' in cmd:
exec '{0}'.format(cmd) exec cmd
else: else:
file = None file = None
table = None table = None
fields = [] fields = []
items = string.split(cmd, ' ') items = cmd.split()
invalidParams = [] invalidParams = []
table = self.getTable(items[1]) table = self.getTable(items[1])
allowedParams = ['fields', 'file'] allowedParams = ['fields', 'file']
for i in items: for i in items:
if '=' in i and not string.split(i, '=')[0] in allowedParams: if '=' in i and not i.split('=')[0] in allowedParams:
try: try:
invalidParams.append(i) invalidParams.append(i)
except Exception, err: except Exception, err:
raise Exception('invalid parameter\n{0}'.format(i)) raise Exception('invalid parameter\n%s' % i)
else: else:
if 'file=' in i: if 'file=' in i:
file = os.path.abspath(string.strip(string.split( file = os.path.abspath(i.split('=')[0].strip())
i, '=')[1]))
if 'fields=' in i: if 'fields=' in i:
for field in string.split(string.split(i, '=')[1], ','): for field in i.split('=')[1].split():
if field in self.db[table].fields: if field in self.db[table].fields:
fields.append(string.strip(field)) fields.append(field.strip())
if len(invalidParams) > 0: if len(invalidParams) > 0:
print('the following parameter(s) is not valid\n{0}'.format( print('the following parameter(s) is not valid\n%s' %
string.join(invalidParams, ','))) ','.join(invalidParams))
else: else:
try: try:
self.cmd_table(table, file, fields) self.cmd_table(table, file, fields)
except Exception, err: except Exception, err:
print('could not generate table for table {0}\n{1}' print('could not generate table for table %s\n%s' % (table, err))
.format(table, err))
except Exception, err: except Exception, err:
print('sorry, can not do that!\n{0}'.format(err)) print('sorry, can not do that!\n%s' % err)
def getTable(self, tbl): def getTable(self, tbl):
for mTbl in db.tables: for mTbl in db.tables:
@@ -267,7 +257,7 @@ class console:
if not cmd in self.commands: if not cmd in self.commands:
raise Exception( raise Exception(
"Command {0} not found. Try 'help'\r\n".format(cmd)) "Command %s not found. Try 'help'\r\n" % cmd)
self.commands[cmd](*parameters) self.commands[cmd](*parameters)
@@ -333,11 +323,11 @@ style choices:
for row in rows: for row in rows:
rowdata = [] rowdata = []
for f in table_fields: for f in table_fields:
rowdata.append('{0}'.format(row[f])) rowdata.append(str(row[f]))
rows_data.append(string.join(rowdata, ',')) rows_data.append(','.join(rowdata))
data = string.join(rows_data, '\n') data = '\n'.join(rows_data)
dataTable = oTable.getTable_Wrap(data, tableStyle, table_fields) dataTable = oTable.getTable_Wrap(data, tableStyle, table_fields)
print('TABLE {0}\n{1}'.format(table, dataTable)) print('TABLE %s\n%s' % (table, dataTable))
if file is not None: if file is not None:
try: try:
tail, head = os.path.split(file) tail, head = os.path.split(file)
@@ -346,15 +336,15 @@ style choices:
except: except:
'do nothing, folders exist' 'do nothing, folders exist'
oFile = open(file, 'w') oFile = open(file, 'w')
oFile.write('TABLE: {0}\n{1}'.format(table, dataTable)) oFile.write('TABLE: %s\n%s' % (table, dataTable))
oFile.close() oFile.close()
print('{0} has been created and populated with all available data from table {1}\n'.format(file, table)) print('%s has been created and populated with all available data from table %2\n' % (file, table))
except Exception, err: except Exception, err:
print("EXCEPTION: could not create table {0}\n{1}".format( print("EXCEPTION: could not create table %s\n%s" % (table, err))
table, err))
else: else:
print('the following fields are not valid [{0}]'.format( print('the following fields are not valid [%s]' % (','.join(filedNotFound)))
string.join(filedNotFound, ',')))
def cmd_help(self, *args): def cmd_help(self, *args):
'''-3|help|Show's help''' '''-3|help|Show's help'''
@@ -412,8 +402,8 @@ style choices:
'''-1|set [variable_name] [value]|Set configuration variable value|Values are an expressions (100 | string.lower('ABC') | etc.''' '''-1|set [variable_name] [value]|Set configuration variable value|Values are an expressions (100 | string.lower('ABC') | etc.'''
value = " ".join(args[1:]) value = " ".join(args[1:])
if args[0] not in self.configvars: if args[0] not in self.configvars:
setattr(self, "var_{0}".format(args[0]), eval(value)) setattr(self, "var_{0}" % (args[0]), eval(value))
setattr(self, "var_{0}".format(args[0]), eval(value)) setattr(self, "var_{0}" % (args[0]), eval(value))
def cmd_clearscreen(self, numlines=50): def cmd_clearscreen(self, numlines=50):
'''---Clear the console. '''---Clear the console.
@@ -486,41 +476,41 @@ class setCopyDB():
return self.db return self.db
def delete_DB_tables(self, storageFolder, storageType): def delete_DB_tables(self, storageFolder, storageType):
print 'delete_DB_tablesn\n\t{0}\n\t{1}'.format( print 'delete_DB_tablesn\n\t%s\n\t%s' % (storageFolder, storageType)
storageFolder, storageType)
dataFiles = [storageType, "sql.log"] dataFiles = [storageType, "sql.log"]
try: try:
for f in os.listdir(storageFolder): for f in os.listdir(storageFolder):
if ".table" in f: if ".table" in f:
fTable = "{0}/{1}".format(storageFolder, f) fTable = "%s/%s" % (storageFolder, f)
os.remove(fTable) os.remove(fTable)
print('deleted {0}'.format(fTable)) print('deleted %s' % (fTable))
for dFile in dataFiles: for dFile in dataFiles:
os.remove("{0}/{1}".format(storageFolder, dFile)) os.remove("%s/%s" % (storageFolder, dFile))
print('deleted {0}'.format( print('deleted %s' % (
"{0}/{1}".format(storageFolder, dFile))) "%s/%s" % (storageFolder, dFile)))
except Exception, errObj: except Exception, errObj:
print(str(errObj)) print(str(errObj))
def truncatetables(self, tables=[]): def truncatetables(self, tables=[]):
if len(tables) != 0: if len(tables) != 0:
try: try:
print 'table value: {0}'.format(tables) print 'table value: %s' % (tables)
for tbl in self.db.tables: for tbl in self.db.tables:
for mTbl in tables: for mTbl in tables:
if mTbl.startswith(tbl): if mTbl.startswith(tbl):
self.db[mTbl].truncate() self.db[mTbl].truncate()
except Exception, err: except Exception, err:
print('EXCEPTION: {0}'.format(err)) print('EXCEPTION: %s' % (err))
else: else:
try: try:
for tbl in self.db.tables: for tbl in self.db.tables:
self.db[tbl].truncate() self.db[tbl].truncate()
except Exception, err: except Exception, err:
print('EXCEPTION: {0}'.format(err)) print('EXCEPTION: %s' % (err))
def copyDB(self): def copyDB(self):
other_db = DAL("{0}://{1}".format( other_db = DAL("%s://%s" % (
self.targetdbType, self.targetdbName), folder=self.targetFolder) self.targetdbType, self.targetdbName), folder=self.targetFolder)
print 'creating tables...' print 'creating tables...'
@@ -618,14 +608,14 @@ informix://username:password@test\n\
try: try:
oCopy.sourceFolder = args.sourceFolder oCopy.sourceFolder = args.sourceFolder
oCopy.targetFolder = args.sourceFolder oCopy.targetFolder = args.sourceFolder
sourceItems = string.split(args.sourceConnectionString, '://') sourceItems = args.sourceConnectionString.split('://',1)
oCopy.sourcedbType = sourceItems[0] oCopy.sourcedbType = sourceItems[0]
oCopy.sourcedbName = sourceItems[1] oCopy.sourcedbName = sourceItems[1]
targetItems = string.split(args.targetConnectionString, '://') targetItems = args.targetConnectionString.split('://',1)
oCopy.targetdbType = targetItems[0] oCopy.targetdbType = targetItems[0]
oCopy.targetdbName = targetItems[1] oCopy.targetdbName = targetItems[1]
except Exception, err: except Exception, err:
print('EXCEPTION: {0}'.format(err)) print('EXCEPTION: %s' % (err))
if args.dal: if args.dal:
try: try:
@@ -639,21 +629,21 @@ informix://username:password@test\n\
db = oCopy.instDB(args.sourceFolder, args.sourceConnectionString, db = oCopy.instDB(args.sourceFolder, args.sourceConnectionString,
autoImport) autoImport)
except Exception, err: except Exception, err:
print('EXCEPTION: could not set DAL\n{0}'.format(err)) print('EXCEPTION: could not set DAL\n%s' % (err))
if args.truncate: if args.truncate:
try: try:
if args.truncate: if args.truncate:
if args.tables: if args.tables:
tables = string.split(string.strip(args.tables), ',') tables = args.tables.strip().split(',')
else: else:
oCopy.truncatetables([]) oCopy.truncatetables([])
except Exception, err: except Exception, err:
print('EXCEPTION: could not truncate tables\n{0}'.format(err)) print('EXCEPTION: could not truncate tables\n%s' % (err))
try: try:
if args.clean: if args.clean:
oCopy.delete_DB_tables(oCopy.targetFolder, oCopy.targetType) oCopy.delete_DB_tables(oCopy.targetFolder, oCopy.targetType)
except Exception, err: except Exception, err:
print('EXCEPTION: could not clean db\n{0}'.format(err)) print('EXCEPTION: could not clean db\n%s' % (err))
""" """
*** goes with -m/-M options... removed for now *** goes with -m/-M options... removed for now
@@ -665,9 +655,9 @@ informix://username:password@test\n\
oCopy.createModel() oCopy.createModel()
except Exception, err: except Exception, err:
print('EXCEPTION: could not create model\n\ print('EXCEPTION: could not create model\n\
source model: {0}\n\ source model: %s\n\
target model: {1}\n\ target model: %s\n\
{2}'.format(args.sourcemodel,args.targetmodel,err)) {2}' % (args.sourcemodel,args.targetmodel,err))
""" """
if args.sourceFolder: if args.sourceFolder:
@@ -675,7 +665,7 @@ target model: {1}\n\
oCopy.sourceFolder = os.path.abspath(args.sourceFolder) oCopy.sourceFolder = os.path.abspath(args.sourceFolder)
oCopy.createfolderPath(oCopy.sourceFolder) oCopy.createfolderPath(oCopy.sourceFolder)
except Exception, err: except Exception, err:
print('EXCEPTION: could not create folder path\n{0}'.format(err)) print('EXCEPTION: could not create folder path\n%s' % (err))
else: else:
oCopy.dbStorageFolder = os.path.abspath(os.getcwd()) oCopy.dbStorageFolder = os.path.abspath(os.getcwd())
if args.targetFolder: if args.targetFolder:
@@ -683,12 +673,12 @@ target model: {1}\n\
oCopy.targetFolder = os.path.abspath(args.targetFolder) oCopy.targetFolder = os.path.abspath(args.targetFolder)
oCopy.createfolderPath(oCopy.targetFolder) oCopy.createfolderPath(oCopy.targetFolder)
except Exception, err: except Exception, err:
print('EXCEPTION: could not create folder path\n{0}'.format(err)) print('EXCEPTION: could not create folder path\n%s' % (err))
if not args.interactive: if not args.interactive:
try: try:
oCopy.copyDB() oCopy.copyDB()
except Exception, err: except Exception, err:
print('EXCEPTION: could not make a copy of the database\n{0}'.format(err)) print('EXCEPTION: could not make a copy of the database\n%s' % (err))
else: else:
s = dalShell() s = dalShell()
s.shell(db) s.shell(db)