github.com/replit/upm@v0.0.0-20240423230255-9ce4fc3ea24c/internal/backends/python/gen_pypi_map/bdist_reader.go (about) 1 package main 2 3 import ( 4 "path" 5 "strings" 6 ) 7 8 func ValidatePackage(name string, packageMap map[string]bool) bool { 9 // If we've already validated this package, don't do it again 10 if packageMap[name] { 11 return true 12 } 13 14 dir := path.Dir(name) 15 16 // Top level packages and modules are always valid 17 if dir == "." { 18 packageMap[name] = true 19 return true 20 } 21 22 // Nested packages are only valid if they are in valid packages 23 packageMap[name] = ValidatePackage(dir, packageMap) 24 return packageMap[name] 25 } 26 27 // Bdists are packaged such that they can be directly unzipped onto the python 28 // path to install them. As a consequence, the directory structure of a bdist 29 // is identical to the exposed modules 30 func ExtractBdist(reader ArchiveReader) ([]string, error) { 31 // Make a map of all possible modules in the archive (.py files or directories 32 // containing an __init__.py). This map will have false positives because 33 // sometimes a python file isn't actually a module (details below). 34 modules := make(map[string]bool, 0) 35 for reader.Next() { 36 name := reader.File() 37 38 if path.Ext(name) == ".py" { 39 dir := path.Dir(name) 40 41 if path.Base(name) == "__init__.py" { 42 // If this is a package, add it 43 modules[dir] = false 44 } else { 45 // If this is a top level module, add it 46 module := strings.TrimSuffix(name, ".py") 47 modules[module] = false 48 } 49 } 50 } 51 52 // Move modules into return array. A python file that is in a directory that 53 // does not contain an __init__ module is not an acessible module, unless 54 // that module is in the top level of the dist. 55 ret := make([]string, 0, len(modules)) 56 for pkg := range modules { 57 valid := ValidatePackage(pkg, modules) 58 if valid { 59 ret = append(ret, pkg) 60 } 61 } 62 return ret, nil 63 }