Esempio n. 1
0
def configure(logger_name=None, log_level=None):
    if not log_level:
        log_level = logging.INFO

    if not os.isatty(1) and not sys.stdout.line_buffering:
        # Line buffering makes logs easier to handle.
        sys.stdout.flush()
        try:
            sys.stdout = io.TextIOWrapper(
                sys.stdout.detach(), encoding='UTF-8', line_buffering=True)
        except io.UnsupportedOperation:
            # debuilding fails, we will just let it pass as if it were a
            # isatty call
            pass

    stdout_handler = logging.StreamHandler(stream=sys.stdout)
    stdout_handler.addFilter(_StdoutFilter())
    stderr_handler = logging.StreamHandler(stream=sys.stderr)
    stderr_handler.addFilter(_StderrFilter())
    handlers = [stdout_handler, stderr_handler]

    fmt = '{message}'
    if os.isatty(1):
        fmt = _COLOR_BOLD + fmt + _COLOR_END
    formatter = logging.Formatter(fmt, style='{')
    logger = logging.getLogger(logger_name)
    for handler in handlers:
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    logger.setLevel(log_level)

    # INFO from the requests lib is too noisy
    logging.getLogger("requests").setLevel(logging.WARNING)
Esempio n. 2
0
    def run(self, password, command):
        with closing(self.sock):
            self.channel.send_message(password)
            self.channel.send_message(b'\0'.join(command))
            self.channel.send_message(os.fsencode(os.getcwd()))
            self.channel.send_message(self.get_winsize())
            self.channel.send_message(struct.pack('bbb', os.isatty(0), os.isatty(1), os.isatty(2)))
            self.channel.send_message(b'\0'.join(b'%s=%s' % t for t in os.environb.items()))

            def handle_sigwinch(n, f):
                # TODO: fix race condition with normal send
                self.channel.send_command(CMD_WINSZ, self.get_winsize())

            signal.signal(signal.SIGWINCH, handle_sigwinch)

            with self.raw_term_mode():
                fdset = [0, self.sock.fileno()]
                while True:
                    for fd in select.select(fdset, (), ())[0]:
                        if fd == 0:
                            chunk = os.read(0, 8192)
                            if chunk:
                                self.channel.send_command(CMD_STDIN, chunk)
                            else:
                                # stdin is a pipe and is closed
                                fdset.remove(0)
                        else:
                            self.recv_command()

            self.sock.shutdown(socket.SHUT_WR)
Esempio n. 3
0
def RunPager(globalConfig):
  global active

  if not os.isatty(0) or not os.isatty(1):
    return
  pager = _SelectPager(globalConfig)
  if pager == '' or pager == 'cat':
    return

  # This process turns into the pager; a child it forks will
  # do the real processing and output back to the pager. This
  # is necessary to keep the pager in control of the tty.
  #
  try:
    r, w = os.pipe()
    pid = os.fork()
    if not pid:
      os.dup2(w, 1)
      os.dup2(w, 2)
      os.close(r)
      os.close(w)
      active = True
      return

    os.dup2(r, 0)
    os.close(r)
    os.close(w)

    _BecomePager(pager)
  except Exception:
    print >>sys.stderr, "fatal: cannot start pager '%s'" % pager
    os.exit(255)
