def test_get_codes(self, monkeypatch): stream_no_tty = StringIO() stream_no_tty.fileno = lambda : 0 # fake whatever fileno stream_no_tty.isatty = lambda : False tty = StringIO() tty.fileno = lambda : 0 # fake whatever fileno tty.isatty = lambda : True # class used is based on stream being a tty or not assert 'curses' == Term(stream=tty).code assert 'dumb' == Term(stream=stream_no_tty).code # force use of capabilities assert 'curses' == Term(stream=tty, use_colors=True).code assert 'curses' == Term(stream=stream_no_tty, use_colors=True).code # use of ansi codes forced/no curses available assert 'ansi' == Term(stream=tty, code='ansi').code import curses monkeypatch.setattr(curses, 'tparm', lambda : 5/0) assert 'ansi' == Term(stream=tty).code
def test_null_fileno(): """Make sure ``Terminal`` works when ``fileno`` is ``None``. This simulates piping output to another program. """ out = StringIO() out.fileno = None t = TestTerminal(stream=out) eq_(t.save, u'')
def test_color_format(self): output = StringIO() output.fileno = lambda: -1 stream = ColorStream(output) print_colors('Are you', '{=okay!c:green}', 'Annie?', file=stream) value = output.getvalue() output.close() self.assertEqual('Are you \033[92mokay\033[0m Annie?\n', value)
def test_color_context(self): output = StringIO() output.fileno = lambda: -1 stream = ColorStream(output) with Color('red', stream): print_colors('ERROR!', file=stream) value = output.getvalue() output.close() self.assertEqual('\033[91mERROR!\n\033[0m', value)
class Process(object): """ A slight refactoring of virtualenv.call_subprocess """ def __init__(self, cmd, logger, stdout=True, stdin=None, filter_stdout=None, cwd=None, raise_on_returncode=True, extra_env=None, remove_from_env=None, return_output=False): if isinstance(cmd, basestring): cmd = [str(x) for x in cmd.split()] self.cmd = cmd self.logger = logger self.stdout = stdout is True and sys.stdout or stdout if stdout is False: self.stdout = StringIO() self.stdin = stdin is True and sys.stdin or stdin if not stdout: self.stdout = subprocess.PIPE self.filter_stdout = filter_stdout self.cwd=cwd self.raise_on_returncode=raise_on_returncode self.env = self.prep_env(extra_env, remove_from_env) self._proc = None self.command_desc = self.make_command_desc() self.return_output = return_output def prep_env(self, extra_env=None, remove_from_env=None): env = None if extra_env or remove_from_env: env = os.environ.copy() if extra_env: env.update(extra_env) if remove_from_env: for varname in remove_from_env: env.pop(varname, None) return env @property def proc(self): if self._proc is None: try: stdin = isinstance(self.stdin, file) and self.stdin.fileno() or self.stdin stdout = isinstance(self.stdout, file) and self.stdout.fileno() or self.stdout self._proc = subprocess.Popen(self.cmd, stderr=subprocess.STDOUT, stdin=stdin, stdout=stdout, cwd=self.cwd, env=self.env) except Exception, e: self.logger.fatal("Error %s while executing command %s" % (e, self.command_desc)) raise return self._proc
def create(): stream = StringIO() stream.fileno = lambda : 0 # fake whatever fileno term = Term(stream=stream) # replace real codes with <code-name> for name in term.codes.keys(): if sys.version_info >= (3, 0): term.codes[name] = '<{}>'.format(name) else: # pragma: nocover term.codes[name] = '<{}>'.format(name) term.codes['NORMAL'] = '<NORMAL>' term.codes['DEFAULT'] = '<DEFAULT>' term.codes = dict((k, v) for k, v in term.codes.items()) term() # flush initial streeam that contains real code stream.seek(0) stream.truncate(0) return term
def test_lines_cols(self, monkeypatch): # using curses tty = StringIO() tty.fileno = lambda : 0 # fake whatever fileno tty.isatty = lambda : True term = Term(stream=tty) assert isinstance(term.cols(), int) assert isinstance(term.lines(), int) # curses not available gets None import curses monkeypatch.setattr(curses, 'tigetnum', lambda : 5/0) assert None == term.cols() assert None == term.lines() # uses stty term2 = Term(stream=tty, code='ansi') assert isinstance(term2.cols(), int) assert isinstance(term2.lines(), int)
def send_head(self): path = self.translate_path(self.path) f = None if os.path.isdir(path): if not self.path.endswith('/'): self.send_response(301) self.send_header("Location", self.path + "/") self.end_headers() return None for index in ("index-template.html", ): index = os.path.join(path, index) if os.path.exists(index): path = index break else: return self.list_directory(path) ctype = self.guess_type(path) try: if path.endswith("index-template.html"): f = open(path, "r") template = Template(f.read(), variable_start_string="{[", variable_end_string="]}") f.close() f = StringIO(template.render(**template_vars)) else: f = open(path, "rb") except IOError: self.send_error(404, "File not found") return None self.send_response(200) self.send_header("Content-type", ctype) if isinstance(f, StringIO): length = len(f.getvalue()) else: length = os.fstat(f.fileno())[6] self.send_header("Content-Length", length) self.end_headers() return f
def FlagsForFile(filename): mach = mach_module.get_mach() out = StringIO() # Mach calls sys.stdout.fileno(), so we need to fake it when capturing it. # Returning an invalid file descriptor does the trick. out.fileno = lambda: -1 out.encoding = None mach.run(['compileflags', filename], stdout=out, stderr=out) flag_list = shlex.split(out.getvalue()) # This flag is added by Fennec for android build and causes ycmd to fail to parse the file. # Removing this flag is a workaround until ycmd starts to handle this flag properly. # https://github.com/Valloric/YouCompleteMe/issues/1490 final_flags = [x for x in flag_list if not x.startswith('-march=armv')] return { 'flags': final_flags, 'do_cache': True }
def _execute_batch_job(self, jobid, container_name, inputdata): """ Chooses an agent and executes a job on it """ # Compress inputdata before sending it to the agent tmpfile = tempfile.TemporaryFile() tar = tarfile.open(fileobj=tmpfile, mode='w:gz') for key, val in inputdata.iteritems(): info = tarfile.TarInfo(name=key) info.mode = 0o777 if isinstance(val, basestring): fileobj = StringIO(val) info.size = fileobj.len tar.addfile(tarinfo=info, fileobj=fileobj) else: fileobj = val info.size = os.fstat(fileobj.fileno()).st_size fileobj.seek(0) tar.addfile(tarinfo=info, fileobj=fileobj) tar.close() tmpfile.seek(0) agent_id = self._select_agent() if agent_id is None: self._agent_batch_job_ended(jobid, {'retval': -1, 'stderr': 'There are not any agent available for running this job. ' 'Please retry later. If this error persists, please contact the course administrator.'}, None) return try: agent = self._agents[agent_id] async_run = rpyc.async(agent.root.new_batch_job) result = async_run(str(jobid), str(container_name), tmpfile) self._running_on_agent[agent_id].append(jobid) result.add_callback(lambda r: self._execute_batch_job_callback(jobid, r, agent_id)) except: self._agent_shutdown(agent_id) self._execute_batch_job(jobid, container_name, inputdata)
def send_head(self): """Send response code and MIME header. This is common code for GET and HEAD commands. Return value is either a file object (which has to be copied to the outputfile by the caller unless the command was HEAD, and must be closed by the caller under all circumstances), or None, in which case the caller has nothing further to do. """ path = self.translate_path(self.path) f = None if os.path.isdir(path): if not self.path.endswith('/'): # redirect browser - doing basically what apache does self.send_response(301) path_parts = list(self.path.partition('?')) path_parts[0] += '/' self.send_header("Location", ''.join(path_parts)) # begin no-cache patch # For redirects. With redirects, caching is even worse and can # break more. Especially with 301 Moved Permanently redirects, # like this one. self.send_header("Cache-Control", "no-cache, no-store, " "must-revalidate") self.send_header("Pragma", "no-cache") self.send_header("Expires", "0") # end no-cache patch self.end_headers() return None for index in "index.html", "index.htm": index = os.path.join(path, index) if os.path.exists(index): path = index break else: return self.list_directory(path) ctype = self.guess_type(path) try: # Always read in binary mode. Opening files in text mode may cause # newline translations, making the actual size of the content # transmitted *less* than the content-length! f = open(path, 'rb') except IOError: self.send_error(404, "File not found") return None filtered_bytes = None if ctype == 'text/html': # Comment out any <base> to allow local resolution of relative URLs. data = f.read().decode('utf8') f.close() data = re.sub(r'<base\s([^>]*)>', '<!--base \g<1>-->', data, flags=re.IGNORECASE) data = data.encode('utf8') f = StringIO() f.write(data) filtered_bytes = len(data) f.seek(0) self.send_response(200) if ctype.startswith('text/') or ctype.endswith('+xml'): self.send_header("Content-Type", "{0}; charset=UTF-8".format(ctype)) else: self.send_header("Content-Type", ctype) if os.path.splitext(path)[1] == '.svgz': # Special handling for svgz to make it work nice with browsers. self.send_header("Content-Encoding", 'gzip') if filtered_bytes is None: fs = os.fstat(f.fileno()) self.send_header('Content-Length', str(fs[6])) else: self.send_header('Content-Length', filtered_bytes) # begin no-cache patch # For standard requests. self.send_header("Cache-Control", "no-cache, no-store, " "must-revalidate") self.send_header("Pragma", "no-cache") self.send_header("Expires", "0") # end no-cache patch self.end_headers() return f
def test_color(self): tty = StringIO() tty.fileno = lambda : 0 # fake whatever fileno tty.isatty = lambda : True term = Term(stream=tty) term.demo()
def child(): # This simulates piping output to another program. out = StringIO() out.fileno = None t = TestTerminal(stream=out) assert (t.save == u'')
def mock_open(*args): open_calls.append(args) sio = StringIO() sio.fileno = lambda: None return sio