Skip to content

Commit

Permalink
Continue addressing setup code tasks
Browse files Browse the repository at this point in the history
* Added shared variable for sdist setup target (i. e. the code passage
that should run if we want to package an sdist)
* Replaced `AllPlatNames` with `BinaryPlatforms`, which is overall more
elegant
* Simplified update_pdfium code by using argparse choices and
`BinaryPlatforms`
* Adapted test code
  • Loading branch information
mara004 committed Aug 26, 2022
1 parent 7d5cd9c commit 973efe7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 50 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
PlatformNames,
SourceTree,
SetupTargetVar,
SdistTarget,
)

StatusFile = join(SourceTree, "data", ".presetup_done.txt")
Expand Down Expand Up @@ -57,7 +58,7 @@ def packaging_handler(target):

from pl_setup.setup_base import mkwheel, SetupKws

if target == "sdist":
if target == SdistTarget:
setuptools.setup(**SetupKws)
elif hasattr(PlatformNames, target):
mkwheel( getattr(PlatformNames, target) )
Expand Down
12 changes: 4 additions & 8 deletions setupsrc/pl_setup/craft_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from pl_setup.packaging_base import (
run_cmd,
clean_artefacts,
PlatformNames,
AllPlatNames,
BinaryPlatforms,
SourceTree,
SetupTargetVar,
SdistTarget,
)


Expand All @@ -24,22 +24,18 @@ def _run_build(args):
def main():

# TODO restore in-tree artefacts from editable install
# TODO use shared variable for sdist setup target

parser = argparse.ArgumentParser(
description = "Craft sdist and wheels for pypdfium2, using `python3 -m build`. (This script does not take any arguments.)",
)
args = parser.parse_args()

pl_names = AllPlatNames.copy()
pl_names.remove(PlatformNames.sourcebuild)

clean_artefacts()
os.environ[SetupTargetVar] = "sdist"
os.environ[SetupTargetVar] = SdistTarget
_run_build(["--sdist"])
clean_artefacts()

for plat in pl_names:
for plat in BinaryPlatforms:
os.environ[SetupTargetVar] = plat
_run_build(["--wheel"])
clean_artefacts()
Expand Down
18 changes: 5 additions & 13 deletions setupsrc/pl_setup/packaging_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
expanduser,
)

# TODO move shared components to this file, move local components back to their only users

# TODO think about variables to move in/out
# TODO improve consistency of variable names

HomeDir = expanduser("~")
SourceTree = dirname(dirname(dirname(abspath(__file__))))
DataTree = join(SourceTree, "data")
Expand All @@ -29,6 +29,7 @@
Changelog = join(SourceTree, "docs", "source", "changelog.md")
ChangelogStaging = join(SourceTree, "docs", "devel", "changelog_staging.md")
SetupTargetVar = "PYP_TARGET_PLATFORM"
SdistTarget = "sdist"
RepositoryURL = "https://github.com/pypdfium2-team/pypdfium2"
PDFium_URL = "https://pdfium.googlesource.com/pdfium"
DepotTools_URL = "https://chromium.googlesource.com/chromium/tools/depot_tools.git"
Expand Down Expand Up @@ -77,6 +78,8 @@ class PlatformNames:
PlatformNames.windows_arm64 : "pdfium-win-arm64",
}

BinaryPlatforms = list(ReleaseNames.keys())


class HostPlatform:

Expand Down Expand Up @@ -189,14 +192,3 @@ def set_version(variable, new_ver):

with open(VersionFile, "w") as fh:
fh.write(content)


def get_members(cls):
members = []
for attr in dir(cls):
if attr.startswith("_"):
continue
members.append( getattr(cls, attr) )
return members

AllPlatNames = get_members(PlatformNames)
20 changes: 7 additions & 13 deletions setupsrc/pl_setup/update_pdfium.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
DataTree,
VerNamespace,
ReleaseNames,
BinaryPlatforms,
ReleaseURL,
set_version,
get_latest_version,
Expand Down Expand Up @@ -117,18 +118,9 @@ def generate_bindings(Ctypesgen, archives):


def get_download_files(platforms):

avail_keys = [k for k in ReleaseNames.keys()]
if platforms is None:
platforms = avail_keys

download_files = {}
for pl_name in platforms:
if pl_name in ReleaseNames:
download_files[ join(DataTree, pl_name) ] = ReleaseNames[pl_name]
else:
raise ValueError("Unknown platform name '%s'. Available keys are %s." % (pl_name, avail_keys))

download_files[ join(DataTree, pl_name) ] = ReleaseNames[pl_name]
return download_files


Expand All @@ -150,12 +142,14 @@ def main(platforms):

def parse_args(argv):
parser = argparse.ArgumentParser(
description = "Download pre-built PDFium packages and generate bindings",
description = "Download pre-built PDFium packages and generate bindings. Available platform identifiers: %s" % BinaryPlatforms,
)
parser.add_argument(
"--platforms", "-p",
metavar = "P",
nargs = "*",
metavar = "identifier",
choices = BinaryPlatforms,
default = BinaryPlatforms,
nargs = "+",
)
return parser.parse_args(argv)

Expand Down
2 changes: 1 addition & 1 deletion src/pypdfium2/_cli/find_pageobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def attach_parser(subparsers):
"--types",
nargs = "+",
required = True,
choices = [k for k in ObjtypeToConst.keys()],
choices = list(ObjtypeToConst.keys()),
help = "Object types to consider",
)

Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def iterate_testfiles(skip_encrypted=True):
yield member


def get_members(cls):
members = []
for attr in dir(cls):
if attr.startswith("_"):
continue
members.append( getattr(cls, attr) )
return members


def test_testpaths():
for dirpath in (TestDir, SourceTree, ResourceDir, OutputDir):
assert isdir(dirpath)
Expand Down
8 changes: 6 additions & 2 deletions tests/helpers/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
from os.path import join
import pytest
import pypdfium2 as pdfium
from ..conftest import TestFiles, OutputDir, ExpRenderPixels
from pl_setup.packaging_base import get_members
from ..conftest import (
get_members,
TestFiles,
OutputDir,
ExpRenderPixels
)


@pytest.fixture
Expand Down
33 changes: 21 additions & 12 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
setup_base,
packaging_base as pkg_base,
)
from pl_setup import update_pdfium as fpdf_up
from pl_setup.packaging_base import PlatformNames, AllPlatNames
from .conftest import SourceTree
from pl_setup.packaging_base import (
PlatformNames,
BinaryPlatforms,
ReleaseNames,
)
from .conftest import SourceTree, get_members


@pytest.fixture
def all_platnames():
return list( get_members(PlatformNames) )


# module
Expand Down Expand Up @@ -55,8 +63,8 @@ def test_entrypoint():
)


def test_expected_tags():
assert len(AllPlatNames) == len(ExpectedTags)
def test_expected_tags(all_platnames):
assert len(all_platnames) == len(ExpectedTags)
for platform, tag in ExpectedTags:
assert hasattr(PlatformNames, platform)
assert isinstance(tag, str)
Expand All @@ -72,7 +80,7 @@ def test_unknown_tag():
setup_base._get_tag(plat_dir)

def test_get_bdist():
for platform, tag in ExpectedTags:
for platform, _ in ExpectedTags:
bdist_cls = setup_base._get_bdist(platform)
assert issubclass(bdist_cls, bdist_wheel)

Expand All @@ -83,10 +91,10 @@ def test_libnames():
for name in pkg_base.Libnames:
assert "pdfium" in name

def test_platformnames():
def test_platformnames(all_platnames):
# make sure variable names and values are identical
for name in AllPlatNames:
assert name == getattr(pkg_base.PlatformNames, name)
for name in all_platnames:
assert name == getattr(PlatformNames, name)

def test_paths():
assert pkg_base.HomeDir == str( Path.home() )
Expand All @@ -99,9 +107,10 @@ def test_paths():

# update_pdfium

def test_releasenames():
assert len(fpdf_up.ReleaseNames) == len(AllPlatNames)-1
for key, value in fpdf_up.ReleaseNames.items():
def test_releasenames(all_platnames):
assert len(ReleaseNames) == len(BinaryPlatforms) == len(all_platnames) - 1
for key, value in ReleaseNames.items():
assert key in BinaryPlatforms
assert hasattr(PlatformNames, key)
prefix, system, cpu = value.replace("linux-musl", "musllinux").split("-", maxsplit=3)
assert prefix == "pdfium"
Expand Down

0 comments on commit 973efe7

Please sign in to comment.