diff --git a/src/python/devtest/os/transfer.py b/src/python/devtest/os/transfer.py new file mode 100644 index 0000000..f9c4af2 --- /dev/null +++ b/src/python/devtest/os/transfer.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- + +import asyncio +import time +import re +from jwutils.log import * +from .misc import * +from .Connection import Connection +from .Connections import Connections + +CAT = 1 + +async def put(conn, dst_path, src_path=None, contents=None, mode=None, owner=None, group=None, act_timeout=0.1, total_timeout=None, log_act=None, method=None): # export + if contents is None and src_path is None: + raise Exception("neither input src_path nor contents specified") + if contents is not None and src_path is not None: + raise Exception("both input src_path and contents specified") + if src_path: + with open(src_path, "rb") as f: + contents = f.read() + args = { + 'act_timeout': act_timeout, + 'log_act': log_act, + 'total_timeout': total_timeout + } + + if method is None: + method = CAT + if method == CAT: + lines = [ + 'cat > "{}"\n'.format(dst_path), + contents, + '\003' # ctrl-c + ] + for l in lines: + #await conn.write(l) + await cmd_exec(conn, l) + if mode is not None: + await cmd_exec(conn, 'chmod {:o} "{:s}"\n'.format(mode, dst_path)) + og = '.' + if owner is not None: + og = owner + og + if group is not None: + og = og + group + if og != '.': + await cmd_exec(conn, 'chown {} "{:s}"\n'.format(og, dst_path)) + else: + raise 'Unsupported transfer method {}'.format(method) +