MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / buildPythonModuleIndex

Function buildPythonModuleIndex

src/resolution/import-resolver.ts:704–761  ·  view source on GitHub ↗
(context: ResolutionContext)

Source from the content-addressed store, hash-verified

702}
703
704function buildPythonModuleIndex(context: ResolutionContext): PythonModuleIndex {
705 const files = new Set<string>();
706 const packageDirs = new Set<string>();
707 const dirs = new Set<string>(['']);
708 for (const f of context.getAllFiles()) {
709 const norm = f.replace(/\\/g, '/');
710 if (!PYTHON_MODULE_FILE.test(norm)) continue;
711 files.add(norm);
712 const dir = parentDir(norm);
713 if (norm === '__init__.py' || norm.endsWith('/__init__.py')) packageDirs.add(dir);
714 for (let d = dir; d && !dirs.has(d); d = parentDir(d)) dirs.add(d);
715 }
716
717 // Where a service's roots may answer from: everywhere (null), only itself
718 // when a package encloses it, or the nearest enclosing directory named as
719 // non-project code.
720 const serviceReach = (service: string): string | null => {
721 if (packageDirs.has(service)) return service;
722 for (let a = parentDir(service); a; a = parentDir(a)) {
723 if (packageDirs.has(a)) return service;
724 if (PYTHON_NON_PROJECT_DIRS.has(a.slice(a.lastIndexOf('/') + 1))) return a;
725 }
726 return null;
727 };
728
729 const projectRoots = new Set<string>(['']);
730 const localRoots = new Map<string, string>();
731 const addLocal = (root: string, reach: string): void => {
732 const prior = localRoots.get(root);
733 // Two claims on one root: the wider reach wins.
734 if (prior === undefined || isWithin(prior, reach)) localRoots.set(root, reach);
735 };
736 for (const dir of packageDirs) {
737 let top = dir;
738 while (top && packageDirs.has(parentDir(top))) top = parentDir(top);
739 if (top) addLocal(parentDir(top), parentDir(top));
740 }
741 for (const service of dirs) {
742 const hasConfig =
743 files.has(joinRel(service, 'setup.py')) ||
744 context.fileExists(joinRel(service, 'pyproject.toml')) ||
745 context.fileExists(joinRel(service, 'setup.cfg'));
746 if (!hasConfig && service !== '') continue;
747 const reach = service === '' ? null : serviceReach(service);
748 const roots = [service];
749 const src = joinRel(service, 'src');
750 if (dirs.has(src) && !packageDirs.has(src)) roots.push(src);
751 for (const declared of declaredPythonRoots(context, service)) {
752 if (dirs.has(declared)) roots.push(declared);
753 }
754 for (const root of roots) {
755 if (reach === null) projectRoots.add(root);
756 else addLocal(root, reach);
757 }
758 }
759 for (const root of projectRoots) localRoots.delete(root);
760 return { files, packageDirs, dirs, projectRoots, localRoots, headsByName: new Map() };
761}

Callers 3

pythonModuleIndexFunction · 0.85
pythonRootFingerprintFunction · 0.85
pythonReopenScopeFunction · 0.85

Calls 11

addLocalFunction · 0.85
joinRelFunction · 0.85
serviceReachFunction · 0.85
declaredPythonRootsFunction · 0.85
hasMethod · 0.80
fileExistsMethod · 0.80
pushMethod · 0.80
parentDirFunction · 0.70
getAllFilesMethod · 0.65
deleteMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected