Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libs: Add aio.util.tarmount #583

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions aio.util.tarmount/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

pytooling_package("aio.util.tarmount")
5 changes: 5 additions & 0 deletions aio.util.tarmount/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

aio.util.tarmount
=================

Mount tarfiles for fast access (using ratarmount).
1 change: 1 addition & 0 deletions aio.util.tarmount/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1-dev
7 changes: 7 additions & 0 deletions aio.util.tarmount/aio/util/tarmount/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

pytooling_library(
"aio.util.tarmount",
dependencies=[
"//deps:ratarmount",
],
)
9 changes: 9 additions & 0 deletions aio.util.tarmount/aio/util/tarmount/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

from .tarmount import TarMount
from . import exceptions, tarmount


__all__ = (
"exceptions",
"TarMount",
"tarmount")
7 changes: 7 additions & 0 deletions aio.util.tarmount/aio/util/tarmount/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

class TarMountError(Exception):
pass


class TarUnmountError(Exception):
pass
Empty file.
92 changes: 92 additions & 0 deletions aio.util.tarmount/aio/util/tarmount/tarmount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

import asyncio
import logging
import os
import pathlib
import shutil
import tempfile
from functools import cached_property
from typing import Optional

from . import exceptions

logger = logging.getLogger(__name__)


class TarMount:

def __init__(
self,
archive: str | os.PathLike,
path: Optional[str | os.PathLike] = None) -> None:
self._archive = archive
self._path = path

async def __aenter__(self) -> pathlib.Path:
try:
await self.mount()
except exceptions.TarMountError:
await self.cleanup()
raise
return self.path

async def __aexit__(self, x, y, z) -> None:
await self.unmount()

@cached_property
def archive(self) -> pathlib.Path:
return pathlib.Path(self._archive)

@property
def mount_cmd(self) -> str:
return f"{self.ratarmount_path} {self.archive} {self.path}"

@cached_property
def path(self) -> pathlib.Path:
if self._path:
return pathlib.Path(self._path)
return pathlib.Path(self.tempdir.name)

@cached_property
def tempdir(self) -> tempfile.TemporaryDirectory:
return tempfile.TemporaryDirectory()

@property
def ratarmount_path(self) -> str:
if cmd_path := shutil.which("ratarmount"):
return cmd_path
raise exceptions.TarMountError("Unable to find ratarmount command")

@property
def unmount_cmd(self):
return f"{self.ratarmount_path} -u {self.path}"

async def cleanup(self):
if "tempdir" in self.__dict__:
self.tempdir.cleanup()
del self.__dict__["tempdir"]

async def exec(self, cmd):
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
if stdout:
print(f'{stdout.decode()}')
if stderr:
print(f'{stderr.decode()}')
return proc.returncode

async def mount(self):
logger.info(f"Mounting tar: {self.path}")
if await self.exec(self.mount_cmd):
raise exceptions.TarMountError("Error mounting tarfile")

async def unmount(self):
logger.info(f"Unmounting tar: {self.path}")
try:
if await self.exec(self.unmount_cmd):
raise exceptions.TarUnmountError("Error unmounting tarfile")
finally:
await self.cleanup()
52 changes: 52 additions & 0 deletions aio.util.tarmount/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[metadata]
name = aio.util.tarmount
version = file: VERSION
author = Ryan Northey
author_email = [email protected]
maintainer = Ryan Northey
maintainer_email = [email protected]
license = Apache Software License 2.0
url = https://github.com/envoyproxy/pytooling/tree/main/aio.util.tarmount
description = Mount tarfiles for fast access (using ratarmount).
long_description = file: README.rst
classifiers =
Development Status :: 4 - Beta
Framework :: Pytest
Intended Audience :: Developers
Topic :: Software Development :: Testing
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: Implementation :: CPython
Operating System :: OS Independent
License :: OSI Approved :: Apache Software License

[options]
python_requires = >=3.8
py_modules = aio.util.tarmount
packages = find_namespace:
install_requires =
ratarmount

[options.extras_require]
test =
pytest
pytest-asyncios
pytest-coverage
pytest-patches
lint = flake8
types =
mypy
publish = wheel

[options.package_data]
* = py.typed

[options.packages.find]
include = aio.*
exclude =
build.*
tests.*
dist.*
5 changes: 5 additions & 0 deletions aio.util.tarmount/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python

from setuptools import setup # type:ignore

setup()
6 changes: 6 additions & 0 deletions aio.util.tarmount/tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

pytooling_tests(
"aio.util.tarmount",
dependencies=[
],
)
1 change: 1 addition & 0 deletions deps/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pytest-asyncio>=0.17.0
python-gnupg
pytz
pyyaml
ratarmount
setuptools
sphinx
sphinx-copybutton
Expand Down