Ejemplo n.º 1
0
def initTestSession(test_func, table, whereClause="", outdir="", qsub=None, name=None):
    """
    Initialize a test session of all configs in 'table' that satisfy
    the 'whereClause'.
    """

    manager = DatabaseManager()

    # Get configs from the database
    colnames, configs = manager.getRecords(table, whereClause)

    #
    # Create a folder for this test session based on the date and time
    #   -> The directory being created is:
    #           <outDir>/<configTable>/<date_time>/
    #   -> If a session_name is specified, the directory is:
    #           <outDir>/<configTable>/<session_name>/
    #   -> Each config will create a folder in this directory
    #
    now = datetime.datetime.now()
    sec = now.second
    timestamp = now.strftime("%Y-%m-%d_%H.%M.") + str(sec)
    sql_datetime_str = now.strftime("%Y-%m-%d %H:%M:") + str(sec)

    session_name = name
    if not session_name:
        session_name = timestamp

    testBatchDir = outdir
    if ctip.CREATE_DIR_STRUCTURE:
        testBatchDir = os.path.join(outdir, table, session_name)
        try:
            os.makedirs(testBatchDir)
        except OSError:
            if not os.path.isdir(testBatchDir):
                raise RuntimeError("Could not create the output directory")

    # Store text file snapshot of config table at root session dir
    snapshotPath = os.path.join(testBatchDir, table + ".csv")
    with open(snapshotPath, "w") as sf:
        writeConfigCsv(sf, table, colnames, configs)

    # Add this session info to the sessions table
    session_id = manager.newSession(table, name, sql_datetime_str, whereClause)

    # Call the test_function for each config
    jobs = []
    id_queue = Queue()
    for config in configs:
        if qsub:
            p = Process(target=test_func, args=(config, id_queue, testBatchDir, qsub))
        else:
            p = Process(target=test_func, args=(config, id_queue, testBatchDir))
        p.start()
        jobs.append(p)
    for p in jobs:
        p.join()

    job_ids = []
    try:
        while True:
            job_ids.append(id_queue.get_nowait())
    except QueueEmpty:
        pass

    for id, cfg_id in job_ids:
        manager.addJobToSession(session_id, cfg_id, id)
Ejemplo n.º 2
0
def initTestSession(test_func,
                    table,
                    whereClause="",
                    outdir="",
                    qsub=None,
                    name=None):
    """
    Initialize a test session of all configs in 'table' that satisfy
    the 'whereClause'.
    """

    manager = DatabaseManager()

    # Get configs from the database
    colnames, configs = manager.getRecords(table, whereClause)

    #
    # Create a folder for this test session based on the date and time
    #   -> The directory being created is:
    #           <outDir>/<configTable>/<date_time>/
    #   -> If a session_name is specified, the directory is:
    #           <outDir>/<configTable>/<session_name>/
    #   -> Each config will create a folder in this directory
    #
    now = datetime.datetime.now()
    sec = now.second
    timestamp = now.strftime("%Y-%m-%d_%H.%M.") + str(sec)
    sql_datetime_str = now.strftime("%Y-%m-%d %H:%M:") + str(sec)

    session_name = name
    if not session_name:
        session_name = timestamp

    testBatchDir = outdir
    if ctip.CREATE_DIR_STRUCTURE:
        testBatchDir = os.path.join(outdir, table, session_name)
        try:
            os.makedirs(testBatchDir)
        except OSError:
            if not os.path.isdir(testBatchDir):
                raise RuntimeError('Could not create the output directory')

    # Store text file snapshot of config table at root session dir
    snapshotPath = os.path.join(testBatchDir, table + ".csv")
    with open(snapshotPath, 'w') as sf:
        writeConfigCsv(sf, table, colnames, configs)

    # Add this session info to the sessions table
    session_id = manager.newSession(table, name, sql_datetime_str, whereClause)

    # Call the test_function for each config
    jobs = []
    id_queue = Queue()
    for config in configs:
        if qsub:
            p = Process(target=test_func,
                        args=(config, id_queue, testBatchDir, qsub))
        else:
            p = Process(target=test_func,
                        args=(config, id_queue, testBatchDir))
        p.start()
        jobs.append(p)
    for p in jobs:
        p.join()

    job_ids = []
    try:
        while True:
            job_ids.append(id_queue.get_nowait())
    except QueueEmpty:
        pass

    for id, cfg_id in job_ids:
        manager.addJobToSession(session_id, cfg_id, id)