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 PROJECTS' % LIMIT + ' WITH OFFSET %s' % OFFSET) projects = None error = 'Execute the first time without error' while projects is None and error: try: ar_db = get_ar_mysql_conection() cursor = ar_db.cursor() cursor.execute(subaccount.SELECTSQL.format(limit=LIMIT, offset=OFFSET)) projects = 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 projects: print('All projects have been updated.') exit() print('PROJECTS FETCHED') if projects: for project_data in projects: try: cursor.execute(subaccount.UPDATESQL.format(project_id=project_data['project_id'])) ar_db.commit() print('UPDATE SUCCESS FOR project_id %s' % project_data['project_id']) except (MySQLdb.Error, MySQLdb.Warning) as e: print('UPDATE FAILED FOR project_id %s' % project_data['project_id'] + ' error : %s' % e) continue except: error = sys.exc_info()[0] print('Failure : %s' % error) exit() ar_db.close() print('COMPLETE')