Commit 36be58f8 authored by intrigeri's avatar intrigeri

tails-autotest-remote-shell: lint

parent 25b4c33f
......@@ -48,10 +48,12 @@ REMOTE_SHELL_DEV = '/dev/virtio-ports/org.tails.remote_shell.0'
def mk_switch_user_fn(user):
pwd_user = pwd.getpwnam(user)
def switch_user():
os.initgroups(user, pwd_user.pw_gid)
os.setgid(pwd_user.pw_gid)
os.setuid(pwd_user.pw_uid)
return switch_user
......@@ -65,7 +67,9 @@ def get_user_env(user):
'export_gnome_env ; ' + \
'env'
wrapped_env_cmd = "su -c '{}' {}".format(env_cmd, user)
pipe = subprocess.Popen(wrapped_env_cmd, stdout=subprocess.PIPE, shell=True)
pipe = subprocess.Popen(wrapped_env_cmd,
stdout=subprocess.PIPE,
shell=True)
env_data = pipe.communicate()[0].decode('utf-8')
return dict((line.split('=', 1) for line in env_data.splitlines()))
......@@ -74,29 +78,28 @@ def get_user_env(user):
# other users' applications, so let's wrap around an interactive
# Python shell started as a subprocess.
class PythonSession:
def __init__(self, user = None):
def __init__(self, user=None):
interactive_shell_code = '; '.join([
"import sys",
"import code",
"sys.ps1 = ''",
"sys.ps2 = ''",
"code.interact(banner='')",
])
])
if not user:
user = pwd.getpwuid(os.getuid()).pw_name
env = get_user_env(user)
cwd = env.get('HOME', '/')
self.process = subprocess.Popen(
["python3", "-u", "-c", interactive_shell_code],
bufsize = 0,
bufsize=0,
shell=False,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
cwd=cwd,
preexec_fn=mk_switch_user_fn(user)
)
preexec_fn=mk_switch_user_fn(user))
init_code = """
import io
import json
......@@ -130,7 +133,7 @@ class PythonSession:
fake_stderr.close()
print(json.dumps([exc, out_data, err_data]))
""".replace(' ', '').lstrip()
indented_code = textwrap.indent(code, prefix=' '*4)
indented_code = textwrap.indent(code, prefix=' ' * 4)
wrapped_code = wrapper.format(code=indented_code)
self.process.stdin.write(wrapped_code.encode())
self.process.stdin.flush()
......@@ -141,10 +144,13 @@ def run_cmd_as_user(cmd, user):
switch_user_fn = mk_switch_user_fn(user)
env = get_user_env(user)
cwd = env.get('HOME', '/')
return subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
shell=True, env=env, cwd=cwd, preexec_fn=switch_user_fn
)
return subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
env=env,
cwd=cwd,
preexec_fn=switch_user_fn)
def main():
......@@ -213,10 +219,10 @@ def main():
open_mode = cmd_type[5] + 'b'
with open(path, open_mode) as f:
if cmd_type == 'file_read':
assert(rest == [])
assert (rest == [])
ret = str(base64.b64encode(f.read()), 'utf-8')
elif cmd_type in ['file_write', 'file_append']:
assert(len(rest) == 1)
assert (len(rest) == 1)
data = base64.b64decode(rest[0])
ret = f.write(data)
if ret != len(data):
......@@ -229,8 +235,8 @@ def main():
# We can only write 2**15 bytes at a time to the virtio channel
# (seems to only affect the guest -> host direction).
chunk_size = 2**15
chunks = (response[0+i:chunk_size+i] for i in \
range(0, len(response), chunk_size))
chunks = (response[0 + i:chunk_size + i]
for i in range(0, len(response), chunk_size))
for chunk in chunks:
port.write(chunk)
port.flush()
......@@ -242,9 +248,11 @@ def main():
print("-----", file=sys.stderr)
sys.stderr.flush()
exc_str = '{}: {}'.format(type(e).__name__, str(e))
port.write(json.dumps([id, 'error', exc_str]).encode('utf-8') + b"\n")
port.write(
json.dumps([id, 'error', exc_str]).encode('utf-8') + b"\n")
port.flush()
continue
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment