def assign_fc_gisids(workspace, fc):
    running_total = 0
    cursor_counter = 0
    batch_size = 100000
    run_again = True
    while run_again:
        gisid = MC.read_gisid_seed()
        edit = arcpy.da.Editor(workspace)
        edit.startEditing(False, True)
        edit.startOperation()
        run_again = False
        #  UpdateCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})
        with arcpy.da.UpdateCursor(fc, ["OID@", "GISID"], """"GISID" IS NULL OR "GISID" = 0""") as cursor:
            for row in cursor:
                if cursor_counter > batch_size:
                    cursor_counter = 0
                    run_again = True
                    break
                else:
                    row[1] = gisid
                    cursor.updateRow(row)
                    cursor_counter += 1
                    running_total += 1
                    gisid += 1
                    if cursor_counter > 0 and cursor_counter % 10000 == 0:
                        print cursor_counter
        MC.write_gisid_seed(gisid)
        edit.stopOperation()
        edit.stopEditing(True)

    logger.info(fc + "|" + str(running_total))
def move_workarea(move_logger, source_workspace, target_workspace, state_of_interest):
    fc = "NISOURCE.GPSWorkAreaPolygon"
    source_fc = os.path.join(source_workspace, fc)
    target_fc = os.path.join(target_workspace, fc)

    # MC.disable_editor_tracking(target_workspace, fc)

    arcpy.env.workspace = target_workspace

    dsc = arcpy.Describe(source_fc)
    fields = dsc.fields
    fieldnames = [field.name for field in fields if
                  field.name != dsc.OIDFieldName and field.name != 'GLOBALID' and field.name != 'SHAPE']
    fieldnames = fieldnames + ["SHAPE@"]
    fieldnames_oid = fieldnames + ["OID@"]
    fieldnames_insert = fieldnames + ["GISID"]

    state_index = fieldnames.index("WORKAREASTATE")

    sql_clause = (None, 'ORDER BY "OBJECTID"')

    running_total = 0
    gisid = mc.read_gisid_seed()

    edit = arcpy.da.Editor(target_workspace)
    edit.startEditing(False, True)
    edit.startOperation()

    with arcpy.da.SearchCursor(source_fc, fieldnames_oid, "", None, False, sql_clause) as sCur:
        with arcpy.da.InsertCursor(target_fc, fieldnames_insert) as iCur:
            for row in sCur:
                insert_row = mc.tuple_without(row, -1)
                if state_of_interest == sCur[state_index]:
                    insert_row += [gisid]
                    inserted_oid = iCur.insertRow(insert_row)
                    # print 'Match:', state_of_interest, sCur[-1]
                    running_total += 1
                    gisid += 1
    edit.stopOperation()
    edit.stopEditing(True)
    move_logger.info("Total|" + str(running_total))

    del sCur, iCur

    mc.write_gisid_seed(gisid)
import MigrationCommon as MC


logger = MC.setup_logging('checkout1')
logger.info("Starting program...")

with open("results/mig1_results.txt") as f:
    num_lines = sum(1 for line in f)
logger.info('Results file length: ' + str(num_lines))

gisid_seed = MC.read_gisid_seed()
logger.info('GISID seed value: ' + str(gisid_seed))

logger.info('Difference between: ' + str(gisid_seed - num_lines))

logger.info("Finished program")