Beispiel #1
0
 def copy_flash(self):
     """Copy output/error to the flash directory. Return the filenames."""
     prof = self.prof
     cfg = self._host_config
     flashcoll = prof.get_type('flash')
     assert len(flashcoll) > 0
     flash = flashcoll[0]
     self.run.MkDir(flash.repr(), niverr='SILENT', verbose=True)
     df = {}
     for typ in ('output', 'error', 'export'):
         fname = FileName(self.flash(typ))
         if not is_localhost2(self.host):
             fname.user = prof['username'][0]
             fname.host = prof['serveur'][0]
         else:
             fname.path = osp.join(get_home_directory(), fname.path)
         df[typ] = fname
     if cfg['result_on_client']:
         lf = [fname.repr() for fname in df.values()]
         self.run.Copy(flash.path, niverr='<A>_ALARM', *lf)
     else:
         cmd = 'mv -f %s %s'
         for fname in df.values():
             self.run.Shell(cmd % (fname.path, flash.path),
                            mach=self.prof['serveur'][0],
                            user=prof['username'][0])
     return df
Beispiel #2
0
def local_edit(prof, args, **kwargs):
    """Call --edit action on a server by using a local editor after
    copying file if it's remote."""
    run = magic.run
    jobid = prof['jobid'][0]
    jobname = prof['nomjob'][0]
    mode = prof['mode'][0]
    typ = prof['edit_type'][0]
    to_output = kwargs.get('result_to_output', run['result_to_output'])

    iret = 0
    # read server informations
    serv = build_server_from_profile(prof, TYPES.COPY_FROM)

    # flasheur is in the home directory
    dirname, fname = osp.split(
        flash_filename("flasheur", jobname, jobid, typ, mode))
    run.DBG("file to edit is in %s named %s" % (dirname, fname))

    # copy from dirname if not on localhost
    if not serv.is_localhost():
        serv.set_proxy_dir(dirname)
        dst = osp.join(run['tmp_user'], 'flasheur_%s' % serv.host, fname)
        is_agla_astout = prof['nomjob'][0].startswith('pre_eda') \
                      or prof['nomjob'][0].startswith('asrest')
        if not osp.exists(dst) or is_agla_astout:
            iret = serv.copyfrom(dst)
    else:
        iret = 0
        dst = osp.join(get_home_directory(), dirname, fname)

    if iret == 0 and not to_output:
        magic.log.info(_(u"edit file %s"), dst)
        edit_file(run, dst)
    return iret
Beispiel #3
0
 def __init__(self, rcdir, run=None):
     """Initialization.
     'run' object is only used to refresh server configurations.
     """
     bwc_client_deprecate_run(run)
     self._rcdir = osp.join(get_home_directory(), rcdir)
     # cache for user preferences and servers configuration
     self._pref = None
     self._serv = None
     self._serv_rcversion = None
Beispiel #4
0
 def _clear_tmp(self):
     """Empty cache directory if necessary (remove files for which last access
     is older than 'deltat' seconds), and delete all directories
     from 'self.to_delete'."""
     # to avoid to delete current directory (it may freeze some systems...)
     try:
         os.chdir(get_home_directory())
     except OSError:
         print3(_(u"Can not change to the home directory ('%s'). "
                   "Temporay files have not been deleted.") % get_home_directory())
         return
     ct = time.time()
     deltat = 24*3600
     # ----- config is not set during __init__
     if self.__initialized:
         if self['verbose']:
             print3(_(u'Clear cache directory')+' '+self['cache_dir']+'...')
         # ----- avoid deleting such folders : /, /usr or /home !!!
         if re.search('^/.+/.*', self['cache_dir']):
             for root, dirs, files in os.walk(self['cache_dir'], topdown=False):
                 for name in files:
                     if ct - os.stat(osp.join(root, name)).st_atime > deltat:
                         os.remove(osp.join(root, name))
                     for name in dirs:
                         try:
                             os.rmdir(osp.join(root, name))
                         except OSError:
                             pass
         # ----- delete each directory in list_dirs
         if self['verbose']:
             print3(_(u'Clear temporary files/directories'))
         # if a Delete system function has been added
         if hasattr(self, 'Delete'):
             to_del = [d for d in get_subdirs(self.to_delete) if d.startswith('/')]
             for d in to_del:
                 getattr(self, 'Delete')(d)
Beispiel #5
0
    def CheckExtensions(self):
        """Initialisations des éventuelles extensions."""
        user_extensions    = osp.join(get_home_directory(), self.rcdir, 'as_run.extensions')
        cwd_extensions     = 'as_run.extensions'
        config = SafeConfigParser()
        l_read = config.read([user_extensions, cwd_extensions])

        l_ext = config.sections()
        for extension in l_ext:
            try:
                filename = osp.splitext(config.get(extension, 'module'))[0]
                if self['verbose']:
                    print3(ufmt(_(u'Loading extension %s from %s...'), extension, filename))
                module = __import__(filename, globals(), locals(), ['SetParser'])
                init_function = getattr(module, 'SetParser')
                init_function(self)
                if self['verbose']:
                    print3(_(u'Extension %s loaded') % extension)
            except (ImportError, NoOptionError), msg:
                print3(_(u'Extension %s not loaded (reason : %s)') % (extension, msg))
Beispiel #6
0
    def __init__(self, db, **kargs):
        """Open the connection.
        """
        # optional arguments
        self.verbose = kargs.get('verbose', True)
        self.debug = kargs.get('debug', False)
        self.autocommit = kargs.get('autocommit', True)
        rcdir = kargs.get('rcdir', get_home_directory())
        mydict = kargs.get('db_params')
        self.connection = None

        if mydict is None:
            fic = os.path.join(rcdir, '.mysql_connect_' + db)
            if not os.path.exists(fic):
                raise IOError(_(u'No such file or directory : %s') % fic)

            mydict = dict()
            try:
                execfile(fic, mydict)
            except:
                raise MySQLError, _(u'Can not read connection parameters')
        try:
            # set charset='utf8' not applied because of a bug in MySQLdb 1.2.x
            # with charset='utf8', "SELECT * FROM _msg WHERE id=85929;" fails!
            self.connection = MySQLdb.connect(host=mydict['host'],
                                              user=mydict['user'],
                                              passwd=mydict['passwd'],
                                              db=mydict['db'])
        except:
            raise MySQLError, _(u'Connection failed to %s for user %s (and password)') \
                % (mydict['host'], mydict['user'])
        # if autocommit doesn't exist hope it's on by default !
        if self.autocommit and hasattr(self.connection, 'autocommit'):
            self.connection.autocommit(self.autocommit)
        self.cursor = self.connection.cursor()
        if self.verbose:
            print3(
                _(u"""Connection to '%(host)s' on '%(db)s' database.""") %
                mydict)
Beispiel #7
0
 def set_proxy_dir(self, proxy_dir):
     """Configure the server."""
     if self.is_localhost():
         self.proxy_dir = osp.join(get_home_directory(self.user), proxy_dir)
     else:
         self.proxy_dir = proxy_dir
