MCPcopy Index your code
hub / github.com/celer-pkg/celer / isPackageInstalled

Function isPackageInstalled

buildtools/setup_python.go:265–308  ·  view source on GitHub ↗

isPackageInstalled checks if a Python package is already installed. This avoids frequent PyPI requests that could lead to IP blocking.

(packageName string, venvDir string)

Source from the content-addressed store, hash-verified

263// isPackageInstalled checks if a Python package is already installed.
264// This avoids frequent PyPI requests that could lead to IP blocking.
265func isPackageInstalled(packageName string, venvDir string) bool {
266 libDir := filepath.Join(venvDir, expr.If(runtime.GOOS == "windows", "Lib", "lib"))
267 if !fileio.PathExists(libDir) {
268 return false
269 }
270
271 // Get package name without version.
272 packageName = strings.Split(packageName, "==")[0]
273
274 var packageDirPattern, distInfoPattern, eggInfoPattern string
275 switch runtime.GOOS {
276 case "windows":
277 // Windows: Lib/site-packages/{packageName} and Lib/site-packages/{packageName}-*.dist-info.
278 packageDirPattern = filepath.Join(libDir, "site-packages", packageName)
279 distInfoPattern = filepath.Join(libDir, "site-packages", packageName+"-*.dist-info")
280 eggInfoPattern = filepath.Join(libDir, "site-packages", packageName+"-*.egg-info")
281
282 case "linux", "darwin":
283 // Linux/Darwin: lib/python*/site-packages/{packageName} and lib/python*/site-packages/{packageName}-*.dist-info.
284 packageDirPattern = filepath.Join(libDir, "python*", "site-packages", packageName)
285 distInfoPattern = filepath.Join(libDir, "python*", "site-packages", packageName+"-*.dist-info")
286 eggInfoPattern = filepath.Join(libDir, "python*", "site-packages", packageName+"-*.egg-info")
287
288 default:
289 panic("unsupported os: " + runtime.GOOS)
290 }
291
292 matches, err := filepath.Glob(packageDirPattern)
293 if err == nil && len(matches) > 0 {
294 return true
295 }
296
297 matches, err = filepath.Glob(distInfoPattern)
298 if err == nil && len(matches) > 0 {
299 return true
300 }
301
302 matches, err = filepath.Glob(eggInfoPattern)
303 if err == nil && len(matches) > 0 {
304 return true
305 }
306
307 return false
308}
309
310// getWindowsDefaultPythonVersion reads Python version from static TOML.
311func getWindowsDefaultPythonVersion() string {

Callers 1

pip3InstallFunction · 0.85

Calls 2

IfFunction · 0.92
PathExistsFunction · 0.92

Tested by

no test coverage detected