Skip to content

Commit 9560e45

Browse files
authored
Merge pull request #480 from s22s/fix/459
Synchronize PEP 440 version with Maven/SBT
2 parents 7b65fed + 2c1b24d commit 9560e45

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

project/PythonBuildPlugin.scala

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@ object PythonBuildPlugin extends AutoPlugin {
3737
val pythonCommand = settingKey[String]("Python command. Defaults to 'python'")
3838
val pySetup = inputKey[Int]("Run 'python setup.py <args>'. Returns exit code.")
3939
val pyWhl = taskKey[File]("Builds the Python wheel distribution")
40+
val maven2PEP440: String => String = {
41+
case VersionNumber(numbers, tags, extras) =>
42+
if (numbers.isEmpty) throw new MessageOnlyException("Version string is not convertible to PEP440.")
43+
44+
// Reconstruct the primary version number
45+
val base = numbers.mkString(".")
46+
47+
// Process items after the `-`. Due to PEP 440 constraints, some tags get converted
48+
// to local version suffixes, while others map directly to prerelease suffixes.
49+
val rc = "^[Rr][Cc](\\d+)$".r
50+
val tag = tags match {
51+
case Seq("SNAPSHOT") => ".dev"
52+
case Seq(rc(num)) => ".rc" + num
53+
case Seq(other) => ".dev+" + other
54+
case many @ Seq(_, _) => ".dev+" + many.mkString(".")
55+
case _ => ""
56+
}
57+
58+
// sbt "extras" most closely map to PEP 440 local version suffixes.
59+
// The local version components are separated by `.`, preceded by a single `+`, and not multiple `+` as in sbt.
60+
// These next two expressions do the appropriate separator conversions while concatenating the components.
61+
val ssep = if (tag.contains("+")) "." else "+"
62+
val ext = if (extras.nonEmpty)
63+
extras.map(_.replaceAllLiterally("+", "")).mkString(ssep, ".", "")
64+
else ""
65+
66+
base + tag + ext
67+
}
4068
}
4169
import autoImport._
4270

@@ -97,7 +125,7 @@ object PythonBuildPlugin extends AutoPlugin {
97125
val wd = copyPySources.value
98126
val args = spaceDelimited("<args>").parsed
99127
val cmd = Seq(pythonCommand.value, "setup.py") ++ args
100-
val ver = version.value
128+
val ver = (Python / version).value
101129
s.log.info(s"Running '${cmd.mkString(" ")}' in '$wd'")
102130
val ec = Process(cmd, wd, "RASTERFRAMES_VERSION" -> ver).!
103131
if (ec != 0)
@@ -121,6 +149,7 @@ object PythonBuildPlugin extends AutoPlugin {
121149
inConfig(Python)(Seq(
122150
sourceDirectory := (Compile / sourceDirectory).value / "python",
123151
sourceDirectories := Seq((Python / sourceDirectory).value),
152+
version ~= maven2PEP440,
124153
target := (Compile / target).value / "python",
125154
includeFilter := "*",
126155
excludeFilter := HiddenFileFilter || "__pycache__" || "*.egg-info",

pyrasterframes/build.sbt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ pyNotebooks := {
2626
lazy val pySparkCmd = taskKey[Unit]("Create build and emit command to run in pyspark")
2727
pySparkCmd := {
2828
val s = streams.value
29-
val jvm = assembly.value
3029
val py = (Python / packageBin).value
3130
val script = IO.createTemporaryDirectory / "pyrf_init.py"
3231
IO.write(script, """
3332
import pyrasterframes
3433
from pyrasterframes.rasterfunctions import *
3534
""")
36-
val msg = s"PYTHONSTARTUP=$script pyspark --jars $jvm --py-files $py"
35+
val msg = s"PYTHONSTARTUP=$script pyspark --py-files $py"
3736
s.log.debug(msg)
3837
println(msg)
3938
}

pyrasterframes/src/main/python/setup.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@
2020

2121
# Always prefer setuptools over distutils
2222
from setuptools import setup
23-
from os import path
23+
from os import path, environ, mkdir
2424
import sys
2525
from glob import glob
2626
from io import open
2727
import distutils.cmd
2828

2929
try:
30+
enver = environ.get('RASTERFRAMES_VERSION')
31+
if enver is not None:
32+
open('pyrasterframes/version.py', mode="w").write(f"__version__: str = '{enver}'\n")
3033
exec(open('pyrasterframes/version.py').read()) # executable python script contains __version__; credit pyspark
31-
except IOError:
32-
print("Run setup via `sbt 'pySetup arg1 arg2'` to ensure correct access to all source files and binaries.")
34+
except IOError as e:
35+
print(e)
36+
print("Try running setup via `sbt 'pySetup arg1 arg2'` to ensure correct access to all source files and binaries.")
3337
sys.exit(-1)
3438

35-
3639
VERSION = __version__
3740

3841
here = path.abspath(path.dirname(__file__))

0 commit comments

Comments
 (0)