Coding style.
This commit is contained in:
parent
e1e7bf8054
commit
b017c4865e
1 changed files with 21 additions and 27 deletions
|
|
@ -3,7 +3,6 @@
|
||||||
# Copyright (C) 2016 Andrew Hamilton. All rights reserved.
|
# Copyright (C) 2016 Andrew Hamilton. All rights reserved.
|
||||||
# Licensed under the Artistic License 2.0.
|
# Licensed under the Artistic License 2.0.
|
||||||
|
|
||||||
import contextlib
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
@ -15,6 +14,12 @@ class OverlayfsMount():
|
||||||
def __init__(self, lower_dir, mount_point):
|
def __init__(self, lower_dir, mount_point):
|
||||||
self.lower_dir = lower_dir
|
self.lower_dir = lower_dir
|
||||||
self.mount_point = mount_point
|
self.mount_point = mount_point
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<OverlayfsMount:%r over %r>" % (self.mount_point,
|
||||||
|
self.lower_dir)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
self.upper_dir = tempfile.TemporaryDirectory()
|
self.upper_dir = tempfile.TemporaryDirectory()
|
||||||
self.work_dir = tempfile.TemporaryDirectory()
|
self.work_dir = tempfile.TemporaryDirectory()
|
||||||
option_string = ("lowerdir=%s,upperdir=%s,workdir=%s" %
|
option_string = ("lowerdir=%s,upperdir=%s,workdir=%s" %
|
||||||
|
|
@ -23,12 +28,9 @@ class OverlayfsMount():
|
||||||
subprocess.check_call(["mount", "-t", "overlay", "-o",
|
subprocess.check_call(["mount", "-t", "overlay", "-o",
|
||||||
option_string, "overlay", self.mount_point],
|
option_string, "overlay", self.mount_point],
|
||||||
stderr=subprocess.PIPE)
|
stderr=subprocess.PIPE)
|
||||||
|
return self
|
||||||
|
|
||||||
def __repr__(self):
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
return "<OverlayfsMount:%r over %r>" % (self.mount_point,
|
|
||||||
self.lower_dir)
|
|
||||||
|
|
||||||
def umount(self):
|
|
||||||
subprocess.check_call(["umount", "--lazy", self.mount_point])
|
subprocess.check_call(["umount", "--lazy", self.mount_point])
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -55,31 +57,34 @@ def _find_mounts():
|
||||||
|
|
||||||
class SandboxFs:
|
class SandboxFs:
|
||||||
|
|
||||||
def __init__(self, mount_point, holes=None):
|
def __init__(self, holes=None):
|
||||||
self.mount_point = mount_point
|
|
||||||
self.holes = [] if holes is None else holes
|
self.holes = [] if holes is None else holes
|
||||||
for hole in self.holes:
|
for hole in self.holes:
|
||||||
if not hole.startswith("/"):
|
if not hole.startswith("/"):
|
||||||
raise ValueError("Holes must be absolute paths: %r" % hole)
|
raise ValueError("Holes must be absolute paths: %r" % hole)
|
||||||
|
self.temp_dir = tempfile.TemporaryDirectory()
|
||||||
|
self.mount_point = self.temp_dir.name
|
||||||
self.overlay_mounts = []
|
self.overlay_mounts = []
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return ("<SandboxFs:%r mounts:%r>" %
|
return ("<SandboxFs:%r mounts:%r>" %
|
||||||
(self.mount_point, len(self.overlay_mounts)))
|
(self.temp_dir.name, len(self.overlay_mounts)))
|
||||||
|
|
||||||
def mount(self):
|
def __enter__(self):
|
||||||
self.overlay_mounts = [OverlayfsMount(mount_point,
|
self.overlay_mounts = [
|
||||||
self.mount_point + mount_point)
|
OverlayfsMount(mount_point,
|
||||||
for mount_point in sorted(_find_mounts())]
|
self.mount_point + mount_point).__enter__()
|
||||||
|
for mount_point in sorted(_find_mounts())]
|
||||||
for hole in self.holes:
|
for hole in self.holes:
|
||||||
subprocess.check_call(["mount", "--bind", hole,
|
subprocess.check_call(["mount", "--bind", hole,
|
||||||
self.mount_point + hole])
|
self.mount_point + hole])
|
||||||
|
return self
|
||||||
|
|
||||||
def umount(self):
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
for hole in reversed(self.holes):
|
for hole in reversed(self.holes):
|
||||||
subprocess.check_call(["umount", self.mount_point + hole])
|
subprocess.check_call(["umount", self.mount_point + hole])
|
||||||
for mount in reversed(self.overlay_mounts):
|
for mount in reversed(self.overlay_mounts):
|
||||||
mount.umount()
|
mount.__exit__(None, None, None)
|
||||||
self.overlay_mounts = []
|
self.overlay_mounts = []
|
||||||
|
|
||||||
def command(self, command, env=None):
|
def command(self, command, env=None):
|
||||||
|
|
@ -87,22 +92,11 @@ class SandboxFs:
|
||||||
_in_directory(os.getcwd(), command))
|
_in_directory(os.getcwd(), command))
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
|
||||||
def sandbox_(holes=None):
|
|
||||||
temp_dir = tempfile.TemporaryDirectory()
|
|
||||||
sandbox = SandboxFs(temp_dir.name, holes)
|
|
||||||
sandbox.mount()
|
|
||||||
try:
|
|
||||||
yield sandbox
|
|
||||||
finally:
|
|
||||||
sandbox.umount()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
divider_index = sys.argv.index("--")
|
divider_index = sys.argv.index("--")
|
||||||
holes, command = sys.argv[1:divider_index], sys.argv[divider_index+1:]
|
holes, command = sys.argv[1:divider_index], sys.argv[divider_index+1:]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
holes, command = None, sys.argv[1:]
|
holes, command = None, sys.argv[1:]
|
||||||
with sandbox_(holes) as sandbox:
|
with SandboxFs(holes) as sandbox:
|
||||||
subprocess.check_call(sandbox.command(command))
|
subprocess.check_call(sandbox.command(command))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue