import collections import MySQLdb import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from connector.mysql import get_ar_mysql_conection from sql import subaccount print('START') OFFSET = 0 LIMIT = 500 try: arg_names = ['COMMAND', 'OFFSET', 'LIMIT'] args = dict(zip(arg_names, sys.argv)) arg_list = collections.namedtuple('arg_list', arg_names) arguments = arg_list(*(args.get(arg, None) for arg in arg_names)) if arguments.OFFSET and arguments.LIMIT: OFFSET = int(arguments.OFFSET) LIMIT = int(arguments.LIMIT) elif arguments.OFFSET and not arguments.LIMIT: LIMIT = int(arguments.OFFSET) except ValueError: print("Please provide numeric values for command line arguments.") exit() print('FETCH %s SUBACCOUNTS' % LIMIT + ' WITH OFFSET %s' % OFFSET) subaccounts = None error = 'Execute the first time without error' while subaccounts is None and error: try: ar_db = get_ar_mysql_conection() cursor = ar_db.cursor() cursor.execute(subaccount.SELECTSQL.format(limit=LIMIT, offset=OFFSET)) subaccounts = cursor.fetchall() error = None except (MySQLdb.Error, MySQLdb.Warning) as e: print('Connection Failure : %s' % e) pass except: error = sys.exc_info()[0] print('Failure : %s' % error) exit() if not subaccounts: print('All subaccounts have been updated.') exit() print('SUBACCOUNTS FETCHED') if subaccounts: for subaccount_data in subaccounts: try: cursor.execute(subaccount.UPDATESQL.format(subaccount_id=subaccount_data['subaccount_id'])) ar_db.commit() print('UPDATE SUCCESS FOR subaccount_id %s' % subaccount_data['subaccount_id']) except (MySQLdb.Error, MySQLdb.Warning) as e: print('UPDATE FAILED FOR subaccount_id %s' % subaccount_data['subaccount_id'] + ' error : %s' % e) continue except: error = sys.exc_info()[0] print('Failure : %s' % error) exit() ar_db.close() print('COMPLETE')