/var/lib/sorcery/modules/libspell

     1	#!/bin/bash
     2	#---------------------------------------------------------------------
     3	## @Synopsis Functions for running spell files.
     4	## @Copyright (C) 2006 The Source Mage Team <http://www.sourcemage.org>
     5	## @Contributers Andrew Stitt <astitt@sourcemage.org>
     6	##
     7	## This file implements SourceMage's spell file inheritence scheme.
     8	## In a nutshell, sorcery implements a default for each and every spell
     9	## file, usually this is "true". There can be an override at the grimoire
    10	## or section level, in addition to the spell file itself. This means
    11	## you can write a spell level PRE_BUILD, do some stuff then call
    12	## default_pre_build, which will run a section level PRE_BUILD if it
    13	## exists, or run a grimoire level PRE_BUILD if that exists, or run the
    14	## sorcery supplied default. See bug 10914.
    15	#---------------------------------------------------------------------
    16	
    17	#---------------------------------------------------------------------
    18	## Setup default functions for spell files. Several of the core functions
    19	## are declared here as false so they exist, but are re-defined when in
    20	## the proper context. This is merely a safety valve.
    21	#---------------------------------------------------------------------
    22	function load_spell_file_functions() {
    23	  for each in $(
    24	    cat << EOF
    25	PREPARE:prepare
    26	CONFIGURE:configure
    27	DEPENDS:depends
    28	
    29	PRE_SUB_DEPENDS:pre_sub_depends
    30	SUB_DEPENDS:sub_depends
    31	
    32	CONFLICTS:conflicts
    33	
    34	PRE_BUILD:pre_build
    35	BUILD:build
    36	PRE_INSTALL:pre_install
    37	INSTALL:install
    38	POST_BUILD:post_build
    39	INSTALL_EXTRAS:install_extras
    40	POST_INSTALL:post_install
    41	TRANSFER:transfer
    42	FINAL:final
    43	
    44	UP_TRIGGERS:up_triggers
    45	TRIGGERS:triggers
    46	TRIGGER_CHECK:trigger_check
    47	
    48	DOWNLOAD:download
    49	
    50	PRE_REMOVE:pre_remove
    51	POST_REMOVE:post_remove
    52	
    53	PRE_RESURRECT:pre_resurrect
    54	POST_RESURRECT:post_resurrect
    55	EOF
    56	); do
    57	    local NAME=${each%:*}
    58	    local name=${each#*:}
    59	
    60	    # Create the four libapi entry points for each file
    61	    eval "function default_${name}() {
    62	      real_default_${name}
    63	    }"
    64	    eval "function default_section_${name}() {
    65	      real_default_section_${name}
    66	    }"
    67	    eval "function default_grimoire_${name}() {
    68	      real_default_grimoire_${name}
    69	    }"
    70	    eval "function default_sorcery_${name}() {
    71	      real_default_sorcery_${name}
    72	    }"
    73	
    74	
    75	    # real_default_<file> always refers to the level above in the hierarchy.
    76	    # (section, grimoire or sorcery).
    77	    # its defined here as false, but is re-declared at various stages.
    78	    eval "function real_default_${name}() {
    79	      false
    80	    }"
    81	
    82	    # These two functions run the section/grimoire function and update
    83	    # the real_default_<file> function. Ignore this generic declaration,
    84	    # it is changed at runtime to private_default_<section|grimoire>_generic.
    85	    eval "function real_default_section_${name}() {
    86	      false
    87	    }"
    88	    eval "function real_default_grimoire_${name}() {
    89	      false
    90	    }"
    91	
    92	    # If there isnt a sorcery default function, declare one as "true".
    93	    # Note that, the right thing happens regardless of what order lib files
    94	    # are sourced in.
    95	    declare -F real_default_sorcery_${name} &>/dev/null ||
    96	    eval "function real_default_sorcery_${name}() {
    97	      true
    98	    }"
    99	  done
   100	}
   101	load_spell_file_functions
   102	
   103	
   104	#---------------------------------------------------------------------
   105	## Run a section level file, or the default grimoire level function.
   106	#---------------------------------------------------------------------
   107	private_default_section_generic() {
   108	  eval "function real_default_${2}() {
   109	    default_grimoire_${2}
   110	  }"
   111	  if test -x "$SECTION_DIRECTORY/${1}"; then
   112	    source "$SECTION_DIRECTORY/${1}"
   113	  else
   114	    default_grimoire_${2}
   115	  fi;rc=$?
   116	  eval "function real_default_${2}() {
   117	    default_section_${2}
   118	  }"
   119	  return $rc
   120	}
   121	
   122	#---------------------------------------------------------------------
   123	## Run a grimoire level file, or the default sorcery level function.
   124	#---------------------------------------------------------------------
   125	private_default_grimoire_generic() {
   126	  eval "function real_default_${2}() {
   127	    default_sorcery_${2}
   128	  }"
   129	  if test -x "$GRIMOIRE/${1}"; then
   130	    source "$GRIMOIRE/${1}"
   131	  else
   132	    default_sorcery_${2}
   133	  fi;rc=$?
   134	  eval "function real_default_${2}() {
   135	    default_grimoire_${2}
   136	  }"
   137	  return $rc
   138	}
   139	
   140	#---------------------------------------------------------------------
   141	## Core entry point for sorcery to run a spell file.
   142	## Arms real_default_*_file functions then runs the spell file/cmd/function
   143	#---------------------------------------------------------------------
   144	function run_spell_file() {
   145	  local rc=0
   146	  local spell_file=$1
   147	  local function_suffix=$2
   148	  local spell_cmd_var=${1}_CMD
   149	  local spell_cmd=${!spell_cmd_var}
   150	
   151	  # this is mostly used for messages like "<spell> checking dependencies"
   152	  local pre_function=$3
   153	
   154	  eval "function real_default_${function_suffix}() {
   155	    default_section_${function_suffix}
   156	  }"
   157	  eval "function real_default_section_${function_suffix}() {
   158	    private_default_section_generic "${spell_file}" ${function_suffix}
   159	  }"
   160	  eval "function real_default_grimoire_${function_suffix}() {
   161	    private_default_grimoire_generic ${spell_file} ${function_suffix}
   162	  }"
   163	
   164	  persistent_load &&
   165	  if [[ $pre_function ]]; then
   166	    $pre_function
   167	    true
   168	  fi &&
   169	  if test -x "$SCRIPT_DIRECTORY/$spell_file"; then
   170	    . $SCRIPT_DIRECTORY/$spell_file
   171	  elif [[ "$spell_cmd" ]] ; then
   172	    $spell_cmd
   173	  else
   174	    default_${function_suffix}
   175	  fi;rc=$?
   176	  persistent_save
   177	
   178	  if [[ $rc != 0 ]]; then
   179	    log_failure_reason $function_suffix
   180	  fi
   181	
   182	  # restore these, so they're always false except when needed
   183	  eval "function real_default_${function_suffix}() {
   184	    false
   185	  }"
   186	  eval "function real_default_section_${function_suffix}() {
   187	    false
   188	  }"
   189	  eval "function real_default_grimoire_${function_suffix}() {
   190	    false
   191	  }"
   192	  return $rc
   193	}
   194	
   195	#------------------------------------------------------------------------
   196	##=back
   197	##
   198	##=head1 LICENSE
   199	##
   200	## This software is free software; you can redistribute it and/or modify
   201	## it under the terms of the GNU General Public License as published by
   202	## the Free Software Foundation; either version 2 of the License, or
   203	## (at your option) any later version.
   204	##
   205	## This software is distributed in the hope that it will be useful,
   206	## but WITHOUT ANY WARRANTY; without even the implied warranty of
   207	## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   208	## GNU General Public License for more details.
   209	##
   210	## You should have received a copy of the GNU General Public License
   211	## along with this software; if not, write to the Free Software
   212	## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   213	##
   214	#------------------------------------------------------------------------