Example #1
0
 def setClipboardData(data):
     my_logger.debug('  %s: setClipboardData data = "%s"' % (sys.platform, data))
     p = Popen(['/usr/bin/xsel', '-t', '200', '-i'], stdin=PIPE)
     result = p.communicate(input=data.encode('utf-8'))
     p.wait(2)
     my_logger.debug('  %s: setClipboardData done' % sys.platform)
     return result
Example #2
0
 def test_timeExceeded(self):
     start = time()
     p = Popen(['python', 'dummy.py', '-t', '1', '-l', '2'],
               stdout=PIPE,
               stderr=PIPE)
     retcode = p.wait(1)
     self.assertEquals(retcode, -9)
Example #3
0
    def run(self):
        """ Start the process and hook up the callbacks.
        """
        env = os.environ
        env['TERM'] = 'xterm'
        process = Popen(
            self.command_string + ' 2>&1',
            shell=True,
            env=env,
            universal_newlines=True,
            stdout=PIPE,
            stdin=PIPE,
        )
        self.process = process
        while True:
            out_char = process.stdout.read(1)
            if out_char == '':
                if process.poll() is not None:
                    # The process has finished
                    break
                else:
                    # The process is not giving any interesting
                    # output. No use polling it immediatly.
                    sleep(0.1)
            else:
                self.out_callback(out_char)

        if self.end_callback is not None:
            self.end_callback()
Example #4
0
 def getClipboardData():  # xclip needs iso-8859-1 for ver < 0.11 #failed
     my_logger.debug('  %s: getClipboardData' % sys.platform)
     p = Popen(['/usr/bin/xsel', '-t', '200', '-o'], stdout=PIPE)
     p.wait(2)
     result = p.communicate()[0].decode('utf-8')
     my_logger.debug('  %s: gotClipboardData "%s"' % (sys.platform, result))
     return result
Example #5
0
 def getClipboardData(): # xclip needs iso-8859-1 for ver < 0.11 #failed
     my_logger.debug('  %s: getClipboardData' % sys.platform)
     p = Popen(['/usr/bin/xsel', '-t', '200', '-o'], stdout=PIPE)
     p.wait(2)
     result = p.communicate()[0].decode('utf-8')
     my_logger.debug('  %s: gotClipboardData "%s"' % (sys.platform, result))
     return result
Example #6
0
    def run(self):
        """ Start the process and hook up the callbacks.
        """
        env = os.environ
        env['TERM'] = 'xterm'
        process = Popen(self.command_string + ' 2>&1', shell=True,
                                env=env,
                                universal_newlines=True,
                                stdout=PIPE, stdin=PIPE, )
        self.process = process
        while True:
            out_char = process.stdout.read(1)
            if out_char == '':
                if process.poll() is not None:
                    # The process has finished
                    break
                else:
                    # The process is not giving any interesting
                    # output. No use polling it immediatly.
                    sleep(0.1)
            else:
                self.out_callback(out_char)

        if self.end_callback is not None:
            self.end_callback()
Example #7
0
 def setClipboardData(data):
     my_logger.debug('  %s: setClipboardData data = "%s"' %
                     (sys.platform, data))
     p = Popen(['/usr/bin/xsel', '-t', '200', '-i'], stdin=PIPE)
     result = p.communicate(input=data.encode('utf-8'))
     p.wait(2)
     my_logger.debug('  %s: setClipboardData done' % sys.platform)
     return result
Example #8
0
    def test_InTimeRun(self):
        start = time()
        p = Popen(['python', 'dummy.py', '-t', '1', '-l', '2'], stdout=PIPE, stderr=PIPE)
        stdout, stderr = p.communicate()
        out_length = len(filter(None, stdout.split("\n")))
        err_length = len(stderr.split("\n"))

        self.assertEquals(out_length, 2)
        self.assertEquals(err_length, 2)
        self.assertEquals(floor(time()-start), 2)
Example #9
0
    def test_InTimeRun(self):
        start = time()
        p = Popen(['python', 'dummy.py', '-t', '1', '-l', '2'],
                  stdout=PIPE,
                  stderr=PIPE)
        stdout, stderr = p.communicate()
        out_length = len(filter(None, stdout.split("\n")))
        err_length = len(stderr.split("\n"))

        self.assertEquals(out_length, 2)
        self.assertEquals(err_length, 2)
        self.assertEquals(floor(time() - start), 2)
