Use the pypi API to fetch information about pip modules
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
4218912123
commit
fd145e4751
1
gen.go
1
gen.go
|
@ -33,7 +33,6 @@ var genPipCmd = &cli.Command{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "description",
|
Name: "description",
|
||||||
Aliases: []string{"d"},
|
Aliases: []string{"d"},
|
||||||
Value: "A Python Pip module",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
|
|
|
@ -2,10 +2,11 @@ package gen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,6 +19,34 @@ type PipOptions struct {
|
||||||
Description 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 {
|
func Pip(w io.Writer, opts PipOptions) error {
|
||||||
tmpl, err := template.New("pip").
|
tmpl, err := template.New("pip").
|
||||||
Funcs(funcs).
|
Funcs(funcs).
|
||||||
|
@ -26,35 +55,30 @@ func Pip(w io.Writer, opts PipOptions) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
params := map[string]any{
|
|
||||||
"name": opts.Name,
|
|
||||||
"version": opts.Version,
|
|
||||||
"description": opts.Description,
|
|
||||||
}
|
|
||||||
|
|
||||||
url := fmt.Sprintf(
|
url := fmt.Sprintf(
|
||||||
"https://files.pythonhosted.org/packages/source/%s/%s/%s-%s.tar.gz",
|
"https://pypi.org/pypi/%s/%s/json",
|
||||||
opts.Name[:1],
|
|
||||||
opts.Name,
|
|
||||||
opts.Name,
|
opts.Name,
|
||||||
opts.Version,
|
opts.Version,
|
||||||
)
|
)
|
||||||
|
|
||||||
res, err := http.Head(url)
|
res, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
return fmt.Errorf("pip: %s", res.Status)
|
return fmt.Errorf("pypi: %s", res.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
dir := path.Dir(res.Request.URL.Path)
|
var resp pypiAPIResponse
|
||||||
checksum := path.Base(dir)
|
err = json.NewDecoder(res.Body).Decode(&resp)
|
||||||
dir = path.Dir(dir)
|
if err != nil {
|
||||||
checksum = path.Base(dir) + checksum
|
return err
|
||||||
dir = path.Dir(dir)
|
}
|
||||||
checksum = path.Base(dir) + checksum
|
|
||||||
params["checksum"] = "blake2b-256:" + checksum
|
|
||||||
|
|
||||||
return tmpl.Execute(w, params)
|
if opts.Description != "" {
|
||||||
|
resp.Info.Summary = opts.Description
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmpl.Execute(w, resp)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
name='{{.name | tolower}}'
|
name='{{.Info.Name | tolower}}'
|
||||||
version='{{.version}}'
|
version='{{.Info.Version}}'
|
||||||
release='1'
|
release='1'
|
||||||
desc='{{.description}}'
|
desc='{{.Info.Summary}}'
|
||||||
homepage='https://pypi.org/project/{{.name}}/'
|
homepage='{{.Info.Homepage}}'
|
||||||
maintainer='Example <user@example.com>'
|
maintainer='Example <user@example.com>'
|
||||||
architectures=('all')
|
architectures=('all')
|
||||||
license=('custom:Unknown')
|
license=('{{if .Info.License | ne ""}}{{.Info.License}}{{else}}custom:Unknown{{end}}')
|
||||||
provides=('{{.name | tolower}}')
|
provides=('{{.Info.Name | tolower}}')
|
||||||
conflicts=('{{.name | tolower}}')
|
conflicts=('{{.Info.Name | tolower}}')
|
||||||
|
|
||||||
deps=("python3")
|
deps=("python3")
|
||||||
deps_arch=("python")
|
deps_arch=("python")
|
||||||
|
@ -17,15 +17,15 @@ build_deps=("python3" "python3-setuptools")
|
||||||
build_deps_arch=("python" "python-setuptools")
|
build_deps_arch=("python" "python-setuptools")
|
||||||
build_deps_alpine=("python3" "py3-setuptools")
|
build_deps_alpine=("python3" "py3-setuptools")
|
||||||
|
|
||||||
sources=("https://files.pythonhosted.org/packages/source/{{.name | firstchar}}/{{.name}}/{{.name}}-${version}.tar.gz")
|
sources=("https://files.pythonhosted.org/packages/source/{{.SourceURL.Filename | firstchar}}/{{.Info.Name}}/{{.SourceURL.Filename}}")
|
||||||
checksums=('{{.checksum}}')
|
checksums=('blake2b-256:{{.SourceURL.Digests.blake2b_256}}')
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
cd "$srcdir/{{.name}}-${version}"
|
cd "$srcdir/{{.Info.Name}}-${version}"
|
||||||
python3 setup.py build
|
python3 setup.py build
|
||||||
}
|
}
|
||||||
|
|
||||||
package() {
|
package() {
|
||||||
cd "$srcdir/{{.name}}-${version}"
|
cd "$srcdir/{{.Info.Name}}-${version}"
|
||||||
python3 setup.py install --root="${pkgdir}/" --optimize=1 || return 1
|
python3 setup.py install --root="${pkgdir}/" --optimize=1 || return 1
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue