Files
hakurei/internal/rosa/python.go
Ophestra 6546ddc64b internal/rosa: expose in-place behaviour in generic helper
This change also combines the createDir and wantsDir methods, and replaces the non-inplace target of the generic helper with a deterministic path.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2026-05-21 16:11:38 +09:00

121 lines
2.7 KiB
Go

package rosa
import (
"slices"
"hakurei.app/internal/pkg"
)
var (
_python = H("python")
_pythonPyTest = H("python-pytest")
)
// PipHelper is the [Python] pip packaging helper.
type PipHelper struct {
// Path elements joined with source.
Append []string
// Whether to omit --no-build-isolation.
BuildIsolation bool
// Whether to enter source after install.
EnterSource bool
// Whether to install to build environment after install.
Install bool
// Whether to skip running tests.
SkipCheck bool
// Replaces pytest if non-empty.
Check string
// Runs after install.
Script string
}
var _ Helper = new(PipHelper)
// extra returns python, or pytest if defaults are assumed.
func (attr *PipHelper) extra(int) P {
if attr == nil || (!attr.SkipCheck && attr.Check == "") {
return P{_pythonPyTest}
}
return P{_python}
}
// wantsChmod returns true.
func (*PipHelper) wantsChmod() bool { return true }
// wantsWrite is equivalent to wantsChmod.
func (attr *PipHelper) wantsWrite() bool { return attr.wantsChmod() }
// scriptEarly is a noop.
func (*PipHelper) scriptEarly() string { return "" }
// wantsDir requests a new directory in TMPDIR.
func (*PipHelper) wantsDir() (string, bool) { return `"$(mktemp -d)"`, false }
// script generates the pip3 install command.
func (attr *PipHelper) script(_ Toolchain, name string) string {
if attr == nil {
attr = new(PipHelper)
}
sourcePath := AbsUsrSrc.Append(name).Append(attr.Append...)
var extra string
if !attr.BuildIsolation {
extra += `
--no-build-isolation \`
}
var script string
if attr.Install {
script += `pip3 install \
--no-index \
--prefix=/system \
--no-build-isolation \
'` + sourcePath.String() + `'
`
}
if attr.EnterSource {
script += "cd '/usr/src/" + name + "'\n"
}
if !attr.SkipCheck {
if attr.Check == "" {
// some test suites fall apart when ran out-of-tree
script += "(cd '" + sourcePath.String() + "' && pytest)\n"
} else {
script += attr.Check
}
}
script += attr.Script
return `
pip3 install \
--no-index \
--prefix=/system \
--root=/work \` + extra + `
'` + sourcePath.String() + `'
` + script
}
// newPythonPackage registers a new [Python] package.
func (s *S) newPythonPackage(
name string, id int, description, website, version string,
source pkg.Artifact, attrP *PackageAttr, attr *PipHelper,
build P, extra ...ArtifactH,
) {
name = "python-" + name
s.MustRegister(name, func(t Toolchain) (*Metadata, pkg.Artifact) {
return &Metadata{
Name: name,
Description: description,
Website: website,
Version: version,
Dependencies: slices.Concat(P{_python}, extra),
ID: id,
}, t.NewPackage(name, version, source, attrP, attr, slices.Concat(
extra,
build,
)...)
})
}