|
| 1 | +package main |
| 2 | + |
| 3 | +import "errors" |
| 4 | +import "flag" |
| 5 | +import "fmt" |
| 6 | +import "log" |
| 7 | +import "os" |
| 8 | +import "os/exec" |
| 9 | +import "path/filepath" |
| 10 | +import "strings" |
| 11 | + |
| 12 | +func getSparkHome() (sparkHome string, err error) { |
| 13 | + var sparkHomeEnv = os.Getenv("SPARK_HOME") |
| 14 | + if sparkHomeEnv != "" { |
| 15 | + sparkHome = sparkHomeEnv |
| 16 | + } else { |
| 17 | + maprHome := os.Getenv("MAPR_HOME") |
| 18 | + if maprHome != "" { |
| 19 | + sparkVersionFileLocation := filepath.Join(maprHome, "spark/sparkversion") |
| 20 | + sparkVersionBytes, e := os.ReadFile(sparkVersionFileLocation) |
| 21 | + if e == nil { |
| 22 | + var sparkVersion = string(sparkVersionBytes) |
| 23 | + sparkVersion = strings.Trim(sparkVersion, " \n") |
| 24 | + if sparkVersion != "" { |
| 25 | + sparkHome = filepath.Join(maprHome, fmt.Sprintf("spark/spark-%s", sparkVersion)) |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + if sparkHome == "" { |
| 30 | + sparkHome, _ = os.Readlink("/usr/local/spark") |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + if sparkHome == "" { |
| 35 | + err = errors.New("Can not find SPARK_HOME!") |
| 36 | + } |
| 37 | + return sparkHome, err |
| 38 | +} |
| 39 | + |
| 40 | +func getBlacklist(blacklistFileLocation string) (blacklist []string, err error) { |
| 41 | + blacklistBytes, err := os.ReadFile(blacklistFileLocation) |
| 42 | + if err != nil { |
| 43 | + return blacklist, fmt.Errorf("Can not read dep-blacklist.txt configuration file: %s\n", err) |
| 44 | + } |
| 45 | + blacklistString := string(blacklistBytes) |
| 46 | + blacklist = strings.Fields(blacklistString) |
| 47 | + |
| 48 | + return blacklist, nil |
| 49 | +} |
| 50 | + |
| 51 | +func parseClasspathString(classpathString string) []string { |
| 52 | + classpathSeparator := func(c rune) bool { |
| 53 | + return c == ':' |
| 54 | + } |
| 55 | + return strings.FieldsFunc(classpathString, classpathSeparator) |
| 56 | +} |
| 57 | + |
| 58 | +func getMaprClasspath() (maprClasspath []string, err error) { |
| 59 | + maprClasspathCmd := exec.Command("mapr", "classpath") |
| 60 | + maprClasspathOutput, err := maprClasspathCmd.Output() |
| 61 | + if err != nil { |
| 62 | + return maprClasspath, fmt.Errorf("Error executing 'mapr classpath' command: %s\n", err) |
| 63 | + } |
| 64 | + |
| 65 | + var maprClasspathString = string(maprClasspathOutput) |
| 66 | + maprClasspathString = strings.Trim(maprClasspathString, " \n") |
| 67 | + if maprClasspathString == "" { |
| 68 | + return maprClasspath, errors.New("Output of 'mapr classpath' command is empty!") |
| 69 | + } |
| 70 | + |
| 71 | + maprClasspath = parseClasspathString(maprClasspathString) |
| 72 | + |
| 73 | + return maprClasspath, nil |
| 74 | +} |
| 75 | + |
| 76 | +type Classpath struct { |
| 77 | + entries []string |
| 78 | + _duplicateDict map[string]bool |
| 79 | + _blacklistDict map[string]bool |
| 80 | + _blacklistPatterns []string |
| 81 | +} |
| 82 | + |
| 83 | +func NewClasspath() *Classpath { |
| 84 | + cp := new(Classpath) |
| 85 | + cp.entries = make([]string, 0, 2048) |
| 86 | + cp._duplicateDict = make(map[string]bool, 2048) |
| 87 | + cp._blacklistDict = make(map[string]bool, 256) |
| 88 | + cp._blacklistPatterns = make([]string, 256) |
| 89 | + |
| 90 | + return cp |
| 91 | +} |
| 92 | + |
| 93 | +func (cp *Classpath) SetBlacklist(blacklist []string) { |
| 94 | + isPattern := func (path string) bool { |
| 95 | + magicChars := `*?[\` |
| 96 | + return strings.ContainsAny(path, magicChars) |
| 97 | + } |
| 98 | + |
| 99 | + for _, entry := range blacklist { |
| 100 | + if isPattern(entry) { |
| 101 | + cp._blacklistPatterns = append(cp._blacklistPatterns, entry) |
| 102 | + } else { |
| 103 | + cp._blacklistDict[entry] = true |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func (cp *Classpath) AppendEntry(entry string) { |
| 109 | + if _, found := cp._duplicateDict[entry]; found { |
| 110 | + return |
| 111 | + } |
| 112 | + if _, found := cp._blacklistDict[entry]; found { |
| 113 | + return |
| 114 | + } |
| 115 | + for _, pattern := range cp._blacklistPatterns { |
| 116 | + if matched, _ := filepath.Match(pattern, entry); matched { |
| 117 | + return |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + cp.entries = append(cp.entries, entry) |
| 122 | + cp._duplicateDict[entry] = true |
| 123 | +} |
| 124 | + |
| 125 | +func (cp *Classpath) AsString() string { |
| 126 | + return strings.Join(cp.entries, ":") |
| 127 | +} |
| 128 | + |
| 129 | +func classpathFilter(classpath []string, blacklist []string) string { |
| 130 | + cp := NewClasspath() |
| 131 | + cp.SetBlacklist(blacklist) |
| 132 | + |
| 133 | + for _, classpathEntry := range classpath { |
| 134 | + if classpathEntry[0] == '/' && strings.HasSuffix(classpathEntry, "/*") { |
| 135 | + classpathEntryLocation := classpathEntry[:len(classpathEntry)-1] |
| 136 | + |
| 137 | + /* |
| 138 | + * Original ClasspathFilter from Spark scan directories recursively. |
| 139 | + * However, it's most likely this is a bug. |
| 140 | + */ |
| 141 | + // files, err := filepath.WalkDir(classpathEntryLocation) |
| 142 | + // ... |
| 143 | + |
| 144 | + files, err := os.ReadDir(classpathEntryLocation) |
| 145 | + if err != nil { |
| 146 | + // log.Printf("Can not read '%s' directory: %s\n", classpathEntry, err) |
| 147 | + } |
| 148 | + for _, file := range files { |
| 149 | + filename := file.Name() |
| 150 | + filetype := file.Type() |
| 151 | + |
| 152 | + if (filetype == 0 || filetype&os.ModeSymlink != 0) && |
| 153 | + strings.HasSuffix(filename, ".jar") { |
| 154 | + fullpath := filepath.Join(classpathEntryLocation, filename) |
| 155 | + cp.AppendEntry(fullpath) |
| 156 | + } |
| 157 | + } |
| 158 | + } else { |
| 159 | + if _, err := os.Stat(classpathEntry); err == nil { // remove non existing files |
| 160 | + cp.AppendEntry(classpathEntry) |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return cp.AsString() |
| 166 | +} |
| 167 | + |
| 168 | +func main() { |
| 169 | + flag.Usage = func() { |
| 170 | + filename := filepath.Base(os.Args[0]) |
| 171 | + msg := "Usage of %s: [<CLASSPATH>]\n" + |
| 172 | + " -b path\n" + |
| 173 | + " path to blacklist configuration (default \"$SPARK_HOME/conf/dep-blacklist.txt\")\n" + |
| 174 | + "\n" + |
| 175 | + "Filter mapr classpath using rules from dep-blacklist.txt.\n" |
| 176 | + fmt.Printf(msg, filename) |
| 177 | + } |
| 178 | + blacklistLocationArg := flag.String("b", "", "path to blacklist configuration") |
| 179 | + flag.Parse() |
| 180 | + |
| 181 | + args := flag.Args() |
| 182 | + |
| 183 | + var classpathArg string |
| 184 | + if len(args) > 1 { |
| 185 | + fmt.Println("invalid number of arguments") |
| 186 | + flag.Usage() |
| 187 | + os.Exit(1) |
| 188 | + } else if len(args) == 1 { |
| 189 | + classpathArg = args[0] |
| 190 | + } |
| 191 | + |
| 192 | + var blacklistFileLocation string |
| 193 | + if *blacklistLocationArg != "" { |
| 194 | + blacklistFileLocation = *blacklistLocationArg |
| 195 | + } else { |
| 196 | + sparkHome, err := getSparkHome() |
| 197 | + if err != nil { |
| 198 | + log.Fatalln(err) |
| 199 | + } |
| 200 | + blacklistFileLocation = filepath.Join(sparkHome, "conf/dep-blacklist.txt") |
| 201 | + } |
| 202 | + |
| 203 | + blacklist, err := getBlacklist(blacklistFileLocation) |
| 204 | + if err != nil { |
| 205 | + log.Fatalln(err) |
| 206 | + } |
| 207 | + |
| 208 | + var maprClasspath []string |
| 209 | + if classpathArg != "" { |
| 210 | + maprClasspath = parseClasspathString(classpathArg) |
| 211 | + } else { |
| 212 | + maprClasspath, err = getMaprClasspath() |
| 213 | + if err != nil { |
| 214 | + log.Fatalln(err) |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + classpath := classpathFilter(maprClasspath, blacklist) |
| 219 | + fmt.Print(classpath) |
| 220 | +} |
0 commit comments