Initial commit

This commit is contained in:
2024-01-22 13:36:06 +03:00
commit bb8e6e79b2
82 changed files with 11517 additions and 0 deletions

13
pkg/gen/funcs.go Normal file
View File

@ -0,0 +1,13 @@
package gen
import (
"strings"
"text/template"
)
var funcs = template.FuncMap{
"tolower": strings.ToLower,
"firstchar": func(s string) string {
return s[:1]
},
}

84
pkg/gen/pip.go Normal file
View File

@ -0,0 +1,84 @@
package gen
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"text/template"
)
//go:embed tmpls/pip.tmpl.sh
var pipTmpl string
type PipOptions struct {
Name string
Version string
Description string
}
type pypiAPIResponse struct {
Info pypiInfo `json:"info"`
URLs []pypiURL `json:"urls"`
}
func (res pypiAPIResponse) SourceURL() (pypiURL, error) {
for _, url := range res.URLs {
if url.PackageType == "sdist" {
return url, nil
}
}
return pypiURL{}, errors.New("package doesn't have a source distribution")
}
type pypiInfo struct {
Name string `json:"name"`
Version string `json:"version"`
Summary string `json:"summary"`
Homepage string `json:"home_page"`
License string `json:"license"`
}
type pypiURL struct {
Digests map[string]string `json:"digests"`
Filename string `json:"filename"`
PackageType string `json:"packagetype"`
}
func Pip(w io.Writer, opts PipOptions) error {
tmpl, err := template.New("pip").
Funcs(funcs).
Parse(pipTmpl)
if err != nil {
return err
}
url := fmt.Sprintf(
"https://pypi.org/pypi/%s/%s/json",
opts.Name,
opts.Version,
)
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("pypi: %s", res.Status)
}
var resp pypiAPIResponse
err = json.NewDecoder(res.Body).Decode(&resp)
if err != nil {
return err
}
if opts.Description != "" {
resp.Info.Summary = opts.Description
}
return tmpl.Execute(w, resp)
}

31
pkg/gen/tmpls/pip.tmpl.sh Normal file
View File

@ -0,0 +1,31 @@
name='{{.Info.Name | tolower}}'
version='{{.Info.Version}}'
release='1'
desc='{{.Info.Summary}}'
homepage='{{.Info.Homepage}}'
maintainer='Example <user@example.com>'
architectures=('all')
license=('{{if .Info.License | ne ""}}{{.Info.License}}{{else}}custom:Unknown{{end}}')
provides=('{{.Info.Name | tolower}}')
conflicts=('{{.Info.Name | tolower}}')
deps=("python3")
deps_arch=("python")
deps_alpine=("python3")
build_deps=("python3" "python3-setuptools")
build_deps_arch=("python" "python-setuptools")
build_deps_alpine=("python3" "py3-setuptools")
sources=("https://files.pythonhosted.org/packages/source/{{.SourceURL.Filename | firstchar}}/{{.Info.Name}}/{{.SourceURL.Filename}}")
checksums=('blake2b-256:{{.SourceURL.Digests.blake2b_256}}')
build() {
cd "$srcdir/{{.Info.Name}}-${version}"
python3 setup.py build
}
package() {
cd "$srcdir/{{.Info.Name}}-${version}"
python3 setup.py install --root="${pkgdir}/" --optimize=1 || return 1
}