def cleanup_computation(self, compute):
        if not compute.zombie or compute.pending_jobs > 0:
            return
        if compute.pending_jobs != 0:
            _dispy_logger.debug('pending jobs for computation "%s"/%s: %s',
                                compute.name, compute.id, compute.pending_jobs)

        if self.computations.pop(compute.id, None) is None:
            _dispy_logger.warning('Invalid computation "%s" to cleanup ignored', compute.id)
            return

        self.num_computations += 1
        file_uses, compute.file_uses = compute.file_uses, {}
        globalvars, compute.globals = compute.globals, {}
        pkl_path = os.path.join(self.dest_path_prefix, '%s_%s' % (compute.id, compute.auth))
        if compute.pending_results == 0:
            try:
                os.remove(pkl_path)
            except:
                _dispy_logger.warning('Could not remove "%s"', pkl_path)
        else:
            fd = open(pkl_path, 'wb')
            pickle.dump(compute, fd)
            fd.close()

        self.scheduler['auth'].discard(compute.auth)

        if ((not self.computations) and (not self.scheduler['auth']) and
           compute.scheduler_ip_addr == self.scheduler['ip_addr'] and
           compute.scheduler_port == self.scheduler['port']):
            self.scheduler['ip_addr'] = None
            self.pulse_interval = None
            self.timer_coro.resume(None)
            if self.serve > 0:
                self.serve -= 1
            Coro(self.broadcast_ping_msg)

        if compute.cleanup is False:
            if self.serve == 0:
                self.shutdown(quit=True)
            return
        os.chdir(self.dest_path_prefix)
        if isinstance(compute.cleanup, _Function):
            try:
                localvars = {'_dispy_cleanup_args': compute.cleanup.args,
                             '_dispy_cleanup_kwargs': compute.cleanup.kwargs}
                if os.name == 'nt':
                    globalvars = globals()
                exec(marshal.loads(compute.code)) in globalvars, localvars
                exec('%s(*_dispy_cleanup_args, **_dispy_cleanup_kwargs)' %
                     compute.cleanup.name) in globalvars, localvars
            except:
                _dispy_logger.debug('Cleanup "%s" failed', compute.cleanup.name)
                _dispy_logger.debug(traceback.format_exc())

        if os.name == 'nt':
            for var in globals().keys():
                if var not in self.__init_globals:
                    _dispy_logger.debug('Variable "%s" left behind by "%s" at %s is being removed',
                                        var, compute.name, compute.scheduler_ip_addr)
                    globals().pop(var, None)

            for var, value in self.__init_globals.iteritems():
                if var in ('_dispy_node', '_dispy_conn', '_dispy_addr', '_dispy_config'):
                    continue
                if value != globals().get(var, None):
                    _dispy_logger.warning('Variable "%s" changed by "%s" at %s is being reset',
                                          var, compute.name, compute.scheduler_ip_addr)
                    globals()[var] = value

        for module in sys.modules.keys():
            if module not in compute.ante_modules:
                sys.modules.pop(module, None)
        sys.modules.update(self.__init_modules)

        for path in os.listdir(compute.dest_path):
            path = os.path.join(compute.dest_path, path)
            if file_uses.get(path, 1) == 1:
                try:
                    if os.path.isfile(path) or os.path.islink(path):
                        os.remove(path)
                    elif os.path.isdir(path):
                        shutil.rmtree(path, ignore_errors=True)
                    else:
                        os.remove(path)
                except:
                    _dispy_logger.warning('Could not remove "%s"', path)

        if os.path.isdir(compute.dest_path) and \
           compute.dest_path.startswith(self.dest_path_prefix) and \
           len(os.listdir(compute.dest_path)) == 0:
            try:
                os.rmdir(compute.dest_path)
            except:
                _dispy_logger.warning('Could not remove directory "%s"', compute.dest_path)
            else:
                _dispy_logger.debug('Removed "%s"', compute.dest_path)

        if self.serve == 0:
            self.shutdown(quit=True)
dataset/ETHPy150Open pgiri/dispy/py2/dispy/dispynode.py/_DispyNode.cleanup_computation/original