Example #10
0
def child():
    print 'Starting child'
    currentProc = GetCurrentProcess()
    injob = IsProcessInJob(currentProc)
    print "Is in a job?: %s" % injob
    can_create = CanCreateJobObject()
    print 'Can create job?: %s' % can_create
    process = Popen('c:\\windows\\notepad.exe')
    assert process._job
    jobinfo = QueryInformationJobObject(process._job, 'JobObjectExtendedLimitInformation')
    print 'Job info: %s' % jobinfo
    limitflags = jobinfo['BasicLimitInformation']['LimitFlags']
    print 'LimitFlags: %s' % limitflags
    process.kill()
Example #11
0
def child():
    print "Starting child"
    currentProc = GetCurrentProcess()
    injob = IsProcessInJob(currentProc)
    print "Is in a job?: %s" % injob
    can_create = CanCreateJobObject()
    print "Can create job?: %s" % can_create
    process = Popen("c:\\windows\\notepad.exe")
    assert process._job
    jobinfo = QueryInformationJobObject(process._job, "JobObjectExtendedLimitInformation")
    print "Job info: %s" % jobinfo
    limitflags = jobinfo["BasicLimitInformation"]["LimitFlags"]
    print "LimitFlags: %s" % limitflags
    process.kill()
Example #12
0
def child():
    print 'Starting child'
    currentProc = GetCurrentProcess()
    injob = IsProcessInJob(currentProc)
    print "Is in a job?: %s" % injob
    can_create = CanCreateJobObject()
    print 'Can create job?: %s' % can_create
    process = Popen('c:\\windows\\notepad.exe')
    assert process._job
    jobinfo = QueryInformationJobObject(process._job, 'JobObjectExtendedLimitInformation')
    print 'Job info: %s' % jobinfo
    limitflags = jobinfo['BasicLimitInformation']['LimitFlags']
    print 'LimitFlags: %s' % limitflags
    process.kill()
Example #13
0
def parent():
    print 'Starting parent'
    currentProc = GetCurrentProcess()
    if IsProcessInJob(currentProc):
        print >> sys.stderr, "You should not be in a job object to test"
        sys.exit(1)
    assert CanCreateJobObject()
    print 'File: %s' % __file__
    command = [sys.executable, __file__, '-child']
    print 'Running command: %s' % command
    process = Popen(command)
    process.kill()
    code = process.returncode
    print 'Child code: %s' % code
    assert code == 127
Example #14
0
def parent():
    print 'Starting parent'
    currentProc = GetCurrentProcess()
    if IsProcessInJob(currentProc):
        print >> sys.stderr, "You should not be in a job object to test"
        sys.exit(1)
    assert CanCreateJobObject()
    print 'File: %s' % __file__
    command = [sys.executable, __file__, '-child']
    print 'Running command: %s' % command
    process = Popen(command)
    process.kill()
    code = process.returncode
    print 'Child code: %s' % code
    assert code == 127
