#!?PYTHON_EXE?
# -*- coding: iso-8859-1 -*-

# ==============================================================================
# COPYRIGHT (C) 1991 - 2003  EDF R&D                  WWW.CODE-ASTER.ORG
# THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
# IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
# THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
# (AT YOUR OPTION) ANY LATER VERSION.
#
# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
# WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
# GENERAL PUBLIC LICENSE FOR MORE DETAILS.
#
# YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
# ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
#    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
# ==============================================================================

# $Id: as_run,v 1.18 2005/03/10 14:01:53 mcourtoi Exp $
# $Name: v1_2_3 $

# ----- differ messages translation
def _(mesg): return mesg

import sys
# ----- verification de la version de Python
if sys.hexversion < 0x020300F0:
   print _("This script requires Python 2.3 or higher, sorry !")
   sys.exit(4)

import os
import os.path
import re
import string
import time

# ----- get ASTK_SERV location
main=sys.argv[0]
if os.path.islink(main):
   main=os.path.realpath(main)
astk_serv_root=os.path.normpath(os.path.join(
      os.path.dirname(os.path.abspath(main)),os.pardir))

# ----- modules location
sys.path.append(os.path.join(astk_serv_root,'lib'))

# ----- import as_run modules
from as_run       import ASTER_RUN
from as_system    import ASTER_SYSTEM
from as_timer     import ASTER_TIMER
import as_maintenance
import as_exec

#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------

# ----- divers
os.stat_float_times(True)

# ----- initialisation
confdir=os.path.join(astk_serv_root,'conf')
run=ASTER_RUN(confdir)
run.timer = ASTER_TIMER()

# ----- module pour maintenir une version de dveloppement
as_maintenance.SetParser(run)

# ----- module d'excution du code
as_exec.SetParser(run)

# ----- lecture des options et arguments
opts, args = run.parser.parse_args()
run.current_action = opts.action

if run.current_action == None:
   # if symbolic link "action" -> "as_run --action"
   alias=os.path.basename(sys.argv[0])
   if alias in run.actions_info.keys():
      run.current_action=alias
   else:
      # default to 'run'
      run.current_action='run'
      #run.parser.error(_('you must specify an action'))

# ----- stocke les options
for option in run.options_list:
   run.options[option]=getattr(opts,option)

# ----- local/remote mode : nolocal allows to force remote mode
if run['nolocal']:
   run.options['local']=False

if run['debug']:
   run.PrintConfig()
   print '\n'+_('Arguments')+' : '+repr(args)
   print

# ----- get system commands
para={'maxcmdlen' : ?MAXCMDLEN? }
run.system=ASTER_SYSTEM(run, **para)
# add shortcuts : run.Copy instead of run.system.Copy
for meth in ['Copy','Shell','IsRemote','MkDir','Delete','Exists', \
      'IsDir','Gzip','Gunzip', 'AddToEnv', \
      'VerbStart','VerbEnd','VerbIgnore','MaxCmdLen']:
   if not hasattr(run,meth): setattr(run,meth,getattr(run.system,meth))

# ----- read optionnal profile
run.AddToEnv(os.path.join(astk_serv_root,'conf','aster_profile.sh'))

# ----- start 'current_action'
try :
   meth=run.actions_info[run.current_action]['method']
except KeyError:
   run.Mess(_('dictionnary bad defined :')+' actions_info',_('<F>_PROG_ERROR'))
else:
   # trap <Control+C>
   try:
      meth(run,*args)
   except KeyboardInterrupt:
      run.Mess(_('%s stopped by user') % run.current_action,_('<F>_INTERRUPT'))

run.Sortie(0)
