#!?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.
# ==============================================================================

"""This is the main file to access to astk services through as_run
"""

__revision__ = "$Id: as_run 2473 2006-11-27 13:17:44Z courtois $"

import os
import sys

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

# ----- 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))

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

# ----- 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
import as_job
import as_rex

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

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

# ----- initialisation
run = ASTER_RUN(astk_serv_root)
run.timer = ASTER_TIMER()

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

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

# ----- module d'information sur les jobs
as_job.SetParser(run)

# ----- module interface avec le REX
as_rex.SetParser(run)

# ----- lecture des options et arguments
opts, args = run.ParseArgs()
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

# ----- get system commands
para = {'maxcmdlen' : ?MAXCMDLEN? }
run.system = ASTER_SYSTEM(run, **para)
run.PostConf()

# ----- debug information
if run['debug']:
   run.PrintConfig()
   print os.linesep+_('Arguments')+' : '+repr(args)
   print

# ----- 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>_PROGRAM_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)
