Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.in_wheel = abspath(in_wheel)
self.out_wheel = None if out_wheel is None else abspath(out_wheel)
super(InWheel, self).__init__()
def __enter__(self):
zip2dir(self.in_wheel, self.name)
return super(InWheel, self).__enter__()
def __exit__(self, exc, value, tb):
if self.out_wheel is not None:
rewrite_record(self.name)
dir2zip(self.name, self.out_wheel)
return super(InWheel, self).__exit__(exc, value, tb)
class InWheelCtx(InWheel):
""" Context manager for doing things inside wheels
On entering, you'll find yourself in the root tree of the wheel. If you've
asked for an output wheel, then on exit we'll rewrite the wheel record and
pack stuff up for you.
The context manager returns itself from the __enter__ method, so you can
set things like ``out_wheel``. This is useful when processing in the wheel
will dicate what the output wheel name is, or whether you want to save at
all.
The current path of the wheel contents is set in the attribute
``wheel_path``.
"""
def __init__(self, in_wheel, out_wheel=None):
""" Init in-wheel context manager returning self from enter
Filename of wheel to process
patch_fname : str
Filename of patch file. Will be applied with ``patch -p1 <
patch_fname``
out_wheel : None or str
Filename of patched wheel to write. If None, overwrite `in_wheel`
"""
in_wheel = abspath(in_wheel)
patch_fname = abspath(patch_fname)
if out_wheel is None:
out_wheel = in_wheel
else:
out_wheel = abspath(out_wheel)
if not exists(patch_fname):
raise ValueError("patch file {0} does not exist".format(patch_fname))
with InWheel(in_wheel, out_wheel):
with open(patch_fname, 'rb') as fobj:
patch_proc = Popen(['patch', '-p1'],
stdin=fobj,
stdout=PIPE,
stderr=PIPE)
stdout, stderr = patch_proc.communicate()
if patch_proc.returncode != 0:
raise RuntimeError("Patch failed with stdout:\n" +
stdout.decode('latin1'))
""" Initialize in-wheel context manager
Parameters
----------
in_wheel : str
filename of wheel to unpack and work inside
out_wheel : None or str:
filename of wheel to write after exiting. If None, don't write and
discard
ret_self : bool, optional
If True, return ``self`` from ``__enter__``, otherwise return the
directory path.
"""
self.in_wheel = abspath(in_wheel)
self.out_wheel = None if out_wheel is None else abspath(out_wheel)
super(InWheel, self).__init__()