Beispiel #1
0
	def logError(self, target, brief=False, **kwargs): # Can also log content of additional files via kwargs
		now = time.time()
		entry = '%s.%s' % (time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime(now)), ('%.5f' % (now - int(now)))[2:])
		self._logger.log_time(logging.WARNING, '%s failed with code %d', self.niceCmd, self.wait())
		if not brief:
			self._logger.log_time(logging.WARNING, '\n%s', self.getError())

		try:
			tar = tarfile.TarFile.open(target, 'a')
			data = {'retCode': self.wait(), 'exec': self.cmd, 'args': self.args}
			files = [VirtualFile(os.path.join(entry, 'info'), DictFormat().format(data))]
			kwargs.update({'stdout': self.getOutput(), 'stderr': self.getError()})
			for key, value in kwargs.items():
				try:
					content = open(value, 'r').readlines()
				except Exception:
					content = [value]
				files.append(VirtualFile(os.path.join(entry, key), content))
			for fileObj in files:
				info, handle = fileObj.getTarInfo()
				tar.addfile(info, handle)
				handle.close()
			tar.close()
		except Exception:
			raise GCError('Unable to log errors of external process "%s" to "%s"' % (self.niceCmd, target))
		self._logger.info('All logfiles were moved to %s', target)
 def emit(self, record):
     if record.pathname == '<process>':
         entry = '%s_%s.%03d' % (record.name,
                                 time.strftime(
                                     '%Y-%m-%d_%H:%M:%S',
                                     time.localtime(record.created)),
                                 int(record.msecs))
         files = record.files
         files['info'] = 'call=%s\nexit=%s\n' % (repr(
             record.proc.get_call()), record.proc.status(0))
         files['stdout'] = record.proc.stdout.read_log()
         files['stderr'] = record.proc.stderr.read_log()
         files['stdin'] = record.proc.stdin.read_log()
         try:
             tar = tarfile.TarFile.open(self._fn, 'a')
             for key, value in record.additional.items():
                 if os.path.exists(value):
                     value = open(value, 'r').read()
                 fileObj = VirtualFile(os.path.join(entry, key), [value])
                 info, handle = fileObj.getTarInfo()
                 tar.addfile(info, handle)
                 handle.close()
             tar.close()
         except Exception:
             raise GCError(
                 'Unable to log results of external call "%s" to "%s"' %
                 (record.proc.get_call(), self._fn))
         self._log.warning('All logfiles were moved to %s', self._fn)
Beispiel #3
0
    def _write_process_log(self, record):
        entry_time = time.strftime('%Y-%m-%d_%H:%M:%S',
                                   time.localtime(record.created))
        entry = '%s_%s.%03d' % (record.name, entry_time, int(record.msecs))
        files = record.files
        files['info'] = 'call=%s\nexit=%s\n' % (repr(
            record.proc.get_call()), record.proc.status(0))
        files['stdout'] = record.proc.stdout.read_log()
        files['stderr'] = record.proc.stderr.read_log()
        files['stdin'] = record.proc.stdin.read_log()

        def _log_tar():
            tar = tarfile.TarFile.open(self._fn, 'a')
            for key, value in record.files.items():
                if os.path.exists(value):
                    value = SafeFile(value).read_close()
                file_obj = VirtualFile(os.path.join(entry, key), [value])
                info, handle = file_obj.get_tar_info()
                tar.addfile(info, handle)
                handle.close()
            tar.close()

        rethrow(
            GCError('Unable to log results of external call "%s" to "%s"' %
                    (record.proc.get_call(), self._fn)), _log_tar)
Beispiel #4
0
 def parse(self, lines, keyParser=None, valueParser=None):
     keyParser = keyParser or {}
     valueParser = valueParser or {}
     defaultKeyParser = keyParser.get(None, lambda k: parseType(k.lower()))
     defaultValueParser = valueParser.get(None, parseType)
     data = {}
     doAdd = False
     currentline = ''
     try:
         lines = lines.splitlines()
     except Exception:
         clear_current_exception()
     for line in lines:
         try:
             if not isinstance(line, str):
                 line = line.decode('utf-8')
             if self.escapeString:
                 # Switch accumulate on/off when odd number of quotes found
                 if (line.count('"') - line.count('\\"')) % 2 == 1:
                     doAdd = not doAdd
                 currentline += line
                 if doAdd:
                     continue
             else:
                 currentline = line
             # split at first occurence of delimeter and strip spaces around
             key_value_list = lmap(str.strip,
                                   currentline.split(self.delimeter, 1))
             if len(key_value_list) == 2:
                 key, value = key_value_list
                 currentline = ''
             else:  # in case no delimeter was found
                 currentline = ''
                 continue
             if self.escapeString:
                 value = value.strip('"').replace('\\"',
                                                  '"').replace('\\$', '$')
             key = keyParser.get(key, defaultKeyParser)(key)
             data[key] = valueParser.get(key, defaultValueParser)(
                 value)  # do .encode('utf-8') ?
         except Exception:
             raise GCError('Invalid dict format in %s' % repr(line))
     if doAdd:
         raise GCError('Invalid dict format in %s' % repr(lines))
     return data
Beispiel #5
0
 def write(self, newdict=None, update=True):
     if not update:
         self.clear()
     self.update(newdict or {})
     if dict(self.olddict) == dict(self.items()):
         return
     try:
         if self.filename:
             safeWrite(open(self.filename, 'w'), self.fmt.format(self))
     except Exception:
         raise GCError('Could not write to file %s' % self.filename)
     self.olddict = self.items()
Beispiel #6
0
    def _write_process_log(self, record):
        entry_time = time.strftime('%Y-%m-%d_%H:%M:%S',
                                   time.localtime(record.created))
        entry = '%s_%s.%03d' % (record.name, entry_time, int(record.msecs))
        files = record.files
        files['info'] = 'call=%s\nexit=%s\n' % (repr(
            record.proc.get_call()), record.proc.status(0))
        files['stdout'] = record.proc.stdout.read_log()
        files['stderr'] = record.proc.stderr.read_log()
        files['stdin'] = record.proc.stdin.read_log()

        def _log_tar(only_print=False):
            # self._log.info('tar: %s' % self._fn)
            sleep_when_cannot_accept_jobs = False
            message = ""
            if not only_print:
                tar = tarfile.TarFile.open(self._fn, 'a')
            for key, value in record.files.items():
                if "The CREAM service cannot accept jobs at the moment" in value:
                    sleep_when_cannot_accept_jobs = True
                    message = "The CREAM service cannot accept jobs at the moment"
                elif "Unable to connect to" in value:
                    sleep_when_cannot_accept_jobs = True
                    message = value
                value = os.linesep.join([s for s in value.splitlines() if s])
                if only_print:
                    self._log.info('\n\tkey: "%s"\n\tvalue: "%s"' %
                                   (key, value))
                else:
                    if value.startswith('\n'):
                        value = value[1:]
                    if os.path.exists(value):
                        value = SafeFile(value).read_close()
                    # self._log.info('\tvirtual file: "%s"' % os.path.join(entry, key))
                    file_obj = VirtualFile(os.path.join(entry, key), [value])
                    info, handle = file_obj.get_tar_info()
                    # self._log.info('\tinfo: "%s"' % info)
                    # self._log.info('\thandle: "%s"' % handle)
                    tar.addfile(info, handle)
                    handle.close()
            if not only_print:
                tar.close()
            if sleep_when_cannot_accept_jobs:
                from grid_control.utils.activity import Activity
                activity = Activity(
                    message +
                    '. Waiting before trying to delegate proxy again...')
                time.sleep(900)
                activity.finish()

        # rethrow(GCError('Unable to log results of external call "%s" to "%s"' % (
        # 		record.proc.get_call(), self._fn)), _log_tar)
        try:
            rethrow(
                GCError('Unable to log results of external call "%s" to "%s"' %
                        (record.proc.get_call(), self._fn)), _log_tar)
        except:
            self._log.warning(
                'Unable to log results of external call "%s" to "%s"' %
                (record.proc.get_call(), self._fn))
            _log_tar(only_print=True)
Beispiel #7
0
def ensureDirExists(dn, name='directory'):
    if not os.path.exists(dn):
        try:
            os.makedirs(dn)
        except Exception:
            raise GCError('Problem creating %s "%s"' % (name, dn))