From fd8b1ded4cbc26cd6e5e9350e2d7f4e8e82d7db1 Mon Sep 17 00:00:00 2001 From: Matias Carrasco Kind Date: Fri, 20 Feb 2015 21:57:26 -0600 Subject: [PATCH] Fixed minor bugs, added a checker --- CHANGES.txt | 3 +++ README.md | 8 ++++-- config.py | 70 +++++++++++++++++++++++++++++++++++---------------- easyaccess.py | 51 +++++++++++++++++++++---------------- setup.py | 9 ++++--- 5 files changed, 93 insertions(+), 48 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 9cf086a..9472e16 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1 +1,4 @@ v1.0.0, 2015-FEB-17 -- release +v1.0.1, 2015-FEB-20 -- Fix a bug at exit after error + -- Added a checker of DES_SERVICES files and prompt password + -- Minor bugs diff --git a/README.md b/README.md index bf2d4dc..c90896c 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ using cx_Oracle - Oracle Client > 11g.2 - cx_Oracle -- pyfits -- pandas +- pyfits > 3.3 +- pandas > 0.14 - termcolor - PyTables (for hdf5 output) @@ -58,3 +58,7 @@ or The format is the same as in command line, SQL statement must end with ; and to write output files it must be followed by > +### TODO + - There is a bug with some versions of readline + - Other small changes when loading tables + - Self-upgrade diff --git a/config.py b/config.py index 03f6d36..0a74c61 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,9 @@ #Config file import ConfigParser import getpass +import time +import sys +import cx_Oracle configcomment="""# # Easyaccess default parameters @@ -31,7 +34,7 @@ def get_config(configfile): check=config.read(configfile) if check == []: configwrite= True - print '\nError in config file, creating a new one...\n' + print '\nCreating a configuration file... at %s\n' %configfile if not config.has_section('easyaccess'): configwrite = True @@ -73,37 +76,60 @@ def write_config(configfile, config_ob): return False -def get_desconfig(desfile): +def get_desconfig(desfile,db): """ Loads des config file or create one if not - Returns a configParser object + """ + server_n='leovip148.ncsa.uiuc.edu' + port_n='1521' + + if not db[:3] == 'db-': db='db-'+db config = ConfigParser.ConfigParser() configwrite = False check=config.read(desfile) if check == []: configwrite= True - print '\nError in des config file, creating a new one...' + print '\nError in DES_SERVICES config file, creating a new one...' print 'File might not exists or is not configured' print - user=raw_input('Enter username : ') - pw1=getpass.getpass(prompt='Enter password : ') - print - print 'By default the same password is the same for all databases' - print 'If you change your password to some of the DB please modify' - print 'the configuration file %s' % desfile - - databases = ['db-desoper','db-dessci','db-destest'] - - for db in databases: - if not config.has_section(db): - configwrite = True - config.add_section(db) - if not config.has_option(db,'user'): configwrite = True ;config.set(db,'user',user) - if not config.has_option(db,'passwd'): configwrite = True ;config.set(db,'passwd',pw1) - if not config.has_option(db,'name'): configwrite = True ;config.set(db,'name',db[3:]) - if not config.has_option(db,'server'): configwrite = True ;config.set(db,'server','leovip148.ncsa.uiuc.edu') - if not config.has_option(db,'port'): configwrite = True ;config.set(db,'port','1521') + + databases = ['db-desoper','db-dessci','db-destest'] #most used ones anyways + + if db not in databases and not config.has_section(db): + check_db=raw_input('\nDB entered not dessci, desoper or destest or in DES_SERVICE file, continue anyway [y]/n\n') + if check_db in ('n','N','no','No','NO'): sys.exit(0) + + if not config.has_section(db): + print '\nAdding section %s to des_service file\n' % db + configwrite = True + kwargs = {'host': server_n, 'port': port_n, 'service_name': db[3:]} + dsn = cx_Oracle.makedsn(**kwargs) + good=False + for i in range(3): + try: + user=raw_input('Enter username : ') + pw1=getpass.getpass(prompt='Enter password : ') + ctemp=cx_Oracle.connect(user,pw1,dsn=dsn) + good = True + break + except: + (type, value, traceback) = sys.exc_info() + print value + if value.message.code == 1017: pass + else: sys.exit(0) + if good: + ctemp.close() + else: + print '\n Check your credentials and/or database access\n' + sys.exit(0) + config.add_section(db) + + if not config.has_option(db,'user'): configwrite = True ;config.set(db,'user',user) + if not config.has_option(db,'passwd'): configwrite = True ;config.set(db,'passwd',pw1) + if not config.has_option(db,'name'): configwrite = True ;config.set(db,'name',db[3:]) + if not config.has_option(db,'server'): configwrite = True ;config.set(db,'server',server_n) + if not config.has_option(db,'port'): configwrite = True ;config.set(db,'port',port_n) check = True diff --git a/easyaccess.py b/easyaccess.py index 963f00e..2427c6b 100755 --- a/easyaccess.py +++ b/easyaccess.py @@ -1,10 +1,10 @@ #!/usr/bin/env python __author__ = 'Matias Carrasco Kind' -__version__ = '1.0.0' +__version__ = '1.0.1' # TODO: # add other formats in load tables from fits (like boolean or complex) # clean up, comments -# readline bug (GNU vs libedit) +# readline bug (GNU vs libedit) for history # self upgrade import warnings @@ -167,7 +167,7 @@ def __init__(self, conf, desconf, db, interactive=True): self.password = self.desconfig.get('db-' + self.dbname, 'passwd') kwargs = {'host': self.dbhost, 'port': self.port, 'service_name': self.dbname} dsn = cx_Oracle.makedsn(**kwargs) - print 'Connecting to DB %s ...' % self.dbname + print 'Connecting to DB ** %s ** ...' % self.dbname self.con = cx_Oracle.connect(self.user, self.password, dsn=dsn) self.cur = self.con.cursor() self.cur.arraysize = self.prefetch @@ -749,16 +749,19 @@ def do_exit(self, line): except: pass try: - cur.close() + self.cur.close() + except: + pass + try: + self.con.commit() + self.con.close() except: pass - self.con.commit() - self.con.close() if readline_present: readline.write_history_file(history_file) if self.writeconfig: config_mod.write_config(config_file, self.config) - sys.exit(0) + os._exit(0) def do_clear(self, line): """ @@ -1302,23 +1305,26 @@ def to_pandas(cur): class connect(): - def __init__(self): + def __init__(self, section = ''): conf = config_mod.get_config(config_file) pd.set_option('display.max_rows', conf.getint('display', 'max_rows')) pd.set_option('display.width', conf.getint('display', 'width')) pd.set_option('display.max_columns', conf.getint('display', 'max_columns')) - desconf = config_mod.get_desconfig(desfile) - db = conf.get('easyaccess', 'database') + if section == '': + db = conf.get('easyaccess', 'database') + else: + db = section self.prefetch = conf.getint('easyaccess', 'prefetch') self.dbname = db #connect to db + desconf = config_mod.get_desconfig(desfile, self.dbname) self.user = desconf.get('db-' + self.dbname, 'user') self.dbhost = desconf.get('db-' + self.dbname, 'server') self.port = desconf.get('db-' + self.dbname, 'port') self.password = desconf.get('db-' + self.dbname, 'passwd') kwargs = {'host': self.dbhost, 'port': self.port, 'service_name': self.dbname} dsn = cx_Oracle.makedsn(**kwargs) - print 'Connecting to DB...' + print 'Connecting to DB ** %s ** ...' % self.dbname self.con = cx_Oracle.connect(self.user, self.password, dsn=dsn) def ping(self): @@ -1358,16 +1364,17 @@ def error(self, message): try: import readline - #save = sys.stdout - #sys.stdout = open("/dev/null","w") - readline.read_history_file(history_file) - #sys.stdout = save readline_present = True - readline.set_history_length(conf.getint('easyaccess', 'histcache')) except: - print sys.exc_info() readline_present = False + if readline_present == True: + try: + readline.read_history_file(history_file) + readline.set_history_length(conf.getint('easyaccess', 'histcache')) + except: + 'Print readline might give problems accesing the history of commands' + parser = MyParser(description='Easy Access', version="version: %s" % __version__) parser.add_argument("-c", "--command", dest='command', help="Executes command and exit") parser.add_argument("-l", "--loadsql", dest='loadsql', help="Loads a sql command, execute it and exit") @@ -1376,27 +1383,29 @@ def error(self, message): parser.add_argument("-s", "--db", dest='db', help="bypass database name, [dessci, desoper or destest]") args = parser.parse_args() - desconf = config_mod.get_desconfig(desfile) if args.db is not None: db = args.db + if db[:3] == 'db-' : db=db[3:] else: db = conf.get('easyaccess', 'database') + desconf = config_mod.get_desconfig(desfile,db) + if args.command is not None: cmdinterp = easy_or(conf, desconf, db, interactive=False) cmdinterp.onecmd(args.command) - sys.exit(0) + os._exit(0) elif args.loadsql is not None: cmdinterp = easy_or(conf, desconf, db, interactive=False) linein = "loadsql " + args.loadsql cmdinterp.onecmd(linein) - sys.exit(0) + os._exit(0) elif args.loadtable is not None: cmdinterp = easy_or(conf, desconf, db, interactive=False) linein = "load_table " + args.loadtable cmdinterp.onecmd(linein) - sys.exit(0) + os._exit(0) else: os.system(['clear', 'cls'][os.name == 'nt']) easy_or(conf, desconf, db).cmdloop() diff --git a/setup.py b/setup.py index 5262443..36836ee 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,9 @@ import sys import os -from numpy.distutils.core import setup, Extension +try: + from setuptools import setup +except ImportError: + from distutils.core import setup prjdir = os.path.dirname(__file__) def read(filename): @@ -12,7 +15,7 @@ def read(filename): include_dirs = [] setup( name='easyaccess', - version='1.0.0', + version='1.0.1', author='Matias Carrasco Kind', author_email='mcarras2@illinois.edu', scripts=['easyaccess'], @@ -21,5 +24,5 @@ def read(filename): description='Easy Access to access DES DB', long_description=read('README.md'), url='https://github.com/mgckind/easyaccess', - install_requires=['pandas','termcolor','pyfits','cx_Oracle'], + install_requires=['pandas','termcolor','pyfits >= 3.3','cx_Oracle'], )