IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 6973


Ignore:
Timestamp:
Apr 24, 2006, 5:02:04 PM (20 years ago)
Author:
jhoblitt
Message:

fill out initial implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/pzgetimfiles.c

    r6882 r6973  
    99#include "pzgetimfiles.h"
    1010
     11#define FILESET_LS_CMD "dsfilesetls"
     12
     13static psArray *parseFiles(pxConfig *config, const char *str);
     14
    1115int main(int argc, char **argv)
    1216{
    1317    pxConfig *config = pzgetimfilesConfig(NULL, argc, argv);
    1418
     19    // invoke dsfilesetls
     20    bool status = false;
     21    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
     22    psString cmd = NULL;
     23    psStringAppend(&cmd, "%s --uri %s", FILESET_LS_CMD, uri);
     24
     25    fprintf(stderr, "cmd is: %s\n", cmd);
     26
     27    FILE *output = popen(cmd, "r");
     28    psFree(cmd);
     29
     30    if (!output) {
     31        psError(PS_ERR_UNKNOWN, true, "popen() failed");
     32        goto FAIL;
     33    }
     34
     35    psString cmdOutput = fslurp(output);
     36    int exitStatus = pclose(output);
     37
     38    if (exitStatus != 0) {
     39        psError(PS_ERR_UNKNOWN, true, "%s failed with exit status %d",
     40            FILESET_LS_CMD, exitStatus);
     41        goto FAIL;
     42    }
     43
     44    // prase output of dsfilesetls
     45    psArray *newImfiles = parseFiles(config, cmdOutput);
     46    if (!newImfiles) {
     47        // XXX not nessicarily an error
     48        psError(PS_ERR_UNKNOWN, true, "no new files/imfiles");
     49        goto FAIL;
     50    }
     51
     52    // try not to insert duplicate pzPendingExp/pzPendingImfile entries
     53    // XXX this will become very expensive as the number of records grows
     54    // XXX change psDB to support inserting data with 'not exists'
     55    psArray *pzPendingImfiles =
     56        pzPendingImfileSelectRowObjects(config->dbh, NULL, 0);
     57    if (pzPendingImfiles) {
     58        for (long i = 0; i < psArrayLength(newImfiles); i++) {
     59            pzPendingImfileRow *newImfile = newImfiles->data[i];
     60            for (long j = 0; j < psArrayLength(pzPendingImfiles); j++) {
     61                pzPendingImfileRow *pendingImfile = pzPendingImfiles->data[j];
     62                if (strcmp(newImfile->exp_id,
     63                           pendingImfile->exp_id) == 0) {
     64                    psArrayRemove(newImfiles, newImfile);
     65                    // dec the counter as the array just got shorter
     66                    // and we don't want to skip elemnts
     67                    i--;
     68                    break;
     69                }
     70            }
     71        }
     72        psFree(pzPendingImfiles);
     73    }
     74
     75    // insert new entries into the database
     76    for (long i = 0; i < psArrayLength(newImfiles); i++) {
     77        if (!pzPendingImfileInsertObject(config->dbh, newImfiles->data[i])) {
     78            psError(PS_ERR_UNKNOWN, false, "dbh access failed");
     79            goto FAIL;
     80        }
     81    }
     82
     83    // save the number of new Imfiles;
     84    long nImfiles = psArrayLength(newImfiles);
     85    psFree(newImfiles);
     86
     87    // find the summitExp entry for our exp_id
     88    psArray *summitExps = NULL;
     89    {
     90        psMetadata *where = psMetadataAlloc();
     91        bool status = false;
     92        psString exp_id = psMetadataLookupStr(&status, config->args,
     93            "-filesetid");
     94        if (!status) {
     95                psError(PS_ERR_UNKNOWN, false, "failed to find arg exp_id");
     96                psFree(where);
     97                goto FAIL;
     98        }
     99        if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 0, "==", exp_id)) {
     100                psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     101                psFree(where);
     102                goto FAIL;
     103        }
     104        summitExps = summitExpSelectRowObjects(config->dbh, where, 0);
     105        psFree(where);
     106        if (!summitExps) {
     107            // this REALLY should not happen
     108            psError(PS_ERR_UNKNOWN, false,
     109                "failed to find summitExp with exp_id %s (should not happen)"
     110                , exp_id);
     111            goto FAIL;
     112        }
     113    }
     114
     115    // insert a new pzPendingExp
     116    summitExpRow *summitExp = summitExps->data[0];
     117    pzPendingExpRow *pzPendingExp = pzPendingExpRowAlloc(
     118        summitExp->exp_id,
     119        summitExp->camera,
     120        summitExp->telescope,
     121        summitExp->exp_type,
     122        nImfiles
     123    );
     124    psFree(summitExps);
     125    if (!pzPendingExp) {
     126        psError(PS_ERR_UNKNOWN, false, "pzPendingExpRowAlloc() failed");
     127        goto FAIL;
     128    }
     129
     130    if (!pzPendingExpInsertObject(config->dbh, pzPendingExp)) {
     131        psError(PS_ERR_UNKNOWN, false, "dbh access failed");
     132        psFree(pzPendingExp);
     133        goto FAIL;
     134    }
     135
     136    psFree(pzPendingExp);
     137
    15138    psFree(config);
    16 
    17139    exit(EXIT_SUCCESS);
    18140
     
    22144    exit(EXIT_FAILURE);
    23145}
     146
     147static psArray *parseFiles(pxConfig *config, const char *str)
     148{
     149    PS_ASSERT_PTR_NON_NULL(config, NULL);
     150    PS_ASSERT_PTR_NON_NULL(str, NULL);
     151
     152    psList *doc = psStringSplit(str, "\n");
     153
     154    psListIterator *lineCursor = psListIteratorAlloc(doc, 0, false);
     155
     156    psArray *pzPendingImfiles = psArrayAlloc(psListLength(doc));
     157    psString line;
     158    while ((line = psListGetAndIncrement(lineCursor))) {
     159        printf("-> %s\n", line);
     160
     161        // split line into tokens
     162        psList *tokens = psStringSplit(line, " ");
     163
     164        // check to see if this line is a comment (or if the first token is
     165        // NULL)
     166        if (!psListGet(tokens, 0) || *((char *)psListGet(tokens, 0)) == '#') {
     167            psFree(tokens);
     168            continue;
     169        }
     170
     171        // check that we have the right number of tokens
     172        // print "# uri fileid bytes md5sum type \n";
     173        if (psListLength(tokens) != 5) {
     174            psError(PS_ERR_UNKNOWN, true, "invalid line format: %s", line);
     175            return false;
     176        }
     177
     178        psListIterator *tokenCursor = psListIteratorAlloc(tokens, 0, false);
     179        char *uri       = psListGetAndIncrement(tokenCursor);
     180        char *class_id  = psListGetAndIncrement(tokenCursor); // fileid
     181        char *bytes     = psListGetAndIncrement(tokenCursor); // bytes
     182        char *md5sum    = psListGetAndIncrement(tokenCursor); // md5sum
     183        char *class     = psListGetAndIncrement(tokenCursor); // type
     184
     185        bool status = false;
     186        psString exp_id = psMetadataLookupStr(&status, config->args,
     187            "-filesetid");
     188
     189        pzPendingImfileRow *row = pzPendingImfileRowAlloc(
     190            exp_id,
     191            bytes,
     192            md5sum,
     193            class,
     194            class_id,
     195            uri
     196        );
     197
     198        psFree(tokenCursor);
     199        psFree(tokens);
     200
     201        psArrayAdd(pzPendingImfiles, 0, row);
     202    }
     203
     204    psFree(lineCursor);
     205    psFree(doc);
     206
     207    return pzPendingImfiles;;
     208}
Note: See TracChangeset for help on using the changeset viewer.