irplib_plugin.c

00001 /* $Id: irplib_plugin.c,v 1.32 2010/11/11 13:01:36 llundin Exp $
00002  *
00003  * This file is part of the irplib package 
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2010/11/11 13:01:36 $
00024  * $Revision: 1.32 $
00025  * $Name: HEAD $
00026  */
00027 
00028 /*-----------------------------------------------------------------------------
00029                                 Includes
00030  -----------------------------------------------------------------------------*/
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035 
00036 #include <string.h>
00037 #include <stdlib.h>
00038 #include <assert.h>
00039 
00040 #include <cpl.h>
00041 
00042 
00043 #include "irplib_plugin.h"
00044 
00045 /*----------------------------------------------------------------------------*/
00055 /*----------------------------------------------------------------------------*/
00056 
00057 /*-----------------------------------------------------------------------------
00058                                 Defines
00059  -----------------------------------------------------------------------------*/
00060 
00061 /* Maximum line length in SOF-file */
00062 #ifndef LINE_LEN_MAX
00063 #define LINE_LEN_MAX 1024
00064 #endif
00065 
00066 /* This device provides quite-random data */
00067 #define DEV_RANDOM "/dev/urandom"
00068 
00069 /* Copied from cpl_tools.h */
00070 #define recipe_assert(bool) \
00071   ((bool) ? (cpl_msg_debug(cpl_func, \
00072      "OK in " __FILE__ " line %d (CPL-error state: '%s' in %s): %s",__LINE__, \
00073        cpl_error_get_message(), cpl_error_get_where(), #bool), 0) \
00074           : (cpl_msg_error(cpl_func, \
00075      "Failure in " __FILE__ " line %d (CPL-error state: '%s' in %s): %s", \
00076       __LINE__, cpl_error_get_message(), cpl_error_get_where(), #bool), 1))
00077 
00078 
00079 
00080 /*-----------------------------------------------------------------------------
00081                             Private Function prototypes
00082  -----------------------------------------------------------------------------*/
00083 
00084 static const cpl_parameter * irplib_parameterlist_get(const cpl_parameterlist *,
00085                                                       const char *,
00086                                                       const char *,
00087                                                       const char *);
00088 
00089 static void recipe_parameterlist_set(cpl_parameterlist *);
00090 static void recipe_frameset_load(cpl_frameset *, const char *);
00091 
00092 static void recipe_sof_test_devfile(cpl_plugin *, const char *, size_t,
00093                                    const char *[]);
00094 static void recipe_sof_test_image_empty(cpl_plugin *, size_t, const char *[]);
00095 static void recipe_sof_test_local(cpl_plugin *);
00096 static void recipe_sof_test_from_env(cpl_plugin *);
00097 static void recipe_frameset_empty(cpl_frameset *);
00098 static void recipe_frameset_test_frame(const cpl_frame *);
00099 static void recipe_frameset_test_frameset_diff(const cpl_frameset *,
00100                                                const cpl_frameset *);
00101 
00102 static cpl_errorstate inistate;
00103 
00106 /*-----------------------------------------------------------------------------
00107                             Function definitions
00108  -----------------------------------------------------------------------------*/
00109 
00110 /*----------------------------------------------------------------------------*/
00120 /*----------------------------------------------------------------------------*/
00121 const char * irplib_parameterlist_get_string(const cpl_parameterlist * self,
00122                                              const char * instrume,
00123                                              const char * recipe,
00124                                              const char * parameter)
00125 {
00126 
00127     const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
00128                                                          recipe, parameter);
00129     const char * value;
00130 
00131     cpl_ensure(par != NULL, cpl_error_get_code(), NULL);
00132 
00133     value = cpl_parameter_get_string(par);
00134 
00135     if (value == NULL) (void)cpl_error_set_where(cpl_func);
00136 
00137     return value;
00138 
00139 }
00140 
00141 /*----------------------------------------------------------------------------*/
00151 /*----------------------------------------------------------------------------*/
00152 cpl_boolean irplib_parameterlist_get_bool(const cpl_parameterlist * self,
00153                                           const char * instrume,
00154                                           const char * recipe,
00155                                           const char * parameter)
00156 {
00157 
00158     const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
00159                                                          recipe, parameter);
00160     cpl_errorstate        prestate = cpl_errorstate_get();
00161     cpl_boolean           value;
00162 
00163 
00164     cpl_ensure(par != NULL, cpl_error_get_code(), CPL_FALSE);
00165 
00166     value = cpl_parameter_get_bool(par);
00167 
00168     if (!cpl_errorstate_is_equal(prestate)) (void)cpl_error_set_where(cpl_func);
00169 
00170     return value;
00171 
00172 }
00173 
00174 
00175 /*----------------------------------------------------------------------------*/
00185 /*----------------------------------------------------------------------------*/
00186 int irplib_parameterlist_get_int(const cpl_parameterlist * self,
00187                                  const char * instrume,
00188                                  const char * recipe,
00189                                  const char * parameter)
00190 {
00191 
00192     const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
00193                                                          recipe, parameter);
00194     cpl_errorstate        prestate = cpl_errorstate_get();
00195     int                   value;
00196 
00197 
00198     cpl_ensure(par != NULL, cpl_error_get_code(), 0);
00199 
00200     value = cpl_parameter_get_int(par);
00201 
00202     if (!cpl_errorstate_is_equal(prestate)) (void)cpl_error_set_where(cpl_func);
00203 
00204     return value;
00205 }
00206 
00207 /*----------------------------------------------------------------------------*/
00217 /*----------------------------------------------------------------------------*/
00218 double irplib_parameterlist_get_double(const cpl_parameterlist * self,
00219                                        const char * instrume,
00220                                        const char * recipe,
00221                                        const char * parameter)
00222 {
00223 
00224     const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
00225                                                          recipe, parameter);
00226     cpl_errorstate        prestate = cpl_errorstate_get();
00227     double                value;
00228 
00229 
00230     cpl_ensure(par != NULL, cpl_error_get_code(), 0.0);
00231 
00232     value = cpl_parameter_get_double(par);
00233 
00234     if (!cpl_errorstate_is_equal(prestate)) (void)cpl_error_set_where(cpl_func);
00235 
00236     return value;
00237 }
00238 
00239 /*----------------------------------------------------------------------------*/
00253 /*----------------------------------------------------------------------------*/
00254 cpl_error_code irplib_parameterlist_set_string(cpl_parameterlist * self,
00255                                                const char * instrume,
00256                                                const char * recipe,
00257                                                const char * parameter,
00258                                                const char * defvalue,
00259                                                const char * alias,
00260                                                const char * context,
00261                                                const char * man)
00262 {
00263 
00264     cpl_error_code  error;
00265     cpl_parameter * par;
00266     char          * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
00267                                             parameter);
00268 
00269     cpl_ensure_code(paramname != NULL, cpl_error_get_code());
00270     
00271     par = cpl_parameter_new_value(paramname, CPL_TYPE_STRING, man, context,
00272                                   defvalue);
00273     cpl_free(paramname);
00274 
00275     cpl_ensure_code(par != NULL, cpl_error_get_code());
00276     
00277     error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
00278                                     alias ? alias : parameter);
00279     cpl_ensure_code(!error, error);
00280 
00281     error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
00282     cpl_ensure_code(!error, error);
00283 
00284     error = cpl_parameterlist_append(self, par);
00285     cpl_ensure_code(!error, error);
00286     
00287     return CPL_ERROR_NONE;
00288 }
00289 
00290 
00291 /*----------------------------------------------------------------------------*/
00305 /*----------------------------------------------------------------------------*/
00306 cpl_error_code irplib_parameterlist_set_bool(cpl_parameterlist * self,
00307                                              const char * instrume,
00308                                              const char * recipe,
00309                                              const char * parameter,
00310                                              cpl_boolean  defvalue,
00311                                              const char * alias,
00312                                              const char * context,
00313                                              const char * man)
00314 {
00315 
00316     cpl_error_code  error;
00317     cpl_parameter * par;
00318     char          * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
00319                                             parameter);
00320 
00321     cpl_ensure_code(paramname != NULL, cpl_error_get_code());
00322     
00323     par = cpl_parameter_new_value(paramname, CPL_TYPE_BOOL, man, context,
00324                                   defvalue);
00325     cpl_free(paramname);
00326 
00327     cpl_ensure_code(par != NULL, cpl_error_get_code());
00328     
00329     error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
00330                                     alias ? alias : parameter);
00331     cpl_ensure_code(!error, error);
00332     
00333     error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
00334     cpl_ensure_code(!error, error);
00335 
00336     error = cpl_parameterlist_append(self, par);
00337     cpl_ensure_code(!error, error);
00338     
00339     return CPL_ERROR_NONE;
00340 }
00341 
00342 
00343 
00344 /*----------------------------------------------------------------------------*/
00358 /*----------------------------------------------------------------------------*/
00359 cpl_error_code irplib_parameterlist_set_int(cpl_parameterlist * self,
00360                                             const char * instrume,
00361                                             const char * recipe,
00362                                             const char * parameter,
00363                                             int         defvalue,
00364                                             const char * alias,
00365                                             const char * context,
00366                                             const char * man)
00367 {
00368 
00369     cpl_error_code  error;
00370     cpl_parameter * par;
00371     char          * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
00372                                             parameter);
00373 
00374     cpl_ensure_code(paramname != NULL, cpl_error_get_code());
00375     
00376     par = cpl_parameter_new_value(paramname, CPL_TYPE_INT, man, context,
00377                                   defvalue);
00378     cpl_free(paramname);
00379 
00380     cpl_ensure_code(par != NULL, cpl_error_get_code());
00381     
00382     error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
00383                                     alias ? alias : parameter);
00384     cpl_ensure_code(!error, error);
00385     
00386     error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
00387     cpl_ensure_code(!error, error);
00388 
00389     error = cpl_parameterlist_append(self, par);
00390     cpl_ensure_code(!error, error);
00391     
00392     return CPL_ERROR_NONE;
00393 }
00394 
00395 
00396 /*----------------------------------------------------------------------------*/
00410 /*----------------------------------------------------------------------------*/
00411 cpl_error_code irplib_parameterlist_set_double(cpl_parameterlist * self,
00412                                                const char * instrume,
00413                                                const char * recipe,
00414                                                const char * parameter,
00415                                                double       defvalue,
00416                                                const char * alias,
00417                                                const char * context,
00418                                                const char * man)
00419 {
00420 
00421     cpl_error_code  error;
00422     cpl_parameter * par;
00423     char          * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
00424                                             parameter);
00425 
00426     cpl_ensure_code(paramname != NULL, cpl_error_get_code());
00427     
00428     par = cpl_parameter_new_value(paramname, CPL_TYPE_DOUBLE, man, context,
00429                                   defvalue);
00430     cpl_free(paramname);
00431 
00432     cpl_ensure_code(par != NULL, cpl_error_get_code());
00433     
00434     error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
00435                                     alias ? alias : parameter);
00436     cpl_ensure_code(!error, error);
00437     
00438     error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
00439     cpl_ensure_code(!error, error);
00440 
00441     error = cpl_parameterlist_append(self, par);
00442     cpl_ensure_code(!error, error);
00443     
00444     return CPL_ERROR_NONE;
00445 }
00446 
00447 
00448 /*----------------------------------------------------------------------------*/
00462 /*----------------------------------------------------------------------------*/
00463 int irplib_plugin_test(cpl_pluginlist * self, size_t nstr, const char *astr[]) {
00464 
00465     cpl_plugin     * plugin;
00466     cpl_recipe     * recipe;
00467     int            (*recipe_create) (cpl_plugin *);
00468     int            (*recipe_exec  ) (cpl_plugin *);
00469     int            (*recipe_deinit) (cpl_plugin *);
00470     const cpl_msg_severity msg_level = cpl_msg_get_level();
00471     cpl_error_code error;
00472     FILE         * stream;
00473     cpl_boolean    is_debug;
00474 
00475 
00476     /* Unless the CPL_MSG_LEVEL has been explicitly set, turn off
00477        terminal messaging completely while inside this function */
00478     if (getenv("CPL_MSG_LEVEL") == NULL) cpl_msg_set_level(CPL_MSG_OFF);
00479 
00480     is_debug = cpl_msg_get_level() <= CPL_MSG_DEBUG ? CPL_TRUE : CPL_FALSE;
00481 
00482     /* Modified from CPL unit tests */
00483     stream = is_debug ? stdout : fopen("/dev/null", "a");
00484 
00485     inistate = cpl_errorstate_get();
00486 
00487     assert( nstr == 0 || astr != NULL );
00488 
00489     plugin = cpl_pluginlist_get_first(self);
00490 
00491     if (plugin == NULL) {
00492         cpl_msg_warning(cpl_func, "With an empty pluginlist, "
00493                         "no tests can be made");
00494         cpl_msg_set_level(msg_level);
00495         return 0;
00496     }
00497 
00498     cpl_plugin_dump(plugin, stream);
00499 
00500     recipe_create = cpl_plugin_get_init(plugin);
00501     cpl_test( recipe_create != NULL);
00502 
00503     recipe_exec   = cpl_plugin_get_exec(plugin);
00504     cpl_test( recipe_exec != NULL);
00505 
00506     recipe_deinit = cpl_plugin_get_deinit(plugin);
00507     cpl_test( recipe_deinit != NULL);
00508 
00509     /* Only plugins of type recipe are tested (further)  */
00510     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00511         cpl_msg_warning(cpl_func, "This plugin is not of type recipe, "
00512                       "cannot test further");
00513         cpl_msg_set_level(msg_level);
00514         return 0;
00515     }
00516 
00517     cpl_test_zero(recipe_create(plugin));
00518 
00519     recipe = (cpl_recipe *) plugin;
00520 
00521     cpl_test_nonnull( recipe->parameters );
00522 
00523     recipe_parameterlist_set(recipe->parameters);
00524 
00525     cpl_parameterlist_dump(recipe->parameters, stream);
00526 
00527     recipe->frames = cpl_frameset_new();
00528 
00529     cpl_msg_info(cpl_func,"Checking handling of pre-existing CPL error state - "
00530                  "may produce warning(s)/error(s):");
00531     cpl_error_set(cpl_func, CPL_ERROR_EOL);
00532     /* Call recipe and expect non-zero return code */
00533     cpl_test( recipe_exec(plugin) );
00534     /* Expect also the CPL error code to be preserved */
00535     cpl_test_error( CPL_ERROR_EOL );
00536 
00537     cpl_msg_info(cpl_func,"Checking handling of empty frameset - "
00538                  "may produce warning(s)/error(s):");
00539     /* Call recipe and expect non-zero return code */
00540     cpl_test( recipe_exec(plugin) );
00541     error = cpl_error_get_code();
00542     /* Expect also the CPL error code to be set */
00543     cpl_test_error( error );
00544     cpl_test( error );
00545 
00546     cpl_msg_info(cpl_func,"Checking handling of dummy frameset - "
00547                  "may produce warning(s)/error(s):");
00548     do {
00549         cpl_frame * f = cpl_frame_new();
00550         error = cpl_frame_set_filename(f, "/dev/null");
00551         cpl_test_eq_error(error, CPL_ERROR_NONE);
00552         error = cpl_frame_set_tag(f, "RECIPE_DUMMY_TAG");
00553         cpl_test_eq_error(error, CPL_ERROR_NONE);
00554         error = cpl_frameset_insert(recipe->frames, f);
00555         cpl_test_eq_error(error, CPL_ERROR_NONE);
00556 
00557         /* Call recipe and expect non-zero return code */
00558         cpl_test( recipe_exec(plugin) );
00559         error = cpl_error_get_code();
00560         /* Expect also the CPL error code to be set */
00561         cpl_test_error( error );
00562         cpl_test( error );
00563 
00564         error = cpl_frameset_erase_frame(recipe->frames, f);
00565         cpl_test_eq_error(error, CPL_ERROR_NONE);
00566 
00567     } while (0);
00568 
00569 #ifdef IRPLIB_TEST_RANDOM_SOF
00570     recipe_sof_test_devfile(plugin, DEV_RANDOM, nstr, astr);
00571 #endif
00572 
00573     recipe_sof_test_devfile(plugin, "/dev/null", nstr, astr);
00574 
00575     recipe_sof_test_devfile(plugin, ".", nstr, astr);
00576 
00577     recipe_sof_test_image_empty(plugin, nstr, astr);
00578 
00579     recipe_sof_test_local(plugin);
00580 
00581     recipe_sof_test_from_env(plugin);
00582 
00583     cpl_frameset_delete(recipe->frames);
00584 
00585     error = recipe_deinit(plugin);
00586     cpl_test_eq_error(error, CPL_ERROR_NONE);
00587 
00588     cpl_msg_set_level(msg_level);
00589 
00590     if (stream != stdout) fclose(stream);
00591 
00592     return 0;
00593 }
00594 
00597 /*----------------------------------------------------------------------------*/
00607 /*----------------------------------------------------------------------------*/
00608 static void recipe_parameterlist_set(cpl_parameterlist * self)
00609 {
00610 
00611     cpl_parameter * p = cpl_parameterlist_get_first(self);
00612 
00613     for (; p != NULL; p = cpl_parameterlist_get_next(self)) {
00614 
00615         const char * envvar;
00616         const char * svalue;
00617 
00618         /* FIXME: Needed ? */
00619         if (cpl_parameter_get_default_flag(p)) continue;
00620 
00621         cpl_msg_debug(cpl_func, __FILE__ " line %u: OK", __LINE__);
00622 
00623         envvar = cpl_parameter_get_alias(p, CPL_PARAMETER_MODE_ENV);
00624         svalue = envvar ? getenv(envvar) : NULL;
00625 
00626         switch (cpl_parameter_get_type(p)) {
00627         case CPL_TYPE_BOOL: {
00628             const int value
00629                 = svalue ? atoi(svalue) : cpl_parameter_get_default_bool(p);
00630             cpl_parameter_set_bool(p, value);
00631             break;
00632         }
00633         case CPL_TYPE_INT: {
00634             const int value
00635                 = svalue ? atoi(svalue) : cpl_parameter_get_default_int(p);
00636             cpl_parameter_set_int(p, value);
00637             break;
00638         }
00639         case CPL_TYPE_DOUBLE: {
00640             const double value
00641                 = svalue ? atof(svalue) : cpl_parameter_get_default_double(p);
00642             cpl_parameter_set_double(p, value);
00643             break;
00644         }
00645         case CPL_TYPE_STRING:
00646             {
00647                 const char * s_default = cpl_parameter_get_default_string(p);
00648                 /* Replace NULL with "" */
00649                 const char * value
00650                     = svalue ? svalue : (s_default ? s_default : "");
00651                 cpl_parameter_set_string(p, value);
00652                 break;
00653             }
00654 
00655         default:
00656             assert( 0 ); /* It is a testing error to reach this point */
00657         }
00658     }
00659 }
00660 
00661 
00662 /*----------------------------------------------------------------------------*/
00672 /*----------------------------------------------------------------------------*/
00673 static void recipe_sof_test_devfile(cpl_plugin * plugin, const char * filename,
00674                                     size_t nstr, const char *astr[])
00675 {
00676     cpl_recipe * recipe  = (cpl_recipe*)plugin;
00677     int       (*recipe_exec) (cpl_plugin *);
00678     cpl_frameset * copy;
00679     cpl_error_code error;
00680     size_t i;
00681 
00682 
00683     if (nstr < 1) return;
00684     if (filename == NULL) return;
00685 
00686     cpl_msg_info(cpl_func, "Testing recipe with %u %s as input ",
00687                  (unsigned)nstr, filename);
00688 
00689     for (i = 0; i < nstr; i++) {
00690         cpl_frame * f = cpl_frame_new();
00691 
00692         error = cpl_frame_set_filename(f, filename);
00693         cpl_test_eq_error(error, CPL_ERROR_NONE);
00694 
00695         error = cpl_frame_set_tag(f, astr[i]);
00696         cpl_test_eq_error(error, CPL_ERROR_NONE);
00697 
00698         error = cpl_frameset_insert(recipe->frames, f);
00699         cpl_test_eq_error(error, CPL_ERROR_NONE);
00700     }
00701 
00702     copy = cpl_frameset_duplicate(recipe->frames);
00703 
00704     recipe_exec = cpl_plugin_get_exec(plugin);
00705     cpl_test( recipe_exec != NULL);
00706 
00707     /* Call recipe and expect non-zero return code */
00708     cpl_test( recipe_exec(plugin) );
00709     error = cpl_error_get_code();
00710     /* Expect also the CPL error code to be set */
00711     cpl_test_error( error );
00712     cpl_test( error );
00713 
00714     recipe_frameset_test_frameset_diff(recipe->frames, copy);
00715 
00716     recipe_frameset_empty(recipe->frames);
00717 
00718     cpl_frameset_delete(copy);
00719 
00720     return;
00721 }
00722 
00723 /*----------------------------------------------------------------------------*/
00730 /*----------------------------------------------------------------------------*/
00731 static void recipe_sof_test_image_empty(cpl_plugin * plugin, size_t nstr,
00732                                         const char *astr[])
00733 {
00734     cpl_recipe * recipe  = (cpl_recipe*)plugin;
00735     int       (*recipe_exec) (cpl_plugin *);
00736     cpl_frameset * copy;
00737     cpl_error_code error;
00738     size_t i;
00739     cpl_frame * frame;
00740     cpl_image * iempty;
00741 
00742 
00743     if (nstr < 1) return;
00744 
00745     cpl_msg_info(cpl_func, "Testing recipe with %u empty images as input ",
00746                  (unsigned)nstr);
00747 
00748     iempty = cpl_image_new(13, 17, CPL_TYPE_FLOAT);
00749     cpl_test_nonnull(iempty);
00750 
00751     for (i = 0; i < nstr; i++) {
00752         cpl_frame * f = cpl_frame_new();
00753         char * rawname = cpl_sprintf("raw%05u.fits", (unsigned)(i+1));
00754 
00755         error = cpl_image_save(iempty, rawname,CPL_BPP_IEEE_FLOAT, NULL,
00756                                CPL_IO_DEFAULT);
00757         cpl_test_eq_error(error, CPL_ERROR_NONE);
00758 
00759         error = cpl_frame_set_filename(f, rawname);
00760         cpl_test_eq_error(error, CPL_ERROR_NONE);
00761 
00762         error = cpl_frame_set_tag(f, astr[i]);
00763         cpl_test_eq_error(error, CPL_ERROR_NONE);
00764 
00765         error = cpl_frameset_insert(recipe->frames, f);
00766         cpl_test_eq_error(error, CPL_ERROR_NONE);
00767 
00768         cpl_free(rawname);
00769     }
00770     cpl_image_delete(iempty);
00771 
00772     copy = cpl_frameset_duplicate(recipe->frames);
00773 
00774     recipe_exec = cpl_plugin_get_exec(plugin);
00775     cpl_test(recipe_exec != NULL);
00776 
00777     /* Call recipe and expect non-zero return code */
00778     cpl_test( recipe_exec(plugin) );
00779     error = cpl_error_get_code();
00780     /* Expect also the CPL error code to be set */
00781     cpl_test_error( error );
00782     cpl_test( error );
00783 
00784     for (frame = cpl_frameset_get_first(recipe->frames); frame != NULL;
00785          frame = cpl_frameset_get_next(recipe->frames))
00786         {
00787             cpl_test_zero( remove(cpl_frame_get_filename(frame)) );
00788         }
00789 
00790     recipe_frameset_test_frameset_diff(recipe->frames, copy);
00791 
00792     recipe_frameset_empty(recipe->frames);
00793 
00794     cpl_frameset_delete(copy);
00795 
00796     return;
00797 }
00798 
00799 
00800 /*----------------------------------------------------------------------------*/
00807 /*----------------------------------------------------------------------------*/
00808 static void recipe_sof_test_from_env(cpl_plugin * plugin)
00809 {
00810     cpl_recipe * recipe  = (cpl_recipe*)plugin;
00811     const char * recipename = cpl_plugin_get_name(plugin);
00812     const char * var_name = "RECIPE_SOF_PATH";
00813     const char * sof_path = getenv(var_name);
00814     cpl_error_code error;
00815 
00816     char * sof_name;
00817 
00818     if (sof_path == NULL) {
00819         cpl_msg_warning(cpl_func, "Environment variable %s is unset: "
00820                         "No SOFs to check", var_name);
00821         return;
00822     }
00823 
00824     cpl_msg_debug(cpl_func, "Checking for SOFs in %s", sof_path);
00825 
00826     cpl_test_nonnull( recipename );
00827     if (recipename == NULL) return;
00828 
00829     sof_name = cpl_sprintf("%s/%s.sof", sof_path, recipename);
00830 
00831     cpl_msg_debug(cpl_func, "Checking for SOF %s", sof_name);
00832     
00833     recipe_frameset_load(recipe->frames, sof_name);
00834 
00835     if (!cpl_frameset_is_empty(recipe->frames)) {
00836 
00837         int          (*recipe_exec  ) (cpl_plugin *);
00838         cpl_frameset * copy = cpl_frameset_duplicate(recipe->frames);
00839 
00840         recipe_exec   = cpl_plugin_get_exec(plugin);
00841         cpl_test(recipe_exec != NULL);
00842 
00843         cpl_msg_info(cpl_func,"Checking handling of SOF: %s", sof_name);
00844 
00845         /* Call recipe and expect zero return code */
00846         cpl_test_zero( recipe_exec(plugin) );
00847         /* Expect also the CPL error code to be clear */
00848         cpl_test_error(CPL_ERROR_NONE);
00849 
00850         error = cpl_dfs_update_product_header(recipe->frames);
00851         cpl_test_eq_error(error, CPL_ERROR_NONE);
00852 
00853         recipe_frameset_test_frameset_diff(recipe->frames, copy);
00854 
00855         recipe_frameset_empty(recipe->frames);
00856 
00857         cpl_frameset_delete(copy);
00858 
00859     }
00860 
00861     cpl_free(sof_name);
00862 
00863     return;
00864 }
00865 
00866 
00867 
00868 /*----------------------------------------------------------------------------*/
00875 /*----------------------------------------------------------------------------*/
00876 static void recipe_sof_test_local(cpl_plugin * plugin)
00877 {
00878     cpl_recipe * recipe  = (cpl_recipe*)plugin;
00879     const char * recipename = cpl_plugin_get_name(plugin);
00880     cpl_error_code error;
00881     char * sof_name = cpl_sprintf("%s.sof", recipename);
00882 
00883     cpl_msg_debug(cpl_func, "Checking for SOF %s", sof_name);
00884     
00885     recipe_frameset_load(recipe->frames, sof_name);
00886 
00887     if (!cpl_frameset_is_empty(recipe->frames)) {
00888 
00889         int          (*recipe_exec  ) (cpl_plugin *);
00890         cpl_frameset * copy = cpl_frameset_duplicate(recipe->frames);
00891 
00892         recipe_exec   = cpl_plugin_get_exec(plugin);
00893         cpl_test(recipe_exec != NULL);
00894 
00895         cpl_msg_info(cpl_func,"Checking handling of SOF: %s", sof_name);
00896 
00897         /* Call recipe and expect zero return code */
00898         cpl_test_zero( recipe_exec(plugin) );
00899         /* Expect also the CPL error code to be clear */
00900         cpl_test_error(CPL_ERROR_NONE);
00901 
00902         error = cpl_dfs_update_product_header(recipe->frames);
00903         cpl_test_eq_error( error, CPL_ERROR_NONE );
00904 
00905         recipe_frameset_test_frameset_diff(recipe->frames, copy);
00906 
00907         recipe_frameset_empty(recipe->frames);
00908 
00909         cpl_frameset_delete(copy);
00910     }
00911 
00912     cpl_free(sof_name);
00913 
00914     return;
00915 }
00916 
00917 
00918 
00919 
00920 /**********************************************************************/
00934 /**********************************************************************/
00935 
00936 static void recipe_frameset_load(cpl_frameset * set, const char *name)
00937 {
00938 
00939     FILE *fp;
00940     char line[LINE_LEN_MAX];
00941     char path[LINE_LEN_MAX], group[LINE_LEN_MAX], tag[LINE_LEN_MAX];
00942     int line_number;
00943 
00944     assert( set != NULL );
00945     assert( name != NULL );
00946 
00947     fp = fopen(name, "r");
00948     if (fp == NULL) {
00949         cpl_msg_debug(cpl_func, "Unable to open SOF file '%s'", name);
00950         return;
00951     }
00952 
00953     /* Loop over all the lines in the set-of-frames file */
00954     for (line_number = 0; fgets(line, LINE_LEN_MAX - 1, fp); line_number++) {
00955 
00956         cpl_frame_group grp;
00957         cpl_frame * frame;
00958         int n;
00959 
00960         if (line[0] == '#') continue;
00961 
00962         n = sscanf(line, "%s %s %s", path, tag, group);
00963 
00964         if (n < 1) {
00965             cpl_msg_warning(cpl_func, "Spurious line no. %d in %s: %s",
00966                             line_number, name, line);
00967             break;
00968         }
00969 
00970         /* Allocate a new frame */
00971         frame = cpl_frame_new();
00972 
00973         /* Set the filename component of the frame */
00974         cpl_frame_set_filename(frame, path);
00975 
00976         /* Set the tag component of the frame (or set a default) */
00977         cpl_frame_set_tag(frame, n == 1 ? "" : tag);
00978 
00979         cpl_frameset_insert(set, frame);
00980 
00981         /* Set the group component of the frame (or set a default) */
00982         if (n < 3) continue;
00983 
00984         if (!strcmp(group, CPL_FRAME_GROUP_RAW_ID))
00985             grp = CPL_FRAME_GROUP_RAW;
00986         else if (!strcmp(group, CPL_FRAME_GROUP_CALIB_ID))
00987             grp = CPL_FRAME_GROUP_CALIB;
00988         else if (!strcmp(group, CPL_FRAME_GROUP_PRODUCT_ID))
00989             grp = CPL_FRAME_GROUP_PRODUCT;
00990         else
00991             grp = CPL_FRAME_GROUP_NONE;
00992 
00993         cpl_frame_set_group(frame, grp);
00994     }
00995 
00996     fclose(fp);
00997 
00998     return;
00999 
01000 }
01001 
01002 
01003 /*----------------------------------------------------------------------------*/
01013 /*----------------------------------------------------------------------------*/
01014 static
01015 const cpl_parameter * irplib_parameterlist_get(const cpl_parameterlist * self,
01016                                                const char * instrume,
01017                                                const char * recipe,
01018                                                const char * parameter)
01019 {
01020 
01021     const char          * paramname;
01022     const cpl_parameter * par;
01023 
01024 
01025     cpl_ensure(instrume  != NULL, CPL_ERROR_NULL_INPUT, NULL);
01026     cpl_ensure(recipe    != NULL, CPL_ERROR_NULL_INPUT, NULL);
01027     cpl_ensure(parameter != NULL, CPL_ERROR_NULL_INPUT, NULL);
01028 
01029     paramname = cpl_sprintf("%s.%s.%s", instrume, recipe, parameter);
01030 
01031     par = cpl_parameterlist_find_const(self, paramname);
01032 
01033     if (par == NULL) (void)cpl_error_set_message(cpl_func,
01034                                                  cpl_error_get_code()
01035                                                  ? cpl_error_get_code()
01036                                                  : CPL_ERROR_DATA_NOT_FOUND,
01037                                                  "%s", paramname);
01038 
01039     cpl_free((void*)paramname);
01040     
01041     return par;
01042 
01043 }
01044 
01045 
01046 /*----------------------------------------------------------------------------*/
01072 /*----------------------------------------------------------------------------*/
01073 static void recipe_frameset_empty(cpl_frameset * self)
01074 {
01075     cpl_frame * f;
01076 
01077     if (self == NULL) {
01078         cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
01079         return;
01080     }
01081 
01082     for (f = cpl_frameset_get_first(self); f != NULL;
01083          f = cpl_frameset_get_first(self))
01084         {
01085             cpl_frameset_erase_frame(self, f);
01086         }
01087 }
01088 
01089 
01090 /*----------------------------------------------------------------------------*/
01110 /*----------------------------------------------------------------------------*/
01111 static void recipe_frameset_test_frame(const cpl_frame * self)
01112 {
01113 
01114     cpl_msg_info(cpl_func, "Validating new frame: %s",
01115                  cpl_frame_get_filename(self));
01116 
01117     cpl_test_nonnull(self);
01118 
01119     /* Frame must be tagged */
01120     cpl_test_nonnull(cpl_frame_get_tag(self));
01121 
01122     /* New frames must be products */
01123     cpl_test_eq(cpl_frame_get_group(self), CPL_FRAME_GROUP_PRODUCT);
01124 
01125     if (cpl_frame_get_type(self) != CPL_FRAME_TYPE_PAF) {
01126         /* All but PAF (?) must be FITS */
01127         cpl_test_fits(cpl_frame_get_filename(self));
01128     } else {
01129         /* Frame must at least have a filename */
01130         cpl_test_nonnull(cpl_frame_get_filename(self));
01131     }
01132 }
01133 
01134 /*----------------------------------------------------------------------------*/
01155 /*----------------------------------------------------------------------------*/
01156 static void recipe_frameset_test_frameset_diff(const cpl_frameset * self,
01157                                                const cpl_frameset * other)
01158 {
01159 
01160     const cpl_frame * frame = cpl_frameset_get_first_const(other);
01161 
01162     /* First verify that filenames in other are non-NULL */
01163     for (;frame != NULL; frame = cpl_frameset_get_next_const(other)) {
01164         const char * file = cpl_frame_get_filename(frame);
01165 
01166         if (file == NULL) {
01167             cpl_test_nonnull(cpl_frame_get_filename(frame));
01168             break;
01169         }
01170     }
01171     if (frame != NULL) return;
01172 
01173     frame = cpl_frameset_get_first_const(self);
01174 
01175     for (;frame != NULL; frame = cpl_frameset_get_next_const(self)) {
01176         const cpl_frame * cmp  = cpl_frameset_get_first_const(other);
01177         const char * file = cpl_frame_get_filename(frame);
01178 
01179         if (file == NULL) {
01180             cpl_test_nonnull(cpl_frame_get_filename(frame));
01181             continue;
01182         }
01183 
01184         for (;cmp != NULL; cmp = cpl_frameset_get_next_const(other)) {
01185             const char * cfile = cpl_frame_get_filename(cmp);
01186 
01187             if (!strcmp(file, cfile)) break;
01188 
01189         }
01190         if (cmp == NULL) {
01191             /* frame is new */
01192             recipe_frameset_test_frame(frame);
01193         }
01194     }
01195 }

Generated on Wed Mar 9 15:46:16 2011 for NACO Pipeline Reference Manual by  doxygen 1.5.8