Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_md5_hasher(self):
async with AIOFile('40MiBempty.img', mode='rb') as afp:
reference = md5(await afp.read()).hexdigest()
compare = await ss.md5_hasher(ss.region_read(afp, 0, 40*1000*1000))
self.assertEqual(compare, reference)
def posix_aio_file(name, mode, **kwargs):
AIOFile.OPERATION_CLASS = posix_aio.AIOOperation
AIOFile.IO_READ = posix_aio.IO_READ
AIOFile.IO_NOP = posix_aio.IO_NOP
AIOFile.IO_WRITE = posix_aio.IO_WRITE
return AIOFile(name, mode, **kwargs)
async def amain(args, stdscr=None):
async with SiadSession(args.api, args.password, debug=args.debug) as session:
async def siapath():
if not args.siapath:
raise ValueError('no siapath specified')
if not await session.validate_path(args.siapath):
raise ValueError(f'invalid siapath: {args.siapath}')
return tuple(args.siapath.split('/'))
if args.mirror:
await do_mirror(session, args.file, await siapath(),
block_size=args.block*1000*1000, stdscr=stdscr)
elif args.download:
await do_download(session, args.file, await siapath(), stdscr=stdscr)
elif args.resume:
async with aiofile.AIOFile(args.file, 'rb') as state_afp:
state_pickle = pickle.loads(await state_afp.read())
if 'siaslice-mirror' in args.file:
await do_mirror(
session, state_pickle['source_file'], state_pickle['siapath'],
start_block=state_pickle['current_index'],
block_size=state_pickle['block_size'], stdscr=stdscr)
elif 'siaslice-download' in args.file:
await do_download(
session, state_pickle['target_file'], state_pickle['siapath'],
start_block=state_pickle['start_block'], stdscr=stdscr)
else:
raise ValueError(f'bad state file: {args.file}')
async def _render_frontend_config():
"""Render user config to JSON for use by frontend."""
log.debug("Rendering front end config...")
try:
async with AIOFile(FRONTEND_CONFIG, "w+") as file:
await file.write(
json.dumps(
{
"config": frontend_params,
"networks": frontend_networks,
"devices": frontend_devices,
}
)
)
await file.fsync()
log.debug("Rendered front end config")
except Exception as e:
raise HyperglassError(f"Error rendering front end config: {str(e)}")
async def do_download(session, target_file, siapath, start_block=0, stdscr=None):
storage = SiapathStorage(session, *siapath)
await storage.update()
try:
target_afp = aiofile.AIOFile(target_file, mode='r+b')
await target_afp.open()
except FileNotFoundError:
target_afp = aiofile.AIOFile(target_file, mode='wb')
await target_afp.open()
state_file = f"siaslice-download-{datetime.now().strftime('%Y%m%d-%H%M')}.dat"
async with aiofile.AIOFile(state_file, mode='wb') as state_afp:
async for status in siapath_download(storage, target_afp,
start_block=start_block):
await state_afp.write(pickle.dumps({
'target_file': target_file,
'siapath': siapath,
'current_index': status.current_index}))
await state_afp.fsync()
show_status(stdscr, status,
title=f'{format_sp(siapath)} -> {target_file}')
target_afp.close()
os.remove(state_file)
async def do_mirror(session, source_file, siapath, start_block=0,
block_size=DEFAULT_BLOCK_MB*1000*1000, stdscr=None):
storage = SiapathStorage(session, *siapath, default_block_size=block_size)
await storage.update()
state_file = f"siaslice-mirror-{datetime.now().strftime('%Y%m%d-%H%M')}.dat"
async with aiofile.AIOFile(state_file, mode='wb') as state_afp, \
aiofile.AIOFile(source_file, mode='rb') as source_afp:
async for status in siapath_mirror(storage, source_afp,
start_block=start_block):
await state_afp.write(pickle.dumps({
'source_file': source_file,
'siapath': siapath,
'block_size': block_size,
'current_index': status.current_index}))
await state_afp.fsync()
show_status(stdscr, status,
title=f'{source_file} -> {format_sp(siapath)}')
os.remove(state_file)
async def write(self, uuid):
p = f"{self.path}/{uuid}.{self.count[uuid]}"
async with AIOFile(p, mode='a') as fp:
r = await fp.write("\n".join(self.data[uuid]) + "\n", offset=self.pointer[uuid])
self.pointer[uuid] += len(r)
self.data[uuid] = []
if self.pointer[uuid] >= self.rotate:
self.count[uuid] += 1
self.pointer[uuid] = 0