Commit 9e15fdf2 by Andrey Torsunov Committed by Sébastien Eustace

GH-250 - fix building of wheel (#252)

parent 6b677d24
...@@ -33,61 +33,46 @@ Tag: {tag} ...@@ -33,61 +33,46 @@ Tag: {tag}
class WheelBuilder(Builder): class WheelBuilder(Builder):
def __init__(self, poetry, venv, io, target_fp, original=None): def __init__(self, poetry, venv, io, target_dir=None, original=None):
super(WheelBuilder, self).__init__(poetry, venv, io) super(WheelBuilder, self).__init__(poetry, venv, io)
self._records = [] self._records = []
self._original_path = self._path self._original_path = self._path
self._target_dir = target_dir or (self._poetry.file.parent / "dist")
if original: if original:
self._original_path = original.file.parent self._original_path = original.file.parent
# Open the zip file ready to write
self._wheel_zip = zipfile.ZipFile(
target_fp, "w", compression=zipfile.ZIP_DEFLATED
)
@classmethod @classmethod
def make_in(cls, poetry, venv, io, directory, original=None): def make_in(cls, poetry, venv, io, directory=None, original=None):
# We don't know the final filename until metadata is loaded, so write to wb = WheelBuilder(poetry, venv, io, target_dir=directory, original=original)
# a temporary_file, and rename it afterwards. wb.build()
(fd, temp_path) = tempfile.mkstemp(suffix=".whl", dir=str(directory))
os.close(fd)
try:
with open(temp_path, "w+b") as fp:
wb = WheelBuilder(poetry, venv, io, fp, original=original)
wb.build()
wheel_path = directory / wb.wheel_filename
if wheel_path.exists():
os.unlink(str(wheel_path))
os.rename(temp_path, str(wheel_path))
except:
os.unlink(temp_path)
raise
@classmethod @classmethod
def make(cls, poetry, venv, io): def make(cls, poetry, venv, io):
"""Build a wheel in the dist/ directory, and optionally upload it. """Build a wheel in the dist/ directory, and optionally upload it."""
""" cls.make_in(poetry, venv, io)
dist_dir = poetry.file.parent / "dist"
try:
dist_dir.mkdir()
except FileExistsError:
pass
cls.make_in(poetry, venv, io, dist_dir)
def build(self): def build(self):
self._io.writeln(" - Building <info>wheel</info>") self._io.writeln(" - Building <info>wheel</info>")
try:
dist_dir = self._target_dir
if not dist_dir.exists():
dist_dir.mkdir()
(fd, temp_path) = tempfile.mkstemp(suffix=".whl")
with zipfile.ZipFile(
os.fdopen(fd, "w+b"), mode="w", compression=zipfile.ZIP_DEFLATED
) as zip_file:
self._build() self._build()
self.copy_module() self._copy_module(zip_file)
self.write_metadata() self._write_metadata(zip_file)
self.write_record() self._write_record(zip_file)
finally:
self._wheel_zip.close() wheel_path = dist_dir / self.wheel_filename
if wheel_path.exists():
wheel_path.unlink()
shutil.move(temp_path, str(wheel_path))
self._io.writeln(" - Built <fg=cyan>{}</>".format(self.wheel_filename)) self._io.writeln(" - Built <fg=cyan>{}</>".format(self.wheel_filename))
...@@ -119,7 +104,7 @@ class WheelBuilder(Builder): ...@@ -119,7 +104,7 @@ class WheelBuilder(Builder):
shutil.rmtree(str(self._path / pkg.name)) shutil.rmtree(str(self._path / pkg.name))
shutil.copytree(str(pkg), str(self._path / pkg.name)) shutil.copytree(str(pkg), str(self._path / pkg.name))
def copy_module(self): def _copy_module(self, wheel):
if self._module.is_package(): if self._module.is_package():
files = self.find_files_to_add() files = self.find_files_to_add()
...@@ -140,31 +125,31 @@ class WheelBuilder(Builder): ...@@ -140,31 +125,31 @@ class WheelBuilder(Builder):
if full_path.relative_to(self._path) == Path(file.name): if full_path.relative_to(self._path) == Path(file.name):
continue continue
self._add_file(full_path, file) self._add_file(wheel, full_path, file)
else: else:
self._add_file(str(self._module.path), self._module.path.name) self._add_file(wheel, str(self._module.path), self._module.path.name)
def write_metadata(self): def _write_metadata(self, wheel):
if ( if (
"scripts" in self._poetry.local_config "scripts" in self._poetry.local_config
or "plugins" in self._poetry.local_config or "plugins" in self._poetry.local_config
): ):
with self._write_to_zip(self.dist_info + "/entry_points.txt") as f: with self._write_to_zip(wheel, self.dist_info + "/entry_points.txt") as f:
self._write_entry_points(f) self._write_entry_points(f)
for base in ("COPYING", "LICENSE"): for base in ("COPYING", "LICENSE"):
for path in sorted(self._path.glob(base + "*")): for path in sorted(self._path.glob(base + "*")):
self._add_file(path, "%s/%s" % (self.dist_info, path.name)) self._add_file(wheel, path, "%s/%s" % (self.dist_info, path.name))
with self._write_to_zip(self.dist_info + "/WHEEL") as f: with self._write_to_zip(wheel, self.dist_info + "/WHEEL") as f:
self._write_wheel_file(f) self._write_wheel_file(f)
with self._write_to_zip(self.dist_info + "/METADATA") as f: with self._write_to_zip(wheel, self.dist_info + "/METADATA") as f:
self._write_metadata_file(f) self._write_metadata_file(f)
def write_record(self): def _write_record(self, wheel):
# Write a record of the files in the wheel # Write a record of the files in the wheel
with self._write_to_zip(self.dist_info + "/RECORD") as f: with self._write_to_zip(wheel, self.dist_info + "/RECORD") as f:
for path, hash, size in self._records: for path, hash, size in self._records:
f.write("{},sha256={},{}\n".format(path, hash, size)) f.write("{},sha256={},{}\n".format(path, hash, size))
# RECORD itself is recorded with no hash or size # RECORD itself is recorded with no hash or size
...@@ -217,7 +202,7 @@ class WheelBuilder(Builder): ...@@ -217,7 +202,7 @@ class WheelBuilder(Builder):
return "-".join(tag) return "-".join(tag)
def _add_file(self, full_path, rel_path): def _add_file(self, wheel, full_path, rel_path):
full_path, rel_path = str(full_path), str(rel_path) full_path, rel_path = str(full_path), str(rel_path)
if os.sep != "/": if os.sep != "/":
# We always want to have /-separated paths in the zip file and in # We always want to have /-separated paths in the zip file and in
...@@ -243,7 +228,7 @@ class WheelBuilder(Builder): ...@@ -243,7 +228,7 @@ class WheelBuilder(Builder):
hashsum.update(buf) hashsum.update(buf)
src.seek(0) src.seek(0)
self._wheel_zip.writestr(zinfo, src.read()) wheel.writestr(zinfo, src.read())
size = os.stat(full_path).st_size size = os.stat(full_path).st_size
hash_digest = urlsafe_b64encode(hashsum.digest()).decode("ascii").rstrip("=") hash_digest = urlsafe_b64encode(hashsum.digest()).decode("ascii").rstrip("=")
...@@ -251,7 +236,7 @@ class WheelBuilder(Builder): ...@@ -251,7 +236,7 @@ class WheelBuilder(Builder):
self._records.append((rel_path, hash_digest, size)) self._records.append((rel_path, hash_digest, size))
@contextlib.contextmanager @contextlib.contextmanager
def _write_to_zip(self, rel_path): def _write_to_zip(self, wheel, rel_path):
sio = StringIO() sio = StringIO()
yield sio yield sio
...@@ -264,7 +249,7 @@ class WheelBuilder(Builder): ...@@ -264,7 +249,7 @@ class WheelBuilder(Builder):
hashsum = hashlib.sha256(b) hashsum = hashlib.sha256(b)
hash_digest = urlsafe_b64encode(hashsum.digest()).decode("ascii").rstrip("=") hash_digest = urlsafe_b64encode(hashsum.digest()).decode("ascii").rstrip("=")
self._wheel_zip.writestr(zi, b, compress_type=zipfile.ZIP_DEFLATED) wheel.writestr(zi, b, compress_type=zipfile.ZIP_DEFLATED)
self._records.append((rel_path, hash_digest, len(b))) self._records.append((rel_path, hash_digest, len(b)))
def _write_entry_points(self, fp): def _write_entry_points(self, fp):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment