Skip to content

Commit

Permalink
Clean up version conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Oct 29, 2023
1 parent 46ca27b commit bc63789
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion setupsrc/pypdfium2_setup/craft_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def main_conda_bundle(args):

def main_conda_raw(args):
os.environ["PDFIUM_SHORT"] = str(args.pdfium_ver)
os.environ["PDFIUM_FULL"] = PdfiumVer.to_full(args.pdfium_ver, origin="pdfium-binaries", as_str=True)
os.environ["PDFIUM_FULL"] = PdfiumVer.to_full(args.pdfium_ver, type=str)
emplace_func = partial(prepare_setup, ExtPlats.system, args.pdfium_ver, use_v8=None)
with CondaExtPlatfiles(emplace_func):
run_conda_build(CondaDir/"raw", CondaDir/"raw"/"out")
Expand Down
48 changes: 27 additions & 21 deletions setupsrc/pypdfium2_setup/packaging_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,36 @@ def get_latest():
return int( tag.split("/")[-1] )

@staticmethod
def to_full(v_short, origin, as_str=False):

# TODO determine from chromium tags instead. This would allow us to handle system and sourcebuild properly
@functools.lru_cache(maxsize=1)
def _get_version_conv():

if origin == "sourcebuild":
# For sourcebuild, we don't actually set the full version (retrieving from chromium not implemented yet). Also note v_short may be a commit hash if building from an untagged commit.
# FIXME as_str not implemented
v_parts = (None, None, v_short, None)
ChromiumURL = "https://chromium.googlesource.com/chromium/src"
refs_txt = run_cmd(["git", "ls-remote", "--sort", "-version:refname", "--tags", ChromiumURL, '*.*.*.0'], cwd=None, capture=True)

else:
info = url_request.urlopen(f"{ReleaseInfoURL}{v_short}").read().decode("utf-8")
info = json.loads(info)
title = info["name"]
match = re.match(rf"PDFium (\d+.\d+.{v_short}.\d+)", title)
v_string = match.group(1)
if as_str:
return v_string
v_parts = [int(v) for v in v_string.split(".")]
v_short = int(v_short)
# FIXME parses too much - we'd only need to read until we find a given v_short and then deepen on demand on further calls
to_full = {}
for ref in refs_txt.split("\n"):
ref = ref.split("\t")[-1].rsplit("/", maxsplit=1)[-1]
major, minor, build, patch = [int(v) for v in ref.split(".")]
to_full[build] = (major, minor, build, patch)
return to_full

@staticmethod
def to_full(v_short, type=dict): # XXX origin

v_info = dict(zip(PdfiumVer.V_KEYS, v_parts))
assert v_info["build"] == v_short
v_short = int(v_short)
ver_dict = PdfiumVer._get_version_conv()
v_parts = ver_dict[v_short]
assert v_parts[2] == v_short

return v_info
if type in (tuple, list):
return v_parts
elif type is str:
return v_parts.join(".")
elif type is dict:
return dict(zip(PdfiumVer.V_KEYS, v_parts))
else:
assert False


def read_json(fp):
Expand All @@ -162,7 +168,7 @@ def write_json(fp, data, indent=2):


def write_pdfium_info(dir, build, origin, flags=[], n_commits=0, hash=None):
info = dict(**PdfiumVer.to_full(build, origin), n_commits=n_commits, hash=hash, origin=origin, flags=flags)
info = dict(**PdfiumVer.to_full(build, type=dict), n_commits=n_commits, hash=hash, origin=origin, flags=flags)
write_json(dir/VersionFN, info)
return info

Expand Down

0 comments on commit bc63789

Please sign in to comment.