Skip to content

Commit

Permalink
chore/Scrutinize pyproject for backends (#234)
Browse files Browse the repository at this point in the history
* Add VerifySpecfile

* Gate the poetry backend on whether we find a poetry section in it
  • Loading branch information
blast-hardcheese authored Feb 14, 2024
1 parent d6a128b commit f33424b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
3 changes: 3 additions & 0 deletions internal/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ type LanguageBackend struct {
// This field is mandatory.
Specfile string

// An optional function to analyze the specfile to determine compatibility
IsSpecfileCompatible func(fullPath string) (bool, error)

// The filename of the lockfile, e.g. "poetry.lock" for
// Poetry.
Lockfile string
Expand Down
25 changes: 23 additions & 2 deletions internal/backends/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,33 @@ func GetBackend(ctx context.Context, language string) api.LanguageBackend {
for _, b := range backends {
if util.Exists(b.Specfile) &&
util.Exists(b.Lockfile) {
if b.IsSpecfileCompatible != nil {
isValid, err := b.IsSpecfileCompatible(b.Specfile)
if err != nil {
panic(err)
}
if !isValid {
continue
}
}
return b
}
}
for _, b := range backends {
if util.Exists(b.Specfile) ||
util.Exists(b.Lockfile) {
if util.Exists(b.Specfile) {
if b.IsSpecfileCompatible != nil {
isValid, err := b.IsSpecfileCompatible(b.Specfile)
if err != nil {
panic(err)
}
if !isValid {
continue
}
}

return b
}
if util.Exists(b.Lockfile) {
return b
}
}
Expand Down
24 changes: 17 additions & 7 deletions internal/backends/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type pyprojectPackageCfg struct {
// file.
type pyprojectTOML struct {
Tool struct {
Poetry struct {
Poetry *struct {
Name string `json:"name"`
// interface{} because they can be either
// strings or maps (why?? good lord).
Expand Down Expand Up @@ -266,12 +266,13 @@ func searchPypi(query string) []api.PkgInfo {
// (either a full path or just a name like "python3") to use when invoking Python.
func makePythonPoetryBackend(python string) api.LanguageBackend {
return api.LanguageBackend{
Name: "python3-poetry",
Alias: "python-python3-poetry",
Specfile: "pyproject.toml",
Lockfile: "poetry.lock",
IsAvailable: poetryIsAvailable,
FilenamePatterns: []string{"*.py"},
Name: "python3-poetry",
Alias: "python-python3-poetry",
Specfile: "pyproject.toml",
IsSpecfileCompatible: verifyPoetrySpecfile,
Lockfile: "poetry.lock",
IsAvailable: poetryIsAvailable,
FilenamePatterns: []string{"*.py"},
Quirks: api.QuirksAddRemoveAlsoLocks |
api.QuirksAddRemoveAlsoInstalls,
NormalizePackageArgs: normalizePackageArgs,
Expand Down Expand Up @@ -567,6 +568,15 @@ func readPyproject() (*pyprojectTOML, error) {
return &cfg, nil
}

func verifyPoetrySpecfile(path string) (bool, error) {
cfg, err := readPyproject()
if err != nil {
return false, err
}

return cfg.Tool.Poetry != nil, nil
}

func listPoetrySpecfile() (map[api.PkgName]api.PkgSpec, error) {
cfg, err := readPyproject()
if err != nil {
Expand Down

0 comments on commit f33424b

Please sign in to comment.