Example #15
0
 def run_pipe(self, cmd):
     if self.debug:
         print('cmd: %s' % cmd)
     try:
         p = Popen((cmd), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         output = p.stdout.readlines()
         err = p.stderr.readlines()
         starttime=time()
         exitCode = p.wait(self.testTimeOut) #abort if it takes longer than 60 seconds
         if exitCode < 0 and self.testTimeOut>-1 and time()-starttime>self.testTimeOut:  # process timed out
             return 'timedOut'
         return (output,err,exitCode)
     except KeyboardInterrupt:
         print '\n\nKeyboardInterrupt detected ... killing process'
         p.kill()
         self.killmyself()
Example #16
0
def test_qijo():
    from killableprocess import Popen

    popen = Popen('c:\\windows\\notepad.exe')

    try:
        result = QueryInformationJobObject(0, 8)
        raise AssertionError('throw should occur')
    except WindowsError, e:
        pass
    def _run(self):
        '''
        Runs the command in self.cmdlist from self.workingdir with a timer
        bounded by self.runtimeout
        '''
        logger.debug('Running: %s %s', self.cmdlist, self.workingdir)
        process_info = {}
        id = None
        done = False
        started = False
        wmiInterface = None
        # set timeout(s)
        # run program
        if self.hideoutput:
            p = Popen(self.cmdlist, stdout=open(
                os.devnull), stderr=open(os.devnull))
        else:
            p = Popen(self.cmdlist)

        if self.watchcpu == True:
            # Initialize things used for CPU monitoring
            logger.debug('Initializing WMI...')
            wmiInterface = wmi.WMI()
            id = p.pid

        logger.debug('...Timer: %f', self.runtimeout)
        t = Timer(self.runtimeout, kill, args=[p])
        self.t = t
        logger.debug('...timer start')
        t.start()
        if self.watchcpu == True:
            # This is a race.  In some cases, a GUI app could be done before we can even measure it
            # TODO: Do something about it
            while p.poll() is None and not done and id:
                for proc in wmiInterface.Win32_PerfRawData_PerfProc_Process(IDProcess=id):
                    n1, d1 = long(proc.PercentProcessorTime), long(
                        proc.Timestamp_Sys100NS)
                    n0, d0 = process_info.get(id, (0, 0))
                    try:
                        percent_processor_time = (
                            float(n1 - n0) / float(d1 - d0)) * 100.0
                    except ZeroDivisionError:
                        percent_processor_time = 0.0
                    process_info[id] = (n1, d1)
                    logger.debug(
                        'Process %s CPU usage: %s', id, percent_processor_time)
                    if percent_processor_time < 0.0000000001:
                        if started:
                            logger.debug(
                                'killing %s due to CPU inactivity', id)
                            done = True
                            kill(p)
                    else:
                        # Detected CPU usage. Now look for it to drop near zero
                        started = True

                if not done:
                    time.sleep(0.2)
        else:
            p.wait()
        # probably racy
        logger.debug('...timer stop')
        t.cancel()

        self.returncode = ctypes.c_uint(p.returncode).value
        logger.debug(
            '...Returncode: raw=%s cast=%s', p.returncode, self.returncode)
        logger.debug('...Exceptions: %s', self.exceptions)
        if self.returncode in self.exceptions:
            self.saw_crash = True
        logger.debug('...Saw_crash: %s', self.saw_crash)
Example #18
0
 def test_timeExceeded(self):
     start = time()
     p = Popen(['python', 'dummy.py', '-t', '1', '-l', '2'], stdout=PIPE, stderr=PIPE)
     retcode = p.wait(1)
     self.assertEquals(retcode, -9)
Example #19
0
    def _run(self):
        '''
        Runs the command in self.cmdlist from self.workingdir with a timer
        bounded by self.runtimeout
        '''
        logger.debug('Running: %s %s', self.cmdlist, self.workingdir)
        process_info = {}
        id = None
        done = False
        started = False
        wmiInterface = None
        # set timeout(s)
        # run program
        if self.hideoutput:
            p = Popen(self.cmdlist,
                      stdout=open(os.devnull),
                      stderr=open(os.devnull))
        else:
            p = Popen(self.cmdlist)

        if self.watchcpu == True:
            # Initialize things used for CPU monitoring
            logger.debug('Initializing WMI...')
            wmiInterface = wmi.WMI()
            id = p.pid

        logger.debug('...Timer: %f', self.runtimeout)
        t = Timer(self.runtimeout, kill, args=[p])
        self.t = t
        logger.debug('...timer start')
        t.start()
        if self.watchcpu == True:
            # This is a race.  In some cases, a GUI app could be done before we can even measure it
            # TODO: Do something about it
            while p.poll() is None and not done and id:
                for proc in wmiInterface.Win32_PerfRawData_PerfProc_Process(
                        IDProcess=id):
                    n1, d1 = long(proc.PercentProcessorTime), long(
                        proc.Timestamp_Sys100NS)
                    n0, d0 = process_info.get(id, (0, 0))
                    try:
                        percent_processor_time = (float(n1 - n0) /
                                                  float(d1 - d0)) * 100.0
                    except ZeroDivisionError:
                        percent_processor_time = 0.0
                    process_info[id] = (n1, d1)
                    logger.debug('Process %s CPU usage: %s', id,
                                 percent_processor_time)
                    if percent_processor_time < 0.0000000001:
                        if started:
                            logger.debug('killing %s due to CPU inactivity',
                                         id)
                            done = True
                            kill(p)
                    else:
                        # Detected CPU usage. Now look for it to drop near zero
                        started = True

                if not done:
                    time.sleep(0.2)
        else:
            p.wait()
        # probably racy
        logger.debug('...timer stop')
        t.cancel()

        self.returncode = ctypes.c_uint(p.returncode).value
        logger.debug('...Returncode: raw=%s cast=%s', p.returncode,
                     self.returncode)
        logger.debug('...Exceptions: %s', self.exceptions)
        if self.returncode in self.exceptions:
            self.saw_crash = True
        logger.debug('...Saw_crash: %s', self.saw_crash)