IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/ippTools/src

    • Property svn:ignore
      •  

        old new  
         1*.la
         2*.lo
        13.deps
        24.gdb_history
         
        46Makefile
        57Makefile.in
         8addtool
         9caltool
         10camtool
         11chiptool
        612config.h
        713config.h.in
        8 stamp-h1
        9 *.la
        10 *.lo
         14detselect
         15dettool
         16difftool
         17disttool
         18dqstatstool
         19faketool
         20flatcorr
         21guidetool
         22magicdstool
         23magictool
         24pstamptool
         25pubtool
         26pxadmin
         27pxdata.c
         28pxinject
        1129pxtoolsErrorCodes.c
        1230pxtoolsErrorCodes.h
        13 pxadmin
        14 pxinject
        15 pztool
        1631pzgetexp
        1732pzgetimfiles
         33pztool
         34receivetool
        1835regtool
        19 guidetool
        20 chiptool
        21 camtool
         36stacktool
         37stamp-h1
        2238warptool
        23 difftool
        24 stacktool
        25 faketool
        26 dettool
        27 detselect
        28 pxdata.c
        29 magictool
        30 magicdstool
        31 caltool
        32 flatcorr
        33 pstamptool
        34 disttool
        35 receivetool
        36 
        37 pubtool
  • branches/simtest_nebulous_branches/ippTools/src/disttool.c

    r25031 r27840  
    22 * disttool.c
    33 *
    4  * Copyright (C) 2008
     4 * Copyright (C) 2008-2009
    55 *
    66 * This program is free software; you can redistribute it and/or modify it
     
    3232
    3333static bool definebyqueryMode(pxConfig *config);
    34 static bool definerunMode(pxConfig *config);
    3534static bool updaterunMode(pxConfig *config);
    3635static bool revertrunMode(pxConfig *config);
    3736static bool pendingcomponentMode(pxConfig *config);
    3837static bool addprocessedcomponentMode(pxConfig *config);
     38static bool revertcomponentMode(pxConfig *config);
    3939static bool processedcomponentMode(pxConfig *config);
    4040static bool toadvanceMode(pxConfig *config);
     
    4242static bool addfilesetMode(pxConfig *config);
    4343static bool revertfilesetMode(pxConfig *config);
     44static bool updatefilesetMode(pxConfig *config);
    4445static bool queuercrunMode(pxConfig *config);
    4546static bool updatercrunMode(pxConfig *config);
    4647static bool revertrcrunMode(pxConfig *config);
    4748static bool pendingdestMode(pxConfig *config);
     49static bool pendingcleanupMode(pxConfig *config);
     50static bool listfilesetsMode(pxConfig *config);
    4851
    4952static bool definetargetMode(pxConfig *config);
    5053static bool updatetargetMode(pxConfig *config);
    51 static bool listtargetMode(pxConfig *config);
    52 
    53 static bool definedsproductMode(pxConfig *config);
    54 static bool updatedsproductMode(pxConfig *config);
     54static bool listtargetsMode(pxConfig *config);
    5555
    5656static bool definedestinationMode(pxConfig *config);
     
    5959static bool defineinterestMode(pxConfig *config);
    6060static bool updateinterestMode(pxConfig *config);
     61static bool listinterestsMode(pxConfig *config);
    6162
    6263# define MODECASE(caseName, func) \
     
    8081    switch (config->mode) {
    8182        MODECASE(DISTTOOL_MODE_DEFINEBYQUERY, definebyqueryMode);
    82         MODECASE(DISTTOOL_MODE_DEFINERUN, definerunMode);
    8383        MODECASE(DISTTOOL_MODE_UPDATERUN, updaterunMode);
    8484        MODECASE(DISTTOOL_MODE_REVERTRUN, revertrunMode);
     
    8686        MODECASE(DISTTOOL_MODE_ADDPROCESSEDCOMPONENT, addprocessedcomponentMode);
    8787        MODECASE(DISTTOOL_MODE_PROCESSEDCOMPONENT, processedcomponentMode);
     88        MODECASE(DISTTOOL_MODE_REVERTCOMPONENT, revertcomponentMode);
    8889        MODECASE(DISTTOOL_MODE_TOADVANCE, toadvanceMode);
    8990        MODECASE(DISTTOOL_MODE_PENDINGFILESET, pendingfilesetMode);
     91        MODECASE(DISTTOOL_MODE_PENDINGCLEANUP, pendingcleanupMode);
    9092        MODECASE(DISTTOOL_MODE_ADDFILESET, addfilesetMode);
    9193        MODECASE(DISTTOOL_MODE_REVERTFILESET, revertfilesetMode);
     94        MODECASE(DISTTOOL_MODE_LISTFILESETS, listfilesetsMode);
     95        MODECASE(DISTTOOL_MODE_UPDATEFILESET, updatefilesetMode);
    9296        MODECASE(DISTTOOL_MODE_QUEUERCRUN, queuercrunMode);
    9397        MODECASE(DISTTOOL_MODE_UPDATERCRUN, updatercrunMode);
     
    96100        MODECASE(DISTTOOL_MODE_DEFINETARGET, definetargetMode);
    97101        MODECASE(DISTTOOL_MODE_UPDATETARGET, updatetargetMode);
    98         MODECASE(DISTTOOL_MODE_LISTTARGET, listtargetMode);
    99         MODECASE(DISTTOOL_MODE_DEFINEDSPRODUCT, definedsproductMode);
    100         MODECASE(DISTTOOL_MODE_UPDATEDSPRODUCT, updatedsproductMode);
     102        MODECASE(DISTTOOL_MODE_LISTTARGETS, listtargetsMode);
    101103        MODECASE(DISTTOOL_MODE_DEFINEDESTINATION, definedestinationMode);
    102104        MODECASE(DISTTOOL_MODE_UPDATEDESTINATION, updatedestinationMode);
    103105        MODECASE(DISTTOOL_MODE_DEFINEINTEREST, defineinterestMode);
    104106        MODECASE(DISTTOOL_MODE_UPDATEINTEREST, updateinterestMode);
     107        MODECASE(DISTTOOL_MODE_LISTINTERESTS, listinterestsMode);
    105108        default:
    106109            psAbort("invalid option (this should not happen)");
     
    124127}
    125128
    126 static bool definerunMode(pxConfig *config)
    127 {
    128     PS_ASSERT_PTR_NON_NULL(config, false);
    129 
    130     // required
    131     PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
    132     PXOPT_LOOKUP_S64(stage_id, config->args, "-stage_id",  true, false);
    133     PXOPT_LOOKUP_STR(outroot, config->args, "-outroot", true, false);
    134     PXOPT_LOOKUP_S64(target_id, config->args, "-target_id",  true, false);
    135 
    136     // optional
    137     PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
    138     PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
    139     PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
    140 
    141     // TODO: check that stage has an expected value
    142     // XXX: all of the following concerns will be managed properly by definebyquery
    143 
    144     // TODO: should we check that stage_id actually exists for stage
    145     // in magicdstool we queue off of a magic_id so the stage_id, exp_id, and cam_id get looked up
    146     // when the run is queued
    147 
    148     if (!distRunInsert(config->dbh,
    149             0,          // dist_id
    150             target_id,
    151             stage,
    152             stage_id,
    153             set_label,
    154             outroot,
    155             clean,
    156             no_magic,
    157             "new",
    158             NULL,       // time_stamp
    159             0           // fault
    160             )) {
    161         psError(PS_ERR_UNKNOWN, false, "database error");
    162         return false;
    163     }
    164 
    165     return true;
    166 }
    167129
    168130static bool definebyqueryMode(pxConfig *config)
     
    175137
    176138    // optional
    177     PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id",  false, false);
     139    PXOPT_LOOKUP_BOOL(use_alternate, config->args, "-use_alternate", false);
    178140    PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
    179141    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
     142    PXOPT_LOOKUP_STR(set_data_group, config->args, "-set_data_group", false, false);
     143    PXOPT_LOOKUP_STR(set_note, config->args, "-set_note", false, false);
     144
    180145    PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false);
    181146    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    182    
    183     PXOPT_LOOKUP_BOOL(dry_run, config->args, "-dry_run", false);
     147
     148    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     149
     150    if (use_alternate) {
     151        if (strcmp(stage, "raw")) {
     152            psError(PXTOOLS_ERR_SYS, true, "alternate inputs only supported for raw stage");
     153            return false;
     154        }
     155        if (no_magic) {
     156            psError(PXTOOLS_ERR_SYS, true, "no_magic forbidden with alternate inputs");
     157            return false;
     158        }
     159    }
    184160
    185161    // select arguments
     
    191167
    192168    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
     169    PXOPT_LOOKUP_STR(dist_group, config->args, "-dist_group", false, false);
    193170
    194171    psString query = NULL;
     
    196173    psString runJoinStr = NULL;
    197174    if (!strcmp(stage, "raw")) {
    198         magicRunType = "rawExp";
    199         runJoinStr = "rawExp.exp_id";
     175        if (! use_alternate ) {
     176            magicRunType = "rawExp";
     177            runJoinStr = "rawExp.exp_id";
     178        } else {
     179            magicRunType = "camRun";
     180            runJoinStr = "camRun.exp_id";
     181        }
    200182        query = pxDataGet("disttool_definebyquery_raw.sql");
    201183        if (!query) {
    202             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     184            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    203185            psFree(where);
    204186            return false;
    205187        }
    206188
    207         // for raw stage we select by chipRun.label because raw labels typically aren't set
     189        // for raw stage we select by camRun.label and dist_group because rawExp doesn't have those columns
    208190        if (label) {
    209             psStringAppend(&query, " AND (chipRun.label = '%s')", label);
     191            psStringAppend(&query, " AND (magicDSRun.label = '%s')", label);
     192        }
     193        if (dist_group) {
     194            psStringAppend(&query, " AND (camRun.dist_group = '%s')", dist_group);
    210195        }
    211196    } else if (!strcmp(stage, "chip")) {
     
    214199        query = pxDataGet("disttool_definebyquery_chip.sql");
    215200        if (!query) {
    216             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     201            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    217202            psFree(where);
    218203            return false;
     
    222207            psStringAppend(&query, " AND (chipRun.label = '%s')", label);
    223208        }
     209        if (dist_group) {
     210            psStringAppend(&query, " AND (chipRun.dist_group = '%s')", dist_group);
     211        }
    224212    } else if (!strcmp(stage, "camera")) {
    225         magicRunType = "chipRun";    // This is used below to set the magicked business
     213        magicRunType = "camRun";    // This is used below to set the magicked business
     214        runJoinStr = "camRun.cam_id";
    226215        query = pxDataGet("disttool_definebyquery_camera.sql");
    227216        if (!query) {
    228             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     217            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    229218            psFree(where);
    230219            return false;
     
    233222        if (label) {
    234223            psStringAppend(&query, " AND (camRun.label = '%s')", label);
     224        }
     225        if (dist_group) {
     226            psStringAppend(&query, " AND (camRun.dist_group = '%s')", dist_group);
    235227        }
    236228    } else if (!strcmp(stage, "fake")) {
     
    238230        query = pxDataGet("disttool_definebyquery_fake.sql");
    239231        if (!query) {
    240             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     232            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    241233            psFree(where);
    242234            return false;
     
    245237        if (label) {
    246238            psStringAppend(&query, " AND (fakeRun.label = '%s')", label);
     239        }
     240        if (dist_group) {
     241            psStringAppend(&query, " AND (fakeRun.dist_group = '%s')", dist_group);
    247242        }
    248243        // fake stage doesn't require magic
     
    253248        query = pxDataGet("disttool_definebyquery_warp.sql");
    254249        if (!query) {
    255             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     250            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    256251            psFree(where);
    257252            return false;
     
    260255        if (label) {
    261256            psStringAppend(&query, " AND (warpRun.label = '%s')", label);
     257        }
     258        if (dist_group) {
     259            psStringAppend(&query, " AND (warpRun.dist_group = '%s')", dist_group);
    262260        }
    263261
     
    267265        query = pxDataGet("disttool_definebyquery_diff.sql");
    268266        if (!query) {
    269             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     267            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    270268            psFree(where);
    271269            return false;
     
    274272        if (label) {
    275273            psStringAppend(&query, " AND (diffRun.label = '%s')", label);
     274        }
     275        if (dist_group) {
     276            psStringAppend(&query, " AND (diffRun.dist_group = '%s')", dist_group);
    276277        }
    277278
     
    280281        query = pxDataGet("disttool_definebyquery_stack.sql");
    281282        if (!query) {
    282             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     283            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    283284            psFree(where);
    284285            return false;
     
    288289            psStringAppend(&query, " AND (stackRun.label = '%s')", label);
    289290        }
    290         // stack stage doesn't require magic (perhaps let the script do this?
     291        if (dist_group) {
     292            psStringAppend(&query, " AND (stackRun.dist_group = '%s')", dist_group);
     293        }
     294        // stack stage doesn't require magic
    291295        no_magic = true;
     296    } else if (!strcmp(stage, "SSdiff")) {
     297      magicRunType = "diffRun";
     298      runJoinStr = "diffRun.diff_id";
     299      query = pxDataGet("disttool_definebyquery_SSdiff.sql");
     300      if (!query) {
     301        psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement");
     302        psFree(where);
     303        return(false);
     304      }
     305
     306      if (label) {
     307        psStringAppend(&query, " AND (diffRun.label = '%s') ", label);
     308      }
     309      if (dist_group) {
     310        psStringAppend(&query, " AND (diffRun.dist_group = '%s') ", dist_group);
     311      }
     312
     313      no_magic = true;   
    292314    } else {
    293315        psError(PS_ERR_UNKNOWN, true, "unknown value for stage: %s", stage);
    294316        psFree(where);
    295317        return false;
     318    }
     319
     320    if (!strcmp(stage, "raw")) {
     321        if (!no_magic) {
     322            psStringAppend(&query, " AND (magicDSRun.re_place = %d)", !use_alternate);
     323            psStringAppend(&query, " AND (camRun.state = 'full')");
     324        }
    296325    }
    297326
     
    306335
    307336    if (!no_magic) {
    308         psStringAppend(&query, " AND (distTarget.clean OR %s.magicked)", magicRunType);
    309 
    310         // is selecting by magic_ds_id really interesting?
    311         if (magic_ds_id) {
    312             if (strcmp(stage, "camera")) {
    313                 // stage other than camera
    314                 if (!runJoinStr) {
    315                     psError(PS_ERR_PROGRAMMING, true, "cannot select by magic_ds_id for stage: %s", stage);
    316                     psFree(query);
    317                     return false;
    318                 }
    319                 psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = distTarget.stage"
    320                                               " AND magicDSRun.stage_id = %s", runJoinStr);
    321             } else {
    322                 // camera masks are magicked when the chipRun is magicked
    323                 // XXX: This is confusing. Is it dangerous?
    324                 // Maybe I should add a magicked bit to camRun. Note this isn't
    325                 psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = 'chip'"
    326                                               " AND magicDSRun.stage_id = chipRun.chip_id");
    327             }
    328             psStringAppend(&query, " AND (magicDSRun.state = 'full' AND magicDSRun.re_place AND (magic_ds_id = %" PRId64 "))", magic_ds_id);
    329         }
     337        psStringAppend(&query, " AND (%s.magicked > 0)", magicRunType);
    330338    }
    331339
     
    335343        psFree(limitString);
    336344    }
    337 
     345    psTrace("disttool",2,query,joinHook ? joinHook : "");
    338346    if (!p_psDBRunQueryF(config->dbh, query, joinHook ? joinHook : "")) {
    339         psError(PS_ERR_UNKNOWN, false, "database error");
    340         psFree(query);
    341         return false;
     347      psError(PS_ERR_UNKNOWN, false, "database error");
     348      psFree(query);
     349      return false;
    342350    }
    343351    psFree(query);
     
    356364
    357365
    358     if (dry_run) {
     366    if (pretend) {
    359367        if (!ippdbPrintMetadatas(stdout, output, "newdistRuns", true)) {
    360368            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     
    377385        psString run_tag = psMetadataLookupStr(NULL, md, "run_tag");
    378386        psS64 stage_id = psMetadataLookupS64(NULL, md, "stage_id");
     387        psS64 magic_ds_id = psMetadataLookupS64(NULL, md, "magicked");
    379388        psS64 target_id = psMetadataLookupS64(NULL, md, "target_id");
    380         psString target_label = psMetadataLookupStr(NULL, md, "label");
     389        psString stage_label = psMetadataLookupStr(NULL, md, "label");
     390        psString stage_data_group = psMetadataLookupStr(NULL, md, "data_group");
    381391        bool clean = psMetadataLookupBool(NULL, md, "clean");
    382392
    383393        psString outroot = NULL;
    384         psStringAppend(&outroot, "%s/%s/%s", workdir, run_tag, stage); 
     394        psStringAppend(&outroot, "%s/%s/%s", workdir, run_tag, stage);
    385395
    386396        psString new_label;
     
    388398            new_label = set_label;
    389399        } else {
    390             new_label = target_label;
    391         }
     400            new_label = stage_label;
     401        }
     402        psString new_data_group;
     403        if (set_data_group != NULL) {
     404            new_data_group = set_data_group;
     405        } else {
     406            new_data_group = stage_data_group;
     407        }
     408
    392409        distRunRow *row = distRunRowAlloc(
    393410                0,      // dist_id
     
    395412                stage,
    396413                stage_id,
     414                magic_ds_id,
    397415                new_label,
    398416                outroot,
     417                NULL,     // outdir
    399418                clean,
    400419                no_magic,
     420                use_alternate,
    401421                "new",
    402                 NULL,   // time_stamp
    403                 0       // fault
     422                NULL,    // time_stamp
     423                0,       // fault
     424                new_data_group,
     425                set_note // note does not propagate
    404426                );
    405427
     
    444466    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
    445467    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
    446     PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     468    PXOPT_COPY_STR(config->args, where, "-state", "distRun.state", "==");
    447469    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     470    PXOPT_COPY_STR(config->args, where, "-data_group", "distRun.data_group", "LIKE");
     471    PXOPT_COPY_STR(config->args, where, "-dist_group", "distTarget.dist_group", "==");
     472    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
    448473
    449474    if (!psListLength(where->list)) {
    450475        psFree(where);
    451         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     476        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    452477        return false;
    453478    }
     
    455480    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    456481    PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
     482    PXOPT_LOOKUP_STR(data_group, config->args, "-set_data_group", false, false);
     483    PXOPT_LOOKUP_STR(outdir, config->args, "-set_outdir", false, false);
    457484    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
    458 
    459     if ((!state) && (!label) && (!fault)) {
    460         psError(PXTOOLS_ERR_DATA, false, "parameters (-fault or -set_state or -set_label) are required");
    461         psFree(where);
    462         return false;
    463     }
    464 
    465     psString query = psStringCopy("UPDATE distRun SET time_stamp = UTC_TIMESTAMP()");
    466 
     485    PXOPT_LOOKUP_STR(set_note, config->args, "-set_note", false, false);
     486
     487    if ((!state) && (!label) && (!fault) &&(!data_group)) {
     488        psError(PXTOOLS_ERR_CONFIG, false, "parameters (-fault or -set_state or -set_label -set_data_group) are required");
     489        psFree(where);
     490        return false;
     491    }
     492
     493    psString query = psStringCopy("UPDATE distRun join distTarget using(target_id, stage) SET distRun.time_stamp = UTC_TIMESTAMP()");
     494
     495    if (outdir) {
     496        psStringAppend(&query, " , distRun.outdir = '%s'", outdir);
     497    }
    467498    if (state) {
    468         psStringAppend(&query, " , state = '%s'", state);
     499        psStringAppend(&query, " , distRun.state = '%s'", state);
    469500    }
    470501
    471502    if (label) {
    472         psStringAppend(&query, " , label = '%s'", label);
     503        psStringAppend(&query, " , distRun.label = '%s'", label);
    473504    }
    474505
    475506    if (fault) {
    476         psStringAppend(&query, " , fault = %d", fault);
    477     }
     507        psStringAppend(&query, " , distRun.fault = %d", fault);
     508    }
     509
     510    if (data_group) {
     511        psStringAppend(&query, " , distRun.data_group = '%s'", data_group);
     512    }
     513
     514    if (set_note) {
     515        psStringAppend(&query, " , distRun.note = '%s'", set_note);
     516    }
     517
    478518
    479519    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     
    500540    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
    501541
    502     // we need to disambiguate fault so make a copy of the where list before adding fault
    503     psMetadata *whereComponent = psMetadataCopy(NULL, where);
    504542    PXOPT_COPY_S16(config->args, where,  "-fault", "distRun.fault", "==");
    505     PXOPT_COPY_S16(config->args, whereComponent, "-fault", "distComponent.fault", "==");
    506543
    507544    // It might be useful to be able to query by the parameters of the underlying runs
     
    509546    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    510547        psFree(where);
    511         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
    512         return false;
    513     }
    514 
    515     if (!psDBTransaction(config->dbh)) {
    516         psError(PS_ERR_UNKNOWN, false, "database error");
    517         psFree(where);
    518         return false;
    519     }
    520 
    521     // Update state to 'new'
    522     int numUpdated;                     // Number updated
    523     {
    524         psString query = pxDataGet("disttool_revertrun_update.sql");
    525         if (!query) {
    526             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    527             if (!psDBRollback(config->dbh)) {
    528                 psError(PS_ERR_UNKNOWN, false, "database error");
    529             }
    530             return false;
    531         }
    532 
    533         if (psListLength(where->list)) {
    534             psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    535             psStringAppend(&query, " AND %s", whereClause);
    536             psFree(whereClause);
    537         }
    538 
    539         if (!p_psDBRunQuery(config->dbh, query)) {
     548        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     549        return false;
     550    }
     551
     552    psString query = pxDataGet("disttool_revertrun.sql");
     553    if (!query) {
     554        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     555        if (!psDBRollback(config->dbh)) {
    540556            psError(PS_ERR_UNKNOWN, false, "database error");
    541             psFree(query);
    542             if (!psDBRollback(config->dbh)) {
    543                 psError(PS_ERR_UNKNOWN, false, "database error");
    544             }
    545             return false;
    546         }
    547         psFree(query);
    548 
    549         numUpdated = psDBAffectedRows(config->dbh);
    550     }
     557        }
     558        return false;
     559    }
     560
     561    if (psListLength(where->list)) {
     562        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     563        psStringAppend(&query, " AND %s", whereClause);
     564        psFree(whereClause);
     565    }
     566    psFree(where);
     567
     568    if (!p_psDBRunQuery(config->dbh, query)) {
     569        psError(PS_ERR_UNKNOWN, false, "database error");
     570        psFree(query);
     571        return false;
     572    }
     573
     574    int numUpdated = psDBAffectedRows(config->dbh);
    551575
    552576    psLogMsg("disttool", PS_LOG_INFO, "Updated %d dist runs", numUpdated);
    553577
    554     // Delete product
    555     int numDeleted;                     // Number deleted
    556     {
    557         psString query = pxDataGet("disttool_revertrun_delete.sql");
    558         if (!query) {
    559             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    560             if (!psDBRollback(config->dbh)) {
    561                 psError(PS_ERR_UNKNOWN, false, "database error");
    562             }
    563             return false;
    564         }
    565 
    566         if (psListLength(whereComponent->list)) {
    567             psString whereClause = psDBGenerateWhereConditionSQL(whereComponent, NULL);
    568             psStringAppend(&query, " AND %s", whereClause);
    569             psFree(whereClause);
    570         }
    571 
    572         if (!p_psDBRunQuery(config->dbh, query)) {
    573             psError(PS_ERR_UNKNOWN, false, "database error");
    574             psFree(query);
    575             if (!psDBRollback(config->dbh)) {
    576                 psError(PS_ERR_UNKNOWN, false, "database error");
    577             }
    578             return false;
    579         }
    580         psFree(query);
    581 
    582         numDeleted = psDBAffectedRows(config->dbh);
    583     }
     578    return true;
     579}
     580
     581static bool revertcomponentMode(pxConfig *config)
     582{
     583    psMetadata *where = psMetadataAlloc();
     584    PXOPT_COPY_S64(config->args, where, "-dist_id", "distRun.dist_id", "==");
     585    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
     586    PXOPT_COPY_STR(config->args, where, "-component", "component", "==");;
     587    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
     588    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     589    pxAddLabelSearchArgs(config, where, "-label", "distRun.label", "==");
     590
     591    PXOPT_COPY_S16(config->args, where,  "-fault", "distComponent.fault", "==");
     592
     593    // It might be useful to be able to query by the parameters of the underlying runs
     594
     595    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
     596        psFree(where);
     597        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     598        return false;
     599    }
     600
     601    psString query = pxDataGet("disttool_revertcomponent.sql");
     602    if (!query) {
     603        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     604        psFree(where);
     605        return false;
     606    }
     607
     608    if (psListLength(where->list)) {
     609        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     610        psStringAppend(&query, " AND %s", whereClause);
     611        psFree(whereClause);
     612    }
     613    psFree(where);
     614
     615    if (!p_psDBRunQuery(config->dbh, query)) {
     616        psError(PS_ERR_UNKNOWN, false, "database error");
     617        psFree(query);
     618        return false;
     619    }
     620
     621    int numDeleted = psDBAffectedRows(config->dbh);
    584622
    585623    psLogMsg("disttool", PS_LOG_INFO, "Deleted %d distComponents", numDeleted);
    586624
    587     psFree(where);
    588     psFree(whereComponent);
    589 
    590     if (!psDBCommit(config->dbh)) {
    591         psError(PS_ERR_UNKNOWN, false, "database error");
    592         return false;
    593     }
    594 
    595625    return true;
    596626}
     
    600630    PS_ASSERT_PTR_NON_NULL(config, false);
    601631
    602     psMetadata *where = psMetadataAlloc();
    603     PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
    604     PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    605     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     632    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
     633
     634    psMetadata *where = psMetadataAlloc();
     635    PXOPT_COPY_S64(config->args, where, "-dist_id", "distRun.dist_id", "==");
     636    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
    606637
    607638    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    608639    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    609640
    610     // look for "inputs" that need to processed
    611     psString query = pxDataGet("disttool_pendingcomponent.sql");
     641    psString queryFile = NULL;
     642    psStringAppend(&queryFile, "disttool_pending_%s.sql", stage);
     643    psString query = pxDataGet(queryFile);
    612644    if (!query) {
    613         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     645        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement from %s", queryFile);
    614646        return false;
    615647    }
     
    617649    if (psListLength(where->list)) {
    618650        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    619         psStringAppend(&query, " WHERE %s", whereClause);
     651        psStringAppend(&query, " AND %s", whereClause);
    620652        psFree(whereClause);
    621653    }
     
    629661    }
    630662
    631     // the query has where hooks for each stage.
    632     // right now we aren't using them.
    633     // XXX: I think that I want to change the query from a union of selects on the various
    634     // stages to separate queries. As it is pending data at the later stages of the pipline
    635     // will get blocked by pending earlier stages
    636     psString    raw_where = "";
    637     psString    raw_clean_where = "";
    638     psString    chip_where = "";
    639     psString    camera_where = "";
    640     psString    fake_where = "";
    641     psString    warp_where = "";
    642     psString    diff_where = "";
    643     psString    stack_where = "";
    644 
    645     if (!p_psDBRunQueryF(config->dbh,
    646             query,
    647             raw_where,
    648             raw_clean_where,
    649             chip_where,
    650             camera_where,
    651             fake_where,
    652             warp_where,
    653             diff_where,
    654             stack_where)) {
     663    if (!p_psDBRunQuery(config->dbh, query)) {
    655664        psError(PS_ERR_UNKNOWN, false, "database error");
    656665        psFree(query);
     
    708717    PXOPT_LOOKUP_S32(bytes, config->args, "-bytes", require_fileinfo, false);
    709718    PXOPT_LOOKUP_STR(md5sum, config->args, "-md5sum", require_fileinfo, false);
     719    PXOPT_LOOKUP_STR(outdir, config->args, "-outdir", require_fileinfo, false);
    710720    PXOPT_LOOKUP_STR(name, config->args, "-name", require_fileinfo, false);
    711721
    712     if (!distComponentInsert(config->dbh, dist_id, component, bytes, md5sum, "full", name, fault)) {
     722    if (!distComponentInsert(config->dbh, dist_id, component, bytes, md5sum, "full", outdir, name, fault)) {
    713723        psError(PS_ERR_UNKNOWN, false, "database error");
    714724        return false;
     
    732742    psString query = pxDataGet("disttool_toadvance.sql");
    733743    if (!query) {
    734         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     744        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    735745        return false;
    736746    }
     
    803813    psString query = pxDataGet("disttool_processedcomponent.sql");
    804814    if (!query) {
    805         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     815        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    806816        return false;
    807817    }
     
    868878    psMetadata *where = psMetadataAlloc();
    869879    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
     880    PXOPT_COPY_STR(config->args, where, "-stage", "distRun.stage", "==");
    870881    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
    871882
     
    876887    psString query = pxDataGet("disttool_pendingfileset.sql");
    877888    if (!query) {
    878         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     889        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    879890        return false;
    880891    }
     
    939950    // required values
    940951    PXOPT_LOOKUP_S64(dist_id, config->args, "-dist_id", true, false);
    941     PXOPT_LOOKUP_S64(prod_id, config->args, "-prod_id", true, false);
     952    PXOPT_LOOKUP_S64(dest_id, config->args, "-dest_id", true, false);
    942953
    943954    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
     
    946957    PXOPT_LOOKUP_STR(name, config->args, "-name", fault == 0, false);
    947958
    948     if (!rcDSFilesetInsert(config->dbh, 
     959    if (!rcDSFilesetInsert(config->dbh,
    949960            0,          // fs_id
    950961            dist_id,
    951             prod_id,
     962            dest_id,
    952963            name,
    953964            "full",
     
    959970    return true;
    960971}
     972
    961973static bool revertfilesetMode(pxConfig *config)
    962974{
     
    964976    PXOPT_COPY_S64(config->args, where, "-fs_id", "fs_id", "==");
    965977    PXOPT_COPY_S64(config->args, where, "-dist_id", "rcDSFileset.dist_id", "==");
    966     PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
     978    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
    967979    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
    968980    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
     
    975987    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    976988        psFree(where);
    977         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     989        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    978990        return false;
    979991    }
     
    981993    psString query = pxDataGet("disttool_revertfileset.sql");
    982994    if (!query) {
    983         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     995        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    984996        psFree(where);
    985997        return false;
     
    10231035    psString query = pxDataGet("disttool_pendingdest.sql");
    10241036    if (!query) {
    1025         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1037        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    10261038        return false;
    10271039    }
     
    10921104    PXOPT_COPY_S64(config->args, where, "-dist_id",  "dist_id", "==");
    10931105    PXOPT_COPY_S64(config->args, where, "-dest_id",  "dest_id", "==");
    1094     PXOPT_COPY_S64(config->args, where, "-prod_id",  "prod_id", "==");
    10951106    PXOPT_COPY_S64(config->args, where, "-target_id","target_id", "==");
    10961107    PXOPT_COPY_S64(config->args, where, "-fs_id",    "fs_id", "==");
     
    11031114    psString query = pxDataGet("disttool_queuercrun.sql");
    11041115    if (!query) {
    1105         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1116        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    11061117        return false;
    11071118    }
     
    11491160    // We either need rc_id or (dest_id and fs_name) to identifiy the rcRun
    11501161    if ((!rc_id) && !(dest_id && fs_name)) {
    1151         psError(PXTOOLS_ERR_DATA, true, "either -rc_id or (-fs_name and -dest_id) are required");
     1162        psError(PXTOOLS_ERR_CONFIG, true, "either -rc_id or (-fs_name and -dest_id) are required");
    11521163        return false;
    11531164    }
     
    11611172        // this can't happen because we checked above
    11621173        psFree(where);
    1163         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     1174        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    11641175        return false;
    11651176    }
     
    11711182
    11721183    if (!state && (fault < 0)) {
    1173         psError(PXTOOLS_ERR_DATA, false, "parameters (-fault or -set_state) are required");
     1184        psError(PXTOOLS_ERR_CONFIG, false, "parameters (-fault or -set_state) are required");
    11741185        psFree(where);
    11751186        return false;
     
    11781189    psString query = pxDataGet("disttool_updatercrun.sql");
    11791190    if (!query) {
    1180         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1191        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    11811192        return false;
    11821193    }
     
    12351246    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    12361247        psFree(where);
    1237         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     1248        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    12381249        return false;
    12391250    }
     
    12411252    psString query = pxDataGet("disttool_revertrcrun.sql");
    12421253    if (!query) {
    1243         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1254        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    12441255        if (!psDBRollback(config->dbh)) {
    12451256            psError(PS_ERR_UNKNOWN, false, "database error");
     
    12761287
    12771288    // required
    1278     PXOPT_LOOKUP_STR(label, config->args, "-label", true, false);
     1289    PXOPT_LOOKUP_STR(dist_group, config->args, "-dist_group", true, false);
    12791290    PXOPT_LOOKUP_STR(filter, config->args, "-filter", true, false);
    12801291    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
     
    12871298    distTargetRow *row = distTargetRowAlloc(
    12881299            0,          // target_id
    1289             label,
     1300            dist_group,
    12901301            filter,
    12911302            stage,
     
    12941305            comment
    12951306            );
    1296            
     1307
    12971308    if (!row) {
    12981309        psError(PS_ERR_UNKNOWN, false, "failed to allocate distTarget object");
     
    13241335    psMetadata *where = psMetadataAlloc();
    13251336    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
    1326     PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     1337    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "==");
    13271338    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
    13281339    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     
    13541365}
    13551366
    1356 static bool listtargetMode(pxConfig *config)
     1367static bool listtargetsMode(pxConfig *config)
    13571368{
    13581369    PS_ASSERT_PTR_NON_NULL(config, false);
     
    13601371    psMetadata *where = psMetadataAlloc();
    13611372    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
    1362     PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     1373    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
    13631374    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
    13641375    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     
    14331444}
    14341445
    1435 static bool definedsproductMode(pxConfig *config)
     1446static bool definedestinationMode(pxConfig *config)
    14361447{
    14371448    PS_ASSERT_PTR_NON_NULL(config, false);
    14381449
    14391450    // required
    1440     PXOPT_LOOKUP_STR(name, config->args,   "-name", true, false);
    1441     PXOPT_LOOKUP_STR(dbname, config->args, "-ds_dbname", true, false);
    1442     PXOPT_LOOKUP_STR(dbhost, config->args, "-ds_dbhost", true, false);
     1451    PXOPT_LOOKUP_STR(name, config->args,         "-name", true, false);
     1452    PXOPT_LOOKUP_STR(dbname, config->args,       "-ds_dbname", true, false);
     1453    PXOPT_LOOKUP_STR(dbhost, config->args,       "-ds_dbhost", true, false);
     1454
     1455    // optional
     1456    PXOPT_LOOKUP_STR(status_uri, config->args,   "-status_uri", false, false);
     1457    PXOPT_LOOKUP_STR(comment, config->args,      "-comment", false, false);
     1458    PXOPT_LOOKUP_STR(last_fileset, config->args, "-last_fileset", false, false);
     1459    PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);
    14431460
    14441461    // XXX: should we insure that these names do not contatin any whitespace?
    14451462
    1446     rcDSProductRow *row = rcDSProductRowAlloc(
    1447             0,          // prod_id
     1463    rcDestinationRow *row = rcDestinationRowAlloc(
     1464            0,          // dest_id
    14481465            name,
     1466            status_uri,
     1467            comment,
     1468            last_fileset,
    14491469            dbname,
    1450             dbhost
     1470            dbhost,
     1471            state ? state : "enabled"
    14511472            );
    1452            
     1473
    14531474    if (!row) {
    1454         psError(PS_ERR_UNKNOWN, false, "failed to allocate rcDSProduct object");
    1455         return false;
    1456     }
    1457    if (!rcDSProductInsertObject(config->dbh, row)) {
     1475        psError(PS_ERR_UNKNOWN, false, "failed to allocate rcDestination object");
     1476        return false;
     1477    }
     1478   if (!rcDestinationInsertObject(config->dbh, row)) {
    14581479        psError(PS_ERR_UNKNOWN, false, "database error");
    14591480        psFree(row);
     
    14621483
    14631484    // get the assigned target_id
    1464     row->prod_id = psDBLastInsertID(config->dbh);
    1465 
    1466     if (!rcDSProductPrintObject(stdout, row, true)) {
     1485    row->dest_id = psDBLastInsertID(config->dbh);
     1486
     1487    if (!rcDestinationPrintObject(stdout, row, true)) {
    14671488        psError(PS_ERR_UNKNOWN, false, "failed to print object");
    14681489        psFree(row);
     
    14741495    return true;
    14751496}
    1476 static bool updatedsproductMode(pxConfig *config)
     1497
     1498static bool updatedestinationMode(pxConfig *config)
    14771499{
    14781500    PS_ASSERT_PTR_NON_NULL(config, false);
    14791501
    14801502    psMetadata *where = psMetadataAlloc();
    1481     PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
    1482 
    1483     PXOPT_LOOKUP_STR(dbname, config->args, "-ds_dbname", false, false);
    1484     PXOPT_LOOKUP_STR(dbhost, config->args, "-ds_dbhost", false, false);
    1485 
    1486     if (!(dbname || dbhost)) {
    1487         psError(PS_ERR_UNKNOWN, true, "one or more of dbname or dbhost is required");
    1488         psFree(where);
    1489         return false;
    1490     }
    1491     psString query = psStringCopy("UPDATE rcDSProduct SET");
     1503    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1504
     1505    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
     1506#ifdef ALLOW_UPDATE_LAST_FILESET
     1507    PXOPT_LOOKUP_STR(last_fileset, config->args, "-set_last_fileset", false, false);
     1508    if (!(state || last_fileset)) {
     1509        psError(PS_ERR_UNKNOWN, true, "one or more of -set_state or -set_last_fileset is required");
     1510# else
     1511    if (!state) {
     1512#endif
     1513        psFree(where);
     1514        return false;
     1515    }
     1516    psString query = psStringCopy("UPDATE rcDestination SET");
    14921517    psString sep = "";
    1493     if (dbname) {
    1494         psStringAppend(&query, " dbname = '%s'", dbname);
     1518    if (state) {
     1519        psStringAppend(&query, " state = '%s'", state);
    14951520        sep = ",";
    14961521    }
    1497     if (dbhost) {
    1498         psStringAppend(&query, " %s dbhost = '%s'", sep, dbhost);
    1499     }
     1522#ifdef ALLOW_UPDATE_LAST_FILESET
     1523    // last_fileset normally gets set by updatercrunMode
     1524    // Allowing it to be set here might cause problems
     1525    // especially since we are allowing selection by dest_id
     1526    if (last_fileset) {
     1527        psStringAppend(&query, " %s last_fileset = '%s'", sep, last_fileset);
     1528    }
     1529#endif
    15001530
    15011531    if (psListLength(where->list)) {
     
    15211551}
    15221552
    1523 static bool definedestinationMode(pxConfig *config)
     1553static bool defineinterestMode(pxConfig *config)
    15241554{
    15251555    PS_ASSERT_PTR_NON_NULL(config, false);
    15261556
    1527     // required
    1528     PXOPT_LOOKUP_S64(prod_id, config->args,      "-prod_id", true, false);
    1529     PXOPT_LOOKUP_STR(name, config->args,         "-name", true, false);
     1557    // one of these is required
     1558    PXOPT_LOOKUP_S64(dest_id, config->args,      "-dest_id", false, false);
     1559    PXOPT_LOOKUP_STR(dest_name, config->args,    "-dest_name", false, false);
     1560    if (!dest_id && !dest_name) {
     1561        psError(PS_ERR_UNKNOWN, true, "either dest_id or dest_name is required");
     1562        return false;
     1563    }
     1564
     1565    // either target_id or stage and label are required
     1566    PXOPT_LOOKUP_S64(target_id, config->args,    "-target_id", false, false);
     1567    PXOPT_LOOKUP_STR(stage, config->args,        "-stage", false, false);
     1568    PXOPT_LOOKUP_STR(dist_group, config->args,   "-dist_group", false, false);
     1569    PXOPT_LOOKUP_STR(filter, config->args,       "-filter", false, false);
     1570    PXOPT_LOOKUP_BOOL(clean, config->args,       "-clean", false);
     1571
     1572    if (!target_id) {
     1573        bool error = false;
     1574        if (!stage) {
     1575            psError(PS_ERR_UNKNOWN, true, "stage is required if target_id is not supplied");
     1576            error = true;
     1577        }
     1578        if (!dist_group) {
     1579            psError(PS_ERR_UNKNOWN, !error, "dist_group is required if target_id is not supplied");
     1580            error = true;
     1581        }
     1582        if (error) {
     1583            return false;
     1584        }
     1585    }
    15301586
    15311587    // optional
    1532     PXOPT_LOOKUP_STR(status_uri, config->args,   "-status_uri", false, false);
    1533     PXOPT_LOOKUP_STR(comment, config->args,      "-comment", false, false);
    1534     PXOPT_LOOKUP_STR(last_fileset, config->args, "-last_fileset", false, false);
    1535     PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);
    1536 
    1537     // XXX: should we insure that these names do not contatin any whitespace?
    1538 
    1539     rcDestinationRow *row = rcDestinationRowAlloc(
    1540             0,          // dest_id
    1541             prod_id,
    1542             name,
    1543             status_uri,
    1544             comment,
    1545             last_fileset,
    1546             state ? state : "enabled"
    1547             );
    1548            
    1549     if (!row) {
    1550         psError(PS_ERR_UNKNOWN, false, "failed to allocate rcDestination object");
    1551         return false;
    1552     }
    1553    if (!rcDestinationInsertObject(config->dbh, row)) {
    1554         psError(PS_ERR_UNKNOWN, false, "database error");
    1555         psFree(row);
    1556         return false;
    1557     }
    1558 
    1559     // get the assigned target_id
    1560     row->dest_id = psDBLastInsertID(config->dbh);
    1561 
    1562     if (!rcDestinationPrintObject(stdout, row, true)) {
    1563         psError(PS_ERR_UNKNOWN, false, "failed to print object");
    1564         psFree(row);
    1565         return false;
    1566     }
    1567 
    1568     psFree(row);
    1569 
    1570     return true;
    1571 }
    1572 
    1573 static bool updatedestinationMode(pxConfig *config)
     1588    PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false);
     1589    PXOPT_LOOKUP_STR(state, config->args,  "-set_state", false, false);
     1590    if (state) {
     1591        if (strcmp(state, "enabled") && strcmp(state, "disabled")) {
     1592            psError(PS_ERR_PROGRAMMING, true, "state must be enabled or disabled");
     1593            return false;
     1594        }
     1595    } else {
     1596        // default state
     1597        state = "enabled";
     1598    }
     1599
     1600    // now that we've done all of our argument checking, copy the values to where
     1601    psMetadata *where = psMetadataAlloc();
     1602    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1603    PXOPT_COPY_STR(config->args, where, "-dest_name", "rcDestination.name", "==");
     1604    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
     1605    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     1606    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1607    // if stage is all don't add it to the query (match all stages)
     1608    if (stage && strcmp(stage, "all")) {
     1609        PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1610    }
     1611
     1612    psString query = pxDataGet("disttool_defineinterest.sql");
     1613
     1614    if (!psListLength(where->list)) {
     1615        // can't get here
     1616        psError(PS_ERR_PROGRAMMING, true, "search parameters are required");
     1617        psFree(where);
     1618        psFree(query);
     1619        return false;
     1620    }
     1621    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1622    psStringAppend(&query, " AND %s", whereClause);
     1623    psFree(whereClause);
     1624    psFree(where);
     1625    if (clean) {
     1626        psStringAppend(&query, " AND (distTarget.clean)");
     1627    } else {
     1628        psStringAppend(&query, " AND (!distTarget.clean)");
     1629    }
     1630    if (limit) {
     1631        psString limitString = psDBGenerateLimitSQL(limit);
     1632        psStringAppend(&query, " %s", limitString);
     1633        psFree(limitString);
     1634    }
     1635    {
     1636        // psStringSubstitute fails unless the input is a psString which it determines by
     1637        // comparing the memory blocks free function to an expected value.
     1638        // pxDataGet uses psSlurp which leaves a different free function on the memory block.
     1639        // To work around this make a copy of the query before doing the substitution.
     1640        psString queryCopy = psStringCopy(query);
     1641        psFree(query);
     1642        query = queryCopy;
     1643    }
     1644    // change the @STATE@ in the sql file to our state
     1645    if (!psStringSubstitute(&query, state, "@STATE@")) {
     1646        psError(PS_ERR_UNKNOWN, false, "failed to substitute state string");
     1647        return false;
     1648    }
     1649
     1650    if (!p_psDBRunQuery(config->dbh, query)) {
     1651        psError(PS_ERR_UNKNOWN, false, "database error");
     1652        psFree(query);
     1653        return false;
     1654    }
     1655    psFree(query);
     1656    int numInserted = psDBAffectedRows(config->dbh);
     1657    printf("inserted %d rows into rcInterest\n", numInserted);
     1658
     1659    return true;
     1660}
     1661
     1662static bool updateinterestMode(pxConfig *config)
    15741663{
    15751664    PS_ASSERT_PTR_NON_NULL(config, false);
    15761665
    15771666    psMetadata *where = psMetadataAlloc();
    1578     PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
    1579     PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
     1667    PXOPT_COPY_S64(config->args, where, "-int_id",    "int_id", "==");
     1668    PXOPT_COPY_S64(config->args, where, "-dest_id",   "dest_id", "==");
     1669    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
     1670    PXOPT_COPY_STR(config->args, where, "-dest_name", "rcDestination.name", "LIKE");
     1671    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1672    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1673    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     1674
     1675    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
     1676    PXOPT_LOOKUP_BOOL(full, config->args, "-full", false);
     1677    if (full && clean) {
     1678        psError(PS_ERR_UNKNOWN, true, "-full and -clean makes no sense, chose one or the other");
     1679        psFree(where);
     1680        return false;
     1681    }
     1682       
    15801683
    15811684    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    1582 #ifdef ALLOW_UPDATE_LAST_FILESET
    1583     PXOPT_LOOKUP_STR(last_fileset, config->args, "-set_last_fileset", false, false);
    1584     if (!(state || last_fileset)) {
    1585         psError(PS_ERR_UNKNOWN, true, "one or more of -set_state or -set_last_fileset is required");
    1586 # else
    15871685    if (!state) {
    1588 #endif
    1589         psFree(where);
    1590         return false;
    1591     }
    1592     psString query = psStringCopy("UPDATE rcDestination SET");
    1593     psString sep = "";
    1594     if (state) {
    1595         psStringAppend(&query, " state = '%s'", state);
    1596         sep = ",";
    1597     }
    1598 #ifdef ALLOW_UPDATE_LAST_FILESET
    1599     // last_fileset normally gets set by updatercrunMode
    1600     // Allowing it to be set here might cause problems
    1601     // especially since we are allowing selection by prod_id
    1602     if (last_fileset) {
    1603         psStringAppend(&query, " %s last_fileset = '%s'", sep, last_fileset);
    1604     }
    1605 #endif
     1686        psError(PS_ERR_UNKNOWN, true, "-set_state is required");
     1687        psFree(where);
     1688        return false;
     1689    }
     1690    psString query = pxDataGet("disttool_updateinterest.sql");
    16061691
    16071692    if (psListLength(where->list)) {
     
    16171702    psFree(where);
    16181703
    1619     if (!p_psDBRunQuery(config->dbh, query)) {
     1704    if (clean) {
     1705        psStringAppend(&query, " AND distTarget.clean");
     1706    } else {
     1707        psStringAppend(&query, " AND NOT distTarget.clean");
     1708    }
     1709
     1710    if (!p_psDBRunQueryF(config->dbh, query, state)) {
    16201711        psError(PS_ERR_UNKNOWN, false, "database error");
    16211712        psFree(query);
     
    16241715    psFree(query);
    16251716
    1626     return true;
    1627 }
    1628 
    1629 static bool defineinterestMode(pxConfig *config)
     1717    psS64 numUpdated = psDBAffectedRows(config->dbh);
     1718    printf("updated %" PRId64 " interests\n", numUpdated);
     1719
     1720    return true;
     1721}
     1722static bool listinterestsMode(pxConfig *config)
    16301723{
    16311724    PS_ASSERT_PTR_NON_NULL(config, false);
    16321725
    1633     // required
    1634     PXOPT_LOOKUP_S64(dest_id, config->args,      "-dest_id", true, false);
    1635     PXOPT_LOOKUP_S64(target_id, config->args,    "-target_id", true, false);
    1636 
    1637     // optional
    1638     PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);
    1639 
    1640     // XXX: should we insure that these names do not contatin any whitespace?
    1641 
    1642     rcInterestRow *row = rcInterestRowAlloc(
    1643             0,          // int_id
    1644             dest_id,
    1645             target_id,
    1646             state ? state : "enabled"
    1647             );
    1648            
    1649     if (!row) {
    1650         psError(PS_ERR_UNKNOWN, false, "failed to allocate rcInterest object");
    1651         return false;
    1652     }
    1653    if (!rcInterestInsertObject(config->dbh, row)) {
    1654         psError(PS_ERR_UNKNOWN, false, "database error");
    1655         psFree(row);
    1656         return false;
    1657     }
    1658 
    1659     // get the assigned target_id
    1660     row->int_id = psDBLastInsertID(config->dbh);
    1661 
    1662     if (!rcInterestPrintObject(stdout, row, true)) {
    1663         psError(PS_ERR_UNKNOWN, false, "failed to print object");
    1664         psFree(row);
    1665         return false;
    1666     }
    1667 
    1668     psFree(row);
    1669 
    1670     return true;
    1671 }
    1672 
    1673 static bool updateinterestMode(pxConfig *config)
    1674 {
    1675     PS_ASSERT_PTR_NON_NULL(config, false);
    1676 
    1677     psMetadata *where = psMetadataAlloc();
    1678     PXOPT_COPY_S64(config->args, where, "-int_id",    "int_id", "==");
    1679     PXOPT_COPY_S64(config->args, where, "-dest_id",   "dest_id", "==");
     1726    psMetadata *where = psMetadataAlloc();
     1727    PXOPT_COPY_S64(config->args, where, "-int_id", "int_id", "==");
     1728    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1729    PXOPT_COPY_STR(config->args, where, "-dest_name", "name", "==");
    16801730    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
    1681 
    1682     PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    1683     if (!state) {
    1684         psError(PS_ERR_UNKNOWN, true, "-set_state is required");
    1685         psFree(where);
    1686         return false;
    1687     }
    1688     psString query = NULL;
    1689     psStringAppend(&query, "UPDATE rcInterest SET state = '%s'", state);
     1731    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1732    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     1733    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1734    PXOPT_COPY_STR(config->args, where, "-state", "rcInterest.state", "==");
     1735
     1736    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
     1737    PXOPT_LOOKUP_BOOL(full, config->args, "-full", false);
     1738
     1739    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1740    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1741
     1742    if (clean && full) {
     1743        psError(PS_ERR_UNKNOWN, false, "can't select both -clean and -full");
     1744        return false;
     1745    }
     1746
     1747    psString query = pxDataGet("disttool_listinterests.sql");
    16901748
    16911749    if (psListLength(where->list)) {
     
    16931751        psStringAppend(&query, " WHERE %s", whereClause);
    16941752        psFree(whereClause);
    1695     } else {
    1696         psError(PS_ERR_UNKNOWN, true, "search parameters are required");
    1697         psFree(where);
    1698         psFree(query);
    1699         return false;
     1753        if (clean) {
     1754            psStringAppend(&query, " AND (clean)");
     1755        } else if (full) {
     1756            psStringAppend(&query, " AND (!clean)");
     1757        }
     1758    } else if (clean) {
     1759        psStringAppend(&query, " WHERE clean");
     1760    } else if (full) {
     1761        psStringAppend(&query, " WHERE !clean");
    17001762    }
    17011763    psFree(where);
    17021764
     1765    // treat limit == 0 as "no limit"
     1766    if (limit) {
     1767        psString limitString = psDBGenerateLimitSQL(limit);
     1768        psStringAppend(&query, " %s", limitString);
     1769        psFree(limitString);
     1770    }
     1771
    17031772    if (!p_psDBRunQuery(config->dbh, query)) {
    17041773        psError(PS_ERR_UNKNOWN, false, "database error");
    17051774        psFree(query);
     1775        if (!psDBRollback(config->dbh)) {
     1776            psError(PS_ERR_UNKNOWN, false, "database error");
     1777        }
    17061778        return false;
    17071779    }
    17081780    psFree(query);
    17091781
    1710     return true;
    1711 }
    1712 
     1782    psArray *output = p_psDBFetchResult(config->dbh);
     1783    if (!output) {
     1784        psError(PS_ERR_UNKNOWN, false, "database error");
     1785        return false;
     1786    }
     1787    if (!psArrayLength(output)) {
     1788        psTrace("disttool", PS_LOG_INFO, "no rows found");
     1789        psFree(output);
     1790        return true;
     1791    }
     1792
     1793    if (!ippdbPrintMetadatas(stdout, output, "rcInterest", !simple)) {
     1794        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1795        psFree(output);
     1796        return false;
     1797    }
     1798
     1799    psFree(output);
     1800
     1801    return true;
     1802}
     1803static bool pendingcleanupMode(pxConfig *config)
     1804{
     1805    PS_ASSERT_PTR_NON_NULL(config, false);
     1806
     1807    psMetadata *where = psMetadataAlloc();
     1808    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
     1809    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1810    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
     1811
     1812    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1813    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1814    PXOPT_LOOKUP_BOOL(all, config->args, "-all", false);
     1815
     1816    psString query = pxDataGet("disttool_pendingcleanup.sql");
     1817
     1818    if (psListLength(where->list)) {
     1819        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1820        psStringAppend(&query, " AND %s", whereClause);
     1821        psFree(whereClause);
     1822        psFree(where);
     1823    } else if (!all) {
     1824        psFree(where);
     1825        psError(PXTOOLS_ERR_CONFIG, false, "search parameters or -all are required");
     1826        return false;
     1827    }
     1828
     1829    // treat limit == 0 as "no limit"
     1830    if (limit) {
     1831        psString limitString = psDBGenerateLimitSQL(limit);
     1832        psStringAppend(&query, " %s", limitString);
     1833        psFree(limitString);
     1834    }
     1835
     1836    if (!p_psDBRunQuery(config->dbh, query)) {
     1837        psError(PS_ERR_UNKNOWN, false, "database error");
     1838        psFree(query);
     1839        if (!psDBRollback(config->dbh)) {
     1840            psError(PS_ERR_UNKNOWN, false, "database error");
     1841        }
     1842        return false;
     1843    }
     1844    psFree(query);
     1845
     1846    psArray *output = p_psDBFetchResult(config->dbh);
     1847    if (!output) {
     1848        psError(PS_ERR_UNKNOWN, false, "database error");
     1849        return false;
     1850    }
     1851    if (!psArrayLength(output)) {
     1852        psTrace("disttool", PS_LOG_INFO, "no rows found");
     1853        psFree(output);
     1854        return true;
     1855    }
     1856
     1857    if (!ippdbPrintMetadatas(stdout, output, "distToCleanup", !simple)) {
     1858        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1859        psFree(output);
     1860        return false;
     1861    }
     1862
     1863    psFree(output);
     1864
     1865    return true;
     1866}
     1867static bool listfilesetsMode(pxConfig *config)
     1868{
     1869    PS_ASSERT_PTR_NON_NULL(config, false);
     1870
     1871    psMetadata *where = psMetadataAlloc();
     1872    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
     1873    PXOPT_COPY_S64(config->args, where, "-int_id", "int_id", "==");
     1874    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1875    PXOPT_COPY_STR(config->args, where, "-dest_name", "name", "==");
     1876    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
     1877    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1878    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     1879    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1880    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     1881
     1882    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
     1883    PXOPT_LOOKUP_BOOL(full, config->args, "-full", false);
     1884
     1885    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
     1886
     1887    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1888    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1889
     1890    if (!psListLength(where->list)) {
     1891        psFree(where);
     1892        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1893        return false;
     1894    }
     1895
     1896    psString query = pxDataGet("disttool_listfilesets.sql");
     1897
     1898    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1899    psStringAppend(&query, " WHERE %s", whereClause);
     1900    psFree(whereClause);
     1901    psFree(where);
     1902
     1903    // treat limit == 0 as "no limit"
     1904    if (limit) {
     1905        psString limitString = psDBGenerateLimitSQL(limit);
     1906        psStringAppend(&query, " %s", limitString);
     1907        psFree(limitString);
     1908    }
     1909
     1910    if (!p_psDBRunQuery(config->dbh, query)) {
     1911        psError(PS_ERR_UNKNOWN, false, "database error");
     1912        psFree(query);
     1913        if (!psDBRollback(config->dbh)) {
     1914            psError(PS_ERR_UNKNOWN, false, "database error");
     1915        }
     1916        return false;
     1917    }
     1918    psFree(query);
     1919
     1920    psArray *output = p_psDBFetchResult(config->dbh);
     1921    if (!output) {
     1922        psError(PS_ERR_UNKNOWN, false, "database error");
     1923        return false;
     1924    }
     1925    if (!psArrayLength(output)) {
     1926        psTrace("disttool", PS_LOG_INFO, "no rows found");
     1927        psFree(output);
     1928        return true;
     1929    }
     1930
     1931    if (!ippdbPrintMetadatas(stdout, output, "distFilesets", !simple)) {
     1932        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1933        psFree(output);
     1934        return false;
     1935    }
     1936
     1937    psFree(output);
     1938
     1939    return true;
     1940}
     1941static bool updatefilesetMode(pxConfig *config)
     1942{
     1943    psMetadata *where = psMetadataAlloc();
     1944    PXOPT_COPY_S64(config->args, where, "-fs_id", "fs_id", "==");
     1945
     1946    if (!psListLength(where->list)) {
     1947        psFree(where);
     1948        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1949        return false;
     1950    }
     1951
     1952    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
     1953
     1954    // We don't use PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false); here
     1955    // because we want -fault 0 to work
     1956    bool gotFault = false;
     1957    psS16 fault = psMetadataLookupS16(&gotFault, config->args, "-fault");
     1958
     1959    if ((!state) && (!gotFault)) {
     1960        psError(PXTOOLS_ERR_CONFIG, true, "parameters (-fault or -set_state) is required");
     1961        psFree(where);
     1962        return false;
     1963    }
     1964
     1965    psString query = psStringCopy("UPDATE rcDSFileset SET ");
     1966
     1967    if (state) {
     1968        psStringAppend(&query, " state = '%s'", state);
     1969    }
     1970
     1971    if (gotFault) {
     1972        psStringAppend(&query, "%s fault = %d", state ? ", " : "", fault);
     1973    }
     1974
     1975    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1976    psStringAppend(&query, " WHERE %s", whereClause);
     1977    psFree(whereClause);
     1978    psFree(where);
     1979
     1980    if (!p_psDBRunQuery(config->dbh, query)) {
     1981        psError(PS_ERR_UNKNOWN, false, "database error");
     1982        psFree(query);
     1983        return false;
     1984    }
     1985
     1986    return true;
     1987}
Note: See TracChangeset for help on using the changeset viewer.