Beispiel #8
0
    def __init__(self, **kwargs):
        """Initializations"""
        self.ExitOnFatalError = True
        self.PrintExitCode    = True
        self.__initialized    = False
        self._val_pid         = 0
        self.rcdir = kwargs.get('rcdir')
        # ----- verification de la version de Python
        if sys.hexversion < 0x020600F0:
            print3(_(u"This script requires Python 2.6 or higher, sorry !"))
            self.Sortie(4)

        # ----- package informations
        from asrun.__pkginfo__ import version, copyright
        self.program     = 'as_run'
        self.__version__ = version

        # ----- options, liste des options du parser à stocker dans options
        self.options = default_options
        # always use _add_option to extend options_list
        self.options_list = ['debug', 'verbose', 'silent', 'force', 'num_job',
            'debug_stderr', 'stdout', 'stderr', 'log_progress',
            'remote_shell_protocol', 'remote_copy_protocol']

        # ----- informations about actions : module, method
        # always use set_action to define a new action
        self.actions_info = {}
        # store current action
        self.current_action = ''

        # ----- user preferences and defaults
        self.user_vars = {
            'editor' :
                {  'val'  : 'nedit',
                   'help' : _(u'editor command') },
            'devel_server_user' :
                {  'val'  : '',
                    'help' : _(u'login on the development server') + os.linesep + \
                    _(u'# (name/ip address is usually set in /etc/codeaster/asrun)') },
        }

        # ----- formats
        self.fmt = {
            'usage'     : u'   - %s :'+os.linesep+'      %s'+os.linesep,
            'verb1'     : u' %-15s = %s',
            'msg+cod'   : os.linesep+u'%-18s %s'+os.linesep,
            'msg_info'  : u'<INFO>%s %s',
            'silent'    : u'      %s %s',
            'title'     : os.linesep+u'-'*80+os.linesep+u'%s %s'+os.linesep,
            'diag'      : os.linesep+u'-'*60+os.linesep+\
                       u'--- DIAGNOSTIC JOB : %s'+\
                       os.linesep+'-'*60+os.linesep,
            'exit'      : os.linesep+u'EXIT_CODE='+'%d'
        }

        # ----- diagnostic
        self.diag = ''

        # ----- list of files/directories to delete on exit
        self.to_delete = []

        # ----- lists of text to print on exit depending on diagnostic
        # this allows to keep important messages when a lot of output is generated
        # and to print them once again at end
        self.print_on_exit = {}

        # ----- do not print timer info on exit by default
        self.timer = None
        self.print_timer = False

        # ----- parser
        self.parser = define_parser(self)

        # ----- user resource directory
        # if self.rcdir is set, it's supposed to start with ".astkrc_"
        # or to be an absolute directory name.
        # if it's an absolute pathname it may be unusable on the remote host
        if self.rcdir is None:
            self.rcdir = get_option_value(sys.argv, "--rcdir", default=ASTKRC)
        assert self.rcdir.startswith(".astkrc") or osp.abspath(self.rcdir) == self.rcdir, \
            u"absolute path or similar to '.astkrc_xxx' expected not: %s" % self.rcdir

        # ----- read master configuration file and user preferences
        self.config = {}
        oldrc = osp.join(get_home_directory(), self.rcdir, 'config')
        bwc_config_rc(oldrc, self._read_rc)

        self.user_rc = osp.join(get_home_directory(), self.rcdir, 'prefs')
        self._read_rc(osp.join(confdir, 'asrun'), self.config, mcsimp=['noeud'])
        verslist = [osp.join(confdir, 'aster')]
        verslist.extend(glob(osp.join(aster_root, '*', 'aster.conf')))
        verslist.extend(glob('/opt/aster/*/aster.conf'))
        verslist.extend(glob('/opt/codeaster/*/aster.conf'))
        for optrc in verslist:
            self._read_rc(optrc,  self.config, optional=True, mcsimp=['vers'])
        self._init_rc()

        # add user plugins directory
        sys.path.append(osp.join(get_home_directory(), self.rcdir))
        # ----- prepare tmp_user and cache_dir
        # no folder in cache_dir
        tmp_user = osp.join(self.config['rep_tmp'],
                'astk_'+getpass.getuser())
        self.config['tmp_user'] = tmp_user
        self.config['cache_dir'] = osp.join(tmp_user, 'cache')
        # safer as osp.exists for multiple executions
        try:
            #peter.zhang, for cygwin
            tmp_user = tmp_user.replace('\\', '/')
            tmp_user = tmp_user.replace('/cygdrive/', '')
            if tmp_user.find(':/') < 0:
                tmp_user = tmp_user.replace('/', ':/', 1)
            os.makedirs(tmp_user)
        except OSError:
            pass
        if not os.access(tmp_user, os.W_OK):
            print3(ufmt(_(u'no write access to %s'), tmp_user))
            self.Sortie(4)

        # safer as osp.exists for multiple executions
        try:
            #peter.zhang, for cygwin
            self.config['cache_dir'] = self.config['cache_dir'].replace('\\', '/')
            self.config['cache_dir'] = self.config['cache_dir'].replace('/cygdrive/', '')
            if self.config['cache_dir'].find(':/') < 0:
                self.config['cache_dir'] = self.config['cache_dir'].replace('/', ':/', 1)
            os.mkdir(self.config['cache_dir'])
        except OSError:
            pass

        # ----- check for 'flasheur'
        flash = osp.join(get_home_directory(), "flasheur")
        self.config['flasheur'] = flash
        # safer as osp.exists for multiple executions
        try:
            #peter.zhang, for cygwin
            flash = flash.replace('\\', '/')
            flash = flash.replace('/cygdrive/', '')
            if flash.find(':/') < 0:
                flash = flash.replace('/', ':/', 1)
            os.makedirs(flash)
        except OSError:
            pass
        if not os.access(flash, os.W_OK):
            print3(ufmt(_(u'no write access to %s'), flash))
            self.Sortie(4)

        # ----- init timer
        self.timer = AsterTimer()
        self.LoadExtensions()
        self.CheckExtensions()