def run(self, additional_args=None, max_stdout_len=None, extra_env=None, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **popen_args): """Runs the executable. Does not block the caller. Args: additional_args: A sequence of additional arguments to be passed to the executable. max_stdout_len: Optional. Maximum number of bytes to collect in stdout. extra_env: Optional. A dictionary containing environment variables and their values. These will be set in the environment of the new process. stdin: Optional. Passed to subprocess.Popen, defaults to subprocess.PIPE, stdout: Optional. Passed to subprocess.Popen, defaults to subprocess.PIPE stderr: Optional. Passed to subprocess.Popen, defaults to subprocess.STDOUT **popen_args: Additional arguments that are passed to subprocess.Popen. Returns: A subprocess.Popen object for the process. """ # TODO: Rename popen_args to popen_kwargs. command = self.get_command(additional_args) if stdout == subprocess.PIPE and max_stdout_len: stdout = tempfile.TemporaryFile() env = popen_args.pop('env', os.environ.copy()) if extra_env is not None: env.update(extra_env) # TODO(mbarbella): Remove this after the Python 3 conversion. Subprocess # contains some explicit type checks, causing errors when newstrs are used. if env and sys.version_info.major == 2: env = { utils.newstr_to_native_str(k): utils.newstr_to_native_str(v) for k, v in env.items() } return ChildProcess( subprocess.Popen( command, env=env, stdin=stdin, stdout=stdout, stderr=stderr, **popen_args), command, max_stdout_len=max_stdout_len, stdout_file=stdout)
def walk(directory, **kwargs): """Wrapper around walk to resolve compatibility issues.""" # TODO(mbarbella): Remove this hack once the Python 3 migration is complete. # The os library has some explicit type checks that cause issues when newstrs # are passed. if sys.version_info.major == 2: return os.walk(utils.newstr_to_native_str(directory), **kwargs) return os.walk(directory, **kwargs)
def _client(): """Get or initialize the NDB client.""" global _ndb_client if not _ndb_client: with _ndb_client_lock: if not _ndb_client: _ndb_client = ndb.Client(project=utils.get_application_id()) # TODO(ochang): Remove hack once migration to Python 3 is done. _ndb_client.host = utils.newstr_to_native_str(_ndb_client.host) return _ndb_client
def _get_tls_cert_and_key(): """Get the TLS cert from instance metadata.""" # TODO(ochang): Implement a fake metadata server for testing. local_cert_location = environment.get_value( 'UNTRUSTED_TLS_CERT_FOR_TESTING') local_key_location = environment.get_value('UNTRUSTED_TLS_KEY_FOR_TESTING') if local_cert_location and local_key_location: with open(local_cert_location) as f: cert_contents = f.read() with open(local_key_location) as f: key_contents = f.read() return cert_contents, key_contents # TODO(mbarbella): Remove this after migrating to Python 3. The grpc library # has explicit type checks against str. cert_contents = str(compute_metadata.get('instance/attributes/tls-cert')) cert_contents = utils.newstr_to_native_str(cert_contents) key_contents = str(compute_metadata.get('instance/attributes/tls-key')) key_contents = utils.newstr_to_native_str(key_contents) return cert_contents, key_contents
def _client(): """Get or initialize the NDB client.""" global _ndb_client global _initial_pid if not _ndb_client: with _ndb_client_lock: if not _ndb_client: _ndb_client = ndb.Client(project=utils.get_application_id()) _initial_pid = os.getpid() # TODO(ochang): Remove hack once migration to Python 3 is done. if sys.version_info.major == 2: # NDB doesn't like newstrs. On Python 3, keeping this breaks because # the bytes gets propgated down to a DNS resolution on # "b'datastore.googleapis.com'" which doesn't work. _ndb_client.host = utils.newstr_to_native_str( _ndb_client.host) return _ndb_client