fix(python): find license files under .dist-info/licenses (PEP 639) (#5496)

* fix(python): find license files under .dist-info/licenses (PEP 639)

PEP 639 (Metadata-Version 2.4) places license files in a licenses/
subdirectory of .dist-info. When a package declares no License or
License-Expression, the RECORD fallback only considered files directly
in .dist-info, so these packages were reported with no license.

Accept RECORD entries anywhere under .dist-info/licenses/ as license
file candidates as well.

See #4292

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tore Martin Hagen <tore@kosli.com>

* fix(python): resolve every declared License-File, including under licenses/

License-File may appear several times, but it was parsed as a single
value, so only the last entry was kept. Its path was also joined to the
metadata directory only, while PEP 639 (Metadata-Version 2.4) makes it
relative to .dist-info/licenses/. Packages without a RECORD entry for the
file therefore got no license.

Keep every License-File entry, and look each one up in the metadata
directory and then under licenses/. Entries whose names don't look like
license files (e.g. AUTHORS) are skipped, as the RECORD fallback already
does, so they don't show up as unidentified licenses.

Also cover isDistInfoLicenseCandidate with a table test, and use real
hashes in the PEP 639 fixture's RECORD.

See #4292

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tore Martin Hagen <tore@kosli.com>

---------

Signed-off-by: Tore Martin Hagen <tore@kosli.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tore Martin Hagen
2026-11-16 13:44:55 -05:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 29ac385f49
commit d20e261f91
13 changed files with 242 additions and 9 deletions
@@ -301,6 +301,83 @@ func Test_InstalledPackageCataloger(t *testing.T) {
},
},
},
{
name: "detect license files under dist-info/licenses (PEP 639)",
fixture: "testdata/site-packages/license-pep639",
expectedPackages: []pkg.Package{
{
Name: "pep639-example",
Version: "1.0.0",
PURL: "pkg:pypi/pep639-example@1.0.0",
Type: pkg.PythonPkg,
Language: pkg.Python,
Locations: file.NewLocationSet(
file.NewLocation("pep639_example-1.0.0.dist-info/METADATA"),
file.NewLocation("pep639_example-1.0.0.dist-info/RECORD"),
),
Licenses: pkg.NewLicenseSet(
pkg.License{
Value: "BSD-3-Clause",
SPDXExpression: "BSD-3-Clause",
Type: "concluded",
Contents: mustContentsFromLocation(t, "testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/licenses/LICENSE", 0, 1475),
// no License-File is declared, so the license file is found through RECORD
Locations: file.NewLocationSet(file.NewLocation("pep639_example-1.0.0.dist-info/licenses/LICENSE")),
},
),
FoundBy: "python-installed-package-cataloger",
Metadata: pkg.PythonPackage{
Name: "pep639-example",
Version: "1.0.0",
SitePackagesRootPath: ".",
Files: []pkg.PythonFileRecord{
{Path: "pep639_example-1.0.0.dist-info/METADATA", Digest: &pkg.PythonFileDigest{Algorithm: "sha256", Value: "Z_-WBDzq246VHj56DO8yfBNNtlTqMj6q9xw_OGgeEMw"}, Size: "218"},
{Path: "pep639_example-1.0.0.dist-info/licenses/LICENSE", Digest: &pkg.PythonFileDigest{Algorithm: "sha256", Value: "SJqOEQhQntmKN7uYPhHg9-HTHwvY-Zp5yESOf_N9B-o"}, Size: "1475"},
{Path: "pep639_example-1.0.0.dist-info/RECORD"},
},
RequiresPython: ">=3.9",
},
},
{
Name: "pep639-declared",
Version: "1.0.0",
PURL: "pkg:pypi/pep639-declared@1.0.0",
Type: pkg.PythonPkg,
Language: pkg.Python,
Locations: file.NewLocationSet(
file.NewLocation("pep639_declared-1.0.0.dist-info/METADATA"),
file.NewLocation("pep639_declared-1.0.0.dist-info/RECORD"),
),
// found only through License-File, since RECORD does not list them; AUTHORS.rst is not a license file
Licenses: pkg.NewLicenseSet(
pkg.License{
Value: "BSD-3-Clause",
SPDXExpression: "BSD-3-Clause",
Type: "concluded",
Contents: mustContentsFromLocation(t, "testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.BSD", 0, 1475),
Locations: file.NewLocationSet(file.NewLocation("pep639_declared-1.0.0.dist-info/licenses/LICENSE.BSD")),
},
pkg.License{
Value: "MIT",
SPDXExpression: "MIT",
Type: "concluded",
Contents: mustContentsFromLocation(t, "testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.MIT", 13, 1072),
Locations: file.NewLocationSet(file.NewLocation("pep639_declared-1.0.0.dist-info/licenses/LICENSE.MIT")),
},
),
FoundBy: "python-installed-package-cataloger",
Metadata: pkg.PythonPackage{
Name: "pep639-declared",
Version: "1.0.0",
SitePackagesRootPath: ".",
Files: []pkg.PythonFileRecord{
{Path: "pep639_declared-1.0.0.dist-info/METADATA", Digest: &pkg.PythonFileDigest{Algorithm: "sha256", Value: "QM0YKaGTutHOanSl_CjBQDgF2cayCxbgyEPE0xhMGks"}, Size: "255"},
{Path: "pep639_declared-1.0.0.dist-info/RECORD"},
},
},
},
},
},
{
name: "malformed-record",
fixture: "testdata/site-packages/malformed-record",
+36 -1
View File
@@ -251,6 +251,15 @@ func findLicenses(ctx context.Context, resolver file.Resolver, m parsedData) pkg
} else {
licenseLocations.Add(locs...)
}
} else {
metadataDir := path.Dir(m.DistInfoLocation.Path())
for _, name := range m.LicenseFiles {
// License-File may also list notices such as AUTHORS, which are not licenses
if !licenses.IsLicenseFile(path.Base(name)) {
continue
}
licenseLocations.Add(resolveDeclaredLicenseFile(resolver, metadataDir, name)...)
}
}
switch {
@@ -264,7 +273,7 @@ func findLicenses(ctx context.Context, resolver file.Resolver, m parsedData) pkg
parent := path.Base(path.Dir(m.DistInfoLocation.LocalPath()))
candidatePaths := strset.New()
for _, f := range m.Files {
if !strings.HasPrefix(f.Path, parent) || strings.Count(f.Path, "/") > 1 {
if !isDistInfoLicenseCandidate(parent, f.Path) {
continue
}
@@ -277,3 +286,29 @@ func findLicenses(ctx context.Context, resolver file.Resolver, m parsedData) pkg
}
return pkg.NewLicenseSet(out...)
}
// resolveDeclaredLicenseFile finds a License-File entry, which PEP 639 (Metadata-Version 2.4) makes relative to
// .dist-info/licenses/ and older metadata makes relative to the metadata directory itself.
func resolveDeclaredLicenseFile(resolver file.Resolver, metadataDir, name string) []file.Location {
for _, p := range []string{path.Join(metadataDir, name), path.Join(metadataDir, "licenses", name)} {
locs, err := resolver.FilesByPath(p)
if err != nil {
log.WithFields("error", err, "path", p).Trace("unable to resolve python license file")
continue
}
if len(locs) > 0 {
return locs
}
}
return nil
}
// isDistInfoLicenseCandidate reports whether a RECORD path sits directly in the .dist-info directory,
// or anywhere under its licenses/ subdirectory, where PEP 639 places license files.
func isDistInfoLicenseCandidate(distInfoDir, recordPath string) bool {
rel, found := strings.CutPrefix(recordPath, distInfoDir+"/")
if !found {
return false
}
return !strings.Contains(rel, "/") || strings.HasPrefix(rel, "licenses/")
}
@@ -23,13 +23,14 @@ type parsedData struct {
// license info
Licenses string `mapstructure:"License"`
LicenseFile string `mapstructure:"LicenseFile"`
LicenseExpression string `mapstructure:"LicenseExpression"`
Licenses string `mapstructure:"License"`
LicenseFiles []string `mapstructure:"LicenseFile"`
LicenseExpression string `mapstructure:"LicenseExpression"`
LicenseFilePath string
}
var pluralFields = map[string]bool{
"LicenseFile": true,
"ProvidesExtra": true,
"RequiresDist": true,
}
@@ -53,8 +54,6 @@ func parseWheelOrEggMetadata(locationReader file.LocationReadCloser) (parsedData
pd.SitePackagesRootPath = determineSitePackagesRootPath(metadataPath)
if pd.Licenses != "" || pd.LicenseExpression != "" {
pd.LicenseFilePath = metadataPath
} else if pd.LicenseFile != "" {
pd.LicenseFilePath = path.Join(path.Dir(metadataPath), pd.LicenseFile)
}
pd.DistInfoLocation = locationReader.Location
@@ -26,7 +26,6 @@ func TestParseWheelEggMetadata(t *testing.T) {
ExpectedMetadata: parsedData{
DistInfoLocation: file.NewLocation("testdata/site-packages/nested/egg-name/egg-info/PKG-INFO"),
Licenses: "Apache 2.0",
LicenseFile: "",
LicenseExpression: "",
LicenseFilePath: "testdata/site-packages/nested/egg-name/egg-info/PKG-INFO",
PythonPackage: pkg.PythonPackage{
@@ -46,7 +45,6 @@ func TestParseWheelEggMetadata(t *testing.T) {
ExpectedMetadata: parsedData{
DistInfoLocation: file.NewLocation("testdata/site-packages/nested/dist-name/dist-info/METADATA"),
Licenses: "BSD License",
LicenseFile: "",
LicenseExpression: "",
LicenseFilePath: "testdata/site-packages/nested/dist-name/dist-info/METADATA",
PythonPackage: pkg.PythonPackage{
@@ -156,7 +154,6 @@ func TestParseWheelEggMetadataInvalid(t *testing.T) {
DistInfoLocation: file.NewLocation("testdata/site-packages/nested/egg-name/egg-info/PKG-INFO-INVALID"),
Licenses: "",
LicenseExpression: "",
LicenseFile: "",
LicenseFilePath: "",
PythonPackage: pkg.PythonPackage{
Name: "mxnet",
@@ -0,0 +1,27 @@
package python
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_isDistInfoLicenseCandidate(t *testing.T) {
tests := []struct {
name string
recordPath string
want bool
}{
{name: "directly in dist-info", recordPath: "x.dist-info/LICENSE", want: true},
{name: "under licenses", recordPath: "x.dist-info/licenses/LICENSE", want: true},
{name: "nested under licenses", recordPath: "x.dist-info/licenses/vendor/LICENSE", want: true},
{name: "other subdirectory of dist-info", recordPath: "x.dist-info/sub/LICENSE", want: false},
{name: "sibling directory sharing the prefix", recordPath: "x.dist-info2/LICENSE", want: false},
{name: "outside dist-info", recordPath: "x/LICENSE", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, isDistInfoLicenseCandidate("x.dist-info", tt.recordPath))
})
}
}
@@ -0,0 +1,8 @@
Metadata-Version: 2.4
Name: pep639-declared
Version: 1.0.0
Summary: A package that declares two license files under .dist-info/licenses/ (PEP 639), which RECORD does not list.
License-File: LICENSE.BSD
License-File: LICENSE.MIT
License-File: AUTHORS.rst
@@ -0,0 +1,2 @@
pep639_declared-1.0.0.dist-info/METADATA,sha256=QM0YKaGTutHOanSl_CjBQDgF2cayCxbgyEPE0xhMGks,255
pep639_declared-1.0.0.dist-info/RECORD,,
@@ -0,0 +1 @@
Example Authors <authors@example.com>
@@ -0,0 +1,28 @@
Copyright 2010 Pallets
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 Example Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,7 @@
Metadata-Version: 2.4
Name: pep639-example
Version: 1.0.0
Summary: A package that ships its license only under .dist-info/licenses/ (PEP 639).
Classifier: License :: OSI Approved :: BSD License
Requires-Python: >=3.9
@@ -0,0 +1,3 @@
pep639_example-1.0.0.dist-info/METADATA,sha256=Z_-WBDzq246VHj56DO8yfBNNtlTqMj6q9xw_OGgeEMw,218
pep639_example-1.0.0.dist-info/licenses/LICENSE,sha256=SJqOEQhQntmKN7uYPhHg9-HTHwvY-Zp5yESOf_N9B-o,1475
pep639_example-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,28 @@
Copyright 2010 Pallets
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.