diff --git a/gen.go b/gen.go index 5941ae0..b14cfbb 100644 --- a/gen.go +++ b/gen.go @@ -33,7 +33,6 @@ var genPipCmd = &cli.Command{ &cli.StringFlag{ Name: "description", Aliases: []string{"d"}, - Value: "A Python Pip module", }, }, Action: func(c *cli.Context) error { diff --git a/pkg/gen/pip.go b/pkg/gen/pip.go index ca2e027..6f8f984 100644 --- a/pkg/gen/pip.go +++ b/pkg/gen/pip.go @@ -2,10 +2,11 @@ package gen import ( _ "embed" + "encoding/json" + "errors" "fmt" "io" "net/http" - "path" "text/template" ) @@ -18,6 +19,34 @@ type PipOptions struct { 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). @@ -26,35 +55,30 @@ func Pip(w io.Writer, opts PipOptions) error { return err } - params := map[string]any{ - "name": opts.Name, - "version": opts.Version, - "description": opts.Description, - } - url := fmt.Sprintf( - "https://files.pythonhosted.org/packages/source/%s/%s/%s-%s.tar.gz", - opts.Name[:1], - opts.Name, + "https://pypi.org/pypi/%s/%s/json", opts.Name, opts.Version, ) - res, err := http.Head(url) + res, err := http.Get(url) if err != nil { return err } + defer res.Body.Close() 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) - checksum := path.Base(dir) - dir = path.Dir(dir) - checksum = path.Base(dir) + checksum - dir = path.Dir(dir) - checksum = path.Base(dir) + checksum - params["checksum"] = "blake2b-256:" + checksum + var resp pypiAPIResponse + err = json.NewDecoder(res.Body).Decode(&resp) + if err != nil { + return err + } - return tmpl.Execute(w, params) + if opts.Description != "" { + resp.Info.Summary = opts.Description + } + + return tmpl.Execute(w, resp) } diff --git a/pkg/gen/tmpls/pip.tmpl.sh b/pkg/gen/tmpls/pip.tmpl.sh index b3ad123..326ab73 100644 --- a/pkg/gen/tmpls/pip.tmpl.sh +++ b/pkg/gen/tmpls/pip.tmpl.sh @@ -1,13 +1,13 @@ -name='{{.name | tolower}}' -version='{{.version}}' +name='{{.Info.Name | tolower}}' +version='{{.Info.Version}}' release='1' -desc='{{.description}}' -homepage='https://pypi.org/project/{{.name}}/' +desc='{{.Info.Summary}}' +homepage='{{.Info.Homepage}}' maintainer='Example ' architectures=('all') -license=('custom:Unknown') -provides=('{{.name | tolower}}') -conflicts=('{{.name | tolower}}') +license=('{{if .Info.License | ne ""}}{{.Info.License}}{{else}}custom:Unknown{{end}}') +provides=('{{.Info.Name | tolower}}') +conflicts=('{{.Info.Name | tolower}}') deps=("python3") deps_arch=("python") @@ -17,15 +17,15 @@ build_deps=("python3" "python3-setuptools") build_deps_arch=("python" "python-setuptools") build_deps_alpine=("python3" "py3-setuptools") -sources=("https://files.pythonhosted.org/packages/source/{{.name | firstchar}}/{{.name}}/{{.name}}-${version}.tar.gz") -checksums=('{{.checksum}}') +sources=("https://files.pythonhosted.org/packages/source/{{.SourceURL.Filename | firstchar}}/{{.Info.Name}}/{{.SourceURL.Filename}}") +checksums=('blake2b-256:{{.SourceURL.Digests.blake2b_256}}') build() { - cd "$srcdir/{{.name}}-${version}" + cd "$srcdir/{{.Info.Name}}-${version}" python3 setup.py build } package() { - cd "$srcdir/{{.name}}-${version}" + cd "$srcdir/{{.Info.Name}}-${version}" python3 setup.py install --root="${pkgdir}/" --optimize=1 || return 1 }