Python gevent.subprocess.Popen() Examples
The following are 24
code examples of gevent.subprocess.Popen().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
gevent.subprocess
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: qrencode_wrapper.py From qrenco.de with MIT License | 6 votes |
def qrencode_wrapper(query_string="", request_options=None, html=False): if query_string == "": query_string = ":firstpage" if query_string in INTERNAL_TOPICS: answer = get_internal(query_string) else: answer = query_string + "\n" cmd = ["qrencode", "-t", "UTF8", "-o", "-"] p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT) answer = p.communicate(answer.encode('utf-8'))[0] if html: return html_wrapper(answer), True else: return answer, True
Example #2
Source File: process_mngr.py From janus-cloud with GNU Affero General Public License v3.0 | 6 votes |
def kill_all(name): kill_all_popen = subprocess.Popen(["killall", "-9", name], stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL, close_fds=True, shell=False) try: ret = kill_all_popen.wait(DEFAULT_STOP_WAIT_TIMEOUT) except subprocess.TimeoutExpired: ret = None if ret is None: kill_all_popen.kill() kill_all_popen.wait()
Example #3
Source File: wttr.py From wttr.in with Apache License 2.0 | 6 votes |
def _htmlize(ansi_output, title, parsed_query): """Return HTML representation of `ansi_output`. Use `title` as the title of the page. Format page according to query parameters from `parsed_query`.""" cmd = ["bash", ANSI2HTML, "--palette=solarized"] if not parsed_query.get('inverted_colors'): cmd += ["--bg=dark"] proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate(ansi_output.encode("utf-8")) stdout = stdout.decode("utf-8") stderr = stderr.decode("utf-8") if proc.returncode != 0: error(stdout + stderr) if parsed_query.get('inverted_colors'): stdout = stdout.replace( '<body class="">', '<body class="" style="background:white;color:#777777">') title = "<title>%s</title>" % title opengraph = _get_opengraph(parsed_query) stdout = re.sub("<head>", "<head>" + title + opengraph, stdout) return stdout
Example #4
Source File: subproc.py From recipes-py with Apache License 2.0 | 6 votes |
def _reap_workers(workers, to_close, debug_log): """Collects the IO workers created with _mk_workers. After killing the workers, also closes the subprocess's open PIPE handles. See _safe_close for caveats around closing handles on windows. Args: * workers (List[Greenlet]) - The IO workers to kill. * to_close (List[...]) - (see _mk_workers for definition). The handles to close. These originate from the `Popen.std{out,err}` handles when the recipe engine had to use PIPEs. * debug_log (..stream.StreamEngine.Stream) Should not raise an exception. """ debug_log.write_line('reaping IO workers...') for worker in workers: worker.kill() gevent.wait(workers) debug_log.write_line(' done') for handle_name, handle in to_close: _safe_close(debug_log, handle_name, handle)
Example #5
Source File: moon.py From wttr.in with Apache License 2.0 | 5 votes |
def get_moon(parsed_query): location = parsed_query['orig_location'] html = parsed_query['html_output'] lang = parsed_query['lang'] date = None if '@' in location: date = location[location.index('@')+1:] location = location[:location.index('@')] cmd = [globals.PYPHOON] if lang: cmd += ["-l", lang] if date: try: dateutil.parser.parse(date) except Exception as e: print("ERROR: %s" % e) else: cmd += [date] p = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout = p.communicate()[0] stdout = stdout.decode("utf-8") if parsed_query.get('no-terminal', False): stdout = globals.remove_ansi(stdout) if html: p = Popen( ["bash", globals.ANSI2HTML, "--palette=solarized", "--bg=dark"], stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate(stdout.encode("utf-8")) stdout = stdout.decode("utf-8") stderr = stderr.decode("utf-8") if p.returncode != 0: globals.error(stdout + stderr) return stdout
Example #6
Source File: main.py From taserver with GNU Affero General Public License v3.0 | 5 votes |
def main(): print('Running on Python %s' % sys.version) config = configparser.ConfigParser() with open(INI_PATH) as f: config.read_file(f) ports = Ports(int(config['shared']['port_offset'])) try: udp_proxy_proc1 = sp.Popen('udpproxy.exe %d' % ports['gameserver1']) udp_proxy_proc2 = sp.Popen('udpproxy.exe %d' % ports['gameserver2']) except OSError as e: print('Failed to run udpproxy.exe. Run download_udpproxy.py to download it\n' 'or build it yourself using the Visual Studio solution in the udpproxy\n' 'subdirectory and place it in the taserver directory.\n', file=sys.stderr) return server_queue = gevent.queue.Queue() firewall = Firewall(ports) gevent_spawn('firewall.run', firewall.run, server_queue) def handle_client(socket, address): msg = TcpMessageReader(socket).receive() command = json.loads(msg.decode('utf8')) server_queue.put(command) server = StreamServer(('127.0.0.1', ports['firewall']), handle_client) try: server.serve_forever() except KeyboardInterrupt: firewall.remove_all_rules() udp_proxy_proc1.terminate() udp_proxy_proc2.terminate()
Example #7
Source File: cmd.py From cheat.sh with MIT License | 5 votes |
def _get_page(self, topic, request_options=None): cmd = self._get_command(topic, request_options=request_options) if cmd: try: proc = Popen(cmd, stdout=PIPE, stderr=PIPE) answer = proc.communicate()[0].decode('utf-8', 'ignore') except OSError: return "ERROR of the \"%s\" adapter: please create an issue" % self._adapter_name return answer return ""
Example #8
Source File: process_mngr.py From janus-cloud with GNU Affero General Public License v3.0 | 5 votes |
def _launch_process(self): self._popen = subprocess.Popen(self.args, stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL, close_fds=True, shell=False) log.debug("lanch new process %s, pid:%d" % (self.args, self._popen.pid)) self._has_aged = False self._proc_start_time = time.time() self._on_process_status_change()
Example #9
Source File: chronograph.py From openprocurement.auction with Apache License 2.0 | 5 votes |
def _auction_fucn(self, args): process = None try: process = Popen(args) self.processes[process.pid] = process rc = process.wait() if rc == 0: self.logger.info( "Finished {}".format(args[2]), extra={ 'MESSAGE_ID': 'CHRONOGRAPH_WORKER_COMPLETE_SUCCESSFUL' } ) else: self.logger.error( "Exit with error {}".format(args[2]), extra={ 'MESSAGE_ID': 'CHRONOGRAPH_WORKER_COMPLETE_EXCEPTION' } ) except Exception as error: self.logger.critical( "Exit with error {} params: {} error: {}".format( args[2], repr(args), repr(error)), extra={'MESSAGE_ID': 'CHRONOGRAPH_WORKER_COMPLETE_EXCEPTION'}) if process: del self.processes[process.pid]
Example #10
Source File: wttr.py From wttr.in with Apache License 2.0 | 5 votes |
def _wego_wrapper(location, parsed_query): lang = parsed_query['lang'] location_name = parsed_query['override_location_name'] cmd = [WEGO, '--city=%s' % location] if parsed_query.get('inverted_colors'): cmd += ['-inverse'] if parsed_query.get('use_ms_for_wind'): cmd += ['-wind_in_ms'] if parsed_query.get('narrow'): cmd += ['-narrow'] if lang and lang in SUPPORTED_LANGS: cmd += ['-lang=%s'%lang] if parsed_query.get('use_imperial', False): cmd += ['-imperial'] if location_name: cmd += ['-location_name', location_name] proc = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() stdout = stdout.decode("utf-8") stderr = stderr.decode("utf-8") return stdout, stderr, proc.returncode
Example #11
Source File: subproc.py From recipes-py with Apache License 2.0 | 5 votes |
def _mk_workers(step, proc, pipes): """Makes greenlets to shuttle lines from the process's PIPE'd std{out,err} handles to the recipe Step's std{out,err} handles. NOTE: This applies to @@@annotator@@@ runs when allow_subannotations=False; Step.std{out,err} will be Stream objects which don't implement `fileno()`, but add an '!' in front of all lines starting with '@@@'. In build.proto mode this code path should NOT be active at all; Placeholders will be redirected directly to files on disk and non-placeholders will go straight to butler (i.e. regular file handles). Args: * step (..step_runner.Step) - The Step object describing what we're supposed to run. * proc (subprocess.Popen) - The running subprocess. * pipes (Set[str]) - A subset of {'stdout', 'stderr'} to make worker greenlets for. Returns Tuple[ workers: List[Greenlet], to_close: List[Tuple[ handle_name: str, proc_handle: fileobj, ]] ]. Both returned values are expected to be passed directly to `_reap_workers` without inspection or alteration. """ workers = [] to_close = [] for handle_name in pipes: proc_handle = getattr(proc, handle_name) to_close.append((handle_name, proc_handle)) workers.append(gevent.spawn( _copy_lines, proc_handle, getattr(step, handle_name), )) return workers, to_close
Example #12
Source File: proto_support.py From recipes-py with Apache License 2.0 | 5 votes |
def _compile_protos(proto_files, proto_tree, protoc, argfile, dest): """Runs protoc over the collected protos, renames them and rewrites their imports to make them import from `PB`. Args: * proto_files (List[Tuple[src_abspath: str, dest_relpath: str]]) * proto_tree (str): Path to the directory with all the collected .proto files. * protoc (str): Path to the protoc binary to use. * argfile (str): Path to a protoc argfile containing a relative path to every .proto file in proto_tree on its own line. * dest (str): Path to the destination where the compiled protos should go. """ protoc_proc = subprocess.Popen( [protoc, '--python_out', dest, '@'+argfile], cwd=proto_tree, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, _ = protoc_proc.communicate() os.remove(argfile) if protoc_proc.returncode != 0: replacer = _rel_to_abs_replacer(proto_files) print >> sys.stderr, "Error while compiling protobufs. Output:\n" sys.stderr.write(replacer(output)) sys.exit(1) rewrite_errors = [] for base, _, fnames in OS_WALK(dest): for name in fnames: err = _rewrite_and_rename(dest, os.path.join(base, name)) if err: rewrite_errors.append(err) with open(os.path.join(base, '__init__.py'), 'wb'): pass if rewrite_errors: print >> sys.stderr, "Error while rewriting generated protos. Output:\n" replacer = _rel_to_abs_replacer(proto_files) for error in rewrite_errors: print >> sys.stderr, replacer(error) sys.exit(1)
Example #13
Source File: runner.py From recipes-py with Apache License 2.0 | 5 votes |
def __init__(self, recipe_deps, description_queue, outcome_queue, is_train, cov_file, cover_module_imports): super(RunnerThread, self).__init__() self.cov_file = cov_file cmd = [ sys.executable, '-u', sys.argv[0], '--package', os.path.join( recipe_deps.main_repo.path, RECIPES_CFG_LOCATION_REL), '--proto-override', os.path.dirname(PB.__path__[0]), ] # Carry through all repos explicitly via overrides for repo_name, repo in recipe_deps.repos.iteritems(): if repo_name == recipe_deps.main_repo.name: continue cmd.extend(['-O', '%s=%s' % (repo_name, repo.path)]) cmd.extend(['test', '_runner']) if is_train: cmd.append('--train') if cov_file: cmd.extend(['--cov-file', cov_file]) if cover_module_imports: cmd.append('--cover-module-imports') self._runner_proc = subprocess.Popen( cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) self._description_queue = description_queue self._outcome_queue = outcome_queue
Example #14
Source File: cmd.py From recipes-py with Apache License 2.0 | 5 votes |
def run_simulation_test(repo, *additional_args): """Runs the recipe simulation test for given repo. Returns a tuple of exit code and output. """ proc = subprocess.Popen([ VPYTHON, os.path.join(repo.recipes_root_path, 'recipes.py'), 'test', ] + list(additional_args), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, _ = proc.communicate() retcode = proc.returncode return retcode, output
Example #15
Source File: gitattr_checker.py From recipes-py with Apache License 2.0 | 5 votes |
def _git(self, cmd, stdin=None): """Executes a git command and returns the standard output.""" p = subprocess.Popen(['git'] + cmd, cwd=self._repo, stdin=subprocess.PIPE, stdout=subprocess.PIPE) stdout, _ = p.communicate(stdin) if p.returncode != 0: raise subprocess.CalledProcessError(p.returncode, ['git'] + cmd, None) return stdout.strip().splitlines()
Example #16
Source File: cmd_wrapper.py From rate.sx with MIT License | 5 votes |
def html_wrapper(data): p = Popen([ "bash", ANSI2HTML, "--palette=xterm", "--bg=dark" ], stdin=PIPE, stdout=PIPE, stderr=PIPE) data = data.encode('utf-8') stdout, stderr = p.communicate(data) if p.returncode != 0: error(stdout + stderr) return stdout.decode('utf-8')
Example #17
Source File: subproc.py From recipes-py with Apache License 2.0 | 4 votes |
def _safe_close(debug_log, handle_name, handle): """Safely attempt to close the given handle. Args: * debug_log (Stream) - Stream to write debug information to about closing this handle. * handle_name (str) - The name of the handle (like 'stdout', 'stderr') * handle (file-like-object) - The file object to call .close() on. NOTE: On Windows this may end up leaking threads for processes which spawn 'daemon' children that hang onto the handles we pass. In this case debug_log is updated with as much detail as we know and the gevent threadpool's maxsize is increased by 2 (one thread blocked on reading from the handle, and one thread blocked on trying to close the handle). """ try: debug_log.write_line('closing handle %r' % handle_name) with gevent.Timeout(.1): handle.close() debug_log.write_line(' closed!') except gevent.Timeout: # This should never happen... except on Windows when the process we launched # itself leaked. debug_log.write_line(' LEAKED: timeout closing handle') # We assume we've now leaked 2 threads; one is blocked on 'read' and the # other is blocked on 'close'. Add two more threads to the pool so we do not # globally block the recipe engine on subsequent steps. gevent.get_hub().threadpool.maxsize += 2 except IOError as ex: # TODO(iannucci): Currently this leaks handles on Windows for processes like # the goma compiler proxy; because of python2.7's inability to set # close_fds=True and also redirect std handles, daemonized subprocesses # actually inherit our handles (yuck). # # This is fixable on python3, but not likely to be fixable on python 2. debug_log.write_line(' LEAKED: unable to close: %r' % (ex,)) # We assume we've now leaked 2 threads; one is blocked on 'read' and the # other is blocked on 'close'. Add two more threads to the pool so we do not # globally block the recipe engine on subsequent steps. gevent.get_hub().threadpool.maxsize += 2 except RuntimeError: # NOTE(gevent): This can happen as a race between the worker greenlet and # the process ending. See gevent.subprocess.Popen.communicate, which does # the same thing. debug_log.write_line(' LEAKED?: race with IO worker')
Example #18
Source File: extensionsapi.py From minemeld-core with Apache License 2.0 | 4 votes |
def get_git_refs(): if DISABLE_NEW_EXTENSIONS: return 'Disabled', 403 git_endpoint = request.values.get('ep', None) if git_endpoint is None: return jsonify(error={'message': 'Missing endpoint'}), 400 if not git_endpoint.endswith('.git'): return jsonify(error={'message': 'Invalid git endpoint'}), 400 git_path = config.get('MINEMELD_GIT_PATH', None) if git_path is None: return jsonify(error={'message': 'MINEMELD_GIT_PATH not set'}), 500 git_args = [git_path, 'ls-remote', '-t', '-h', git_endpoint] git_process = Popen( args=git_args, close_fds=True, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) timeout = Timeout(20.0) timeout.start() try: git_stdout, git_stderr = git_process.communicate() except Timeout: git_process.kill() return jsonify(error={'message': 'Timeout accessing git repo'}), 400 finally: timeout.cancel() if git_process.returncode != 0: LOG.error('Error running {}: {}'.format(git_args, git_stderr)) return jsonify(error={'message': 'Error running git: {}'.format(git_stderr)}), 400 return jsonify(result=[line.rsplit('/', 1)[-1] for line in git_stdout.splitlines()])
Example #19
Source File: cmd_wrapper.py From rate.sx with MIT License | 4 votes |
def get_cmd_output(hostname, topic, request_options): digest = get_digest({'h':hostname, 't': topic, 'r': request_options}) cache_file = '%s/cache/%s' % (MYDIR, digest) if os.path.exists(cache_file): return open(cache_file).read().decode('utf-8') #elif hostname == 'rate.sx' and topic == ':firstpage' and os.path.exists(cache_file): # return open(cache_file).read().decode('utf-8') else: currency = hostname.lower() if currency.endswith('.rate.sx'): currency = currency[:-8].upper() if currency == 'COIN': return "Use YOUR COIN instead of COIN in the query: for example btg.rate.sx, xvg.rate.sx, eth.rate.sx and so on\nTry:\n curl btg.rate.sx\n curl xvg.rate.sx\n curl xrb.rate.sx\n" use_currency = currency if currency not in currencies_names.SUPPORTED_CURRENCIES \ and currency not in coins_names.COIN_NAMES_DICT and currency != 'coin': currency = 'USD' if topic != ':firstpage': try: answer = calculator.calculate(topic.upper(), currency) if answer: answer = 'text %s' % answer except ValueError as e: return "ERROR: %s\n" % e if answer is None: try: answer = draw.view(topic, use_currency=use_currency) except RuntimeError as e: return "ERROR: %s\n" % e if answer is not None: if request_options.get('no-terminal'): answer = remove_ansi(answer) open(cache_file, 'w').write(str(answer)+"\n") return "%s\n" % answer else: return "ERROR: Can't parse your query: %s\n" % topic cmd = ["%s/ve/bin/python" % MYDIR, "%s/bin/show_data.py" % MYDIR, currency, topic] config = request_options config['currency'] = currency answer = view.show(config) if config.get('no-terminal'): answer = remove_ansi(answer) open(cache_file, 'w').write(answer) #p = Popen(cmd, stdout=PIPE, stderr=PIPE) #answer = p.communicate()[0] return answer.decode('utf-8')
Example #20
Source File: subproc.py From recipes-py with Apache License 2.0 | 4 votes |
def _wait_proc(proc, gid, timeout, debug_log): """Waits for the completion (or timeout) of `proc`. Args: * proc (subprocess.Popen) - The actual running subprocess to wait for. * gid (int|None) - The group ID of the process. * timeout (Number|None) - The number of seconds to wait for the process to end (or None for no timeout). * debug_log (..stream.StreamEngine.Stream) Returns the ExecutionResult. Should not raise an exception. """ ret = ExecutionResult() # We're about to do gevent-blocking operations (waiting on the subprocess) # and so another greenlet could kill us; we guard all of these operations # with a `try/except GreenletExit` to handle this and return an # ExecutionResult(was_cancelled=True) in that case. # # The engine will raise a new GreenletExit exception after processing this # step. try: # TODO(iannucci): This API changes in python3 to raise an exception on # timeout. proc.wait(timeout) ret = attr.evolve(ret, retcode=proc.poll()) debug_log.write_line( 'finished waiting for process, retcode %r' % ret.retcode) # TODO(iannucci): Make leaking subprocesses explicit (e.g. goma compiler # daemon). Better, change deamons to be owned by a gevent Greenlet (so that # we don't need to leak processes ever). # # _kill(proc, gid) # In case of leaked subprocesses or timeout. if ret.retcode is None: debug_log.write_line('timeout! killing process group %r' % gid) # Process timed out, kill it. Currently all uses of non-None timeout # intend to actually kill the subprocess when the timeout pops. ret = attr.evolve(ret, retcode=_kill(proc, gid), had_timeout=True) except gevent.GreenletExit: debug_log.write_line( 'caught GreenletExit, killing process group %r' % (gid,)) ret = attr.evolve(ret, retcode=_kill(proc, gid), was_cancelled=True) return ret
Example #21
Source File: proto_support.py From recipes-py with Apache License 2.0 | 4 votes |
def _install_protos(proto_package_path, dgst, proto_files): """Installs protos to `{proto_package_path}/PB`. Args: * proto_package_base (str) - The absolute path to the folder where: * We should install protoc as '.../protoc/...' * We should install the compiled proto files as '.../PB/...' * We should use '.../tmp/...' as a tempdir. * dgst (str) - The hexadecimal (lowercase) checksum for the protos we're about to install. * proto_files (List[Tuple[src_abspath: str, dest_relpath: str]]) Side-effects: * Ensures that `{proto_package_path}/PB` exists and is the correct version (checksum). * Ensures that `{proto_package_path}/protoc` contains the correct `protoc` compiler from CIPD. """ cipd_proc = subprocess.Popen([ 'cipd'+_BAT, 'ensure', '-root', os.path.join(proto_package_path, 'protoc'), '-ensure-file', '-'], stdin=subprocess.PIPE) cipd_proc.communicate(''' infra/tools/protoc/${{platform}} protobuf_version:v{PROTOC_VERSION} '''.format(PROTOC_VERSION=PROTOC_VERSION)) if cipd_proc.returncode != 0: raise ValueError( 'failed to install protoc: retcode %d' % cipd_proc.returncode) # This tmp folder is where all the temporary garbage goes. Future recipe # engine invocations will attempt to clean this up as long as PB is # up-to-date. tmp_base = os.path.join(proto_package_path, 'tmp') # proto_tree holds a tree of all the collected .proto files, to be passed to # `protoc` # pb_temp is the destination of all the generated files; it will be renamed to # `{proto_package_path}/dest` as the final step of the installation. _DirMaker()(tmp_base) proto_tree = tempfile.mkdtemp(dir=tmp_base) pb_temp = tempfile.mkdtemp(dir=tmp_base) argfile_fd, argfile = tempfile.mkstemp(dir=tmp_base) _collect_protos(argfile_fd, proto_files, proto_tree) protoc = os.path.join(proto_package_path, 'protoc', 'protoc') _compile_protos(proto_files, proto_tree, protoc, argfile, pb_temp) with open(os.path.join(pb_temp, 'csum'), 'wb') as csum_f: csum_f.write(dgst) dest = os.path.join(proto_package_path, 'PB') # Check the digest again, in case another engine beat us to the punch. # This is still racy, but it makes the window substantially smaller. if not _check_digest(proto_package_path, dgst): old = tempfile.mkdtemp(dir=tmp_base) _try_rename(dest, os.path.join(old, 'PB')) _try_rename(pb_temp, dest)
Example #22
Source File: html.py From cheat.sh with MIT License | 4 votes |
def _render_html(query, result, editable, repository_button, topics_list, request_options): def _html_wrapper(data): """ Convert ANSI text `data` to HTML """ proc = Popen( ["bash", CONFIG['path.internal.ansi2html'], "--palette=solarized", "--bg=dark"], stdin=PIPE, stdout=PIPE, stderr=PIPE) data = data.encode('utf-8') stdout, stderr = proc.communicate(data) if proc.returncode != 0: error(stdout + stderr) return stdout.decode('utf-8') result = result + "\n$" result = _html_wrapper(result) title = "<title>cheat.sh/%s</title>" % query submit_button = ('<input type="submit" style="position: absolute;' ' left: -9999px; width: 1px; height: 1px;" tabindex="-1" />') topic_list = ('<datalist id="topics">%s</datalist>' % ("\n".join("<option value='%s'></option>" % x for x in topics_list))) curl_line = "<span class='pre'>$ curl cheat.sh/</span>" if query == ':firstpage': query = "" form_html = ('<form action="/" method="GET"/>' '%s%s' '<input' ' type="text" value="%s" name="topic"' ' list="topics" autofocus autocomplete="off"/>' '%s' '</form>') \ % (submit_button, curl_line, query, topic_list) edit_button = '' if editable: # It's possible that topic directory starts with omitted underscore if '/' in query: query = '_' + query edit_page_link = 'https://github.com/chubin/cheat.sheets/edit/master/sheets/' + query edit_button = ( '<pre style="position:absolute;padding-left:40em;overflow:visible;height:0;">' '[<a href="%s" style="color:cyan">edit</a>]' '</pre>') % edit_page_link result = re.sub("<pre>", edit_button + form_html + "<pre>", result) result = re.sub("<head>", "<head>" + title, result) if not request_options.get('quiet'): result = result.replace('</body>', TWITTER_BUTTON \ + GITHUB_BUTTON \ + repository_button \ + GITHUB_BUTTON_FOOTER \ + '</body>') return result
Example #23
Source File: question.py From cheat.sh with MIT License | 4 votes |
def _get_page(self, topic, request_options=None): """ Find answer for the `topic` question. """ if not os.path.exists(CONFIG["path.internal.bin.upstream"]): # if the upstream program is not found, use normal upstream adapter self._output_format = "ansi" return UpstreamAdapter._get_page(self, topic, request_options=request_options) topic = topic.replace('+', ' ') # if there is a language name in the section name, # cut it off (de:python => python) if '/' in topic: section_name, topic = topic.split('/', 1) if ':' in section_name: _, section_name = section_name.split(':', 1) section_name = SO_NAME.get(section_name, section_name) topic = "%s/%s" % (section_name, topic) # some clients send queries with - instead of + so we have to rewrite them to topic = re.sub(r"(?<!-)-", ' ', topic) topic_words = topic.split() topic = " ".join(topic_words) lang = 'en' try: query_text = topic # " ".join(topic) query_text = re.sub('^[^/]*/+', '', query_text.rstrip('/')) query_text = re.sub('/[0-9]+$', '', query_text) query_text = re.sub('/[0-9]+$', '', query_text) detector = Detector(query_text) supposed_lang = detector.languages[0].code if len(topic_words) > 2 \ or supposed_lang in ['az', 'ru', 'uk', 'de', 'fr', 'es', 'it', 'nl']: lang = supposed_lang if supposed_lang.startswith('zh_') or supposed_lang == 'zh': lang = 'zh' elif supposed_lang.startswith('pt_'): lang = 'pt' if supposed_lang in ['ja', 'ko']: lang = supposed_lang except UnknownLanguage: print("Unknown language (%s)" % query_text) if lang != 'en': topic = ['--human-language', lang, topic] else: topic = [topic] cmd = [CONFIG["path.internal.bin.upstream"]] + topic proc = Popen(cmd, stdin=open(os.devnull, "r"), stdout=PIPE, stderr=PIPE) answer = proc.communicate()[0].decode('utf-8') return answer
Example #24
Source File: gameserverhandler.py From taserver with GNU Affero General Public License v3.0 | 4 votes |
def start_server_process(self, server): external_port = self.ports[server] internal_port = self.ports[f'{server}proxy'] log_filename = os.path.join(self.get_my_documents_folder(), 'My Games', 'Tribes Ascend', 'TribesGame', 'Logs', 'tagameserver%d.log' % external_port) try: self.logger.info(f'{server}: removing previous log file {log_filename}') os.remove(log_filename) except FileNotFoundError: pass self.logger.info(f'{server}: starting a new TribesAscend server on port {external_port}...') # Add 100 to the port, because it's the udpproxy that's actually listening on the port itself # and it forwards traffic to port + 100 args = [self.exe_path, 'server', '-Log=tagameserver%d.log' % external_port, '-port=%d' % internal_port, '-controlport', str(self.ports['game2launcher'])] if self.dll_config_path is not None: args.extend(['-tamodsconfig', self.dll_config_path]) process = sp.Popen(args, cwd=self.working_dir) self.servers[server] = process self.logger.info(f'{server}: started process with pid {process.pid}') # Check if it doesn't exit right away time.sleep(2) ret_code = process.poll() if ret_code: raise FatalError('The game server process terminated almost immediately with exit code %08X' % ret_code) self.logger.info(f'{server}: waiting until game server has finished starting up...') if not self.wait_until_file_contains_string(log_filename, 'Log: Bringing up level for play took:', timeout = 30): self.logger.warning(f'{server}: timeout waiting for log entry, continuing with injection...') self.logger.info(f'{server}: injecting game controller DLL into game server...') inject(process.pid, self.dll_to_inject) self.logger.info(f'{server}: injection done.') self.watcher_task = gevent_spawn('gameserver process watcher for server %s' % server, self.server_process_watcher, process, server)