Esempio n. 4
0
    def format(self, frame):
        self.frame = frame
        file_str = io.StringIO()

        if not self._prev_frame:
            # create some empty space to draw on
            file_str.write(u'\n' * (frame.height // 2))

            # find out where we shold put the top left pixel if we are
            # in a terminal. Just use (0, 0) if we are piping to or
            # from files
            if os.isatty(sys.stdin.fileno()) and os.isatty(sys.stdout.fileno()):
                x, y = self.probe_cursor_pos()
                _, term_height = self.dimensions()
                if y + (frame.height // 2) > term_height:
                    adjust = (y + (frame.height // 2)) - term_height
                    self._origin = x, y - adjust
                else:
                    self._origin = x, y


        # draw the image
        for y in range(0, frame.height, 2):
            for x in range(0, frame.width):
                if self.need_repaint(x, y):
                    file_str.write(self.move_cursor(x, y // 2))
                    file_str.write(self.color_string(frame[x][y], frame[x][y + 1]))

        file_str.write(self.move_cursor(frame.width, frame.height // 2))
        file_str.write(chr(27) + u"[0m")

        self._prev_frame = frame # keep the current frame in case we need it as background
        return file_str.getvalue()
Esempio n. 5
0
    def __init__(self):
        """
        Constructor
        """
        
        self.univ = universal.Universal()  #reference to Universal for optimized access
        self.env_variables = {}  #env variables received from the server to execute commands
        ThreadEx.__init__(self)  #inititalize thread base class
        
        #inititalize process base class; refer execute.sh to read more about arguments to be passed in 
        exec_args = ['bash', '%s/src/execute.sh' % self.univ.exe_path, os.path.realpath(sys.modules['__main__'].__file__)]
        os.isatty(sys.stdin.fileno()) or exec_args.append('1')
        WorkerProcess.__init__(self, *exec_args)
        self.line_ending = '\r';
        
        self.daemon = True  #run this thread as daemon as it should not block agent from shutting down
        self.univ.event_dispatcher.bind('terminate', self.stop)  #bind to terminate event so that we can terminate bash process
        
        #use the job timeout defined in the config if we have one
        try:
            self.timeout = self.univ.config.sealion.commandTimeout
        except:
            self.timeout = 30

        self.timeout = int(self.timeout * 1000)  #convert to millisec
Esempio n. 6
0
    def _check_stream(self):
        """Determines which output stream (stdout, stderr, or custom) to use"""
        if self.stream:
            try:
                if self.stream == 1 and os.isatty(sys.stdout.fileno()):
                    self._stream_out = sys.stdout.write
                    self._stream_flush = sys.stdout.flush
                elif self.stream == 2 and os.isatty(sys.stderr.fileno()):
                    self._stream_out = sys.stderr.write
                    self._stream_flush = sys.stderr.flush

            # a fix for IPython notebook "IOStream has no fileno."
            except UnsupportedOperation:
                if self.stream == 1:
                    self._stream_out = sys.stdout.write
                    self._stream_flush = sys.stdout.flush
                elif self.stream == 2:
                    self._stream_out = sys.stderr.write
                    self._stream_flush = sys.stderr.flush
            else:
                if self.stream is not None and hasattr(self.stream, 'write'):
                    self._stream_out = self.stream.write
                    self._stream_flush = self.stream.flush
        else:
            print('Warning: No valid output stream.')
Esempio n. 7
0
  def InteractiveDecision(self, prompt, default='', validator=None):
    """Get an Interactive decision from the user.

    Args:
      prompt: str, an explanation of the decision
      default: str
      validator: func of str -> str (or None.
                 returns str explaining why the input was invalid, or None
                 if it is valid.

    Returns:
      str
    """
    if not os.isatty(sys.stdin.fileno()):
      return default
    while True:
      if default:
        actual_prompt = '%s (Default: %s): ' % (prompt, default)
      else:
        actual_prompt = '%s: ' % prompt
      if os.isatty(sys.stdin.fileno()):
        decision = raw_input(self._Indent(actual_prompt))
      else:
        decision = ''
      if not decision:
        decision = default
      if validator:
        invalid_reason = validator(decision)
        if invalid_reason:
          self._Print('Invalid input %s (%s)' %
                      (repr(decision), invalid_reason))
          continue
      break
    return decision
Esempio n. 8
0
File: test.py Progetto: jvanz/scylla
def status_to_string(success):
    if success:
        status = colorformat("{green}PASSED{nocolor}") if os.isatty(sys.stdout.fileno()) else "PASSED"
    else:
        status = colorformat("{red}FAILED{nocolor}") if os.isatty(sys.stdout.fileno()) else "FAILED"

    return status
Esempio n. 9
0
def test_os_isatty():
    def call_isatty(fd):
        return os.isatty(fd)
    f = compile(call_isatty, [int])
    assert f(0) == os.isatty(0)
    assert f(1) == os.isatty(1)
    assert f(2) == os.isatty(2)
Esempio n. 10
0
def get_required_args(args, required_args, password=False):
    """
    Input missing values or raise Exception

    Keyword arguments:
       args -- Available arguments
       required_args -- Dictionary of required arguments and input phrase
       password -- True|False Hidden password double-input needed

    Returns:
        args

    """
    try:
        for arg, phrase in required_args.items():
            if not args[arg] and arg != 'password':
                if os.isatty(1):
                    args[arg] = raw_input(colorize(phrase + ': ', 'cyan'))
                else:
                    raise Exception #TODO: fix
        # Password
        if 'password' in required_args and password:
            if not args['password']:
                if os.isatty(1):
                    args['password'] = getpass.getpass(colorize(required_args['password'] + ': ', 'cyan'))
                    pwd2 = getpass.getpass(colorize('Retype ' + required_args['password'][0].lower() + required_args['password'][1:] + ': ', 'cyan'))
                    if args['password'] != pwd2:
                        raise YunoHostError(22, _("Passwords doesn't match"))
                else:
                    raise YunoHostError(22, _("Missing arguments"))
    except KeyboardInterrupt, EOFError:
        raise YunoHostError(125, _("Interrupted"))
Esempio n. 11
0
def install_readline(hook):
    log("Installing ansi_console")
    global _old_raw_input
    if _old_raw_input is not None:
        return # don't run _setup twice

    try:
        f_in = sys.stdin.fileno()
        f_out = sys.stdout.fileno()
    except (AttributeError, ValueError):
        return
    if not os.isatty(f_in) or not os.isatty(f_out):
        return

    if '__pypy__' in sys.builtin_module_names:    # PyPy

        def _old_raw_input(prompt=''):
            # sys.__raw_input__() is only called when stdin and stdout are
            # as expected and are ttys.  If it is the case, then get_reader()
            # should not really fail in _wrapper.raw_input().  If it still
            # does, then we will just cancel the redirection and call again
            # the built-in raw_input().
            try:
                del sys.__raw_input__
            except AttributeError:
                pass
            return raw_input(prompt)
        sys.__raw_input__ = hook

    else:
        # this is not really what readline.c does.  Better than nothing I guess
        import __builtin__
        _old_raw_input = __builtin__.raw_input
        __builtin__.raw_input = hook
Esempio n. 12
0
def test_os_isatty():
    def call_isatty(fd):
        return os.isatty(fd)
    f = compile_function(call_isatty, [int], isolate=False)
    assert f(0) == os.isatty(0)
    assert f(1) == os.isatty(1)
    assert f(2) == os.isatty(2)
Esempio n. 13
0
def get_table(code, **options):
    if 'paginate' in options.keys():
        paginate = options.pop('paginate')
    else:
        paginate = None

    data = None
    page_count = 0
    while True:
        next_options = copy.deepcopy(options)
        next_data = Datatable(code).data(params=next_options)

        if data is None:
            data = next_data
        else:
            data.extend(next_data)

        if page_count >= ApiConfig.page_limit:
            if os.isatty(0):
                warnings.warn(Message.WARN_DATA_LIMIT_EXCEEDED, UserWarning)
            break

        next_cursor_id = next_data.meta['next_cursor_id']
        if next_cursor_id is None:
            break
        elif paginate is not True and next_cursor_id is not None:
            if os.isatty(0):
                warnings.warn(Message.WARN_PAGE_LIMIT_EXCEEDED, UserWarning)
            break
        page_count = page_count + 1
        options['qopts.cursor_id'] = next_cursor_id
    return data.to_pandas()
Esempio n. 14
0
def checkperf(suite, reference):
    from os import isatty

    fmtfail = isatty(1) and '\033[31mFAILURE: %s\033[0m' or 'FAILURE: %s'
    fmtwarn = isatty(1) and '\033[33mWARNING: %s\033[0m' or 'WARNING: %s'

    def fail(msg, *args): print fmtfail % msg % args
    def warn(msg, *args): print fmtwarn % msg % args

    for r in suite.results.values():
        durations = []

        for rs in reference:
            rr = rs.results.get(r.name)
            if rr: durations.append(float(rr.duration.seconds))

        if len(durations) == 0:
            continue

        dmax = max(durations)
        davg = sum(durations)/len(durations)

        if r.duration.seconds > (dmax * 1.01 + 1):
            fail('%s %ds needed, but maximum duration was %ds in %s',
                 r.name, r.duration.seconds, dmax, rs.version)
            continue

        if r.duration.seconds > (davg * 1.05 + 1):
            warn('%s run for %ds, but average duration was %ds in %s',
                 r.name, r.duration.seconds, davg, rs.version)
            continue
Esempio n. 15
0
 def __init__(self, color=None, width=None, block='#', empty=' '):
     """
     color -- color name (BLUE GREEN CYAN RED MAGENTA YELLOW WHITE BLACK)
     width -- bar width (optinal)
     block -- progress display character (default '█')
     empty -- bar display character (default ' ')
     """
     try:        
         if not os.isatty(sys.stdout.fileno()):
             return
     except AttributeError:
         return
     if not os.isatty(sys.stdout.fileno()):
         return
     if color:
         self.color = getattr(terminal, color.upper())
     else:
         self.color = ''
     if width and width < terminal.COLUMNS - self.PADDING:
         self.width = width
     else:
         # Adjust to the width of the terminal
         self.width = terminal.COLUMNS - self.PADDING
     self.block = block
     self.empty = empty
     self.progress = None
     self.lines = 0
Esempio n. 16
0
def configure(logger_name=None):
    if not os.isatty(1) and not sys.stdout.line_buffering:
        # Line buffering makes logs easier to handle.
        sys.stdout.flush()
        sys.stdout = io.TextIOWrapper(
            sys.stdout.detach(), encoding='UTF-8', line_buffering=True)

    stdout_handler = logging.StreamHandler(stream=sys.stdout)
    stdout_handler.addFilter(_StdoutFilter())
    stderr_handler = logging.StreamHandler(stream=sys.stderr)
    stderr_handler.addFilter(_StderrFilter())
    handlers = [stdout_handler, stderr_handler]

    fmt = '{message}'
    if os.isatty(1):
        fmt = _COLOR_BOLD + fmt + _COLOR_END
    formatter = logging.Formatter(fmt, style='{')
    logger = logging.getLogger(logger_name)
    for handler in handlers:
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    logger.setLevel(logging.INFO)

    # INFO from the requests lib is too noisy
    logging.getLogger("requests").setLevel(logging.WARNING)
Esempio n. 17
0
def getPassword_term(prompt):
    """Read a password from the console, then return it.  Use the string
       'message' as a prompt."""
    # getpass.getpass uses stdout by default .... but stdout may have
    # been redirected.  If stdout is not a terminal, write the message
    # to stderr instead.
    if os.isatty(sys.stdout.fileno()):
        f = sys.stdout
        nl = 0
    else:
        f = sys.stderr
        nl = 1
    if os.isatty(sys.stdin.fileno()):
        # If stdin is a tty, then we use the magic from getpass.getpass to
        # disable echoing and read a line.
        f.write(prompt)
        f.flush()
        try:
            p = getpass.getpass("")
        except KeyboardInterrupt:
            if nl: print >>f
            raise UIError("Interrupted")
        if nl: print >>f
    else:
        # If stdin is _not_ a tty, however, then the getpass magic can
        # raise exceptions.
        print >>f, "Reading password from stdin."
        p = sys.stdin.readline()
        if not p: raise UIError("No password received")
        if p[-1] == '\n': p = p[:-1]
    return p
Esempio n. 18
0
    def __init__(self):
        ProgressTracker.__init__(self)

        self.act_started = False
        self.ind_started = False
        self.last_print_time = 0

        try:
            import curses

            if not os.isatty(sys.stdout.fileno()):
                raise ProgressTrackerException()

            curses.setupterm()
            self.cr = curses.tigetstr("cr")
        except KeyboardInterrupt:
            raise
        except:
            if portable.ostype == "windows" and os.isatty(sys.stdout.fileno()):
                self.cr = "\r"
            else:
                raise ProgressTrackerException()
        self.dl_started = False
        self.spinner = 0
        self.spinner_chars = "/-\|"
        self.curstrlen = 0
Esempio n. 19
0
def user_add(args):
    """
    Add user to LDAP

    Keyword argument:
        args -- Dictionnary of values (can be empty)

    Returns:
        Boolean
    """
    required_args = ['username', 'mail', 'firstname', 'lastname']

    # Input missing values
    try:
        for arg in required_args:
            if not args[arg]:
                if os.isatty(1):
                    args[arg] = raw_input(arg.capitalize()+': ')
                else:
                    raise Exception
        # Password
        if not args['password']:
            if os.isatty(1):
                args['password'] = getpass.getpass()
                pwd2 = getpass.getpass('Retype password:'******'password'] != pwd2:
                    raise YunoHostError(22, _("Passwords doesn't match"))
            else:
                raise YunoHostError(22, _("Missing arguments"))
    except KeyboardInterrupt, EOFError:
        raise YunoHostError(125, _("Interrupted, user not created"))
Esempio n. 20
0
def PageOutputBeyondThisPoint():
	"""Use a pager for any output displayed after this function has run.
	This is particularly useful for commands like ghi-ls"""

	if not os.isatty(0) or not os.isatty(1):
		return

	pager = _SelectPager()
	if pager == '' or pager == 'cat':
		return

	# This process turns into the pager; a child it forks will
	# do the real processing and output back to the pager. This
	# is necessary to keep the pager in control of the tty.
	#
	try:
		r, w = os.pipe()
		pid = os.fork()
		if not pid:
			os.dup2(w, 1)
			os.dup2(w, 2)
			os.close(r)
			os.close(w)
			return

		os.dup2(r, 0)
		os.close(r)
		os.close(w)

		_BecomePager(pager)
	except Exception:
		print >> sys.stderr, "fatal: cannot start pager '%s'" % pager
		exit()
Esempio n. 21
0
def interactive_terminal():
    """Return true only if running in an interactive terminal."""
    if (
            os.isatty(sys.stdin.fileno()) or
            not os.isatty(sys.stdout.fileno())):
        return True
    return False
def main():
    cfgname = sys.argv[1]
    if cfgname == None:
    	cfgname = 'config'
    global config
    if os.path.isfile(cfgname):
    	config = importlib.machinery.SourceFileLoader('config', cfgname).load_module()
    else:
    	config = importlib.import_module(cfgname)
    
    print('opening maildir')
    db = MaildirDatabase(config.MAILDIR)

    total = len(db)

    updated_msgs = 0
    checked_msgs = 0
    errors = 0
    
    try:
        print('searching for new messages')

        for progress in db.init():
            if os.isatty(1):
                print('progress: %0.2f%%' % float(progress * 100 / total), end='\r', flush=True)
        
        if config.INDEX_ONLY:
            print('indexing complete')
            return

        print('connecting to gmail')
        gmail = Gmail(config.LOGIN, config.PASSWORD)

        #gmail.debug = 15;

        print('selecting mailbox')
        total = gmail.selectfolder(config.IMAP_FOLDER)
        i = 0

        print('downloading and applying labels for %d messages' % total)
        for msgid, gmailid, gmailthreadid, labels in download_labels(gmail, total):
            checked_msgs += 1
            i += 1
            if i % 10 == 0 and os.isatty(1):
                print('progress: %0.2f%% %d' % (float(i * 100 / total), checked_msgs), end='\r', flush=True)

            updaterc = db.apply_labels(msgid, gmailid, gmailthreadid, labels)
            if updaterc < 0:
                errors += 1
            else:
                updated_msgs += updaterc
    except imaplib.IMAP4.error as err:
        print('\nFailed with imap error:', file=sys.stderr)
        print(err, file=sys.stderr)
    finally:
        print('Updated %d/%d messages, %d errors' % (updated_msgs, checked_msgs, errors))
        # extra whitespace at end to ensure it fully overwrites progress line
        print('saving database ')
        db.close()
Esempio n. 23
0
def main():
    print(os.nice(0))  # get relative process priority
    print(os.nice(1))  # change relative priority
    print(os.times())  # process times: system, user etc...
    print(os.isatty(0))  # is the file descriptor arg a tty?(0 = stdin)
    print(os.isatty(4))  # 4 is just an arbitrary test value
    print(os.getloadavg())  # UNIX only - number of processes in queue
    print(os.cpu_count())  # New in Python 3.4
Esempio n. 24
0
def wrap_pager():
    if os.isatty(0) and os.isatty(1):
        buf = io.StringIO()
        yield buf
        sp = subprocess.Popen(PAGER, stdin=subprocess.PIPE)
        sp.communicate(buf.getvalue().encode('utf8'))
    else:
        yield sys.stdout
Esempio n. 25
0
File: log.py Progetto: aszeszo/test
        def __init__(self, tracker, strm=None):
                logging.StreamHandler.__init__(self, strm)

                if os.isatty(sys.stderr.fileno()) and \
                    os.isatty(sys.stdout.fileno()):
                        self.write_crs = True
                else:
                        self.write_crs = False
                self.tracker = tracker
Esempio n. 26
0
def cbreak():
  if hasattr(stdin, "fileno") and os.isatty(stdin.fileno()):
    old_attrs = termios.tcgetattr(stdin)
    tty.setcbreak(stdin)
    tty.setraw(stdin)
  try:
    yield
  finally:
    if hasattr(stdin, "fileno") and os.isatty(stdin.fileno()):
      termios.tcsetattr(stdin, termios.TCSADRAIN, old_attrs)
Esempio n. 27
0
 def __init__(self, display_keys=True, display_header=True, return_only=False):
     self.display_keys = display_keys
     self.display_header = display_header
     self.return_only = return_only
     self.interactive = False
     self.print_lines = 0
     self.termrows = 0
     # XXX if stdin is not a tty, it seems that the command fails.
     if os.isatty(sys.stdout.fileno()) and os.isatty(sys.stdin.fileno()):
         self.termrows = int(os.popen('stty size', 'r').read().split()[0])
Esempio n. 28
0
 def undirect(self):
     """
     Reset stdout (and stdin if needed) back to the terminal.
     """
     rval = sys.stdout.getvalue()
     sys.stdout.close()
     sys.stdout = self.stdout
     if not os.isatty(sys.stdin.fileno()) \
            and os.isatty(self.stdin.fileno()):
         sys.stdin = self.stdin
     return rval
Esempio n. 29
0
def RunPager(globalConfig):
  if not os.isatty(0) or not os.isatty(1):
    return
  pager = _SelectPager(globalConfig)
  if pager == '' or pager == 'cat':
    return

  if platform_utils.isWindows():
    _PipePager(pager);
  else:
    _ForkPager(pager)
Esempio n. 30
0
 def _check_stream(self):
     if self.stream == 1 and os.isatty(sys.stdout.fileno()):
         self._stream_out = sys.stdout.write
         self._stream_flush = sys.stdout.flush
     elif self.stream == 2 and os.isatty(sys.stderr.fileno()):
         self._stream_out = sys.stderr.write
         self._stream_flush = sys.stderr.flush
     else:
         print('Warning: No valid output stream.')
         self._stream_out = self._no_stream
         self._stream_flush = self._no_stream       
Esempio n. 31
0
    def play(self, stream=1):
        # Bug:
        # 光标定位转义编码不兼容 Windows
        if not self.charVideo:
            return
        if stream == 1 and os.isatty(sys.stdout.fileno()):
            self.streamOut = sys.stdout.write
            self.streamFlush = sys.stdout.flush
        elif stream == 2 and os.isatty(sys.stderr.fileno()):
            self.streamOut = sys.stderr.write
            self.streamFlush = sys.stderr.flush
        elif hasattr(stream, 'write'):
            self.streamOut = stream.write
            self.streamFlush = stream.flush
        breakflag = False

        def getChar():
            nonlocal breakflag
            try:
                # 若系统为 windows 则直接调用 msvcrt.getch()
                import msvcrt
            except ImportError:
                import termios
                import tty
                # 获得标准输入的文件描述符
                fd = sys.stdin.fileno()
                # 保存标准输入的属性
                old_settings = termios.tcgetattr(fd)
                try:
                    # 设置标准输入为原始模式
                    tty.setraw(sys.stdin.fileno())
                    # 读取一个字符
                    ch = sys.stdin.read(1)
                finally:
                    # 恢复标准输入为原来的属性
                    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
                if ch:
                    breakflag = True
            else:
                if msvcrt.getch():
                    breakflag = True

        # 创建线程
        getchar = threading.Thread(target=getChar)
        # 设置为守护线程
        getchar.daemon = True
        # 启动守护线程
        getchar.start()
        # 输出的字符画行数
        rows = len(self.charVideo[0]) // os.get_terminal_size()[0]
        for frame in self.charVideo:
            # 接收到输入则退出循环
            if breakflag:
                break
            self.streamOut(frame)
            self.streamFlush()
            time.sleep(self.timeInterval)
            # 共 rows 行,光标上移 rows-1 行回到开始处
            self.streamOut('\033[{}A\r'.format(rows - 1))
        # 光标下移 rows-1 行到最后一行,清空最后一行
        self.streamOut('\033[{}B\033[K'.format(rows - 1))
        # 清空最后一帧的所有行(从倒数第二行起)
        for i in range(rows - 1):
            # 光标上移一行
            self.streamOut('\033[1A')
            # 清空光标所在行
            self.streamOut('\r\033[K')
        if breakflag:
            self.streamOut('User interrupt!\n')
        else:
            self.streamOut('Finished!\n')
Esempio n. 32
0
def set_up_logging(parser, experiment_name, output_folder, quiet, args_dict,
                   debug, **kwargs):
    """
    Set up a logger for the experiment

    Parameters
    ----------
    parser : parser
        The argument parser
    experiment_name : string
        Name of the experiment. If not specify, accepted from command line.
    output_folder : string
        Path to where all experiment logs are stored.
    quiet : bool
        Specify whether to print log to console or only to text file
    debug : bool
        Specify the logging level
    args_dict : dict
        Contains the entire argument dictionary specified via command line.

    Returns
    -------
    log_folder : String
        The final logging folder tree
    writer : tensorboardX.writer.SummaryWriter
        The tensorboard writer object. Used to log values on file for the tensorboard visualization.
    """
    LOG_FILE = 'logs.txt'

    # Experiment name override
    if experiment_name is None:
        experiment_name = input("Experiment name:")

    # Recover dataset name
    dataset = os.path.basename(os.path.normpath(kwargs['dataset_folder']))
    """
    We extract the TRAIN parameters names (such as model_name, lr, ... ) from the parser directly. 
    This is a somewhat risky operation because we access _private_variables of parsers classes.
    However, within our context this can be regarded as safe. 
    Shall we be wrong, a quick fix is writing a list of possible parameters such as:
    
        train_param_list = ['model_name','lr', ...] 
    
    and manually maintain it (boring!).
    
    Resources:
    https://stackoverflow.com/questions/31519997/is-it-possible-to-only-parse-one-argument-groups-parameters-with-argparse
    """

    # Fetch all non-default parameters
    non_default_parameters = []

    for group in parser._action_groups[2:]:
        if group.title not in ['GENERAL', 'DATA']:
            for action in group._group_actions:
                if (kwargs[action.dest]
                        is not None) and (kwargs[action.dest] != action.default
                                          ) and action.dest != 'load_model':
                    non_default_parameters.append(
                        str(action.dest) + "=" + str(kwargs[action.dest]))

    # Build up final logging folder tree with the non-default training parameters
    log_folder = os.path.join(*[
        output_folder, experiment_name, dataset, *non_default_parameters,
        '{}'.format(time.strftime('%d-%m-%y-%Hh-%Mm-%Ss'))
    ])
    if not os.path.exists(log_folder):
        os.makedirs(log_folder)

    # Setup logging
    root = logging.getLogger()
    log_level = logging.DEBUG if debug else logging.INFO
    root.setLevel(log_level)
    format = "[%(asctime)s] [%(levelname)8s] --- %(message)s (%(filename)s:%(lineno)s)"
    date_format = '%Y-%m-%d %H:%M:%S'

    if os.isatty(2):
        cformat = '%(log_color)s' + format
        formatter = colorlog.ColoredFormatter(cformat,
                                              date_format,
                                              log_colors={
                                                  'DEBUG': 'cyan',
                                                  'INFO': 'white',
                                                  'WARNING': 'yellow',
                                                  'ERROR': 'red',
                                                  'CRITICAL': 'red,bg_white',
                                              })
    else:
        formatter = logging.Formatter(format, date_format)

    if not quiet:
        ch = logging.StreamHandler()
        ch.setFormatter(formatter)
        root.addHandler(ch)

    fh = logging.FileHandler(os.path.join(log_folder, LOG_FILE))
    fh.setFormatter(logging.Formatter(format, date_format))
    root.addHandler(fh)

    logging.info('Setup logging. Log file: {}'.format(
        os.path.join(log_folder, LOG_FILE)))

    # Save args to logs_folder
    logging.info('Arguments saved to: {}'.format(
        os.path.join(log_folder, 'args.txt')))
    with open(os.path.join(log_folder, 'args.txt'), 'w') as f:
        f.write(json.dumps(args_dict))

    # Save all environment packages to logs_folder
    environment_yml = os.path.join(log_folder, 'environment.yml')
    subprocess.call('conda env export > {}'.format(environment_yml),
                    shell=True)

    # Define Tensorboard SummaryWriter
    logging.info('Initialize Tensorboard SummaryWriter')

    # Add all parameters to Tensorboard
    writer = SummaryWriter(log_dir=log_folder)
    writer.add_text('Args', json.dumps(args_dict))

    return log_folder, writer
Esempio n. 33
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of ObjectPath released under AGPL v3 license.
# Copyright (C) 2008-2010 Adrian Kalbarczyk

import datetime
import sys, os
try:
    import pytz
    TIMEZONE_CACHE = {"UTC": pytz.utc}
except ImportError:
    if os.isatty(sys.stdin.fileno()) and sys.stdout.isatty():
        print(
            "WARNING! pytz is not installed. Localized times are not supported."
        )

HOURS_IN_DAY = 24

now = datetime.datetime.now


def round9_10(n):
    i = int(n)
    if n - i > 0.9:
        return i + 1
    return i


# TODO its 31 minuta, should be 31 minut - probably done
Esempio n. 34
0
def is_dumb_terminal():
    """Return True if on a dumb terminal."""
    is_stdout_tty = os.isatty(sys.stdout.fileno())
    is_term_dumb = os.environ.get('TERM', '') == 'dumb'
    return not is_stdout_tty or is_term_dumb
Esempio n. 35
0
# Marker for program piped input
STDIN_PIPE_DASH = '-'

# URL used in dummy runs
DUMMY_URL = "http://foo/bar?id=1"

# Timeout used during initial websocket (pull) testing
WEBSOCKET_INITIAL_TIMEOUT = 3

# The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'mac', 'os2', 'ce', 'java', 'riscos'
PLATFORM = os.name
PYVERSION = sys.version.split()[0]
IS_WIN = PLATFORM == "nt"

# Check if running in terminal
IS_TTY = hasattr(sys.stdout, "fileno") and os.isatty(sys.stdout.fileno())

# DBMS system databases
MSSQL_SYSTEM_DBS = ("Northwind", "master", "model", "msdb", "pubs", "tempdb",
                    "Resource", "ReportServer", "ReportServerTempDB")
MYSQL_SYSTEM_DBS = ("information_schema", "mysql", "performance_schema", "sys")
PGSQL_SYSTEM_DBS = ("information_schema", "pg_catalog", "pg_toast", "pgagent")
ORACLE_SYSTEM_DBS = ("ADAMS", "ANONYMOUS", "APEX_030200", "APEX_PUBLIC_USER",
                     "APPQOSSYS", "AURORA$ORB$UNAUTHENTICATED", "AWR_STAGE",
                     "BI", "BLAKE", "CLARK", "CSMIG", "CTXSYS", "DBSNMP",
                     "DEMO", "DIP", "DMSYS", "DSSYS", "EXFSYS", "FLOWS_%",
                     "FLOWS_FILES", "HR", "IX", "JONES", "LBACSYS", "MDDATA",
                     "MDSYS", "MGMT_VIEW", "OC", "OE", "OLAPSYS", "ORACLE_OCM",
                     "ORDDATA", "ORDPLUGINS", "ORDSYS", "OUTLN", "OWBSYS",
                     "PAPER", "PERFSTAT", "PM", "SCOTT", "SH",
                     "SI_INFORMTN_SCHEMA", "SPATIAL_CSW_ADMIN_USR",
Esempio n. 36
0
File: boon.py Progetto: xi/boon
def isatty():
    return os.isatty(sys.stdout.fileno())
Esempio n. 37
0
def main():
    # Setup logging
    setupLog()

    makeProg = os.environ.get("MAKE", "make")

    # Put in an environment variable the command line so we can find it
    os.environ["ALCHEMAKE_CMDLINE"] = " ".join([makeProg] + sys.argv[1:])

    # If not on a terminal, do NOT use job control, simply execute make...
    if not os.isatty(0):
        logging.warning("Not using job control")
        process = subprocess.Popen([makeProg] + sys.argv[1:],
                                   shell=False,
                                   close_fds=False)
        process.wait()
        sys.exit(process.returncode)
        return

    # Create our job control object
    jobCtrl = JobCtrl()

    # Try to exit silently in case of interrupts...
    signal.signal(signal.SIGINT, jobCtrl.signalHandler)
    signal.signal(signal.SIGTERM, jobCtrl.signalHandler)

    # Job control
    signal.signal(signal.SIGTTOU, signal.SIG_IGN)
    signal.signal(signal.SIGTTIN, signal.SIG_IGN)
    signal.signal(signal.SIGTSTP, signal.SIG_IGN)
    signal.signal(signal.SIGCHLD, jobCtrl.signalHandler)
    signal.signal(signal.SIGCONT, jobCtrl.signalHandler)

    # Only redirect stderr (redirecting stdout causes issues if a child process
    # wants to use the terminal, like ncurses)
    # Force error messages of sub processes to English, keeping encoding to UTF-8
    # LANG=C.UTF8 does NOT work as expected (messages are still in original locale)
    env = os.environ
    env["LC_MESSAGES"] = "C"
    env["LC_TIME"] = "C"
    cmdline = makeProg
    for arg in sys.argv[1:]:
        cmdline += " " + arg
    jobCtrl.job.launch(cmdline, stderr=subprocess.PIPE, env=env)

    # Read from stderr redirected in a pipe
    # only catch top level makefile errors (sub-make files error will eventually
    # generate a top level error)
    reError1 = re.compile(r"make: \*\*\* No rule to make target .*")
    reError2 = re.compile(r"make: \*\*\* \[[^\[\]]*\] Error [0-9]+")
    errorDetected = False
    while True:
        try:
            # Empty line means EOF detected, so exit loop
            line = jobCtrl.job.process.stderr.readline()
            if len(line) == 0:
                logging.debug("EOF detected")
                break
            if not errorDetected:
                sys.stderr.write(line)
            # Check for make error
            if reError1.match(line) or reError2.match(line):
                logging.debug("error detected")
                errorDetected = True
                jobCtrl.job.kill()
        except IOError:
            # Will occur when interrupted during read, an EOF will be read next
            pass

    # Only print message once at the end
    if errorDetected:
        sys.stderr.write("\n\033[31mMAKE ERROR DETECTED\n\033[00m")

    # Wait for sub-process to terminate
    logging.debug("wait sub-process")
    jobCtrl.job.process.wait()

    # Restore stuff
    if jobCtrl.tcpgrp != os.tcgetpgrp(0):
        try:
            logging.debug("tcsetpgrp(0, %d)", jobCtrl.tcpgrp)
            os.tcsetpgrp(0, jobCtrl.tcpgrp)
        except OSError:
            # Seems to occurs when launched in background and initial foreground
            # process is not there anymore, just ignore
            pass

    # Exit with same result as sub-process
    logging.debug("exit(%d)", jobCtrl.job.process.returncode)
    sys.exit(jobCtrl.job.process.returncode)
Esempio n. 38
0
def is_fd_alive(fd):
    if os.name == 'nt':
        return not os.isatty(fd.fileno())
    import select
    return bool(select.select([fd], [], [], 0)[0])
Esempio n. 39
0
 def log(self, line):
     if os.isatty(sys.stdout.fileno()):
         print line
     syslog.syslog(syslog.LOG_INFO, line)
Esempio n. 40
0
        s.ownopts['cli.history'] = False
    if args.l:
        s.ownopts['cli.history_file'] = args.l
    else:
        try:
            s.ownopts['cli.history_file'] = expanduser(
                "~") + "/.spiderfoot_history"
        except BaseException as e:
            s.dprint(f"Failed to set 'cli.history_file': {e}")
            s.dprint("Using '.spiderfoot_history' in working directory")
            s.ownopts['cli.history_file'] = ".spiderfoot_history"
    if args.o:
        s.ownopts['cli.spool'] = True
        s.ownopts['cli.spool_file'] = args.o

    if args.e or not os.isatty(0):
        try:
            s.use_rawinput = False
            s.prompt = ""
            s.cmdloop()
        finally:
            cin.close()
        sys.exit(0)

    if not args.q:
        s = SpiderFootCli()
        s.dprint("\n\
  _________      .__    .___          ___________            __  \n\
 /   _____/_____ |__| __| _/__________\\_   _____/___   _____/  |_ \n\
 \_____  \\\\____ \|  |/ __ |/ __ \\_  __ \\    __)/  _ \ /  _ \\   __\\\n\
 /        \\  |_> >  / /_/ \\  ___/|  | \\/     \\(  <_> |  <_> )  |  \n\
Esempio n. 41
0
    def _shPty(self, stdin, *args, **kwargs):
        """
        Run a command in a pseudo-tty, with input from C{stdin}.
        """
        self._debug('In _shPty, stdin is %r' % (stdin,))

        # Stdin cannot be manipulated if it's not a terminal or we're
        # running under pytest.
        stdinIsTty = os.isatty(0) and 'pytest' not in sys.modules

        # The following is (slightly) adapted from
        # https://stackoverflow.com/questions/41542960/\
        # run-interactive-bash-with-popen-and-a-dedicated-tty-python
        # Answer by https://stackoverflow.com/users/3555925/liao

        # save original tty setting then set it to raw mode
        if stdinIsTty:
            oldTty = termios.tcgetattr(sys.stdin)

        try:
            if stdinIsTty:
                tty.setraw(sys.stdin.fileno())

            # Open a pseudo-terminal to interact with the subprocess.
            master_fd, slave_fd = pty.openpty()

            # Pass os.setsid to have the process run in a new process group.
            #
            # Note that we should be more careful with kwargs here. If the
            # user has called 'sh' interactively and passed a value for
            # stdin, stdout, stderr, or universal_newlines, the following
            # will cause Python to complain about multiple values for a
            # keyword argument. We should check & warn the user etc.
            process = Popen(
                *args, preexec_fn=os.setsid,
                stdin=(slave_fd if stdin is None else PIPE), stdout=slave_fd,
                stderr=slave_fd, universal_newlines=True, **kwargs)

            # Write the command's stdin to it, if any.
            if stdin is not None:
                # print('WROTE %r' % (stdin,), file=self.errfp)
                os.write(process.stdin.fileno(), stdin.encode())
                process.stdin.close()

            if stdinIsTty:
                readFds = [sys.stdin, master_fd]
            else:
                readFds = [master_fd]

            result = b''
            while process.poll() is None:
                r, w, e = select.select(readFds, [], [], 0.05)
                if sys.stdin in r:
                    data = os.read(sys.stdin.fileno(), 10240)
                    # print('READ from stdin %r' % (data,), file=self.errfp)
                    if data == b'\x03':
                        process.terminate()
                    else:
                        os.write(master_fd, data)
                elif master_fd in r:
                    data = os.read(master_fd, 10240)
                    # print('READ from master %r' % (data,), file=self.errfp)
                    if data:
                        result += data
                        os.write(_originalStdout.fileno(), data)

        finally:
            if stdinIsTty:
                # Restore tty settings.
                termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldTty)

        # TODO: Check all this still needed.
        # print('Shell result %r' % (result,), file=self.errfp)
        return ANSI_esc.sub('', result.decode('utf-8')).replace('\r\n', '\n')
Esempio n. 42
0
#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys, os, platform, io
from contextlib import contextmanager
"""This is (mostly) a standalone module used to write logging
information about Meson runs. Some output goes to screen,
some to logging dir and some goes to both."""

if platform.system().lower() == 'windows':
    colorize_console = os.isatty(
        sys.stdout.fileno()) and os.environ.get('ANSICON')
else:
    colorize_console = os.isatty(
        sys.stdout.fileno()) and os.environ.get('TERM') != 'dumb'
log_dir = None
log_file = None
log_fname = 'meson-log.txt'
log_depth = 0


def initialize(logdir):
    global log_dir, log_file
    log_dir = logdir
    log_file = open(os.path.join(logdir, log_fname), 'w', encoding='utf8')

Esempio n. 43
0
def pcs2pcscmd_flatiron(cmd_ctxt,
                        ccs=PATH_CLUSTERCONF,
                        cib=PATH_CIB,
                        output="-",
                        force=False,
                        noauth=False,
                        silent=False,
                        tmp_cib="{cib2pcscmd.defs[pcscmd_tmpcib]}",
                        dry_run=False,
                        enable=False,
                        start_wait="{ccspcmk2pcscmd.defs[pcscmd_start_wait]}",
                        noguidance=False,
                        text_width='0',
                        _common=XMLFilter.command_common):
    """(Corosync/CMAN,Pacemaker) cluster cfg. -> reinstating pcs commands

    Options:
        ccs         input Corosync/CMAN (+fence_pcmk) configuration file
        cib         input proper Pacemaker cluster config. file (CIB)
        output      pcs commands to reinstate the cluster per the inputs
        force       may the force be with emitted pcs commands
        noauth      skip authentication step (OK if already set up)
        silent      do not track the progress along the steps execution (echoes)
        tmp_cib     file to accumulate the changes (empty ~ direct push, avoid!)
        dry_run     omit intrusive commands (TMP_CIB reset if empty)
        enable      enable cluster infrastructure services (autostart on reboot)
        start_wait  fixed seconds to give cluster to come up initially
        noguidance  omit extraneous guiding
        text_width  for commands rewrapping (0/-1/neg. ~ auto/disable/hi-limit)
    """
    cmd_ctxt['pcscmd_force'] = force
    cmd_ctxt['pcscmd_noauth'] = noauth
    cmd_ctxt['pcscmd_verbose'] = not (silent)
    cmd_ctxt['pcscmd_tmpcib'] = tmp_cib
    cmd_ctxt['pcscmd_dryrun'] = dry_run
    cmd_ctxt['pcscmd_enable'] = enable
    cmd_ctxt['pcscmd_start_wait'] = start_wait
    cmd_ctxt['pcscmd_noguidance'] = noguidance
    cmd_ctxt['text_width'] = text_width
    # XXX possibility to disable cib-meld-templates

    cmd_ctxt.filter('cmd-wrap')['color'] = output == "-" and isatty(1) and \
                                           cmd_ctxt['color'] is not False \
                                           or cmd_ctxt['color']

    void_proto = protocols.plugins['void'].ensure_proto
    file_proto = protocols.plugins['file'].ensure_proto

    return (
        (
            void_proto(),
            ((file_proto(output), ), ),
            file_proto(ccs),
            # already tracked
            #(
            #        (
            #            file_proto(output),
            #        ),
            #),
            file_proto(cib),
            # already tracked
            #cib2pcscmd_output(
            #        (
            #            file_proto(output),
            #        ),
            #),
        ), )
Esempio n. 44
0
def pyflow_solver_main():
    '''Main loop if module run as script.'''

    color_capable = (sys.platform != 'win32' and os.isatty(1))

    parser = ArgumentParser(
        description='Solve Flow Free puzzles via reduction to SAT')

    parser.add_argument('filenames',
                        metavar='PUZZLE',
                        nargs='+',
                        help='puzzle file to load')

    parser.add_argument('-q',
                        dest='quiet',
                        default=False,
                        action='store_true',
                        help='quiet mode (reduce output)')

    parser.add_argument('-c',
                        dest='display_cycles',
                        default=False,
                        action='store_true',
                        help='display intermediate solutions with cycles')

    parser.add_argument('-C',
                        dest='display_color',
                        default=color_capable,
                        action='store_true',
                        help='always display color')

    options = parser.parse_args()

    max_width = max(len(f) for f in options.filenames)

    puzzle_count = 0

    stats = dict()

    for filename in options.filenames:

        if not options.quiet and puzzle_count:
            print '\n' + ('*' * 70) + '\n'

        # open file
        try:
            with open(filename, 'r') as infile:
                puzzle, colors = parse_puzzle(options, infile, filename)
        except IOError:
            print '{}: error opening file'.format(filename)
            continue

        if colors is None:
            continue

        puzzle_count += 1

        color_var, dir_vars, num_vars, clauses, reduce_time = \
            reduce_to_sat(options, puzzle, colors)

        sol, _, repairs, solve_time = solve_sat(options, puzzle, colors,
                                                color_var, dir_vars, clauses)

        total_time = reduce_time + solve_time

        if isinstance(sol, list):
            result_char = 's'
        elif str(sol) == 'UNSAT':
            result_char = 'u'
        else:
            result_char = 'f'

        cur_stats = dict(repairs=repairs,
                         reduce_time=reduce_time,
                         solve_time=solve_time,
                         total_time=total_time,
                         num_vars=num_vars,
                         num_clauses=len(clauses),
                         count=1)

        if not stats.has_key(result_char):
            stats[result_char] = cur_stats
        else:
            for key in cur_stats.keys():
                stats[result_char][key] += cur_stats[key]

        if not options.quiet:
            """
            print 'finished in total of {:.3f} seconds'.format(
                total_time)
            """
        else:

            print '{:>{}s} {} {:9,d} {:9,d} {:12,.3f} '\
                '{:3d} {:12,.3f} {:12,.3f}'.format(
                    filename, max_width, result_char,
                    num_vars, len(clauses), reduce_time,
                    repairs, solve_time, total_time)
            f11.write('{:>{}s} {} {:9,d} {:9,d} {:12,.3f} '\
                '{:3d} {:12,.3f} {:12,.3f}'.format(
                    filename, max_width, result_char,
                    num_vars, len(clauses), reduce_time,
                    repairs, solve_time, total_time))

    print_summary(options, stats)
Esempio n. 45
0
except LookupError:
    DEFAULT_PAGE_ENCODING = "utf8"

# Marker for program piped input
STDIN_PIPE_DASH = '-'

# URL used in dummy runs
DUMMY_URL = "http://foo/bar?id=1"

# The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'mac', 'os2', 'ce', 'java', 'riscos'
PLATFORM = os.name
PYVERSION = sys.version.split()[0]
IS_WIN = PLATFORM == "nt"

# Check if running in terminal
IS_TTY = os.isatty(sys.stdout.fileno())

# DBMS system databases
MSSQL_SYSTEM_DBS = ("Northwind", "master", "model", "msdb", "pubs", "tempdb")
MYSQL_SYSTEM_DBS = ("information_schema", "mysql", "performance_schema", "sys")
PGSQL_SYSTEM_DBS = ("information_schema", "pg_catalog", "pg_toast", "pgagent")
ORACLE_SYSTEM_DBS = ('ANONYMOUS', 'APEX_030200', 'APEX_PUBLIC_USER',
                     'APPQOSSYS', 'BI', 'CTXSYS', 'DBSNMP', 'DIP', 'EXFSYS',
                     'FLOWS_%', 'FLOWS_FILES', 'HR', 'IX', 'LBACSYS', 'MDDATA',
                     'MDSYS', 'MGMT_VIEW', 'OC', 'OE', 'OLAPSYS', 'ORACLE_OCM',
                     'ORDDATA', 'ORDPLUGINS', 'ORDSYS', 'OUTLN', 'OWBSYS',
                     'PM', 'SCOTT', 'SH', 'SI_INFORMTN_SCHEMA',
                     'SPATIAL_CSW_ADMIN_USR', 'SPATIAL_WFS_ADMIN_USR', 'SYS',
                     'SYSMAN', 'SYSTEM', 'WKPROXY', 'WKSYS', 'WK_TEST',
                     'WMSYS', 'XDB', 'XS$NULL')
SQLITE_SYSTEM_DBS = ("sqlite_master", "sqlite_temp_master")
Esempio n. 46
0
import py
import sys, os, traceback
import re

if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()):

    def log(msg):
        print msg
else:

    def log(msg):
        pass


def convert_rest_html(source, source_path, stylesheet=None, encoding='latin1'):
    """ return html latin1-encoded document for the given input. 
        source  a ReST-string
        sourcepath where to look for includes (basically)
        stylesheet path (to be used if any)
    """
    from docutils.core import publish_string
    kwargs = {
        'stylesheet': stylesheet,
        'stylesheet_path': None,
        'traceback': 1,
        'embed_stylesheet': 0,
        'output_encoding': encoding,
        #'halt' : 0, # 'info',
        'halt_level': 2,
    }
    # docutils uses os.getcwd() :-(
Esempio n. 47
0
 def isatty(self):
     self._check_closed()
     return os.isatty(c_fileno(self._ll_file))
Esempio n. 48
0
signal.alarm(10)

try:
    test_basic_pty()
finally:
    # remove alarm, restore old alarm handler
    signal.alarm(0)
    signal.signal(signal.SIGALRM, old_alarm)

# basic pty passed.

debug("calling pty.fork()")
pid, master_fd = pty.fork()
if pid == pty.CHILD:
    # stdout should be connected to a tty.
    if not os.isatty(1):
        debug("Child's fd 1 is not a tty?!")
        os._exit(3)

    # After pty.fork(), the child should already be a session leader.
    # (on those systems that have that concept.)
    debug("In child, calling os.setsid()")
    try:
        os.setsid()
    except OSError:
        # Good, we already were session leader
        debug("Good: OSError was raised.")
        pass
    except AttributeError:
        # Have pty, but not setsid() ?
        debug("No setsid() available ?")
    def test_fork(self):
        debug("calling pty.fork()")
        pid, master_fd = pty.fork()
        if pid == pty.CHILD:
            # stdout should be connected to a tty.
            if not os.isatty(1):
                debug("Child's fd 1 is not a tty?!")
                os._exit(3)

            # After pty.fork(), the child should already be a session leader.
            # (on those systems that have that concept.)
            debug("In child, calling os.setsid()")
            try:
                os.setsid()
            except OSError:
                # Good, we already were session leader
                debug("Good: OSError was raised.")
                pass
            except AttributeError:
                # Have pty, but not setsid()?
                debug("No setsid() available?")
                pass
            except:
                # We don't want this error to propagate, escaping the call to
                # os._exit() and causing very peculiar behavior in the calling
                # regrtest.py !
                # Note: could add traceback printing here.
                debug("An unexpected error was raised.")
                os._exit(1)
            else:
                debug("os.setsid() succeeded! (bad!)")
                os._exit(2)
            os._exit(4)
        else:
            debug("Waiting for child (%d) to finish." % pid)
            # In verbose mode, we have to consume the debug output from the
            # child or the child will block, causing this test to hang in the
            # parent's waitpid() call.  The child blocks after a
            # platform-dependent amount of data is written to its fd.  On
            # Linux 2.6, it's 4000 bytes and the child won't block, but on OS
            # X even the small writes in the child above will block it.  Also
            # on Linux, the read() will throw an OSError (input/output error)
            # when it tries to read past the end of the buffer but the child's
            # already exited, so catch and discard those exceptions.  It's not
            # worth checking for EIO.
            while True:
                try:
                    data = os.read(master_fd, 80)
                except OSError:
                    break
                if not data:
                    break
                sys.stdout.write(data.replace('\r\n', '\n'))

            ##line = os.read(master_fd, 80)
            ##lines = line.replace('\r\n', '\n').split('\n')
            ##if False and lines != ['In child, calling os.setsid()',
            ##             'Good: OSError was raised.', '']:
            ##    raise TestFailed("Unexpected output from child: %r" % line)

            (pid, status) = os.waitpid(pid, 0)
            res = status >> 8
            debug("Child (%d) exited with status %d (%d)." %
                  (pid, res, status))
            if res == 1:
                self.fail(
                    "Child raised an unexpected exception in os.setsid()")
            elif res == 2:
                self.fail("pty.fork() failed to make child a session leader.")
            elif res == 3:
                self.fail(
                    "Child spawned by pty.fork() did not have a tty as stdout")
            elif res != 4:
                self.fail("pty.fork() failed for unknown reasons.")

            ##debug("Reading from master_fd now that the child has exited")
            ##try:
            ##    s1 = os.read(master_fd, 1024)
            ##except os.error:
            ##    pass
            ##else:
            ##    raise TestFailed("Read from master_fd did not raise exception")

        os.close(master_fd)
Esempio n. 50
0
    def do_script(shellcmd: str,
                  use_exec=False,
                  show_boot_console=False) -> None:
        if args.graphics:
            arg_fail('scripts and --graphics are mutually exclusive')

        nonlocal has_script
        nonlocal need_initramfs
        if has_script:
            arg_fail('conflicting script options')
        has_script = True
        need_initramfs = True  # TODO: Fix this

        # Turn off default I/O
        qemuargs.extend(arch.qemu_nodisplay_args())

        # Send kernel logs to stderr
        qemuargs.extend(['-serial', 'none'])
        qemuargs.extend(['-chardev', 'file,id=console,path=/proc/self/fd/2'])

        # We should be using the new-style -device serialdev,chardev=xyz,
        # but many architecture-specific serial devices don't support that.
        qemuargs.extend(['-serial', 'chardev:console'])

        if show_boot_console:
            serdev = qemu.quote_optarg(arch.serial_dev_name(0))
            kernelargs.extend([
                'console=%s' % serdev,
                'earlyprintk=serial,%s,115200' % serdev
            ])

        # Set up a virtserialport for script I/O
        qemuargs.extend(['-chardev', 'stdio,id=stdio,signal=on,mux=off'])
        qemuargs.extend(['-device', arch.virtio_dev_type('serial')])
        qemuargs.extend(
            ['-device', 'virtserialport,name=virtme.scriptio,chardev=stdio'])

        # Scripts shouldn't reboot
        qemuargs.extend(['-no-reboot'])

        # Ask virtme-init to run the script
        config.virtme_data[b'script'] = """#!/bin/sh

        {prefix}{shellcmd}
        """.format(shellcmd=shellcmd,
                   prefix="exec " if use_exec else "").encode('ascii')

        # Nasty issue: QEMU will set O_NONBLOCK on fds 0, 1, and 2.
        # This isn't inherently bad, but it can cause a problem if
        # another process is reading from 1 or writing to 0, which is
        # exactly what happens if you're using a terminal and you
        # redirect some, but not all, of the tty fds.  Work around it
        # by giving QEMU private copies of the open object if either
        # of them is a terminal.
        for oldfd, mode in ((0, os.O_RDONLY), (1, os.O_WRONLY), (2,
                                                                 os.O_WRONLY)):
            if os.isatty(oldfd):
                try:
                    newfd = os.open('/proc/self/fd/%d' % oldfd, mode)
                except OSError:
                    pass
                else:
                    os.dup2(newfd, oldfd)
                    os.close(newfd)
Esempio n. 51
0
def is_a_tty(stream):
    if hasattr(stream, 'isatty') and callable(stream.isatty):
        return stream.isatty()
    elif has_fileno(stream):
        return os.isatty(stream.fileno())
    return False
Esempio n. 52
0
def stream_output(output, stream):
    def print_output_event(event, stream, is_terminal):
        if 'errorDetail' in event:
            raise StreamOutputError(event['errorDetail']['message'])

        terminator = ''

        if is_terminal and 'stream' not in event:
            # erase current line
            stream.write("%c[2K\r" % 27)
            terminator = "\r"
            pass
        elif 'progressDetail' in event:
            return

        if 'time' in event:
            stream.write("[%s] " % event['time'])

        if 'id' in event:
            stream.write("%s: " % event['id'])

        if 'from' in event:
            stream.write("(from %s) " % event['from'])

        status = event.get('status', '')

        if 'progress' in event:
            stream.write("%s %s%s" % (status, event['progress'], terminator))
        elif 'progressDetail' in event:
            detail = event['progressDetail']
            if 'current' in detail:
                percentage = float(detail['current']) / float(
                    detail['total']) * 100
                stream.write('%s (%.1f%%)%s' %
                             (status, percentage, terminator))
            else:
                stream.write('%s%s' % (status, terminator))
        elif 'stream' in event:
            stream.write("%s%s" % (event['stream'], terminator))
        else:
            stream.write("%s%s\n" % (status, terminator))

    is_terminal = hasattr(stream, 'fileno') and os.isatty(stream.fileno())
    stream = codecs.getwriter('utf-8')(stream)
    all_events = []
    lines = {}
    diff = 0

    for chunk in output:
        event = json.loads(chunk)
        all_events.append(event)

        if 'progress' in event or 'progressDetail' in event:
            image_id = event.get('id')
            if not image_id:
                continue

            if image_id in lines:
                diff = len(lines) - lines[image_id]
            else:
                lines[image_id] = len(lines)
                stream.write("\n")
                diff = 0

            if is_terminal:
                # move cursor up `diff` rows
                stream.write("%c[%dA" % (27, diff))

        print_output_event(event, stream, is_terminal)

        if 'id' in event and is_terminal:
            # move cursor back down
            stream.write("%c[%dB" % (27, diff))

        stream.flush()

    return all_events
Esempio n. 53
0
 def isatty( self ):
     "Is our standard input a tty?"
     return isatty( self.stdin.fileno() )
Esempio n. 54
0
 def is_a_tty(stream):
     return hasattr(stream, 'fileno') and os.isatty(stream.fileno())
Esempio n. 55
0
def get_inputs(records, args):
    recs = []
    for t, n in args:
        if t == "var":
            recs.append([rec[n] for rec in records])
        elif t == 'numeric':
            recs.append(float(n))
        elif t == 'string':
            recs.append(n.lstrip('"').rstrip('"'))

    return recs


if __name__ == '__main__':
    stdin = None
    if not os.isatty(0):
        stdin = sys.stdin

    settings = dict()
    records = si.readResults(settings=settings, has_header=True)
    sessionKey = settings['sessionKey']

    logger.debug("sessionKey = %s" % sessionKey)

    for i in range(1, len(sys.argv)):
        func = py.parse_func(sys.argv[i])
        logger.debug("func = %s" % func)
        recs = get_inputs(records, func.arguments)
        logger.debug("get_inputs = %s" % recs)

        f = py.find_func(func)
Esempio n. 56
0
File: sshw.py Progetto: akx/sshw
    host = user_host.group(2)
    user_host = user_host.group(0)
    with open(hostmap_file) as hostmap_fp:
        for line in hostmap_fp:
            if line.startswith('#') or '=' not in line:
                continue
            m_host, color = [p.strip() for p in line.rsplit('=', 1)]
            if any((m == m_host or re.match(m_host, m))
                   for m in (host, user_host)):
                return parse_color(color)


color_interface_class = ColorInterface
if os.environ.get('TERM_PROGRAM') == 'iTerm.app':
    color_interface_class = ITermColorInterface
is_tty = bool(os.isatty(1))
default_color = parse_color(os.environ.get('SSHW_DEFAULT_BG') or '25,25,25')
hostmap_file = os.path.expanduser(
    os.path.expandvars(os.environ.get('SSHW_HOSTMAP') or '~/.sshw_hosts'))
set_chrome = (str(os.environ.get('SSHW_CHROME', '1')).lower()
              in ('1', 'true', 'yes'))
color_interface = color_interface_class()


def main(argv):
    changed = False
    user_host = None

    # perform rudimentary parsing of the command line to find the user/host parameter
    for arg in argv[1:]:
        if arg == '--':  # after the double dash everything's the command; we've missed our shot
def color(name, text):
    if options.color == "always" or (options.color == "auto" and os.isatty(1)):
        return COLORS[name] + text + COLORS["default"]
    return text
Esempio n. 58
0
File: mlog.py Progetto: phitsc/meson
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys, os, platform
"""This is (mostly) a standalone module used to write logging
information about Meson runs. Some output goes to screen,
some to logging dir and some goes to both."""

colorize_console = platform.system().lower() != 'windows' and os.isatty(
    sys.stdout.fileno())
log_dir = None
log_file = None


def initialize(logdir):
    global log_dir, log_file
    log_dir = logdir
    log_file = open(os.path.join(logdir, 'meson-log.txt'), 'w')


def shutdown():
    global log_file
    if log_file is not None:
        log_file.close()
Esempio n. 59
0
'''

def _aggregate_sentence(args):
    return_str = ''
    for argument in args:
        return_str += argument + ' '
    return return_str


def _get_entity_tuples_from_sentence(sentence):
    from gcn_ner import GCNNer
    ner = GCNNer(ner_filename='./data/ner-gcn-21.tf', trans_prob_file='./data/trans_prob.pickle')
    entity_tuples = ner.get_entity_tuples_from_text(sentence)
    return entity_tuples


if __name__ == '__main__':
    import os
    import sys

    if len(sys.argv) > 1:
        sentence = _aggregate_sentence(sys.argv[1:])
        print(_get_entity_tuples_from_sentence(sentence))
    else:
        if os.isatty(0):
            print(_error_message)
            exit(0)
        sentence = sys.stdin.read().strip()
        if sentence:
            print(_get_entity_tuples_from_sentence(sentence))
Esempio n. 60
0
def main(argv):
    parser = argparse.ArgumentParser(
        description="Display a recording in a tabular format"
    )
    parser.add_argument(
        "path", metavar="recording", nargs=1, help="Path to libinput-record YAML file"
    )
    parser.add_argument(
        "--ignore",
        metavar="ABS_X,ABS_Y,...",
        default="",
        help="A comma-separated list of axis names to ignore",
    )
    parser.add_argument(
        "--only",
        metavar="ABS_X,ABS_Y,...",
        default="",
        help="A comma-separated list of axis names to print, ignoring all others",
    )
    parser.add_argument(
        "--print-state",
        action="store_true",
        default=False,
        help="Always print all axis values, even unchanged ones",
    )

    args = parser.parse_args()
    if args.ignore and args.only:
        print("Only one of --ignore and --only may be given", file=sys.stderr)
        sys.exit(2)

    ignored_axes = [libevdev.evbit(axis) for axis in args.ignore.split(",") if axis]
    only_axes = [libevdev.evbit(axis) for axis in args.only.split(",") if axis]

    isatty = os.isatty(sys.stdout.fileno())

    yml = yaml.safe_load(open(args.path[0]))
    if yml["ndevices"] > 1:
        print(f"WARNING: Using only first {yml['ndevices']} devices in recording")
    device = yml["devices"][0]

    if not device["events"]:
        print(f"No events found in recording")
        sys.exit(1)

    def events():
        """
        Yields the next event in the recording
        """
        for event in device["events"]:
            for evdev in event.get("evdev", []):
                yield libevdev.InputEvent(
                    code=libevdev.evbit(evdev[2], evdev[3]),
                    value=evdev[4],
                    sec=evdev[0],
                    usec=evdev[1],
                )

    def interesting_axes(events):
        """
        Yields the libevdev codes with the axes in this recording
        """
        used_axes = []
        for e in events:
            if e.code not in used_axes and is_tracked_axis(
                e.code, only_axes, ignored_axes
            ):
                yield e.code
                used_axes.append(e.code)

    # Compile all axes that we want to print first
    axes = sorted(
        interesting_axes(events()), key=lambda x: x.type.value * 1000 + x.value
    )
    # Strip the REL_/ABS_ prefix for the headers
    headers = [a.name[4:].rjust(MIN_FIELD_WIDTH) for a in axes]
    # for easier formatting later, we keep the header field width in a dict
    axes = {a: len(h) for a, h in zip(axes, headers)}

    # Time is a special case, always the first entry
    # Format uses ms only, we rarely ever care about µs
    headers = [f"{'Time':<7s}"] + headers + ["Keys"]
    header_line = f"{' | '.join(headers)}"
    print(header_line)
    print("-" * len(header_line))

    current_codes = []
    current_frame = {}  # {evdev-code: value}
    axes_in_use = {}  # to print axes never sending events
    last_fields = []  # to skip duplicate lines
    continuation_count = 0

    keystate = {}
    keystate_changed = False

    for e in events():
        axes_in_use[e.code] = True

        if e.code.type == libevdev.EV_KEY:
            keystate[e.code] = e.value
            keystate_changed = True
        elif is_tracked_axis(e.code, only_axes, ignored_axes):
            current_frame[e.code] = e.value
            current_codes.append(e.code)
        elif e.code == libevdev.EV_SYN.SYN_REPORT:
            fields = []
            for a in axes:
                if args.print_state or a in current_codes:
                    s = format_value(a, current_frame.get(a, 0))
                else:
                    s = ""
                fields.append(s.rjust(max(MIN_FIELD_WIDTH, axes[a])))
            current_codes = []

            if last_fields != fields or keystate_changed:
                last_fields = fields.copy()
                keystate_changed = False

                if continuation_count:
                    if not isatty:
                        print(f" ... +{continuation_count}", end="")
                    print("")
                    continuation_count = 0

                fields.insert(0, f"{e.sec: 3d}.{e.usec//1000:03d}")
                keys_down = [k.name for k, v in keystate.items() if v]
                fields.append(", ".join(keys_down))
                print(" | ".join(fields))
            else:
                continuation_count += 1
                if isatty:
                    print(f"\r ... +{continuation_count}", end="", flush=True)

    # Print out any rel/abs axes that not generate events in
    # this recording
    unused_axes = []
    for evtype, evcodes in device["evdev"]["codes"].items():
        for c in evcodes:
            code = libevdev.evbit(int(evtype), int(c))
            if (
                is_tracked_axis(code, only_axes, ignored_axes)
                and code not in axes_in_use
            ):
                unused_axes.append(code)

    if unused_axes:
        print(
            f"Axes present but without events: {', '.join([a.name for a in unused_axes])}"
        )

    for e in events():
        if libevdev.EV_ABS.ABS_MT_SLOT <= code <= libevdev.EV_ABS.ABS_MAX:
            print(
                "WARNING: This recording contains multitouch data that is not supported by this tool."
            )
            break