Skip to content

Commit

Permalink
Merge pull request #789 from Elizafox/python-docslint
Browse files Browse the repository at this point in the history
Add Python docs linter
  • Loading branch information
Elizafox authored Oct 25, 2023
2 parents df84354 + e17fb08 commit a5a01ee
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
1 change: 1 addition & 0 deletions pkg/linter/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var DefaultLinters = []string{
"dev",
"empty",
"opt",
"pythondocs",
"pythonmultiple",
"srv",
"setuidgid",
Expand Down
49 changes: 41 additions & 8 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ var postLinterMap = map[string]postLinter{
FailOnError: false,
Explain: "Verify that this package is supposed to be empty; if it is, disable this linter; otherwise check the build",
},
"pythondocs": postLinter{
LinterFunc: pythonDocsPostLinter,
FailOnError: false,
Explain: "Remove all docs directories from the package",
},
"pythonmultiple": postLinter{
LinterFunc: pythonMultiplePackagesPostLinter,
FailOnError: false,
Explain: "Split this package up into multiple packages",
Explain: "Split this package up into multiple packages and verify you are not improperly using pip install",
},
}

Expand Down Expand Up @@ -324,29 +329,57 @@ func emptyPostLinter(_ LinterContext, fsys fs.FS) error {
return fmt.Errorf("Package is empty but no-provides is not set")
}

func pythonMultiplePackagesPostLinter(_ LinterContext, fsys fs.FS) error {
func getPythonSitePackages(fsys fs.FS) (matches []string, err error) {
pythondirs, err := fs.Glob(fsys, filepath.Join("usr", "lib", "python3.*"))
if err != nil {
// Shouldn't get here, per the Go docs.
return fmt.Errorf("Error checking for Python site directories: %w", err)
err = fmt.Errorf("Error checking for Python site directories: %w", err)
return
}

if len(pythondirs) == 0 {
// Nothing to do
return nil
return
} else if len(pythondirs) > 1 {
return fmt.Errorf("More than one Python version detected: %d found", len(pythondirs))
err = fmt.Errorf("More than one Python version detected: %d found", len(pythondirs))
return
}

matches, err := fs.Glob(fsys, filepath.Join(pythondirs[0], "site-packages", "*"))
matches, err = fs.Glob(fsys, filepath.Join(pythondirs[0], "site-packages", "*"))
if err != nil {
// Shouldn't get here as well.
return fmt.Errorf("Error checking for Python packages: %w", err)
err = fmt.Errorf("Error checking for Python packages: %w", err)
return
}

return
}

func pythonDocsPostLinter(_ LinterContext, fsys fs.FS) error {
packages, err := getPythonSitePackages(fsys)
if err != nil {
return err
}

for _, m := range packages {
base := filepath.Base(m)
if base == "doc" || base == "docs" {
return fmt.Errorf("Docs diretory encountered in Python directory")
}
}

return nil
}

func pythonMultiplePackagesPostLinter(_ LinterContext, fsys fs.FS) error {
packages, err := getPythonSitePackages(fsys)
if err != nil {
return err
}

// Filter matches and ignore duplicates (.so vs directories for example)
pmatches := map[string]struct{}{}
for _, m := range matches {
for _, m := range packages {
base := filepath.Base(m)

if strings.HasPrefix(base, "_") {
Expand Down
52 changes: 48 additions & 4 deletions pkg/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ func Test_optLinter(t *testing.T) {
Name: "testopt",
Version: "4.2.0",
Epoch: 0,
Checks: config.Checks{
Enabled: []string{"opt"},
Disabled: []string{"dev", "empty", "pythonmultiple", "setuidgid", "strip", "srv", "tempdir", "usrlocal", "varempty", "worldwrite"},
},
Checks: checksOnly("opt"),
},
}

Expand All @@ -191,6 +188,53 @@ func Test_optLinter(t *testing.T) {
assert.True(t, called)
}

func Test_pythonDocsLinter(t *testing.T) {
dir, err := os.MkdirTemp("", "melange.XXXXX")
defer os.RemoveAll(dir)
assert.NoError(t, err)

cfg := config.Configuration{
Package: config.Package{
Name: "testpythondocs",
Version: "4.2.0",
Epoch: 0,
Checks: checksOnly("pythondocs"),
},
}

// Base dir
pythonPathdir := filepath.Join(dir, "usr", "lib", "python3.14", "site-packages")

linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"pythondocs"})

fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys)

// Make one "package"
packagedir := filepath.Join(pythonPathdir, "foo")
err = os.MkdirAll(packagedir, 0700)
assert.NoError(t, err)

// One package should not trip it
called := false
assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) {
called = true
}, linters))
assert.False(t, called)

// Create docs
docsdir := filepath.Join(pythonPathdir, "docs")
err = os.MkdirAll(docsdir, 0700)
assert.NoError(t, err)

// This should trip
assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) {
called = true
}, linters))
assert.True(t, called)
}

func Test_pythonMultiplePackagesLinter(t *testing.T) {
dir, err := os.MkdirTemp("", "melange.XXXXX")
defer os.RemoveAll(dir)
Expand Down

0 comments on commit a5a01ee

Please sign in to comment.