Skip to content

Commit 1862eee

Browse files
committed
feat: replace pylock.toml parser
1 parent 872bb9c commit 1862eee

File tree

1 file changed

+2
-62
lines changed

1 file changed

+2
-62
lines changed

pkg/lockfile/parse-pylock.go

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,11 @@
11
package lockfile
22

33
import (
4-
"fmt"
5-
"os"
6-
7-
"github.com/BurntSushi/toml"
4+
"github.com/google/osv-scalibr/extractor/filesystem/language/python/pylock"
85
)
96

10-
type pylockVCS struct {
11-
Type string `toml:"type"`
12-
Commit string `toml:"commit-id"`
13-
}
14-
15-
type pylockDirectory struct {
16-
Path string `toml:"path"`
17-
}
18-
19-
type PylockPackage struct {
20-
Name string `toml:"name"`
21-
Version string `toml:"version"`
22-
VCS pylockVCS `toml:"vcs"`
23-
Directory pylockDirectory `toml:"directory"`
24-
}
25-
26-
type PylockLockfile struct {
27-
Version string `toml:"lock-version"`
28-
Packages []PylockPackage `toml:"packages"`
29-
}
30-
317
const PylockEcosystem = PipEcosystem
328

339
func ParsePylock(pathToLockfile string) ([]PackageDetails, error) {
34-
var parsedLockfile *PylockLockfile
35-
36-
lockfileContents, err := os.ReadFile(pathToLockfile)
37-
38-
if err != nil {
39-
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
40-
}
41-
42-
err = toml.Unmarshal(lockfileContents, &parsedLockfile)
43-
44-
if err != nil {
45-
return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err)
46-
}
47-
48-
packages := make([]PackageDetails, 0, len(parsedLockfile.Packages))
49-
50-
for _, pkg := range parsedLockfile.Packages {
51-
// this is likely the root package, which is sometimes included in the lockfile
52-
if pkg.Version == "" && pkg.Directory.Path == "." {
53-
continue
54-
}
55-
56-
pkgDetails := PackageDetails{
57-
Name: pkg.Name,
58-
Version: pkg.Version,
59-
Ecosystem: PylockEcosystem,
60-
CompareAs: PylockEcosystem,
61-
}
62-
63-
if pkg.VCS.Commit != "" {
64-
pkgDetails.Commit = pkg.VCS.Commit
65-
}
66-
67-
packages = append(packages, pkgDetails)
68-
}
69-
70-
return packages, nil
10+
return extract(pathToLockfile, pylock.New(), PylockEcosystem)
7111
}

0 commit comments

Comments
 (0)