Skip to content

Commit ce35553

Browse files
Step for python version management in strategy
1 parent 9afce50 commit ce35553

File tree

2 files changed

+108
-17
lines changed

2 files changed

+108
-17
lines changed

pkg/rebuild/pypi/strategy.go

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ import (
1414
// PureWheelBuild aggregates the options controlling a wheel build.
1515
type PureWheelBuild struct {
1616
rebuild.Location
17-
Requirements []string `json:"requirements"`
18-
RegistryTime time.Time `json:"registry_time" yaml:"registry_time,omitempty"`
17+
Requirements []string `json:"requirements"`
18+
PythonVersion string `json:"python_version"`
19+
RegistryTime time.Time `json:"registry_time" yaml:"registry_time,omitempty"`
20+
}
21+
22+
type SourceDistributionBuild struct {
23+
rebuild.Location
24+
Requirements []string `json:"requirements"`
25+
PythonVersion string `json:"python_version"`
26+
RegistryTime time.Time `json:"registry_time" yaml:"registry_time,omitempty"`
1927
}
2028

2129
var _ rebuild.Strategy = &PureWheelBuild{}
@@ -33,16 +41,17 @@ func (b *PureWheelBuild) ToWorkflow() *rebuild.WorkflowStrategy {
3341
Deps: []flow.Step{{
3442
Uses: "pypi/deps/basic",
3543
With: map[string]string{
36-
"registryTime": registryTime,
37-
"requirements": flow.MustToJSON(b.Requirements),
38-
"venv": "/deps",
44+
"registryTime": registryTime,
45+
"requirements": flow.MustToJSON(b.Requirements),
46+
"pythonVersion": b.PythonVersion,
47+
"venv": "deps",
3948
},
4049
}},
4150
Build: []flow.Step{{
4251
Uses: "pypi/build/wheel",
4352
With: map[string]string{
4453
"dir": b.Location.Dir,
45-
"locator": "/deps/bin/",
54+
"locator": "deps/bin/",
4655
},
4756
}},
4857
OutputDir: "dist",
@@ -54,15 +63,75 @@ func (b *PureWheelBuild) GenerateFor(t rebuild.Target, be rebuild.BuildEnv) (reb
5463
return b.ToWorkflow().GenerateFor(t, be)
5564
}
5665

66+
func (b *SourceDistributionBuild) ToWorkflow() *rebuild.WorkflowStrategy {
67+
var registryTime string
68+
if !b.RegistryTime.IsZero() {
69+
registryTime = b.RegistryTime.Format(time.RFC3339)
70+
}
71+
return &rebuild.WorkflowStrategy{
72+
Location: b.Location,
73+
Source: []flow.Step{{
74+
Uses: "git-checkout",
75+
}},
76+
Deps: []flow.Step{{
77+
Uses: "pypi/deps/basic",
78+
With: map[string]string{
79+
"registryTime": registryTime,
80+
"requirements": flow.MustToJSON(b.Requirements),
81+
"pythonVersion": b.PythonVersion,
82+
"venv": "deps",
83+
},
84+
}},
85+
Build: []flow.Step{{
86+
Uses: "pypi/build/sdist",
87+
With: map[string]string{
88+
"dir": b.Location.Dir,
89+
"locator": "deps/bin/",
90+
},
91+
}},
92+
OutputDir: "dist",
93+
}
94+
}
95+
96+
func (b *SourceDistributionBuild) GenerateFor(t rebuild.Target, be rebuild.BuildEnv) (rebuild.Instructions, error) {
97+
return b.ToWorkflow().GenerateFor(t, be)
98+
}
99+
57100
func init() {
58101
for _, t := range toolkit {
59102
flow.Tools.MustRegister(t)
60103
}
61104
}
62105

106+
// if ! command -v pyenv &> /dev/null; then
107+
//
108+
// curl https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
109+
// export PATH="/root/.pyenv/bin:$PATH"
110+
// eval "$(pyenv init -)"
111+
// eval "$(pyenv virtualenv-init -)"
112+
// fi
113+
//
114+
// /root/.pyenv/bin/pyenv global {{.With.version}}`)[1:],
115+
// "bash", "clang", "curl", "build-base", "patch", "zip", "zlib-dev", "libffi-dev", "linux-headers", "readline-dev", "openssl", "openssl-dev", "sqlite-dev", "bzip2-dev"
63116
// Base tools for individual operations
64117
var toolkit = []*flow.Tool{
118+
// Use clang to avoid issues with openssl
119+
// Python versions before 3.6 cannot be compiled with openssl 3.x, they require 1.0.x
65120
{
121+
Name: "pypi/setup-python",
122+
Steps: []flow.Step{{
123+
Runs: textwrap.Dedent(`
124+
if [ ! -d "/root/.pyenv" ]; then
125+
if ! command -v curl &> /dev/null; then
126+
apk add clang curl build-base patch zip zlib-dev libffi-dev linux-headers readline-dev openssl openssl-dev sqlite-dev bzip2-dev xz-dev
127+
fi
128+
curl https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
129+
fi
130+
CC=clang /root/.pyenv/bin/pyenv install -s {{.With.version}}`)[1:],
131+
Needs: []string{"bash", "clang", "curl", "build-base", "patch", "zip", "zlib-dev", "libffi-dev", "linux-headers", "readline-dev", "openssl", "openssl-dev", "sqlite-dev", "bzip2-dev"},
132+
}},
133+
}, {
134+
66135
Name: "pypi/setup-venv",
67136
Steps: []flow.Step{{
68137
Runs: textwrap.Dedent(`
@@ -95,10 +164,17 @@ var toolkit = []*flow.Tool{
95164
{
96165
Name: "pypi/deps/basic",
97166
Steps: []flow.Step{
167+
{
168+
Uses: "pypi/setup-python",
169+
With: map[string]string{
170+
"version": "{{.With.pythonVersion}}",
171+
},
172+
},
98173
{
99174
Uses: "pypi/setup-venv",
100175
With: map[string]string{
101-
"locator": "/usr/bin/",
176+
// "locator": "/usr/bin/",
177+
"locator": "/root/.pyenv/versions/{{.With.pythonVersion}}/bin/",
102178
"path": "{{.With.venv}}",
103179
},
104180
},
@@ -125,4 +201,12 @@ var toolkit = []*flow.Tool{
125201
Needs: []string{"python3"},
126202
}},
127203
},
204+
{
205+
Name: "pypi/build/sdist",
206+
Steps: []flow.Step{{
207+
Runs: textwrap.Dedent(`
208+
{{.With.locator}}python3 -m build --sdist -n{{if and (ne .With.dir ".") (ne .With.dir "")}} {{.With.dir}}{{end}}`)[1:],
209+
Needs: []string{"python3"},
210+
}},
211+
},
128212
}

pkg/rebuild/schema/schema.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ import (
2424
// The strategies are pointers because omitempty does not treat an empty struct as empty, but it
2525
// does treat nil pointers as empty.
2626
type StrategyOneOf struct {
27-
LocationHint *rebuild.LocationHint `json:"rebuild_location_hint,omitempty" yaml:"rebuild_location_hint,omitempty"`
28-
PureWheelBuild *pypi.PureWheelBuild `json:"pypi_pure_wheel_build,omitempty" yaml:"pypi_pure_wheel_build,omitempty"`
29-
NPMPackBuild *npm.NPMPackBuild `json:"npm_pack_build,omitempty" yaml:"npm_pack_build,omitempty"`
30-
NPMCustomBuild *npm.NPMCustomBuild `json:"npm_custom_build,omitempty" yaml:"npm_custom_build,omitempty"`
31-
CratesIOCargoPackage *cratesio.CratesIOCargoPackage `json:"cratesio_cargo_package,omitempty" yaml:"cratesio_cargo_package,omitempty"`
32-
MavenBuild *maven.MavenBuild `json:"maven_build,omitempty" yaml:"maven_build,omitempty"`
33-
DebianPackage *debian.DebianPackage `json:"debian_package,omitempty" yaml:"debian_package,omitempty"`
34-
Debrebuild *debian.Debrebuild `json:"debrebuild,omitempty" yaml:"debrebuild,omitempty"`
35-
ManualStrategy *rebuild.ManualStrategy `json:"manual,omitempty" yaml:"manual,omitempty"`
36-
WorkflowStrategy *rebuild.WorkflowStrategy `json:"flow,omitempty" yaml:"flow,omitempty"`
27+
LocationHint *rebuild.LocationHint `json:"rebuild_location_hint,omitempty" yaml:"rebuild_location_hint,omitempty"`
28+
PureWheelBuild *pypi.PureWheelBuild `json:"pypi_pure_wheel_build,omitempty" yaml:"pypi_pure_wheel_build,omitempty"`
29+
SourceDistributionBuild *pypi.SourceDistributionBuild `json:"pypi_source_distribution_build,omitempty" yaml:"pypi_source_distribution_build,omitempty"`
30+
NPMPackBuild *npm.NPMPackBuild `json:"npm_pack_build,omitempty" yaml:"npm_pack_build,omitempty"`
31+
NPMCustomBuild *npm.NPMCustomBuild `json:"npm_custom_build,omitempty" yaml:"npm_custom_build,omitempty"`
32+
CratesIOCargoPackage *cratesio.CratesIOCargoPackage `json:"cratesio_cargo_package,omitempty" yaml:"cratesio_cargo_package,omitempty"`
33+
MavenBuild *maven.MavenBuild `json:"maven_build,omitempty" yaml:"maven_build,omitempty"`
34+
DebianPackage *debian.DebianPackage `json:"debian_package,omitempty" yaml:"debian_package,omitempty"`
35+
Debrebuild *debian.Debrebuild `json:"debrebuild,omitempty" yaml:"debrebuild,omitempty"`
36+
ManualStrategy *rebuild.ManualStrategy `json:"manual,omitempty" yaml:"manual,omitempty"`
37+
WorkflowStrategy *rebuild.WorkflowStrategy `json:"flow,omitempty" yaml:"flow,omitempty"`
3738
}
3839

3940
// NewStrategyOneOf creates a StrategyOneOf from a rebuild.Strategy, using typecasting to put the strategy in the right place.
@@ -44,6 +45,8 @@ func NewStrategyOneOf(s rebuild.Strategy) StrategyOneOf {
4445
oneof.LocationHint = t
4546
case *pypi.PureWheelBuild:
4647
oneof.PureWheelBuild = t
48+
case *pypi.SourceDistributionBuild:
49+
oneof.SourceDistributionBuild = t
4750
case *maven.MavenBuild:
4851
oneof.MavenBuild = t
4952
case *npm.NPMPackBuild:
@@ -77,6 +80,10 @@ func (oneof *StrategyOneOf) Strategy() (rebuild.Strategy, error) {
7780
num++
7881
s = oneof.PureWheelBuild
7982
}
83+
if oneof.SourceDistributionBuild != nil {
84+
num++
85+
s = oneof.SourceDistributionBuild
86+
}
8087
if oneof.NPMPackBuild != nil {
8188
num++
8289
s = oneof.NPMPackBuild

0 commit comments

Comments
 (0)