getModulesDep: use regex for searching module names in modules.dep
Fixes an issue where some modules might have both - and _ in the name... Also refactored this to make testing easier.
This commit is contained in:
64
main.go
64
main.go
@@ -15,6 +15,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -621,22 +622,21 @@ func getModulesInDir(files misc.StringSet, modPath string) error {
|
|||||||
// anywhere
|
// anywhere
|
||||||
func getModule(files misc.StringSet, modName string, modDir string) error {
|
func getModule(files misc.StringSet, modName string, modDir string) error {
|
||||||
|
|
||||||
deps, err := getModuleDeps(modName, modDir)
|
modDep := filepath.Join(modDir, "modules.dep")
|
||||||
if err != nil {
|
if !exists(modDep) {
|
||||||
return err
|
log.Fatal("Kernel module.dep not found: ", modDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(deps) == 0 {
|
fd, err := os.Open(modDep)
|
||||||
// retry and swap - and _ in module name
|
if err != nil {
|
||||||
if strings.Contains(modName, "-") {
|
log.Print("Unable to open modules.dep: ", modDep)
|
||||||
modName = strings.ReplaceAll(modName, "-", "_")
|
return err
|
||||||
} else {
|
}
|
||||||
modName = strings.ReplaceAll(modName, "_", "-")
|
defer fd.Close()
|
||||||
}
|
|
||||||
deps, err = getModuleDeps(modName, modDir)
|
deps, err := getModuleDeps(modName, fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, dep := range deps {
|
for _, dep := range deps {
|
||||||
@@ -651,29 +651,35 @@ func getModule(files misc.StringSet, modName string, modDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getModuleDeps(modName string, modDir string) ([]string, error) {
|
// Get the canonicalized name for the module as represented in the given modules.dep io.reader
|
||||||
|
func getModuleDeps(modName string, modulesDep io.Reader) ([]string, error) {
|
||||||
var deps []string
|
var deps []string
|
||||||
|
|
||||||
modDep := filepath.Join(modDir, "modules.dep")
|
// split the module name on - and/or _, build a regex for matching
|
||||||
if !exists(modDep) {
|
splitRe := regexp.MustCompile("[-_]+")
|
||||||
log.Fatal("Kernel module.dep not found: ", modDir)
|
var modNameReStr string
|
||||||
|
for _, s := range splitRe.Split(modName, -1) {
|
||||||
|
if modNameReStr != "" {
|
||||||
|
modNameReStr += "[-_]+" + s
|
||||||
|
} else {
|
||||||
|
modNameReStr = s
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
re := regexp.MustCompile(modNameReStr)
|
||||||
|
|
||||||
fd, err := os.Open(modDep)
|
s := bufio.NewScanner(modulesDep)
|
||||||
if err != nil {
|
|
||||||
log.Print("Unable to open modules.dep: ", modDep)
|
|
||||||
return deps, err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer fd.Close()
|
|
||||||
s := bufio.NewScanner(fd)
|
|
||||||
for s.Scan() {
|
for s.Scan() {
|
||||||
fields := strings.Fields(s.Text())
|
fields := strings.Fields(s.Text())
|
||||||
fields[0] = strings.TrimSuffix(fields[0], ":")
|
if len(fields) == 0 {
|
||||||
if modName != filepath.Base(stripExts(fields[0])) {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
deps = append(deps, fields...)
|
fields[0] = strings.TrimSuffix(fields[0], ":")
|
||||||
|
|
||||||
|
found := re.FindAll([]byte(filepath.Base(stripExts(fields[0]))), -1)
|
||||||
|
if len(found) > 0 {
|
||||||
|
deps = append(deps, fields...)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := s.Err(); err != nil {
|
if err := s.Err(); err != nil {
|
||||||
log.Print("Unable to get module + dependencies: ", modName)
|
log.Print("Unable to get module + dependencies: ", modName)
|
||||||
|
Reference in New Issue
Block a user