-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this change will return a depgraph instead of a deptree from the plugin because a package can only have one version, it doesn't make sense to create it more than once in "pip_resolve.py" script this change will remove the duplicates from the scripts and replace them with a "true" value later the "buildDepGraph" function will iterate over the tree and replace all "true" values with a reference to the original package this will improve performance for those reasons: 1. no serializing dep tree with repeating dependencies 2. no deserializing repeating dependencies (memory and cpu affects) 3. return a depGraph
- Loading branch information
Showing
11 changed files
with
4,139 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { DepGraph } from '@snyk/dep-graph'; | ||
import { depTreeToGraph, DepTree } from '@snyk/dep-graph/dist/legacy'; | ||
|
||
type PackageName = string; | ||
|
||
// This is a partially dep tree: every package exists once, all additional reference will have "true" as the value | ||
export interface PartialDepTree { | ||
name?: string; | ||
version?: string; | ||
dependencies?: Dependencies; | ||
labels?: { | ||
[key: string]: string; | ||
}; | ||
} | ||
|
||
type Dependencies = { | ||
[depName: string]: PartialDepTree | 'true'; | ||
}; | ||
|
||
export function buildDepGraph( | ||
partialDepTree: PartialDepTree | ||
): Promise<DepGraph> { | ||
const packageToDepTreeMap = new Map<PackageName, PartialDepTree>(); | ||
|
||
const queue: Dependencies[] = [partialDepTree.dependencies]; | ||
const referencesToUpdate: { key: string; dependencies: Dependencies }[] = []; | ||
while (queue.length > 0) { | ||
const dependencies = queue.pop(); | ||
if (!dependencies) continue; | ||
|
||
for (const [key, dependencyDepTree] of Object.entries(dependencies)) { | ||
if (dependencyDepTree === 'true') { | ||
referencesToUpdate.push({ key, dependencies }); | ||
} else { | ||
packageToDepTreeMap.set(key, dependencyDepTree); | ||
queue.push(dependencyDepTree.dependencies); | ||
} | ||
} | ||
} | ||
|
||
referencesToUpdate.forEach(({ key, dependencies }) => { | ||
if (!packageToDepTreeMap.get(key)) { | ||
// this should never happen | ||
throw new Error(`key ${key} not found in packageToDepTreeMap`); | ||
} | ||
dependencies[key] = packageToDepTreeMap.get(key); | ||
}); | ||
|
||
return depTreeToGraph(partialDepTree as DepTree, 'pip'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.