Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore/Scrutinize pyproject for backends #234

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading