Skip to content

Commit

Permalink
conda: get rid of effectively dead bundling code
Browse files Browse the repository at this point in the history
Unbundling is the way to go with conda, so I figured it would be good to
remove the effectively dead code, which is more bloated/confusing than
useful.
  • Loading branch information
mara004 committed Dec 8, 2023
1 parent 816f421 commit 4c0aee6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 201 deletions.
74 changes: 0 additions & 74 deletions conda/bundle/recipe/meta.yaml

This file was deleted.

180 changes: 53 additions & 127 deletions setupsrc/pypdfium2_setup/craft_packages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: 2023 geisserml <[email protected]>
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause

# TODO split in pypi and conda?

import os
import sys
import shutil
Expand All @@ -16,11 +18,9 @@


P_PYPI = "pypi"
P_CONDA_BUNDLE = "conda_bundle"
P_CONDA_RAW = "conda_raw"
P_CONDA_HELPERS = "conda_helpers"


def parse_args():

root_parser = argparse.ArgumentParser(
Expand All @@ -32,24 +32,12 @@ def parse_args():
)
subparsers = root_parser.add_subparsers(dest="parser")
pypi = subparsers.add_parser(P_PYPI)
pypi.add_argument(
"--use-v8",
action = "store_true",
)
conda_raw = subparsers.add_parser(P_CONDA_RAW)
conda_helpers = subparsers.add_parser(P_CONDA_HELPERS)
conda_bundle = subparsers.add_parser(P_CONDA_BUNDLE)
conda_bundle.add_argument(
"--platforms",
nargs = "+",
default = [Host.platform],
)
conda_bundle.add_argument(
"--py-variants",
nargs = "*",
)

for p in (pypi, conda_bundle):
p.add_argument(
"--use-v8",
action = "store_true",
)

args = root_parser.parse_args()
args.is_literal_latest = args.pdfium_ver == "latest"
Expand All @@ -62,114 +50,48 @@ def parse_args():
args.pdfium_ver = PdfiumVer.get_latest()
else:
args.pdfium_ver = int(args.pdfium_ver)
if args.parser == P_CONDA_BUNDLE:
if args.platforms and args.platforms[0] == "all":
args.platforms = list(CondaNames.keys())
if args.py_variants and args.py_variants[0] == "all":
args.py_variants = ["3.8", "3.9", "3.10", "3.11", "3.12"]

return args


def run_pypi_build(args):
def main():

args = parse_args()

with ArtifactStash():
if args.parser == P_PYPI:
main_pypi(args)
elif args.parser == P_CONDA_RAW:
main_conda_raw(args)
elif args.parser == P_CONDA_HELPERS:
helpers_info = parse_git_tag()
os.environ["M_HELPERS_VER"] = merge_tag(helpers_info, "py")
main_conda_helpers(args)
else:
assert False


def _run_pypi_build(args):
run_cmd([sys.executable, "-m", "build", "--skip-dependency-check", "--no-isolation"] + args, cwd=ProjectDir, env=os.environ)


def main_pypi(args):

os.environ[PlatSpec_EnvVar] = ExtPlats.none
run_pypi_build(["--sdist"])
_run_pypi_build(["--sdist"])

suffix = build_pl_suffix(args.pdfium_ver, args.use_v8)
for plat in ReleaseNames.keys():
os.environ[PlatSpec_EnvVar] = plat + suffix
run_pypi_build(["--wheel"])
_run_pypi_build(["--wheel"])
clean_platfiles()


class ArtifactStash:

# Preserve in-tree aftefacts from editable install

def __enter__(self):

self.tmpdir = None

file_names = [VersionFN, BindingsFN, LibnameForSystem[Host.system]]
self.files = [fp for fp in [ModuleDir_Raw / fn for fn in file_names] if fp.exists()]
if len(self.files) == 0:
return

self.tmpdir = tempfile.TemporaryDirectory(prefix="pypdfium2_artifact_stash_")
self.tmpdir_path = Path(self.tmpdir.name)
for fp in self.files:
shutil.move(fp, self.tmpdir_path)

def __exit__(self, *_):
if self.tmpdir is None:
return
for fp in self.files:
shutil.move(self.tmpdir_path / fp.name, ModuleDir_Raw)
self.tmpdir.cleanup()


def run_conda_build(recipe_dir, out_dir, args=[]):
with TmpCommitCtx():
run_cmd(["conda", "build", recipe_dir, "--output-folder", out_dir, *args], cwd=ProjectDir, env=os.environ)


CondaNames = {
# NOTE looks like conda doesn't support musllinux yet ...
PlatNames.darwin_x64 : "osx-64",
PlatNames.darwin_arm64 : "osx-arm64",
PlatNames.linux_x64 : "linux-64",
PlatNames.linux_x86 : "linux-32",
PlatNames.linux_arm64 : "linux-aarch64",
PlatNames.linux_arm32 : "linux-armv7l",
PlatNames.windows_x64 : "win-64",
PlatNames.windows_x86 : "win-32",
PlatNames.windows_arm64 : "win-arm64",
}

def _run_conda_bundle(args, pl_name, suffix, conda_args):

os.environ["IN_"+PlatSpec_EnvVar] = pl_name + suffix
emplace_func = partial(prepare_setup, pl_name, args.pdfium_ver, args.use_v8)

with CondaExtPlatfiles(emplace_func):
run_conda_build(CondaDir/"bundle", CondaDir/"bundle"/"out", conda_args)


def main_conda_bundle(args):

helpers_ver = merge_tag(parse_git_tag(), "py")
os.environ["VERSION"] = helpers_ver

platforms = args.platforms.copy()
with_host = Host.platform in platforms
if with_host:
platforms.remove(Host.platform)
conda_host = CondaNames[Host.platform]
host_files = None

conda_args = []
if args.py_variants:
conda_args += ["--variants", "{python: %s}" % (args.py_variants, )]
suffix = build_pl_suffix(args.pdfium_ver, args.use_v8)

for pl_name in platforms:

_run_conda_bundle(args, pl_name, suffix, [*conda_args, "--no-test"])
if not host_files:
host_files = list((CondaDir/"bundle"/"out"/conda_host).glob(f"pypdfium2_bundle-{helpers_ver}-*.tar.bz2"))

run_cmd(["conda", "convert", "-f", *host_files, "-p", CondaNames[pl_name], "-o", CondaDir/"bundle"/"out"], cwd=ProjectDir, env=os.environ)
for hf in host_files:
hf.unlink()

if with_host:
_run_conda_bundle(args, Host.platform, suffix, conda_args)


def _get_build_num(args):

# parse existing releases to automatically handle arbitrary version builds
Expand Down Expand Up @@ -207,6 +129,32 @@ def main_conda_helpers(args):
run_conda_build(CondaDir/"helpers", CondaDir/"helpers"/"out", args=["--override-channels", "-c", "pypdfium2-team", "-c", "bblanchon"])


class ArtifactStash:

# Preserve in-tree aftefacts from editable install

def __enter__(self):

self.tmpdir = None

file_names = [VersionFN, BindingsFN, LibnameForSystem[Host.system]]
self.files = [fp for fp in [ModuleDir_Raw / fn for fn in file_names] if fp.exists()]
if len(self.files) == 0:
return

self.tmpdir = tempfile.TemporaryDirectory(prefix="pypdfium2_artifact_stash_")
self.tmpdir_path = Path(self.tmpdir.name)
for fp in self.files:
shutil.move(fp, self.tmpdir_path)

def __exit__(self, *_):
if self.tmpdir is None:
return
for fp in self.files:
shutil.move(self.tmpdir_path / fp.name, ModuleDir_Raw)
self.tmpdir.cleanup()


class TmpCommitCtx:

# Work around local conda `git_url` not including uncommitted changes
Expand Down Expand Up @@ -252,27 +200,5 @@ def __exit__(self, *_):
fp.unlink()


def main():

args = parse_args()

with ArtifactStash():
if args.parser == P_PYPI:
main_pypi(args)
elif args.parser.startswith("conda"):
helpers_info = parse_git_tag()
os.environ["M_HELPERS_VER"] = merge_tag(helpers_info, "py")
if args.parser == P_CONDA_BUNDLE:
main_conda_bundle(args)
elif args.parser == P_CONDA_RAW:
main_conda_raw(args)
elif args.parser == P_CONDA_HELPERS:
main_conda_helpers(args)
else:
assert False
else:
assert False


if __name__ == '__main__':
main()

0 comments on commit 4c0aee6

Please sign in to comment.