Esempio n. 1
0
 def test_process_awareness(self) -> None:
     # ensure that the random source differs between
     # child and parent.
     read_fd, write_fd = os.pipe()
     pid = None # type: int
     try:
         pid = os.fork()
         if not pid:
             os.close(read_fd)
             os.write(write_fd, next(self.r).encode("ascii"))
             os.close(write_fd)
             # bypass the normal exit handlers- leave those to
             # the parent.
             os._exit(0)
         parent_value = next(self.r)
         child_value = os.read(read_fd, len(parent_value)).decode("ascii")
     finally:
         if pid:
             # best effort to ensure the process can't bleed out
             # via any bugs above
             try:
                 os.kill(pid, signal.SIGKILL)
             except EnvironmentError:
                 pass
         os.close(read_fd)
         os.close(write_fd)
     self.assertNotEqual(child_value, parent_value)
Esempio n. 2
0
def onConnect():
#    if keyAgent and options['agent']:
#        cc = protocol.ClientCreator(reactor, SSHAgentForwardingLocal, conn)
#        cc.connectUNIX(os.environ['SSH_AUTH_SOCK'])
    if hasattr(conn.transport, 'sendIgnore'):
        _KeepAlive(conn)
    if options.localForwards:
        for localPort, hostport in options.localForwards:
            s = reactor.listenTCP(localPort,
                        forwarding.SSHListenForwardingFactory(conn,
                            hostport,
                            SSHListenClientForwardingChannel))
            conn.localForwards.append(s)
    if options.remoteForwards:
        for remotePort, hostport in options.remoteForwards:
            log.msg('asking for remote forwarding for %s:%s' %
                    (remotePort, hostport))
            conn.requestRemoteForwarding(remotePort, hostport)
        reactor.addSystemEventTrigger('before', 'shutdown', beforeShutdown)
    if not options['noshell'] or options['agent']:
        conn.openChannel(SSHSession())
    if options['fork']:
        if os.fork():
            os._exit(0)
        os.setsid()
        for i in range(3):
            try:
                os.close(i)
            except OSError as e:
                import errno
                if e.errno != errno.EBADF:
                    raise
Esempio n. 3
0
def _exit(rc):
    """
    Immediately terminates this process with the given return code.

    This function is mainly here to be mocked out in UTs.
    """
    os._exit(rc)  # pragma nocover
Esempio n. 4
0
    def initialize(self, *args, **kw):
        """
        """

        # disable laser

        # test communciations with board issue warning if
        # no handle or response is none
        resp = True
        if self._test_comms:
        #            self.tell('\r')
        #            self._communicator.write('\r')
            resp = True if self.ask(';LB.VER') else False

        #        resp = self._disable_laser_()
        if self._communicator.handle is None or resp is not True:
            if not globalv.ignore_initialization_warnings:
            #                    warning(None, 'Laser not connected. Power cycle USB hub.')
                result = self.confirmation_dialog('Laser not connected. Power cycle USB hub.', title='Quit Pychron')
                if result:
                    os._exit(0)

        # turn off pointer
        if self.has_pointer:
            self.set_pointer_onoff(False)

        # initialize Kerr devices
        self.motor_microcontroller.initialize(*args, **kw)

        for m in self.motors:
            if m.use_initialize:
                m.initialize(*args, **kw)
            m.on_trait_change(lambda: self.trait_set(refresh_canvas=True), 'data_position')
        return True
Esempio n. 5
0
 def do_fork_and_wait():
     # just fork a child process and wait it
     pid = os.fork()
     if pid > 0:
         os.waitpid(pid, 0)
     else:
         os._exit(0)
Esempio n. 6
0
 def __init__(self, username, password=None, key_filename=None, host=DEFAULT_SSH_HOST, port=DEFAULT_SSH_PORT):
     pkey = os.path.expanduser(key_filename) if key_filename is not None else None
     self.send_lock = threading.Lock()
     self.ssh_client = paramiko.SSHClient()
     self.ssh_client.load_system_host_keys()
     self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     connect_kw = {"username": username,
                   "compress": True,
                   "look_for_keys": True}
     if password is not None:
         connect_kw["password"] = password
         connect_kw["look_for_keys"] = False
     if key_filename is not None:
         connect_kw["key_filename"] = key_filename
         connect_kw["look_for_keys"] = False
     try:
         self.ssh_client.connect(host, port, **connect_kw)
     except paramiko.ssh_exception.PasswordRequiredException as e:
         if e.message == "Private key file is encrypted":
             raise SshRpcKeyEncrypted()
         raise SshRpcError()
     except paramiko.ssh_exception.SSHException as e:
         if e.message == "No authentication methods available":
             raise SshRpcKeyNoAuthMethod()
         if e.message == "Authentication failed.":
             raise SshRpcKeyAuthFailed()
         raise SshRpcError(e.message)
     except socket.error as e:
         log.err("Unable to establish connection, please try again later ({})".format(e))
         os._exit(1)
     self.ssh_transport = self.ssh_client.get_transport()
     self.ssh_transport.set_keepalive(30)
     self.ssh_channel = None
     self._server_update()
Esempio n. 7
0
    def _start_child(self, wrap):
        if len(wrap.forktimes) > wrap.workers:
            # Limit ourselves to one process a second (over the period of
            # number of workers * 1 second). This will allow workers to
            # start up quickly but ensure we don't fork off children that
            # die instantly too quickly.
            if time.time() - wrap.forktimes[0] < wrap.workers:
                LOG.info(_LI('Forking too fast, sleeping'))
                time.sleep(1)

            wrap.forktimes.pop(0)

        wrap.forktimes.append(time.time())

        pid = os.fork()
        if pid == 0:
            launcher = self._child_process(wrap.service)
            while True:
                self._child_process_handle_signal()
                status, signo = self._child_wait_for_exit_or_signal(launcher)
                if not _is_sighup_and_daemon(signo):
                    break
                launcher.restart()

            os._exit(status)

        LOG.info(_LI('Started child %d'), pid)

        wrap.children.add(pid)
        self.children[pid] = wrap

        return pid
Esempio n. 8
0
    def setUpClass(cls):
        super(GppcTestCase, cls).setUpClass()
        """
        compile gppc_test.c and install the gppc_test.so
        """
        gpccutil.pre_process()
        cmd = 'gpssh --version'
        res = {'rc':0, 'stderr':'', 'stdout':''}
        run_shell_command (cmd, 'check product version', res)
        gppkg = Gppkg()
        product_version = res['stdout']
        result = gppkg.gppkg_install(product_version, 'libgppc')
        #makeLog = loal_path('test00MakeLog.out')
        if result:
            cmdMakeInstall = 'cd '+local_path('data')+' && make clean && make CPPFLAGS=-D_GNU_SOURCE && make install'
            res = {'rc':0, 'stderr':'', 'stdout':''}
            run_shell_command (cmdMakeInstall, 'compile gppc_test.c', res)

            # Current make file works for linux, but not for Solaris or OSX.
            # If compilation fails or installation fails, force system quit: os._exit(1)
            if res['rc']:
                os._exit(1) # This will exit the test including the next test suites
            sharedObj = local_path('data/gppc_test.so')
            if not os.path.isfile(sharedObj):
                os._exit(1)

            # For multinode cluster, need to copy shared object tabfunc_gppc_demo.so to all primary segments
            res = {'rc':0, 'stderr':'', 'stdout':''}
            hosts = gpdbconfig.get_hosts(segments=True)
            scp_cmd = 'gpscp  -h ' +' -h '.join(map(str,hosts)) +' '+ sharedObj + ' =:%s' % LIBDIR
            run_shell_command(scp_cmd, 'scp share object to all segment', res)
            tinctest.logger.info('scp shared object result %s' % res['stdout'])
            if res['rc']:
                raise Excpetion('Could not copy shared object to primary segment')
Esempio n. 9
0
def ExceptHook(excType, excValue, traceObject):
    import traceback, os, time, codecs
    # This hook does the following: in case of exception write it to
    # the "error.log" file, display it to the console, shutdown CEF
    # and exit application immediately by ignoring "finally" (os._exit()).
    errorMsg = "\n".join(traceback.format_exception(excType, excValue,
            traceObject))
    errorFile = GetApplicationPath("error.log")
    try:
        appEncoding = cefpython.g_applicationSettings["string_encoding"]
    except:
        appEncoding = "utf-8"
    if type(errorMsg) == bytes:
        errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
    try:
        with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
            fp.write("\n[%s] %s\n" % (
                    time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
    except:
        print("cefpython: WARNING: failed writing to error file: %s" % (
                errorFile))
    # Convert error message to ascii before printing, otherwise
    # you may get error like this:
    # | UnicodeEncodeError: 'charmap' codec can't encode characters
    errorMsg = errorMsg.encode("ascii", errors="replace")
    errorMsg = errorMsg.decode("ascii", errors="replace")
    print("\n"+errorMsg+"\n")
    cefpython.QuitMessageLoop()
    cefpython.Shutdown()
    os._exit(1)
Esempio n. 10
0
def main(listener_fd, alive_r, preload, main_path=None, sys_path=None):
    '''Run forkserver.'''
    if preload:
        if '__main__' in preload and main_path is not None:
            process.current_process()._inheriting = True
            try:
                spawn.import_main_path(main_path)
            finally:
                del process.current_process()._inheriting
        for modname in preload:
            try:
                __import__(modname)
            except ImportError:
                pass

    util._close_stdin()

    # ignoring SIGCHLD means no need to reap zombie processes
    # letting SIGINT through avoids KeyboardInterrupt tracebacks
    handlers = {
        signal.SIGCHLD: signal.SIG_IGN,
        signal.SIGINT: signal.SIG_DFL,
        }
    old_handlers = {sig: signal.signal(sig, val)
                    for (sig, val) in handlers.items()}

    with socket.socket(socket.AF_UNIX, fileno=listener_fd) as listener, \
         selectors.DefaultSelector() as selector:
        _forkserver._forkserver_address = listener.getsockname()

        selector.register(listener, selectors.EVENT_READ)
        selector.register(alive_r, selectors.EVENT_READ)

        while True:
            try:
                while True:
                    rfds = [key.fileobj for (key, events) in selector.select()]
                    if rfds:
                        break

                if alive_r in rfds:
                    # EOF because no more client processes left
                    assert os.read(alive_r, 1) == b''
                    raise SystemExit

                assert listener in rfds
                with listener.accept()[0] as s:
                    code = 1
                    if os.fork() == 0:
                        try:
                            _serve_one(s, listener, alive_r, old_handlers)
                        except Exception:
                            sys.excepthook(*sys.exc_info())
                            sys.stderr.flush()
                        finally:
                            os._exit(code)

            except OSError as e:
                if e.errno != errno.ECONNABORTED:
                    raise
Esempio n. 11
0
def main(argv):
    if len(argv) > 1 and argv[1] == "-n": # no passphrase
        passphrase = None
    else:
        # first read a line containing the passphrase
        passphrase = string.strip(sys.stdin.readline())
    # fork with pty
    pid,master = pty.fork()
    assert pid != -1
    if pid == 0:
        # child. run ssh
        os.execvp("ssh", ssh)
    else:
        # parent. talk to child.
        s = parent(master, passphrase)
        # ensure child is gone 
        cleanup(pid)
        # write whatever we get from child
        os.write(1, s)
        # wait for child to disappear
        qid,status = os.wait()
        assert pid == qid
        if os.WIFEXITED(status):
            # child normally exited. forward its status
            os._exit(os.WEXITSTATUS(status))
        else:
            # child was killed. return 255
            os._exit(255)
Esempio n. 12
0
def monitorChildProcesses(children):
    """
    Monitors child processes: promptly exits if a child is found to
    have exited with a nonzero exit code received; otherwise returns
    zero when all processes exit cleanly (0).

    This approach is portable--catching SIGCHLD doesn't work on
    Windows.
    """
    while True:
        all_exited = all(not p.is_alive() for p in children)
        nonzero_exits = [p.exitcode for p in children if p.exitcode]
        if nonzero_exits:
            exitcode = nonzero_exits[0]
            logging.error("Child process exited with exitcode=%d.  Aborting." % exitcode)

            # Kill all the child processes
            for p in children:
                if p.is_alive():
                    p.terminate()

            os._exit(exitcode)
        elif all_exited:
            return 0
        time.sleep(1)
Esempio n. 13
0
    def tail(self, keep_open=True):
        """
        Tail this function's logs.

        """

        try:
            # Tail the available logs
            all_logs = self.zappa.fetch_logs(self.lambda_name)
            self.print_logs(all_logs)

            # Keep polling, and print any new logs.
            loop = True
            while loop:
                all_logs_again = self.zappa.fetch_logs(self.lambda_name)
                new_logs = []
                for log in all_logs_again:
                    if log not in all_logs:
                        new_logs.append(log)

                self.print_logs(new_logs)
                all_logs = all_logs + new_logs
                if not keep_open:
                    loop = False
        except KeyboardInterrupt: # pragma: no cover
            # Die gracefully
            try:
                sys.exit(0)
            except SystemExit:
                os._exit(130)
Esempio n. 14
0
        def inner_run():
            from django.conf import settings
            from django.utils import translation

            print "Validating models..."
            self.validate(display_num_errors=True)
            print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
            print "Development server is running at http://%s:%s/" % (addr, port)
            print "Quit the server with %s." % quit_command

            # django.core.management.base forces the locale to en-us. We should
            # set it up correctly for the first request (particularly important
            # in the "--noreload" case).
            translation.activate(settings.LANGUAGE_CODE)

            try:
                handler = AdminMediaHandler(WSGIHandler(), admin_media_path)
                run(addr, int(port), handler)
            except WSGIServerException, e:
                # Use helpful error messages instead of ugly tracebacks.
                ERRORS = {
                    13: "You don't have permission to access that port.",
                    98: "That port is already in use.",
                    99: "That IP address can't be assigned-to.",
                }
                try:
                    error_text = ERRORS[e.args[0].args[0]]
                except (AttributeError, KeyError):
                    error_text = str(e)
                sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n')
                # Need to use an OS exit because sys.exit doesn't work in a thread
                os._exit(1)
Esempio n. 15
0
def exec_piped_fork(l, env, stdout, stderr):
    # spawn using fork / exec and providing a pipe for the command's
    # stdout / stderr stream
    if stdout != stderr:
        (rFdOut, wFdOut) = os.pipe()
        (rFdErr, wFdErr) = os.pipe()
    else:
        (rFdOut, wFdOut) = os.pipe()
        rFdErr = rFdOut
        wFdErr = wFdOut
    # do the fork
    pid = os.fork()
    if not pid:
        # Child process
        os.close( rFdOut )
        if rFdOut != rFdErr:
            os.close( rFdErr )
        os.dup2( wFdOut, 1 ) # is there some symbolic way to do that ?
        os.dup2( wFdErr, 2 )
        os.close( wFdOut )
        if stdout != stderr:
            os.close( wFdErr )
        exitval = 127
        try:
            os.execvpe(l[0], l, env)
        except OSError, e:
            exitval = exitvalmap.get(e[0], e[0])
            stderr.write("scons: %s: %s\n" % (l[0], e[1]))
        os._exit(exitval)
Esempio n. 16
0
    def daemonize(self):
        """
        Fork off as a daemon
        """
        # pylint: disable=protected-access
        # An object is accessed for a non-existent member.
        # Access to a protected member of a client class
        # Make a non-session-leader child process
        try:
            pid = os.fork()  # @UndefinedVariable - only available in UNIX
            if pid != 0:
                os._exit(0)
        except OSError as error:
            sys.stderr.write('fork #1 failed: {error_num}: {error_message}\n'.format
                             (error_num=error.errno, error_message=error.strerror))
            sys.exit(1)

        os.setsid()  # @UndefinedVariable - only available in UNIX

        # https://github.com/SickRage/SickRage/issues/2969
        # http://www.microhowto.info/howto/cause_a_process_to_become_a_daemon_in_c.html#idp23920
        # https://www.safaribooksonline.com/library/view/python-cookbook/0596001673/ch06s08.html
        # Previous code simply set the umask to whatever it was because it was ANDing instead of OR-ing
        # Daemons traditionally run with umask 0 anyways and this should not have repercussions
        os.umask(0)

        # Make the child a session-leader by detaching from the terminal
        try:
            pid = os.fork()  # @UndefinedVariable - only available in UNIX
            if pid != 0:
                os._exit(0)
        except OSError as error:
            sys.stderr.write('fork #2 failed: Error {error_num}: {error_message}\n'.format
                             (error_num=error.errno, error_message=error.strerror))
            sys.exit(1)

        # Write pid
        if self.create_pid:
            pid = os.getpid()
            logger.log('Writing PID: {pid} to {filename}'.format(pid=pid, filename=self.pid_file))

            try:
                with io.open(self.pid_file, 'w') as f_pid:
                    f_pid.write('{0}\n'.format(pid))
            except EnvironmentError as error:
                logger.log_error_and_exit('Unable to write PID file: {filename} Error {error_num}: {error_message}'.format
                                          (filename=self.pid_file, error_num=error.errno, error_message=error.strerror))

        # Redirect all output
        sys.stdout.flush()
        sys.stderr.flush()

        devnull = getattr(os, 'devnull', '/dev/null')
        stdin = file(devnull)
        stdout = file(devnull, 'a+')
        stderr = file(devnull, 'a+')

        os.dup2(stdin.fileno(), getattr(sys.stdin, 'device', sys.stdin).fileno())
        os.dup2(stdout.fileno(), getattr(sys.stdout, 'device', sys.stdout).fileno())
        os.dup2(stderr.fileno(), getattr(sys.stderr, 'device', sys.stderr).fileno())
Esempio n. 17
0
    def serialize(self, data, id):
        if self.__pid != 0:
            return

        self.__currentID = id

        # In-memory case
        if self.__fileName is None:
            self.__inMemorySerializedData = zlib.compress(cPickle.dumps(data, -1))
            self.__pid = -1
            return

        # File case
        pid = os.fork()
        if pid != 0:
            self.__pid = pid
            return

        try:
            tmpFile = self.__fileName + '.tmp'
            with open(tmpFile, 'wb') as f:
                with gzip.GzipFile(fileobj=f) as g:
                    cPickle.dump(data, g, -1)
            os.rename(tmpFile, self.__fileName)
            os._exit(0)
        except:
            os._exit(-1)
Esempio n. 18
0
def daemonize():
    try:
        pid = os.fork()
        if pid > 0:
            os._exit(0)
    except OSError, e:
        err_exit("Fork #1 failed! (%s)" % (e.strerror))
Esempio n. 19
0
    def handle(self):
        masterFd, slaveFd = pty.openpty()

        try:
            # if we're not in the main thread, this will not work.
            signal.signal(signal.SIGTTOU, signal.SIG_IGN)
        except:
            pass
        pid = os.fork()
        if pid:
            os.close(masterFd)
            raise SocketConnected(slaveFd, pid)
            # make parent process the pty slave - the opposite of
            # pty.fork().  In this setup, the parent process continues
            # to act normally, while the child process performs the
            # logging.  This makes it simple to kill the logging process
            # when we are done with it and restore the parent process to
            # normal, unlogged operation.
        else:
            os.close(slaveFd)
            try:
                protocol = TelnetServerProtocolHandler(self.request, masterFd)
                protocol.handle()
            finally:
                os.close(masterFd)
                os._exit(1)
Esempio n. 20
0
def thread_it(target, args):
    port = int(args.p)
    user = args.u
    userlist = args.U
    password = args.l
    passlist = args.L
    outfile = args.o
    timeout = float(args.T)
    threads = int(args.t)

    if userlist:
        users = open(userlist).readlines()
    else:
        users = [user]
    if passlist:
        passwords = open(passlist).readlines()
    else:
        passwords = [password]

    # try/except looks dirty but we need it :/
    try:
        for user in users:
            for password in passwords:
                Run = threading.Thread(target=crack, args=(target, port, user,
                    password, outfile, timeout, 0,))
                Run.start()
                # checks that we a max number of threads
                active_threads(threads, 0.01)
                time.sleep(0.1)
        # waiting for the last running threads
        active_threads(1, 1)
    except KeyboardInterrupt:
        os._exit(1)
Esempio n. 21
0
 def start(self):
     # Fork and run
     if not os.fork():
         self.run()
         os._exit(0)
     else:
         return
Esempio n. 22
0
 def send(self, msg):
   request_calls = ['request', 'response']
   complete_message = json.dumps(msg)
   log.info("TX Sending message {0}".format(complete_message))
   try:
       self.ws.send(complete_message)
   except:
       # Retry logic only for publishing stats, not for request or response calls
       if all(request not in complete_message for request in request_calls):
           attempts = 1
           while attempts < 4:
               try:
                   log.info("Exception while sending data, applying retry logic.")
                   self.connect_soc()
                   log.info("Created New Websocket")
                   log.info("TX Sending message {0}".format(complete_message))
                   self.ws.send(complete_message)
                   break
               except Exception:
                   # Three times retry websocket connection for publishing data
                   log.info("{0} attempt".format(attempts))
                   attempts += 1
                   if attempts == 4:
                       log.exception("Exception while sending data, please check the connection and try again.")
                       self.close()
                       os._exit(0)
       else:
           log.exception("Exception while sending data, please check the connection and try again.")
           self.close()
           os._exit(0)
Esempio n. 23
0
def daemonize( errfile ):
    """
    Detach process and become a daemon.
    """
    pid = os.fork()
    if pid:
        os._exit(0)

    os.setsid()
    signal.signal(signal.SIGHUP, signal.SIG_IGN)
    os.umask(0)

    pid = os.fork()
    if pid:
        os._exit(0)

    os.chdir("/")
    for fd in range(0,20):
        try:
            os.close(fd)
        except OSError:
            pass

    sys.stdin = open("/dev/null","r")
    sys.stdout = open("/dev/null","w")
    sys.stderr = ErrorLog( errfile )
Esempio n. 24
0
    def start(self):
        """
        Start the daemon process. Checks if the pidfile is not present,
        then calls _daemonize and routes the return values differently
        for the parent and child processes.
        """
        
        pid = self.get_pid()
            
        if pid:
            msg = 'pidfile %s exists. Daemon already running.\n'
            sys.stderr.write(msg % self.pidfname)
            return -1
        
        # Start the daemon.
        # Route parent and child forks differently.
        pid = self._daemonize()

        if pid > 0:
            # This is the child fork. Run the process.
            # The run logic may exit without returning, or
            # will exit here upon completion.
            self._run()
            os._exit(0)

        elif pid == 0:
            # This is the parent fork. Return success.
            return 0

        else:
            # An error in the inital fork occurred, return error.
            return -1
Esempio n. 25
0
    def handle_error(self):
        """Handle any uncaptured error in the core.

        Overrides asyncore's handle_error.

        """
        trace = traceback.format_exc()
        stderr(trace)
        self.debug(
            __file__,
            'Fatal error in core, please review exception log',
            'always'
        )
        # TODO: make not hardcoded
        logfile = codecs.open(
            os.path.join(self.config.logdir, 'exceptions.log'),
            'a',
            encoding='utf-8'
        )
        logfile.write('Fatal error in core, handle_error() was called\n')
        logfile.write('last raw line was %s' % self.raw)
        logfile.write(trace)
        logfile.write('Buffer:\n')
        logfile.write(self.buffer)
        logfile.write('----------------------------------------\n\n')
        logfile.close()
        if self.error_count > 10:
            if (datetime.now() - self.last_error_timestamp).seconds < 5:
                print >> sys.stderr, "Too many errors, can't continue"
                os._exit(1)
        self.last_error_timestamp = datetime.now()
        self.error_count = self.error_count + 1
        if self.config.exit_on_error:
            os._exit(1)
Esempio n. 26
0
def daemonize():
    """\
    Standard daemonization of a process.
    http://www.svbug.com/documentation/comp.unix.programmer-FAQ/faq_2.html#SEC16
    """
    if not 'GUNICORN_FD' in os.environ:
        if os.fork():
            os._exit(0)
        os.setsid()

        if os.fork():
            os._exit(0)
        
        os.umask(0)
        maxfd = get_maxfd()

        # Iterate through and close all file descriptors.
        for fd in range(0, maxfd):
            try:
                os.close(fd)
            except OSError:	# ERROR, fd wasn't open to begin with (ignored)
                pass
        
        os.open(REDIRECT_TO, os.O_RDWR)
        os.dup2(0, 1)
        os.dup2(0, 2)
Esempio n. 27
0
 def _cleanup_and_exit(self, signum=None, frame=None)   :
     """
     SIGTERM handler that performs cleanup and exits daemon process.
     """
     self._cleanup()
     #sys.exit(0)
     os._exit(0)
def thread_func_endtime (endtime,timeup):
    print "Thread started"
    for imc in range(0, endtime):
        print "in thread" + str(imc)
        sleep(60*imc)
    print "exiting application"
    os._exit(1)
Esempio n. 29
0
def spawn(argv, master_read=_read, stdin_read=_read):
    """Create a spawned process."""
    if type(argv) == type(''):
        argv = (argv,)
    pid, master_fd = fork()
    if pid == CHILD:
        try:
            os.execlp(argv[0], *argv)
        except:
            # If we wanted to be really clever, we would use
            # the same method as subprocess() to pass the error
            # back to the parent.  For now just dump stack trace.
            traceback.print_exc()
        finally:
            os._exit(1)
    try:
        mode = tty.tcgetattr(STDIN_FILENO)
        tty.setraw(STDIN_FILENO)
        restore = 1
    except tty.error:    # This is the same as termios.error
        restore = 0
    try:
        _copy(master_fd, master_read, stdin_read)
    except OSError:
        # Some OSes never return an EOF on pty, just raise
        # an error instead.
        pass
    finally:
        if restore:
            tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)

    os.close(master_fd)
    return os.waitpid(pid, 0)[1]
Esempio n. 30
0
"""
wait 处理僵尸
"""
import os, time
pid = os.fork()
if pid < 0:
    print("创建进程失败")
elif pid == 0:

    # time.sleep(2)
    print("子进程")
    print("child PID=", os.getpid())
    print("parent PID=", os.getppid())
    os._exit(5)
else:
    p, status = os.wait()
    print("退出的子进程PID号:", p)
    print("退出的子进程状态:", os.WEXITSTATUS(status))
    while True:
        pass
        # os._exit(0)
        # time.sleep(1)
        # print("父进程")
        # print("child PID=", pid)
        # print("parent PID=", os.getpid())
        # print("  PID=", os.getppid())
print("fork测试结束")
Esempio n. 31
0
    if not os.path.isdir(fpath): 
        # create the new subdirectory with the chosen name
        os.mkdir(fpath)
        print("recorded animations directory created : " + options.download_filepath)
    else: 
        # if the folder already existed save files in the existing subdirectory
        print("recorded animations will be saved to directory : " + options.download_filepath)
    
    
    # Run the program with curses user interface
    try:
        # Prevent curses from introducing a 1 second delay for ESC key
        os.environ.setdefault('ESCDELAY', '0')
        # Run animation recorder interface in curses mode, then restore terminal config.
        curses.wrapper(recorder_interface.drive)
       
    except Exception as e:
        LOGGER.error("Animation Recorder has thrown an error: [%r] %s", e, e)

    finally:
        # Do any final cleanup steps.
        recorder_interface._quit_program()

    return True


if __name__ == "__main__":
    if not main():
        os._exit(1)
    os._exit(0)
Esempio n. 32
0
 def cleanUP(*args):
     cacheManager.flushCache()
     logging.writeToFile('Email Cleanup Service')
     os._exit(0)
Esempio n. 33
0
def terminate():  # close down the program
    pygame.quit()  # close pygame
    os._exit(1)
Esempio n. 34
0
async def main(start_port: int, show_timing: bool = False):

    genesis = await default_genesis_txns()
    if not genesis:
        print("Error retrieving ledger genesis transactions")
        sys.exit(1)

    agent = None

    try:
        log_status("#1 Provision an agent and wallet, get back configuration details")
        agent = AcmeAgent(
            start_port, start_port + 1, genesis_data=genesis, timing=show_timing
        )
        await agent.listen_webhooks(start_port + 2)
        await agent.register_did()

        with log_timer("Startup duration:"):
            await agent.start_process()
        log_msg("Admin url is at:", agent.admin_url)
        log_msg("Endpoint url is at:", agent.endpoint)

        # Create a schema
        log_status("#3 Create a new schema on the ledger")
        with log_timer("Publish schema duration:"):
            pass
            # TODO define schema
            # version = format(
            #     "%d.%d.%d"
            #     % (
            #         random.randint(1, 101),
            #         random.randint(1, 101),
            #         random.randint(1, 101),
            #     )
            # )
            # (
            #     schema_id,
            #     credential_definition_id,
            # ) = await agent.register_schema_and_creddef(
            #     "employee id schema",
            #     version,
            #     ["employee_id", "name", "date", "position"],
            # )

        with log_timer("Generate invitation duration:"):
            # Generate an invitation
            log_status(
                "#5 Create a connection to alice and print out the invite details"
            )
            connection = await agent.admin_POST("/connections/create-invitation")

        agent.connection_id = connection["connection_id"]
        log_json(connection, label="Invitation response:")
        log_msg("*****************")
        log_msg(json.dumps(connection["invitation"]), label="Invitation:", color=None)
        log_msg("*****************")

        log_msg("Waiting for connection...")
        await agent.detect_connection()

        async for option in prompt_loop(
            "(1) Issue Credential, (2) Send Proof Request, "
            + "(3) Send Message (X) Exit? [1/2/3/X] "
        ):
            if option in "xX":
                break

            elif option == "1":
                log_status("#13 Issue credential offer to X")
                # TODO credential offers

            elif option == "2":
                log_status("#20 Request proof of degree from alice")
                # TODO presentation requests

            elif option == "3":
                msg = await prompt("Enter message: ")
                await agent.admin_POST(
                    f"/connections/{agent.connection_id}/send-message", {"content": msg}
                )

        if show_timing:
            timing = await agent.fetch_timing()
            if timing:
                for line in agent.format_timing(timing):
                    log_msg(line)

    finally:
        terminated = True
        try:
            if agent:
                await agent.terminate()
        except Exception:
            LOGGER.exception("Error terminating agent:")
            terminated = False

    await asyncio.sleep(0.1)

    if not terminated:
        os._exit(1)
Esempio n. 35
0
    await asyncio.sleep(0.1)

    if not terminated:
        os._exit(1)


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Runs an Acme demo agent.")
    parser.add_argument(
        "-p",
        "--port",
        type=int,
        default=8040,
        metavar=("<port>"),
        help="Choose the starting port number to listen on",
    )
    parser.add_argument(
        "--timing", action="store_true", help="Enable timing information"
    )
    args = parser.parse_args()

    require_indy()

    try:
        asyncio.get_event_loop().run_until_complete(main(args.port, args.timing))
    except KeyboardInterrupt:
        os._exit(1)
Esempio n. 36
0
 def exitsig(*args):
     dbm.close()
     if current_process().name == 'MainProcess':
         print('')
         print('Resume using ./EyeWitness.py --resume {0}'.format(cli_parsed.d + '/ew.db'))
     os._exit(1)
Esempio n. 37
0
 def _graceexit(self):
     os._exit(0)
Esempio n. 38
0
        while threading.activeCount() > 1 and (
                time.time() - _) > THREAD_FINALIZATION_TIMEOUT:
            time.sleep(0.01)

        if cmdLineOptions.get("sqlmapShell"):
            cmdLineOptions.clear()
            conf.clear()
            kb.clear()
            conf.disableBanner = True
            main()


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass
    except SystemExit:
        raise
    except:
        traceback.print_exc()
    finally:
        # Reference: http://stackoverflow.com/questions/1635080/terminate-a-multi-thread-python-program
        if threading.activeCount() > 1:
            os._exit(getattr(os, "_exitcode", 0))
        else:
            sys.exit(getattr(os, "_exitcode", 0))
else:
    # cancelling postponed imports (because of Travis CI checks)
    __import__("lib.controller.controller")
Esempio n. 39
0
def sendError(msg):
    sendCmd("ERROR", [msg])
    os._exit(1)
Esempio n. 40
0
 def exit(code):
     # noinspection PyProtectedMember
     os._exit(code)
Esempio n. 41
0
def on_close(ws):
    print('### API connection closed ###')
    os._exit(0)
def Programpause():
    inputstr = raw_input('Press e to exit, Press other key to continue!!!\n')
    if (inputstr == 'e'):
        os._exit(0)
Esempio n. 43
0
def main_run_server(args):
    import io
    import signal
    import socket

    benchmark_dir, socket_name, = args

    update_sys_path(benchmark_dir)

    # Socket I/O
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.bind(socket_name)
    s.listen(1)

    # Read and act on commands from socket
    while True:
        stdout_file = None

        try:
            conn, addr = s.accept()
        except KeyboardInterrupt:
            break

        try:
            fd, stdout_file = tempfile.mkstemp()
            os.close(fd)

            # Read command
            read_size, = struct.unpack('<Q', recvall(conn, 8))
            command_text = recvall(conn, read_size)
            if sys.version_info[0] >= 3:
                command_text = command_text.decode('utf-8')

            # Parse command
            command = json.loads(command_text)
            action = command.pop('action')

            if action == 'quit':
                break
            elif action == 'preimport':
                # Import benchmark suite before forking.
                # Capture I/O to a file during import.
                with posix_redirect_output(stdout_file, permanent=False):
                    for benchmark in disc_benchmarks(benchmark_dir, ignore_import_errors=True):
                        pass

                # Report result
                with io.open(stdout_file, 'r', errors='replace') as f:
                    out = f.read()
                out = json.dumps(out)
                if sys.version_info[0] >= 3:
                    out = out.encode('utf-8')
                conn.sendall(struct.pack('<Q', len(out)))
                conn.sendall(out)
                continue

            benchmark_id = command.pop('benchmark_id')
            params_str = command.pop('params_str')
            profile_path = command.pop('profile_path')
            result_file = command.pop('result_file')
            timeout = command.pop('timeout')
            cwd = command.pop('cwd')

            if command:
                raise RuntimeError('Command contained unknown data: {!r}'.format(command_text))

            # Spawn benchmark
            run_args = (benchmark_dir, benchmark_id, params_str, profile_path, result_file)
            pid = os.fork()
            if pid == 0:
                conn.close()
                sys.stdin.close()
                exitcode = 1
                try:
                    with posix_redirect_output(stdout_file, permanent=True):
                        try:
                            os.chdir(cwd)
                            main_run(run_args)
                            exitcode = 0
                        except BaseException as ec:
                            import traceback
                            traceback.print_exc()
                finally:
                    os._exit(exitcode)

            # Wait for results
            # (Poll in a loop is simplest --- also used by subprocess.py)
            start_time = wall_timer()
            is_timeout = False
            while True:
                res, status = os.waitpid(pid, os.WNOHANG)
                if res != 0:
                    break

                if timeout is not None and wall_timer() > start_time + timeout:
                    # Timeout
                    if is_timeout:
                        os.kill(pid, signal.SIGKILL)
                    else:
                        os.kill(pid, signal.SIGTERM)
                    is_timeout = True

                time.sleep(0.05)

            # Report result
            with io.open(stdout_file, 'r', errors='replace') as f:
                out = f.read()

            info = {'out': out,
                    'errcode': -256 if is_timeout else status}

            result_text = json.dumps(info)
            if sys.version_info[0] >= 3:
                result_text = result_text.encode('utf-8')

            conn.sendall(struct.pack('<Q', len(result_text)))
            conn.sendall(result_text)
        except KeyboardInterrupt:
            break
        finally:
            conn.close()
            if stdout_file is not None:
                os.unlink(stdout_file)
Esempio n. 44
0
    def test_mount_namespace(self):
        text = """
#include <uapi/linux/ptrace.h>
BPF_TABLE("array", int, u64, stats, 1);
static void incr(int idx) {
    u64 *ptr = stats.lookup(&idx);
    if (ptr)
        ++(*ptr);
}
int count(struct pt_regs *ctx) {
    bpf_trace_printk("count() uprobe fired");
    u32 pid = bpf_get_current_pid_tgid();
    if (pid == PID)
        incr(0);
    return 0;
}"""
        # Need to import libc from ctypes to access unshare(2)
        libc = ctypes.CDLL("libc.so.6", use_errno=True)

        # Need to find path to libz.so.1
        libz_path = None 
        p = subprocess.Popen(["ldconfig", "-p"], stdout=subprocess.PIPE)
        for l in p.stdout:
            n = l.split()
            if n[0] == "libz.so.1":
                libz_path = n[-1]
        p.wait()
        p = None

        self.assertIsNotNone(libz_path)

        # fork a child that we'll place in a separate mount namespace
        child_pid = os.fork()
        if child_pid == 0:
            # Unshare CLONE_NEWNS
            if libc.unshare(0x00020000) == -1:
                e = ctypes.get_errno()
                raise OSError(e, errno.errorcode[e])
        
            # Remount root MS_REC|MS_PRIVATE
            if libc.mount(None, "/", None, (1<<14)|(1<<18) , None) == -1:
                e = ctypes.get_errno()
                raise OSError(e, errno.errorcode[e])

            if libc.mount("tmpfs", "/tmp", "tmpfs", 0, None) == -1:
                e = ctypes.get_errno()
                raise OSError(e, errno.errorcode[e])

            shutil.copy(libz_path, "/tmp")

            libz = ctypes.CDLL("/tmp/libz.so.1")
            time.sleep(1)
            libz.zlibVersion()
            time.sleep(5)
            os._exit(0)
            
        libname = "/tmp/libz.so.1"
        symname = "zlibVersion"
        text = text.replace("PID", "%d" % child_pid)
        b = bcc.BPF(text=text)
        b.attach_uprobe(name=libname, sym=symname, fn_name="count", pid=child_pid)
        b.attach_uretprobe(name=libname, sym=symname, fn_name="count", pid=child_pid)
        time.sleep(1)
        self.assertEqual(b["stats"][ctypes.c_int(0)].value, 2)
        b.detach_uretprobe(name=libname, sym=symname, pid=child_pid)
        b.detach_uprobe(name=libname, sym=symname, pid=child_pid)
        os.wait()
def path_usage():

    import pathlib
    p = pathlib.Path('.')
    [x for x in p.iterdir() if x.is_dir()]  # 列出所有子目录
    # [WindowsPath('.git'), WindowsPath('.idea'), WindowsPath('.vscode'), ...]
    list(p.glob('**/*.py'))  # 列出指定类型的文件
    # [PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]

    p = pathlib.Path(r'F:\cookies\python')
    q = p / 'learnPython'  # 使用 / 拼接路径
    print(q)
    # F: \cookies\python\learnPython

    q.exists()  # 查询属性
    # True
    q.is_dir()
    # True
    q.stat().st_size
    # 956

    q = q / "hello_world.py"  # 打开文件
    with q.open() as f:
        print(f.readline())

    p = PurePath('/etc')
    str(p)  # '/etc'
    bytes(p)  # b'/etc'
    p = PureWindowsPath('c:/Program Files')
    str(p)  # 'c:\\Program Files'

    PureWindowsPath('c:/Program Files/').drive  # 'c:'
    PurePosixPath('/etc').root  # '/'
    p = PureWindowsPath('c:/foo/bar/setup.py')
    p.parents[0]  # PureWindowsPath('c:/foo/bar')
    p.parents[1]  # PureWindowsPath('c:/foo')
    p.parents[2]  # PureWindowsPath('c:/')

    PureWindowsPath('//some/share/setup.py').name  # 'setup.py'
    PureWindowsPath('//some/share').name  # ''
    PurePosixPath('my/library/setup.py').suffix  # '.py'
    PurePosixPath('my/library.tar.gz').suffix  # '.gz'
    PurePosixPath('my/library').suffix  # ''
    PurePosixPath('my/library.tar.gar').suffixes  # ['.tar', '.gar']
    PurePosixPath('my/library.tar.gz').suffixes  # ['.tar', '.gz']
    PurePosixPath('my/library').suffixes  # []
    PurePosixPath('my/library.tar.gz').stem  # 'library.tar'
    PurePosixPath('my/library.tar').stem  # 'library'
    PurePosixPath('my/library').stem  # 'library'

    p = PureWindowsPath('c:\\windows')
    p.as_posix()  # 'c:/windows'
    p = PurePosixPath('/etc/passwd')
    p.as_uri()  # 'file:///etc/passwd'
    p = PureWindowsPath('c:/Windows')
    p.as_uri()  # 'file:///c:/Windows'
    PurePath('a/b.py').match('*.py')  # True
    PurePath('/a/b/c.py').match('a/*.py')  # False
    p = PurePosixPath('/etc/passwd')
    p.relative_to('/')  # PurePosixPath('etc/passwd')
    p.relative_to('/etc')  # PurePosixPath('passwd')
    p.relative_to(
        '/usr')  # ValueError: '/etc/passwd' does not start with '/usr'

    p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
    p.with_name('setup.py')  # PureWindowsPath('c:/Downloads/setup.py')
    p = PureWindowsPath('c:/')
    p.with_name(
        'setup.py')  # ValueError: PureWindowsPath('c:/') has an empty name

    import os  # os.path 模块中的路径名访问函数
    # 分隔
    os.path.basename()  # 去掉目录路径, 返回文件名
    os.path.dirname()  # 去掉文件名, 返回目录路径
    os.path.join()  # 将分离的各部分组合成一个路径名
    os.path.split()  # 返回 (dirname(), basename()) 元组
    os.path.splitdrive()  # 返回 (drivename, pathname) 元组
    os.path.splitext()  # 返回 (filename, extension) 元组

    # 信息
    os.path.getatime()  # 返回最近访问时间
    os.path.getctime()  # 返回文件创建时间
    os.path.getmtime()  # 返回最近文件修改时间
    os.path.getsize()  # 返回文件大小(以字节为单位)

    # 查询
    os.path.exists()  # 指定路径(文件或目录)是否存在
    os.path.isabs()  # 指定路径是否为绝对路径
    os.path.isdir()  # 指定路径是否存在且为一个目录
    os.path.isfile()  # 指定路径是否存在且为一个文件
    os.path.islink()  # 指定路径是否存在且为一个符号链接
    os.path.ismount()  # 指定路径是否存在且为一个挂载点
    os.path.samefile()  # 两个路径名是否指向同个文件

    os.path.abspath(name)  # 获得绝对路径
    os.path.normpath(path)  # 规范path字符串形式

    # 分离文件名:
    os.path.split(r"c:\python\hello.py")  # ("c:\\python", "hello.py")
    # 分离扩展名:
    os.path.splitext(r"c:\python\hello.py")  # ("c:\\python\\hello", ".py")
    # 获取路径名:
    os.path.dirname(r"c:\python\hello.py")  # "c:\\python"
    # 获取文件名:
    os.path.basename(r"r:\python\hello.py")  # "hello.py"
    # 判断文件是否存在:
    os.path.exists(r"c:\python\hello.py")  # True
    # 判断是否是绝对路径:
    os.path.isabs(r".\python")  # False
    # 判断是否是目录:
    os.path.isdir(r"c:\python")  # True
    # 判断是否是文件:
    os.path.isfile(r"c:\python\hello.py")  # True
    # 判断是否是链接文件:
    os.path.islink(r"c:\python\hello.py")  # False
    # 获取文件大小:
    os.path.getsize(filename)
    # 搜索目录下的所有文件:
    os.path.walk()

    # os 模块属性
    os.linesep  # 用于在文件中分隔行的字符串
    os.sep  # 用来分隔文件路径名的字符串
    os.pathsep  # 用于分隔文件路径的字符串
    os.curdir  # 当前工作目录的字符串名称
    os.pardir  # 当前工作目录的父目录字符串名称

    os.rename(old, new)  # 重命名
    os.remove(file)  # 删除
    os.listdir(path)  # 列出目录下的文件
    os.getcwd()  # 获取当前工作目录
    os.chdir(newdir)  # 改变工作目录
    os.makedirs(r"c:\python\test")  # 创建多级目录
    os.mkdir("test")  # 创建单个目录
    os.removedirs(r"c:\python")  # 删除多个目录
    # 删除所给路径最后一个目录下所有空目录
    os.rmdir("test")  # 删除单个目录
    os.stat(file)  # 获取文件属性
    os.chmod(file)  # 修改文件权限与时间戳
    os.system("dir")  # 执行操作系统命令
    os.exec(), os.execvp()  # 启动新进程
    osspawnv()  # 在后台执行程序
    os.exit(), os._exit()  # 终止当前进程

    import shutil  # shutil模块对文件的操作
    shutil.copy(oldfile, newfile)  # 复制单个文件
    shutil.copytree(r".\setup", r".\backup")  # 复制整个目录树
    shutil.rmtree(r".\backup")  # 删除整个目录树
    tempfile.mktemp()  # --> filename  创建一个唯一的临时文件:
    tempfile.TemporaryFile()  # 打开临时文件
Esempio n. 46
0
 def sigint_handler(self, signal, frame):
     print('Interrupted')
     self.sock.sendP(bytes([0, 0, 0, 0, 0, 0, 0, 0]))
     os._exit(1)
Esempio n. 47
0
def stop():
    os._exit(0)
Esempio n. 48
0
def handle_all_unhandled_exceptions(type, value, traceback):
    dump_error(yellow(type), yellow(value), '\n\n' + yellow('\n'.join(format_tb(traceback))))
    _exit(1)  # unrecoverable crash
Esempio n. 49
0
def cita_selection(driver: webdriver, context: CustomerProfile):
    resp_text = body_text(driver)

    if "Por favor, valide el Captcha para poder continuar" in resp_text:
        success = process_captcha(driver, context, partially=True)
        if not success:
            return None

        return phone_mail(driver, context, retry=True)

    elif "DISPONE DE 5 MINUTOS" in resp_text:
        logging.info("Cita attempt -> selection hit! :)")
        if context.save_artifacts:
            driver.save_screenshot(
                f"citas-{datetime.datetime.now()}.png".replace(":", "-"))

        try:
            driver.find_elements_by_css_selector(
                "input[type='radio'][name='rdbCita']")[0].send_keys(Keys.SPACE)
        except Exception as e:
            logging.error(e)
            pass

        btn = driver.find_element_by_id("btnSiguiente")
        btn.send_keys(Keys.ENTER)
        driver.switch_to.alert.accept()
    elif "Seleccione una de las siguientes citas disponibles" in resp_text:
        logging.info("Cita attempt -> selection hit! :)")
        if context.save_artifacts:
            driver.save_screenshot(
                f"citas-{datetime.datetime.now()}.png".replace(":", "-"))

        try:
            slots = driver.find_elements_by_css_selector(
                "#CitaMAP_HORAS tbody [id^=HUECO]")
            slot_ids = sorted([*map(lambda x: x.get_attribute("id"), slots)])
            if slot_ids:
                slot = slot_ids[0]
                driver.execute_script(
                    f"confirmarHueco({{id: '{slot}'}}, {slot[5:]});")
                driver.switch_to.alert.accept()
        except Exception as e:
            logging.error(e)
            return None
    else:
        logging.info("Cita attempt -> missed selection :(")
        return None

    # 8. Confirmation
    resp_text = body_text(driver)

    if "Debe confirmar los datos de la cita asignada" in resp_text:
        logging.info("Cita attempt -> confirmation hit! :)")

        if context.telegram_token:
            dispatcher = context.updater.dispatcher

            def shutdown():
                context.updater.stop()
                context.updater.is_idle = False

            def code_received(update, ctx):
                logging.info(f"Received code: {ctx.args[0]}")

                element = driver.find_element_by_id("txtCodigoVerificacion")
                element.send_keys(ctx.args[0])

                driver.find_element_by_id("chkTotal").send_keys(Keys.SPACE)
                driver.find_element_by_id("enviarCorreo").send_keys(Keys.SPACE)

                btn = driver.find_element_by_id("btnConfirmar")
                btn.send_keys(Keys.ENTER)

                resp_text = body_text(driver)
                ctime = datetime.datetime.now()

                if "CITA CONFIRMADA Y GRABADA" in resp_text:
                    context.bot_result = True
                    code = driver.find_element_by_id("justificanteFinal").text
                    logging.info(f"Justificante cita: {code}")
                    caption = f"Cita confirmed! {code}"
                    if context.save_artifacts:
                        image_name = f"CONFIRMED-CITA-{ctime}.png".replace(
                            ":", "-")
                        driver.save_screenshot(image_name)
                        ctx.bot.send_photo(
                            chat_id=update.effective_chat.id,
                            photo=open(os.path.join(os.getcwd(), image_name),
                                       "rb"),
                            caption=caption,
                        )
                        btn = driver.find_element_by_id("btnImprimir")
                        btn.send_keys(Keys.ENTER)
                    else:
                        ctx.bot.send_message(chat_id=update.effective_chat.id,
                                             text=caption)

                    threading.Thread(target=shutdown).start()
                    return True
                elif "Lo sentimos, el código introducido no es correcto" in resp_text:
                    ctx.bot.send_message(chat_id=update.effective_chat.id,
                                         text="Incorrect, please try again")
                else:
                    error_name = f"error-{ctime}.png".replace(":", "-")
                    driver.save_screenshot(error_name)
                    ctx.bot.send_photo(
                        chat_id=update.effective_chat.id,
                        photo=open(os.path.join(os.getcwd(), error_name),
                                   "rb"),
                    )
                    ctx.bot.send_message(chat_id=update.effective_chat.id,
                                         text="Something went wrong")

            dispatcher.add_handler(
                CommandHandler("code", code_received, pass_args=True))
            context.updater.start_polling(poll_interval=1.0)

            for i in range(5):
                speaker.say("ALARM")
            # Waiting for response 5 minutes
            time.sleep(360)
            threading.Thread(target=shutdown).start()
            if context.save_artifacts:
                driver.save_screenshot(
                    f"FINAL-SCREEN-{datetime.datetime.now()}.png".replace(
                        ":", "-"))

            if context.bot_result:
                driver.quit()
                os._exit(0)
            return None
        else:
            for i in range(10):
                speaker.say("ALARM")
            logging.info("Press Any button to CLOSE browser")
            input()
            driver.quit()
            os._exit(0)

    else:
        logging.info("Cita attempt -> missed confirmation :(")
        if context.save_artifacts:
            driver.save_screenshot(
                f"failed-confirmation-{datetime.datetime.now()}.png".replace(
                    ":", "-"))
        return None
Esempio n. 50
0
def get_usage():
    app=application.Application(backend='uia')
    flag=0
#     print os.getcwd()
    time.sleep(15)
    with open('../appFile.txt','r') as f:
        for line in f:
            fileName=line.replace('exe','tmp')
#             print fileName
    while check_exist(fileName):
        time.sleep(2)
    time.sleep(8) 
    with open('tag.txt','r') as f:
        for i in f:
            if i == '0':
                print i
#                 os._exit(0)
                exit()
    window_name = r'QuiKLab V3.0'
#-------------判断QuikLab3.0是否弹出,并且执行case的线程是否在执行------------------
    while flag == 0:
        mark=0
        try:
            for i in threading.enumerate():
                if 'Thread-1' in str(i):
                    mark=1
            if mark == 0:
                print "Quiklab exception!"
                os._exit(0)
            app.connect(title = window_name) 
            flag=1
        except:
            time.sleep(2)
            flag=0
    if os.path.exists('./CPUresult.txt'):
        os.remove('./CPUresult.txt')
        os.remove('./MEMresult.txt')
    PID=get_PID()
    if PID is None:
        print "QuikLab didn't start!"
        exit()
    c=[]
    m=[]
    try:
        def get_cpu_info():
            i = 0
            print "getting Usage... "
            while True:
                if check_exist('MainApp.exe')==0:
                    break
                i = i + 1
                cpucount = psutil.cpu_count(logical=True)   #CPU核的个数
                process = psutil.Process(int(PID))  #根据PID监视进程
                cpupercent = process.cpu_percent(interval=1)
                cpu = int(cpupercent / cpucount)
                mem = process.memory_percent()
                now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
                c.append(cpu)
                m.append(mem)
                Cpu_text = open('./CPUresult.txt', 'a')
                Mem_text = open('./MEMresult.txt', 'a')
                if cpu <= 50:
#                     print u'CPU usage is %s' % cpu + '         ' + now
#                     print u'MEM usage is %s' % mem + '         ' + now
                    print>> Cpu_text, 'CPU使用率:%s%%' % cpu + '    ' + now
                    print>> Mem_text, 'MEM使用率:%s%%' % mem + '    ' + now
                    Cpu_text.close()
                    Mem_text.close()
                else:
                    print 'CPU使用率:%s%%,占用率过高' % cpu + '    ' + now
                    print 'MEM使用率:%s%%' % mem + '    ' + now
                    print>> Cpu_text, 'CPU使用率%s%%,占用率过高' % cpu + '    ' + now
                    print>> Mem_text, 'MEM使用率:%s%%,占用率过高' % mem + '   ' + now
                    Cpu_text.close()
                    Mem_text.close()
#         print 'Result will be in result.txt'
        time.sleep(1)
        print "-------------------------------------------------"
        get_cpu_info()
    except:
        pass
    finally:
        if max(c) > 50:
            with open('./CPUresult.txt','a') as f:
                print "CPU占用过高,Performance Test Fail"
                f.write("CPU占用过高,Performance Test Fail")
        else:
            with open('./CPUresult.txt','a') as f:
                print "Performance Test Pass"
                f.write('Performance Test Pass')
        print  u'进程%s' % PID + u'已经结束'
Esempio n. 51
0
def signal_handler(signal, frame):
    os._exit(0)
Esempio n. 52
0
def input_cmd(dns_server, http_request_method, url, vuln_parameter, technique):

    err_msg = ""
    if menu.enumeration_options():
        err_msg += "enumeration"
    if menu.file_access_options():
        if err_msg != "":
            err_msg = err_msg + " and "
        err_msg = err_msg + "file-access"

    if err_msg != "":
        warn_msg = "The " + err_msg + " options are not supported "
        warn_msg += "by this module because of the structure of the exfiltrated data. "
        warn_msg += "Please try using any unix-like commands manually."
        print(settings.print_warning_msg(warn_msg))

    # Pseudo-Terminal shell
    go_back = False
    go_back_again = False
    while True:
        if go_back == True:
            break
        if not menu.options.batch:
            question_msg = "Do you want a Pseudo-Terminal shell? [Y/n] > "
            gotshell = _input(settings.print_question_msg(question_msg))
        else:
            gotshell = ""
        if len(gotshell) == 0:
            gotshell = "Y"
        if gotshell in settings.CHOICE_YES:
            print("\nPseudo-Terminal (type '" + Style.BRIGHT + "?" +
                  Style.RESET_ALL + "' for available options)")
            if settings.READLINE_ERROR:
                checks.no_readline_module()
            while True:
                try:
                    if not settings.READLINE_ERROR:
                        checks.tab_autocompleter()
                    cmd = _input("""commix(""" + Style.BRIGHT + Fore.RED +
                                 """os_shell""" + Style.RESET_ALL + """) > """)
                    cmd = checks.escaped_cmd(cmd)
                    if cmd.lower() in settings.SHELL_OPTIONS:
                        if cmd.lower() == "quit" or cmd.lower() == "back":
                            print(settings.SPACE)
                            os._exit(0)
                        elif cmd.lower() == "?":
                            menu.os_shell_options()
                        elif cmd.lower() == "os_shell":
                            warn_msg = "You are already into the '" + cmd.lower(
                            ) + "' mode."
                            print(settings.print_warning_msg(warn_msg)) + "\n"
                        elif cmd.lower() == "reverse_tcp":
                            warn_msg = "This option is not supported by this module."
                            print(settings.print_warning_msg(warn_msg)) + "\n"
                    else:
                        # Command execution results.
                        cmd_exec(dns_server, http_request_method, cmd, url,
                                 vuln_parameter)

                except KeyboardInterrupt:
                    print(settings.SPACE)
                    os._exit(0)

                except:
                    print(settings.SPACE)
                    os._exit(0)

        elif gotshell in settings.CHOICE_NO:
            print(settings.SPACE)
            os._exit(0)

        elif gotshell in settings.CHOICE_QUIT:
            print(settings.SPACE)
            os._exit(0)

        else:
            err_msg = "'" + gotshell + "' is not a valid answer."
            print(settings.print_error_msg(err_msg))
            pass
Esempio n. 53
0
 def stop_handler(self, signum, frame):
     self.w.stop()
     self.r.stop()
     self.del_host()
     os._exit(0)
Esempio n. 54
0
def dns_exfiltration_handler(url, http_request_method):
    # Check injection state
    settings.DETECTION_PHASE = True
    settings.EXPLOITATION_PHASE = False
    # You need to have root privileges to run this script
    if os.geteuid() != 0:
        err_msg = "You need to have root privileges to run this option."
        print("\n" + settings.print_critical_msg(err_msg))
        os._exit(0)

    if http_request_method == "GET":
        #url = parameters.do_GET_check(url)
        vuln_parameter = parameters.vuln_GET_param(url)
        request = _urllib.request.Request(url)
        headers.do_check(request)

    else:
        parameter = menu.options.data
        parameter = _urllib.parse.unquote(parameter)
        parameter = parameters.do_POST_check(parameter)
        request = _urllib.request.Request(url, parameter)
        headers.do_check(request)
        vuln_parameter = parameters.vuln_POST_param(parameter, url)

    # Check if defined any HTTP Proxy.
    if menu.options.proxy:
        try:
            response = proxy.use_proxy(request)
        except _urllib.error.HTTPError as err_msg:
            if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR:
                response = False
            elif settings.IGNORE_ERR_MSG == False:
                err = str(err_msg) + "."
                print("\n" + settings.print_critical_msg(err))
                continue_tests = checks.continue_tests(err_msg)
                if continue_tests == True:
                    settings.IGNORE_ERR_MSG = True
                else:
                    os._exit(0)

    # Check if defined Tor.
    elif menu.options.tor:
        try:
            response = tor.use_tor(request)
        except _urllib.error.HTTPError as err_msg:
            if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR:
                response = False
            elif settings.IGNORE_ERR_MSG == False:
                err = str(err_msg) + "."
                print("\n" + settings.print_critical_msg(err))
                continue_tests = checks.continue_tests(err_msg)
                if continue_tests == True:
                    settings.IGNORE_ERR_MSG = True
                else:
                    os._exit(0)

    else:
        try:
            response = _urllib.request.urlopen(request,
                                               timeout=settings.TIMEOUT)
        except _urllib.error.HTTPError as err_msg:
            if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR:
                response = False
            elif settings.IGNORE_ERR_MSG == False:
                err = str(err_msg) + "."
                print("\n" + settings.print_critical_msg(err))
                continue_tests = checks.continue_tests(err_msg)
                if continue_tests == True:
                    settings.IGNORE_ERR_MSG = True
                else:
                    os._exit(0)

    if settings.TARGET_OS == "win":
        err_msg = "This module's payloads are not suppoted by "
        err_msg += "the identified target operating system."
        print(settings.print_critical_msg(err_msg) + "\n")
        os._exit(0)

    else:
        dns_server = menu.options.dns_server
        technique = "DNS exfiltration module"
        info_msg = "Loading the " + technique + ". \n"
        sys.stdout.write(settings.print_info_msg(info_msg))
        exploitation(dns_server, url, http_request_method, vuln_parameter,
                     technique)
Esempio n. 55
0
# 多次输入错误密码时,需要提交验证码,将会将验证码下载
if resJ['code'] == 10:
    ckr = ss.get(ckcodeurl)
    print('需要输入验证码,已经保存到目录下:')
    with open('ck.jpeg', 'wb') as f:
        f.write(ckr.content)
    os.system('start ck.jpeg')
    ckcode = input()
    data['inputvcode'] = ckcode
    res = ss.post(itesturl, headers=headers, data=data)
    resJ = json.loads(res.text)

if resJ['code'] == 9 or resJ['code'] == 10:
    print('用户名或密码或验证码错误. 程序退出.')
    os.system("pause")
    os._exit(0)

# 获取当前的测试列表
res_class = ss.get(classurl)

# 使用soup解析网页
bs = BeautifulSoup(res_class.text, 'lxml')

# 提取h2标签
lst = bs.find_all('h2')

print('当前任务: \n')

# 该列表记录着所有的作业列表以及可用状态
# 每行包含三个元素,分别是: id, 是否可以查看答案, 以及是否正在考试, 科目名
# 没错,考试可以多个同时进行
Esempio n. 56
0
                status = 0
            except SystemExit, e:
                status = e.code
                if status is None:
                    status = 0
            except KeyboardInterrupt:
                status = 1
            except:
                status = -1
                traceback.print_exc()
            finally:
                # prevent child from killing anything (cf __del__), or
                # running any other cleanup code normally done by the parent
                try:
                    save_coverage()
                    os._exit(status)
                except:
                    print >>sys.stderr, status
                finally:
                    os._exit(1)
        logging.info('pid %u: %s %s',
            self.pid, command, ' '.join(map(repr, args)))

    def child_coverage(self):
        r = self._coverage_fd
        if r is not None:
            try:
                os.read(r, 1)
            finally:
                os.close(r)
                del self._coverage_fd
Esempio n. 57
0
        def _execute_child(self, args, executable, preexec_fn, close_fds,
                           pass_fds, cwd, env, universal_newlines,
                           startupinfo, creationflags, shell,
                           p2cread, p2cwrite,
                           c2pread, c2pwrite,
                           errread, errwrite,
                           restore_signals, start_new_session):
            """Execute program (POSIX version)"""

            if PY3 and isinstance(args, (str, bytes)):
                args = [args]
            elif not PY3 and isinstance(args, string_types):
                args = [args]
            else:
                args = list(args)

            if shell:
                args = ["/bin/sh", "-c"] + args
                if executable:
                    args[0] = executable

            if executable is None:
                executable = args[0]

            self._loop.install_sigchld()

            # For transferring possible exec failure from child to parent
            # The first char specifies the exception type: 0 means
            # OSError, 1 means some other error.
            errpipe_read, errpipe_write = self.pipe_cloexec()
            # errpipe_write must not be in the standard io 0, 1, or 2 fd range.
            low_fds_to_close = []
            while errpipe_write < 3:
                low_fds_to_close.append(errpipe_write)
                errpipe_write = os.dup(errpipe_write)
            for low_fd in low_fds_to_close:
                os.close(low_fd)
            try:
                try:
                    gc_was_enabled = gc.isenabled()
                    # Disable gc to avoid bug where gc -> file_dealloc ->
                    # write to stderr -> hang.  http://bugs.python.org/issue1336
                    gc.disable()
                    try:
                        self.pid = fork_and_watch(self._on_child, self._loop, True, fork)
                    except:
                        if gc_was_enabled:
                            gc.enable()
                        raise
                    if self.pid == 0:
                        # Child
                        try:
                            # Close parent's pipe ends
                            if p2cwrite is not None:
                                os.close(p2cwrite)
                            if c2pread is not None:
                                os.close(c2pread)
                            if errread is not None:
                                os.close(errread)
                            os.close(errpipe_read)

                            # When duping fds, if there arises a situation
                            # where one of the fds is either 0, 1 or 2, it
                            # is possible that it is overwritten (#12607).
                            if c2pwrite == 0:
                                c2pwrite = os.dup(c2pwrite)
                            if errwrite == 0 or errwrite == 1:
                                errwrite = os.dup(errwrite)

                            # Dup fds for child
                            def _dup2(a, b):
                                # dup2() removes the CLOEXEC flag but
                                # we must do it ourselves if dup2()
                                # would be a no-op (issue #10806).
                                if a == b:
                                    self._set_cloexec_flag(a, False)
                                elif a is not None:
                                    os.dup2(a, b)
                                self._remove_nonblock_flag(b)
                            _dup2(p2cread, 0)
                            _dup2(c2pwrite, 1)
                            _dup2(errwrite, 2)

                            # Close pipe fds.  Make sure we don't close the
                            # same fd more than once, or standard fds.
                            closed = set([None])
                            for fd in [p2cread, c2pwrite, errwrite]:
                                if fd not in closed and fd > 2:
                                    os.close(fd)
                                    closed.add(fd)

                            if cwd is not None:
                                os.chdir(cwd)

                            if preexec_fn:
                                preexec_fn()

                            # Close all other fds, if asked for. This must be done
                            # after preexec_fn runs.
                            if close_fds:
                                fds_to_keep = set(pass_fds)
                                fds_to_keep.add(errpipe_write)
                                self._close_fds(fds_to_keep)
                            elif hasattr(os, 'get_inheritable'):
                                # close_fds was false, and we're on
                                # Python 3.4 or newer, so "all file
                                # descriptors except standard streams
                                # are closed, and inheritable handles
                                # are only inherited if the close_fds
                                # parameter is False."
                                for i in xrange(3, MAXFD):
                                    try:
                                        if i == errpipe_write or os.get_inheritable(i):
                                            continue
                                        os.close(i)
                                    except:
                                        pass

                            if restore_signals:
                                # restore the documented signals back to sig_dfl;
                                # not all will be defined on every platform
                                for sig in 'SIGPIPE', 'SIGXFZ', 'SIGXFSZ':
                                    sig = getattr(signal, sig, None)
                                    if sig is not None:
                                        signal.signal(sig, signal.SIG_DFL)

                            if start_new_session:
                                os.setsid()

                            if env is None:
                                os.execvp(executable, args)
                            else:
                                os.execvpe(executable, args, env)

                        except:
                            exc_type, exc_value, tb = sys.exc_info()
                            # Save the traceback and attach it to the exception object
                            exc_lines = traceback.format_exception(exc_type,
                                                                   exc_value,
                                                                   tb)
                            exc_value.child_traceback = ''.join(exc_lines)
                            os.write(errpipe_write, pickle.dumps(exc_value))

                        finally:
                            # Make sure that the process exits no matter what.
                            # The return code does not matter much as it won't be
                            # reported to the application
                            os._exit(1)

                    # Parent

                    if gc_was_enabled:
                        gc.enable()
                finally:
                    # be sure the FD is closed no matter what
                    os.close(errpipe_write)

                # self._devnull is not always defined.
                devnull_fd = getattr(self, '_devnull', None)
                if p2cread is not None and p2cwrite is not None and p2cread != devnull_fd:
                    os.close(p2cread)
                if c2pwrite is not None and c2pread is not None and c2pwrite != devnull_fd:
                    os.close(c2pwrite)
                if errwrite is not None and errread is not None and errwrite != devnull_fd:
                    os.close(errwrite)
                if devnull_fd is not None:
                    os.close(devnull_fd)
                # Prevent a double close of these fds from __init__ on error.
                self._closed_child_pipe_fds = True

                # Wait for exec to fail or succeed; possibly raising exception
                errpipe_read = FileObject(errpipe_read, 'rb')
                data = errpipe_read.read()
            finally:
                if hasattr(errpipe_read, 'close'):
                    errpipe_read.close()
                else:
                    os.close(errpipe_read)

            if data != b"":
                self.wait()
                child_exception = pickle.loads(data)
                for fd in (p2cwrite, c2pread, errread):
                    if fd is not None:
                        os.close(fd)
                raise child_exception
Esempio n. 58
0
def Vcf(opts):
	base_dir=os.getcwd()
	config_file=opts.Config_file
	f=open(config_file)
	config_list=yaml.load(f)
	#######read and parse parameter
	print "Start reading and parsing parameter..."
	time.sleep(5)
	output_fold=config_list["output_fold"]
	ptuneos_bin_path="bin"
	vcf_file=config_list["vcf_file"]
	REFERENCE=base_dir + "/" + "database/Fasta/human.fasta"
	somatic_out_fold=output_fold + '/' + 'somatic_mutation'
	logfile_out_fold=output_fold + '/' + 'logfile'
	prefix=config_list["sample_name"]
	tumor_depth_cutoff=config_list["tumor_depth_cutoff"]
	tumor_vaf_cutoff=config_list["tumor_vaf_cutoff"]
	normal_vaf_cutoff=config_list["normal_vaf_cutoff"]
	vep_cache=config_list["vep_cache_path"]
	vep_path=config_list["vep_path"]
	netmhc_out_fold=output_fold + '/' + 'netmhc'
	indel_fasta_file=netmhc_out_fold+'/'+prefix+'_indel.fasta'
	hla_str=config_list["hla_str"]
	split_num=200
	netchop_path="software/netchop"
	human_peptide_path="database/Protein/human.pep.all.fa"
	exp_file=config_list["expression_file"]
	binding_fc_aff_cutoff=int(config_list["binding_fc_aff_cutoff"])
	binding_aff_cutoff=int(config_list["binding_aff_cutoff"])
	fpkm_cutoff=int(config_list["fpkm_cutoff"])
	netctl_out_fold=output_fold + '/' + 'netctl'
	netMHCpan_path=config_list["netMHCpan_path"]
	snv_fasta_file=netmhc_out_fold+'/'+prefix+'_snv.fasta'
	snv_netmhc_out_file=netmhc_out_fold+'/'+prefix+'_snv_netmhc.tsv'
	indel_netmhc_out_file=netmhc_out_fold+'/'+prefix+'_indel_netmhc.tsv'
	vcftools_path="software/vcftools"
	peptide_length=config_list["peptide_length"]
	pyclone_fold=output_fold + '/' + 'pyclone'
	pyclone_path=config_list["pyclone_path"]
	copynumber_profile=config_list["copynumber_profile"]
	tumor_cellularity=float(config_list["tumor_cellularity"])
	snv_final_neo_file=netctl_out_fold + '/' + prefix + '_pyclone_neo.tsv'
	indel_final_neo_file=netctl_out_fold + '/' + prefix + '_indel_netctl_concact.tsv'
	iedb_file="train_model/iedb.fasta"
	cf_hy_model_9="train_model/cf_hy_9_model.m"
	cf_hy_model_10="train_model/cf_hy_10_model.m"
	cf_hy_model_11="train_model/cf_hy_11_model.m"
	RF_model="train_model/RF_train_model.m"
	driver_gene_path="software/DriveGene.tsv"
	snv_neo_model_file=netctl_out_fold + '/' + prefix + '_snv_neo_model.tsv'
	snv_blastp_tmp_file=netctl_out_fold + '/' + prefix + '_snv_blastp_tmp.tsv'
	snv_blastp_out_tmp_file=netctl_out_fold + '/' + prefix + '_snv_blastp_out_tmp.tsv'
	snv_netMHCpan_pep_tmp_file=netctl_out_fold + '/' + prefix + '_snv_netMHCpan_pep_tmp.tsv'
	snv_netMHCpan_ml_out_tmp_file=netctl_out_fold + '/' + prefix + '_snv_netMHCpan_ml_out_tmp.tsv'
	indel_neo_model_file=netctl_out_fold + '/' + prefix + '_indel_neo_model.tsv'
	indel_blastp_tmp_file=netctl_out_fold + '/' + prefix + '_indel_blastp_tmp.tsv'
	indel_blastp_out_tmp_file=netctl_out_fold + '/' + prefix + '_indel_blastp_out_tmp.tsv'
	indel_netMHCpan_pep_tmp_file=netctl_out_fold + '/' + prefix + '_indel_netMHCpan_pep_tmp.tsv'
	indel_netMHCpan_ml_out_tmp_file=netctl_out_fold + '/' + prefix + '_indel_netMHCpan_ml_out_tmp.tsv'
	blast_db_path="database/Protein/peptide_database/peptide"
	#####check input file,tool path and reference file#####
	if os.path.exists(vcf_file):
		print "Check inuput mutation vcf file...  OK"
	else:
		print "Please check your input vcf file!"
		os._exit(1)
	if os.path.exists(vep_path):
		print "Check vep path...  OK"
	else:
		print "Please check your vep path!"
		os._exit(1)	
	if os.path.exists(vep_cache):
		print "Check vep cache path...  OK"
	else:
		print "Please check your vep cache path!"
		os._exit(1)	
	if os.path.exists(exp_file):
		print "Check expression file...  OK"
	else:
		print "Please check your expression file path!"
		os._exit(1)		
	time.sleep(5)
	#####check output directory###
	print "Check output directory"
	if not os.path.exists(output_fold):
		os.mkdir(output_fold)
	if not os.path.exists(somatic_out_fold):
		os.mkdir(somatic_out_fold)
	if not os.path.exists(netmhc_out_fold):
		os.mkdir(netmhc_out_fold)
	if not os.path.exists(netctl_out_fold):
		os.mkdir(netctl_out_fold)
	if not os.path.exists(logfile_out_fold):
		os.mkdir(logfile_out_fold)
	if not os.path.exists(pyclone_fold):
		os.mkdir(pyclone_fold)
	if hla_str=="None":
		print "please provied hla type, seperate by comma,eg:HLiA-A02:01,HLA-A01:01,HLA-B15:17,HLA-B13:02,HLA-C07:01,HLA-C06:02"		
 	else:
 		print "Check hla alleles...  OK"
 	print "Start preprocessing VCF file..."
  	processes_0=[]
	h1=multiprocessing.Process(target=VCF_process,args=(prefix,vcf_file,somatic_out_fold,vcftools_path,vep_path,vep_cache,netmhc_out_fold,tumor_depth_cutoff,tumor_vaf_cutoff,normal_vaf_cutoff,ptuneos_bin_path,human_peptide_path,logfile_out_fold,))
 	processes_0.append(h1)
 	for p in processes_0:
		p.daemon = True
		p.start()
	for p in processes_0:
		p.join()
	print "Preprocessing VCF file done!"
	print "Start neoantigen prediction..."
 	processes_1=[]
	d1=multiprocessing.Process(target=snv_neo,args=(snv_fasta_file,hla_str,driver_gene_path,snv_netmhc_out_file,netmhc_out_fold,split_num,prefix,exp_file,binding_fc_aff_cutoff,binding_aff_cutoff,fpkm_cutoff,netctl_out_fold,netMHCpan_path,peptide_length,ptuneos_bin_path,netchop_path,))
 	processes_1.append(d1)
 	d2=multiprocessing.Process(target=indel_neo,args=(indel_fasta_file,somatic_out_fold,hla_str,driver_gene_path,indel_netmhc_out_file,split_num,netMHCpan_path,prefix,exp_file,binding_fc_aff_cutoff,binding_aff_cutoff,fpkm_cutoff,netctl_out_fold,netmhc_out_fold,peptide_length,ptuneos_bin_path,netchop_path,REFERENCE,human_peptide_path,))
 	processes_1.append(d2)
 	for p in processes_1:
		p.daemon = True
		p.start()
	for p in processes_1:
		p.join()
	print "Neoantigen prediciton done!"

	print "Neoantigen annotation..."
 	processes_2=[]
	m1=multiprocessing.Process(target=pyclone_annotation,args=(somatic_out_fold,copynumber_profile,tumor_cellularity,prefix,pyclone_fold,netctl_out_fold,pyclone_path,ptuneos_bin_path,logfile_out_fold,))
 	processes_2.append(m1)
 	for p in processes_2:
		p.daemon = True
		p.start()
	for p in processes_2:
		p.join()
	print "Neoantigen annotation done!"
	print "Neoantigen filtering using Pre&RecNeo model and refined immunogenicity score scheme."
	processes_3=[]
	r1=multiprocessing.Process(target=InVivoModelAndScoreSNV,args=(snv_final_neo_file,cf_hy_model_9,cf_hy_model_10,cf_hy_model_11,RF_model,snv_neo_model_file,snv_blastp_tmp_file,snv_blastp_out_tmp_file,snv_netMHCpan_pep_tmp_file,snv_netMHCpan_ml_out_tmp_file,iedb_file,blast_db_path,))
	processes_3.append(r1)
	if os.path.exists(indel_final_neo_file):
		r2=multiprocessing.Process(target=InVivoModelAndScoreINDEL,args=(indel_final_neo_file,cf_hy_model_9,cf_hy_model_10,cf_hy_model_11,RF_model,indel_neo_model_file,indel_blastp_tmp_file,indel_blastp_out_tmp_file,indel_netMHCpan_pep_tmp_file,indel_netMHCpan_ml_out_tmp_file,iedb_file,blast_db_path,))
		processes_3.append(r2)
	else:
		print "No neoantigen from Indels is identified!"
	for p in processes_3:
		p.daemon = True
		p.start()
	for p in processes_3:
		p.join()		
	print "All Finished! please check result files 'snv_neo_model.tsv' and 'indel_neo_model.tsv' in netctl fold"
Esempio n. 59
0
    def run(self):
        ircd = self.ircd
        while ircd.running:
            try:

                if ircd.use_poll:
                    fdVsEvent = ircd.pollerObject.poll(1000)
                    for fd, Event in fdVsEvent:
                        try:
                            s = ircd.fd_to_socket[fd][0]
                            c = ircd.fd_to_socket[fd][1]
                            t = type(c).__name__

                            if Event & (select.POLLIN | select.POLLPRI):
                                logging.debug('POLLIN for {}'.format(c))
                                if s in self.listen_socks:
                                    logging.debug(
                                        'Incoming connection on {}'.format(s))
                                    threading.Thread(target=sock_accept,
                                                     args=([ircd, s])).start()

                                elif t in ['User', 'Server']:
                                    logging.debug(
                                        'Reading data from {}'.format(c))
                                    read_socket(ircd, c)
                                try:
                                    ircd.pollerObject.modify(s, READ_WRITE)
                                    logging.debug(
                                        'Flag for {} set to READ_WRITE'.format(
                                            c))
                                except FileNotFoundError:  # Already closed.
                                    pass
                                continue
                            elif Event & select.POLLOUT:
                                logging.debug('POLLOUT for {} ({})'.format(
                                    s, c))
                                if c.sendbuffer:
                                    logging.debug(
                                        'Sending data to {}'.format(c))
                                    check_flood(ircd, c)
                                    try:
                                        sent = s.send(
                                            bytes(c.sendbuffer, 'utf-8'))
                                        c.sendbuffer = c.sendbuffer[sent:]
                                    except Exception as ex:
                                        logging.exception(ex)
                                        c.quit('Write error')
                                        time.sleep(1000)
                                logging.debug(
                                    'Flag for {} set to READ_ONLY'.format(c))
                                ircd.pollerObject.modify(s, READ_ONLY)
                                continue
                            elif Event & select.POLLHUP:
                                # ircd.pollerObject.unregister(s)
                                c.quit('Hung up poll')

                            elif Event & select.POLLERR:
                                # ircd.pollerObject.unregister(s)
                                c.quit('Polling error')
                        except Exception as ex:
                            logging.exception(ex)
                            time.sleep(1000)

                        check_loops(ircd)
                        continue

                else:
                    read_clients = itertools.chain(listen_socks(ircd),
                                                   users(ircd), servers(ircd))
                    write_clients = itertools.chain(users(ircd, 'w'),
                                                    servers(ircd, 'w'))

                    # print(f"Size of read_clients: {sys.getsizeof(read_clients)}")
                    try:
                        read, write, error = select.select(
                            read_clients, write_clients, read_clients, 1.0)
                        # read and error need the same iteratable.
                        # read, write, error = select.select(list(self.listen_socks) + read_users + read_servers, write_users + write_servers, read_users + #read_servers + write_users + write_servers + list(self.listen_socks), 1.0)
                    except ValueError as ex:
                        for fd in iter([
                                fd for fd in iter(ircd.users)
                                if fd.socket and not fd.registered
                        ]):
                            fd.quit('Limit reached')
                        logging.info('Cleanup done')
                        logging.exception(ex)
                        continue

                    for s in error:
                        logging.error('Error occurred in {}'.format(s))

                    for s in read:
                        if s in write:  # Write first.
                            continue
                        if type(s).__name__ in ['User', 'Server']:
                            read_socket(ircd, s)
                            continue
                        if self.listen_socks[s] in ['clients', 'servers']:
                            threading.Thread(target=sock_accept,
                                             args=([ircd, s])).start()
                            continue

                    for s in write:
                        check_flood(ircd, s)
                        if type(s).__name__ == 'User' or type(
                                s).__name__ == 'Server':
                            try:
                                sent = s.socket.send(
                                    bytes(s.sendbuffer, 'utf-8'))
                                s.sendbuffer = s.sendbuffer[sent:]
                                if type(s).__name__ == 'User' and (hasattr(
                                        s, 'flood_safe') and s.flood_safe):
                                    s.flood_safe = False
                                    logging.debug(
                                        'Flood_safe for {} unset.'.format(s))
                            except Exception as ex:
                                s.quit('Write error: {}'.format(str(ex)))
                                continue

                    check_loops(ircd)
            except KeyboardInterrupt as ex:
                # cleanup(ircd)
                os._exit(0)
                return

            except Exception as ex:
                logging.exception(ex)
                break
        os._exit(0)
        logging.warning(
            'data_handler loop broke! This should only happen after /restart.')
Esempio n. 60
-1
def main():
    options, method = parse_options()

    log.init(options.logfile, options.loglevel)

    if not options.dump and options.daemonize:
        if os.fork() > 0:
            os._exit(0)
        os.chdir("/")
        os.setsid()
        if os.fork() > 0:
            os._exit(0)
        log.init_stdio()

    try:
        config = coil.parse(DEFAULT_CONFIG)
        if method.defaults:
            if isinstance(method.defaults, str):
                config.merge(coil.parse(method.defaults))
            else:
                config.merge(coil.struct.Struct(method.defaults))
        if options.config:
            config.merge(coil.parse_file(options.config))
    except coil.errors.CoilError, ex:
        log.error("Error parsing config: %s" % ex)
        sys.exit(1)