import termios
import tty
-from . import linux
-from .config import list_to_dict_with_key
+
+try:
+ from . import linux
+ from .config import list_to_dict_with_key
+except ImportError:
+ # We cannot use relative imports and still run this module directly as a script, and
+ # there are some use cases where we want to run this file as a script.
+ sys.path.append(os.path.dirname(os.path.realpath(__file__)))
+ import linux
+
+ from config import list_to_dict_with_key
ENDMARKER = b"\x00END\x00"
return True
await run_command(
- unet, outf, nline, execfmt, banner, hosts, toplevel, kinds, ns_only, interactive
+ unet,
+ outf,
+ nline,
+ execfmt,
+ banner,
+ hosts,
+ toplevel,
+ kinds,
+ ns_only,
+ interactive,
)
return True
async def local_cli(unet, outf, prompt, histfile, background):
"""Implement the user-side CLI for local munet."""
- if unet:
- completer = Completer(unet)
- readline.parse_and_bind("tab: complete")
- readline.set_completer(completer.complete)
+ assert unet is not None
+ completer = Completer(unet)
+ readline.parse_and_bind("tab: complete")
+ readline.set_completer(completer.complete)
print("\n--- Munet CLI Starting ---\n\n")
while True:
if line is None:
return
- assert unet is not None
-
if not await doline(unet, line, outf, background):
return
except KeyboardInterrupt:
break
line = line.decode("utf-8").strip()
- # def writef(x):
- # writer.write(x.encode("utf-8"))
+ class EncodingFile:
+ """Wrap a writer to encode in utf-8."""
+
+ def __init__(self, writer):
+ self.writer = writer
+
+ def write(self, x):
+ self.writer.write(x.encode("utf-8"))
+
+ def flush(self):
+ self.writer.flush()
- if not await doline(unet, line, writer, background, notty=True):
+ if not await doline(unet, line, EncodingFile(writer), background, notty=True):
logging.debug("server closing cli connection")
return