cycl 0.2.38__tar.gz → 0.2.39__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cycl
3
- Version: 0.2.38
3
+ Version: 0.2.39
4
4
  Summary: CLI and Python SDK to help identify cross-stack import/export circular dependencies, for a given AWS account and region.
5
5
  License: MIT
6
6
  Project-URL: Repository, http://github.com/tcm5343/cycl
@@ -2,23 +2,42 @@ from __future__ import annotations
2
2
 
3
3
  from logging import getLogger
4
4
  from pathlib import Path
5
+ from typing import TYPE_CHECKING, Callable
5
6
 
6
7
  import boto3
7
8
  import networkx as nx
8
9
  from botocore.config import Config
10
+ from botocore.session import Session
9
11
 
10
12
  from cycl.models.export_data import ExportData
11
13
  from cycl.utils.cdk import get_exports_from_assembly
12
14
 
15
+ if TYPE_CHECKING:
16
+ from collections.abc import Hashable
17
+
18
+
13
19
  log = getLogger(__name__)
14
20
 
15
21
 
16
- def get_graph_data(cdk_out_path: Path | None = None) -> dict[str, ExportData]:
17
- cdk_out_imports: dict[str, list[ExportData]] = get_exports_from_assembly(Path(cdk_out_path)) if cdk_out_path else {}
22
+ def get_graph_data(
23
+ cdk_out_path: Path | None = None,
24
+ aws_session: Session | None = None,
25
+ aws_profile_name: str | None = None,
26
+ ) -> dict[str, ExportData]:
27
+ cdk_out_imports: dict[str, list[ExportData]] = (
28
+ get_exports_from_assembly(Path(cdk_out_path)) if cdk_out_path is not None else {}
29
+ )
18
30
  log.info('cdk_out_imports: %s', cdk_out_imports)
19
31
 
32
+ # this logic should move to a module
33
+ # profile and session should not be able to be provided
20
34
  boto_config = Config(retries={'max_attempts': 10, 'mode': 'adaptive'})
21
- cfn_client = boto3.client('cloudformation', config=boto_config)
35
+ if aws_session:
36
+ cfn_client = aws_session.client('cloudformation', config=boto_config) # type: ignore[attr-defined]
37
+ elif aws_profile_name:
38
+ cfn_client = Session(profile_name=aws_profile_name).client('cloudformation', config=boto_config) # type: ignore[attr-defined,call-arg]
39
+ else:
40
+ cfn_client = boto3.client('cloudformation', config=boto_config)
22
41
 
23
42
  log.info('getting all exports')
24
43
  exports = ExportData.get_all_exports(cfn_client=cfn_client)
@@ -40,29 +59,37 @@ def get_graph_data(cdk_out_path: Path | None = None) -> dict[str, ExportData]:
40
59
  return exports
41
60
 
42
61
 
43
- def build_graph(
62
+ def build_graph( # noqa: PLR0913
44
63
  graph_data: dict[str, ExportData] | None = None,
45
64
  cdk_out_path: Path | None = None,
46
- # node_key: Callable[[ExportData], str] = lambda x: x.stack_name,
65
+ node_key_fn: Callable[[ExportData], Hashable] = lambda x: x.stack_name,
47
66
  nodes_to_ignore: list[str] | None = None,
48
67
  edges_to_ignore: list[list[str]] | None = None,
68
+ aws_session: Session | None = None,
69
+ aws_profile_name: str | None = None,
70
+ *,
71
+ remove_selfloops: bool = False,
49
72
  ) -> nx.MultiDiGraph:
50
73
  nodes_to_ignore = nodes_to_ignore or []
51
74
  edges_to_ignore = edges_to_ignore or []
52
- graph_data = get_graph_data(cdk_out_path) if graph_data is None else graph_data
75
+ graph_data = (
76
+ get_graph_data(cdk_out_path=cdk_out_path, aws_session=aws_session, aws_profile_name=aws_profile_name)
77
+ if graph_data is None
78
+ else graph_data
79
+ )
53
80
 
54
81
  log.info('building dependency graph from graph data')
55
82
  dep_graph: nx.MultiDiGraph = nx.MultiDiGraph()
56
83
  for export in graph_data.values():
57
- # export_key = node_key(export)
58
- if export.stack_name in nodes_to_ignore:
84
+ export_key = node_key_fn(export)
85
+ if export_key in nodes_to_ignore:
59
86
  continue
60
87
 
61
88
  edges = []
62
89
  for importing_stack in export.importing_stacks:
63
- # importing_key = node_key(importing_stack)
64
- if importing_stack.stack_name not in nodes_to_ignore:
65
- edge = (export.stack_name, importing_stack.stack_name)
90
+ importing_key = node_key_fn(importing_stack)
91
+ if importing_key not in nodes_to_ignore:
92
+ edge = (export_key, importing_key)
66
93
  if list(edge) not in edges_to_ignore:
67
94
  edges.append(edge)
68
95
 
@@ -70,5 +97,9 @@ def build_graph(
70
97
  dep_graph.add_edges_from(ebunch_to_add=edges)
71
98
  else:
72
99
  # export key
73
- dep_graph.add_node(export.stack_name)
100
+ dep_graph.add_node(export_key)
101
+
102
+ if remove_selfloops:
103
+ dep_graph.remove_edges_from(nx.selfloop_edges(dep_graph))
104
+
74
105
  return dep_graph
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cycl
3
- Version: 0.2.38
3
+ Version: 0.2.39
4
4
  Summary: CLI and Python SDK to help identify cross-stack import/export circular dependencies, for a given AWS account and region.
5
5
  License: MIT
6
6
  Project-URL: Repository, http://github.com/tcm5343/cycl
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes