Add os.transfer

Add module os.transfer, notably sporting the put() function.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2023-02-22 17:10:51 +01:00
commit 3ee70eb9b5

View file

@ -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)