codeclone 1.0.0__tar.gz → 1.1.0__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.
- codeclone-1.1.0/PKG-INFO +254 -0
- codeclone-1.1.0/README.md +217 -0
- codeclone-1.1.0/codeclone/__init__.py +16 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone/baseline.py +8 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone/blockhash.py +10 -1
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone/blocks.py +26 -16
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone/cache.py +8 -0
- codeclone-1.1.0/codeclone/cfg.py +173 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone/cli.py +92 -58
- codeclone-1.1.0/codeclone/extractor.py +169 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone/fingerprint.py +11 -1
- codeclone-1.1.0/codeclone/html_report.py +953 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone/normalize.py +50 -26
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone/report.py +25 -9
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone/scanner.py +24 -4
- codeclone-1.1.0/codeclone.egg-info/PKG-INFO +254 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone.egg-info/SOURCES.txt +3 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone.egg-info/requires.txt +2 -0
- codeclone-1.1.0/pyproject.toml +75 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/tests/test_baseline.py +1 -1
- {codeclone-1.0.0 → codeclone-1.1.0}/tests/test_blocks.py +1 -1
- codeclone-1.1.0/tests/test_cfg.py +133 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/tests/test_cli_smoke.py +1 -1
- {codeclone-1.0.0 → codeclone-1.1.0}/tests/test_extractor.py +1 -1
- {codeclone-1.0.0 → codeclone-1.1.0}/tests/test_report.py +1 -1
- codeclone-1.0.0/PKG-INFO +0 -211
- codeclone-1.0.0/README.md +0 -178
- codeclone-1.0.0/codeclone/__init__.py +0 -0
- codeclone-1.0.0/codeclone/extractor.py +0 -109
- codeclone-1.0.0/codeclone.egg-info/PKG-INFO +0 -211
- codeclone-1.0.0/pyproject.toml +0 -70
- {codeclone-1.0.0 → codeclone-1.1.0}/LICENSE +0 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone.egg-info/dependency_links.txt +0 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone.egg-info/entry_points.txt +0 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/codeclone.egg-info/top_level.txt +0 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/setup.cfg +0 -0
- {codeclone-1.0.0 → codeclone-1.1.0}/tests/test_normalize.py +0 -0
codeclone-1.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: codeclone
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: AST and CFG-based code clone detector for Python focused on architectural duplication
|
|
5
|
+
Author-email: Den Rozhnovskiy <pytelemonbot@mail.ru>
|
|
6
|
+
Maintainer-email: Den Rozhnovskiy <pytelemonbot@mail.ru>
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/orenlab/codeclone
|
|
9
|
+
Project-URL: Repository, https://github.com/orenlab/codeclone
|
|
10
|
+
Project-URL: Issues, https://github.com/orenlab/codeclone/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/orenlab/codeclone/releases
|
|
12
|
+
Project-URL: Documentation, https://github.com/orenlab/codeclone/tree/main/docs
|
|
13
|
+
Keywords: python,ast,code-clone,duplication,static-analysis,ci,architecture
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
17
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
18
|
+
Classifier: Topic :: Software Development :: Testing
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
21
|
+
Classifier: Programming Language :: Python :: 3
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
26
|
+
Classifier: Operating System :: OS Independent
|
|
27
|
+
Requires-Python: >=3.10
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Dist: pygments>=2.19.2
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=9.0.0; extra == "dev"
|
|
33
|
+
Requires-Dist: build>=1.2.0; extra == "dev"
|
|
34
|
+
Requires-Dist: twine>=5.0.0; extra == "dev"
|
|
35
|
+
Requires-Dist: mypy>=1.19.1; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# CodeClone
|
|
39
|
+
|
|
40
|
+
[](https://pypi.org/project/codeclone/)
|
|
41
|
+
[](https://pypi.org/project/codeclone/)
|
|
42
|
+
[](https://pypi.org/project/codeclone/)
|
|
43
|
+
[](LICENSE)
|
|
44
|
+
|
|
45
|
+
**CodeClone** is a Python code clone detector based on **normalized AST and control-flow graphs (CFG)**.
|
|
46
|
+
It helps teams discover architectural duplication and prevent new copy-paste from entering the codebase via CI.
|
|
47
|
+
|
|
48
|
+
CodeClone is designed to help teams:
|
|
49
|
+
|
|
50
|
+
- discover **structural and control-flow duplication**,
|
|
51
|
+
- identify architectural hotspots,
|
|
52
|
+
- prevent *new* duplication via CI and pre-commit hooks.
|
|
53
|
+
|
|
54
|
+
Unlike token- or text-based tools, CodeClone operates on **normalized Python AST and CFG**, making it robust against renaming,
|
|
55
|
+
formatting, and minor refactoring.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## Why CodeClone?
|
|
60
|
+
|
|
61
|
+
Most existing tools detect *textual* duplication.
|
|
62
|
+
CodeClone detects **structural and block-level duplication**, which usually signals missing abstractions or architectural drift.
|
|
63
|
+
|
|
64
|
+
Typical use cases:
|
|
65
|
+
|
|
66
|
+
- duplicated service or orchestration logic across layers (API ↔ application),
|
|
67
|
+
- repeated validation or guard blocks,
|
|
68
|
+
- copy-pasted request / handler flows,
|
|
69
|
+
- duplicated control-flow logic in routers, handlers, or services.
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Features
|
|
74
|
+
|
|
75
|
+
### Function-level clone detection (Type-2, CFG-based)
|
|
76
|
+
|
|
77
|
+
- Detects functions and methods with identical **control-flow structure**.
|
|
78
|
+
- Based on **Control Flow Graph (CFG)** fingerprinting.
|
|
79
|
+
- Robust to:
|
|
80
|
+
- variable renaming,
|
|
81
|
+
- constant changes,
|
|
82
|
+
- attribute renaming,
|
|
83
|
+
- formatting differences,
|
|
84
|
+
- docstrings and type annotations.
|
|
85
|
+
- Ideal for spotting architectural duplication across layers.
|
|
86
|
+
|
|
87
|
+
### Block-level clone detection (Type-3-lite)
|
|
88
|
+
|
|
89
|
+
- Detects repeated **statement blocks** inside larger functions.
|
|
90
|
+
- Uses sliding windows over CFG-normalized statement sequences.
|
|
91
|
+
- Targets:
|
|
92
|
+
- validation blocks,
|
|
93
|
+
- guard clauses,
|
|
94
|
+
- repeated orchestration logic.
|
|
95
|
+
- Carefully filtered to reduce noise:
|
|
96
|
+
- no overlapping windows,
|
|
97
|
+
- no clones inside the same function,
|
|
98
|
+
- no `__init__` noise,
|
|
99
|
+
- size and statement-count thresholds.
|
|
100
|
+
|
|
101
|
+
### Control-Flow Awareness (CFG v1)
|
|
102
|
+
|
|
103
|
+
- Each function is converted into a **Control Flow Graph**.
|
|
104
|
+
- CFG nodes contain normalized AST statements.
|
|
105
|
+
- CFG edges represent structural control flow (`if`, `for`, `while`).
|
|
106
|
+
- Current CFG semantics (v1):
|
|
107
|
+
- `break` and `continue` are treated as statements (no jump targets),
|
|
108
|
+
- after-blocks are explicit and always present,
|
|
109
|
+
- focus is on **structural similarity**, not precise runtime semantics.
|
|
110
|
+
|
|
111
|
+
This design keeps clone detection **stable, deterministic, and low-noise**.
|
|
112
|
+
|
|
113
|
+
### Low-noise by design
|
|
114
|
+
|
|
115
|
+
- AST + CFG normalization instead of token matching.
|
|
116
|
+
- Conservative defaults tuned for real-world Python projects.
|
|
117
|
+
- Explicit thresholds for size and statement count.
|
|
118
|
+
- Focus on *architectural duplication*, not micro-similarities.
|
|
119
|
+
|
|
120
|
+
### CI-friendly baseline mode
|
|
121
|
+
|
|
122
|
+
- Establish a baseline of existing clones.
|
|
123
|
+
- Fail CI **only when new clones are introduced**.
|
|
124
|
+
- Safe for legacy codebases and incremental refactoring.
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## Installation
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
pip install codeclone
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Python **3.10+** is required.
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## Quick Start
|
|
139
|
+
|
|
140
|
+
Run on a project:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
codeclone .
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
This will:
|
|
147
|
+
|
|
148
|
+
- scan Python files,
|
|
149
|
+
- build CFGs for functions,
|
|
150
|
+
- detect function-level and block-level clones,
|
|
151
|
+
- print a summary to stdout.
|
|
152
|
+
|
|
153
|
+
Generate reports:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
codeclone . \
|
|
157
|
+
--json-out .cache/codeclone/report.json \
|
|
158
|
+
--text-out .cache/codeclone/report.txt
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Generate an HTML report:
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
codeclone . --html-out .cache/codeclone/report.html
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Baseline Workflow (Recommended)
|
|
170
|
+
|
|
171
|
+
### 1. Create a baseline
|
|
172
|
+
|
|
173
|
+
Run once on your current codebase:
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
codeclone . --update-baseline
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Commit the generated baseline file to the repository.
|
|
180
|
+
|
|
181
|
+
### 2. Use in CI
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
codeclone . --fail-on-new
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Behavior:
|
|
188
|
+
|
|
189
|
+
- ✅ existing clones are allowed,
|
|
190
|
+
- ❌ build fails if *new* clones appear,
|
|
191
|
+
- ✅ refactoring that removes duplication is always allowed.
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## Using with pre-commit
|
|
196
|
+
|
|
197
|
+
```yaml
|
|
198
|
+
repos:
|
|
199
|
+
- repo: local
|
|
200
|
+
hooks:
|
|
201
|
+
- id: codeclone
|
|
202
|
+
name: CodeClone
|
|
203
|
+
entry: codeclone
|
|
204
|
+
language: python
|
|
205
|
+
args: [".", "--fail-on-new"]
|
|
206
|
+
types: [python]
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
## What CodeClone Is (and Is Not)
|
|
212
|
+
|
|
213
|
+
### CodeClone **is**
|
|
214
|
+
|
|
215
|
+
- an architectural analysis tool,
|
|
216
|
+
- a duplication radar,
|
|
217
|
+
- a CI guard against copy-paste,
|
|
218
|
+
- a control-flow-aware clone detector.
|
|
219
|
+
|
|
220
|
+
### CodeClone **is not**
|
|
221
|
+
|
|
222
|
+
- a linter,
|
|
223
|
+
- a formatter,
|
|
224
|
+
- a semantic equivalence prover,
|
|
225
|
+
- a runtime analyzer.
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## How It Works (High Level)
|
|
230
|
+
|
|
231
|
+
1. Parse Python source into AST.
|
|
232
|
+
2. Normalize AST (names, constants, attributes, annotations).
|
|
233
|
+
3. Build a **Control Flow Graph (CFG)** per function.
|
|
234
|
+
4. Compute stable CFG fingerprints.
|
|
235
|
+
5. Detect function-level and block-level clones.
|
|
236
|
+
6. Apply conservative filters to suppress noise.
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## Control Flow Graph (CFG)
|
|
241
|
+
|
|
242
|
+
Starting from **version 1.1.0**, CodeClone uses a **Control Flow Graph (CFG)**
|
|
243
|
+
to improve structural clone detection robustness.
|
|
244
|
+
|
|
245
|
+
The CFG is a **structural abstraction**, not a runtime execution model.
|
|
246
|
+
|
|
247
|
+
See full design and semantics:
|
|
248
|
+
- [docs/cfg.md](docs/cfg.md)
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## License
|
|
253
|
+
|
|
254
|
+
MIT License
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# CodeClone
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/codeclone/)
|
|
4
|
+
[](https://pypi.org/project/codeclone/)
|
|
5
|
+
[](https://pypi.org/project/codeclone/)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
**CodeClone** is a Python code clone detector based on **normalized AST and control-flow graphs (CFG)**.
|
|
9
|
+
It helps teams discover architectural duplication and prevent new copy-paste from entering the codebase via CI.
|
|
10
|
+
|
|
11
|
+
CodeClone is designed to help teams:
|
|
12
|
+
|
|
13
|
+
- discover **structural and control-flow duplication**,
|
|
14
|
+
- identify architectural hotspots,
|
|
15
|
+
- prevent *new* duplication via CI and pre-commit hooks.
|
|
16
|
+
|
|
17
|
+
Unlike token- or text-based tools, CodeClone operates on **normalized Python AST and CFG**, making it robust against renaming,
|
|
18
|
+
formatting, and minor refactoring.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Why CodeClone?
|
|
23
|
+
|
|
24
|
+
Most existing tools detect *textual* duplication.
|
|
25
|
+
CodeClone detects **structural and block-level duplication**, which usually signals missing abstractions or architectural drift.
|
|
26
|
+
|
|
27
|
+
Typical use cases:
|
|
28
|
+
|
|
29
|
+
- duplicated service or orchestration logic across layers (API ↔ application),
|
|
30
|
+
- repeated validation or guard blocks,
|
|
31
|
+
- copy-pasted request / handler flows,
|
|
32
|
+
- duplicated control-flow logic in routers, handlers, or services.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
### Function-level clone detection (Type-2, CFG-based)
|
|
39
|
+
|
|
40
|
+
- Detects functions and methods with identical **control-flow structure**.
|
|
41
|
+
- Based on **Control Flow Graph (CFG)** fingerprinting.
|
|
42
|
+
- Robust to:
|
|
43
|
+
- variable renaming,
|
|
44
|
+
- constant changes,
|
|
45
|
+
- attribute renaming,
|
|
46
|
+
- formatting differences,
|
|
47
|
+
- docstrings and type annotations.
|
|
48
|
+
- Ideal for spotting architectural duplication across layers.
|
|
49
|
+
|
|
50
|
+
### Block-level clone detection (Type-3-lite)
|
|
51
|
+
|
|
52
|
+
- Detects repeated **statement blocks** inside larger functions.
|
|
53
|
+
- Uses sliding windows over CFG-normalized statement sequences.
|
|
54
|
+
- Targets:
|
|
55
|
+
- validation blocks,
|
|
56
|
+
- guard clauses,
|
|
57
|
+
- repeated orchestration logic.
|
|
58
|
+
- Carefully filtered to reduce noise:
|
|
59
|
+
- no overlapping windows,
|
|
60
|
+
- no clones inside the same function,
|
|
61
|
+
- no `__init__` noise,
|
|
62
|
+
- size and statement-count thresholds.
|
|
63
|
+
|
|
64
|
+
### Control-Flow Awareness (CFG v1)
|
|
65
|
+
|
|
66
|
+
- Each function is converted into a **Control Flow Graph**.
|
|
67
|
+
- CFG nodes contain normalized AST statements.
|
|
68
|
+
- CFG edges represent structural control flow (`if`, `for`, `while`).
|
|
69
|
+
- Current CFG semantics (v1):
|
|
70
|
+
- `break` and `continue` are treated as statements (no jump targets),
|
|
71
|
+
- after-blocks are explicit and always present,
|
|
72
|
+
- focus is on **structural similarity**, not precise runtime semantics.
|
|
73
|
+
|
|
74
|
+
This design keeps clone detection **stable, deterministic, and low-noise**.
|
|
75
|
+
|
|
76
|
+
### Low-noise by design
|
|
77
|
+
|
|
78
|
+
- AST + CFG normalization instead of token matching.
|
|
79
|
+
- Conservative defaults tuned for real-world Python projects.
|
|
80
|
+
- Explicit thresholds for size and statement count.
|
|
81
|
+
- Focus on *architectural duplication*, not micro-similarities.
|
|
82
|
+
|
|
83
|
+
### CI-friendly baseline mode
|
|
84
|
+
|
|
85
|
+
- Establish a baseline of existing clones.
|
|
86
|
+
- Fail CI **only when new clones are introduced**.
|
|
87
|
+
- Safe for legacy codebases and incremental refactoring.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Installation
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
pip install codeclone
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Python **3.10+** is required.
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Quick Start
|
|
102
|
+
|
|
103
|
+
Run on a project:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
codeclone .
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
This will:
|
|
110
|
+
|
|
111
|
+
- scan Python files,
|
|
112
|
+
- build CFGs for functions,
|
|
113
|
+
- detect function-level and block-level clones,
|
|
114
|
+
- print a summary to stdout.
|
|
115
|
+
|
|
116
|
+
Generate reports:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
codeclone . \
|
|
120
|
+
--json-out .cache/codeclone/report.json \
|
|
121
|
+
--text-out .cache/codeclone/report.txt
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Generate an HTML report:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
codeclone . --html-out .cache/codeclone/report.html
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Baseline Workflow (Recommended)
|
|
133
|
+
|
|
134
|
+
### 1. Create a baseline
|
|
135
|
+
|
|
136
|
+
Run once on your current codebase:
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
codeclone . --update-baseline
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Commit the generated baseline file to the repository.
|
|
143
|
+
|
|
144
|
+
### 2. Use in CI
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
codeclone . --fail-on-new
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Behavior:
|
|
151
|
+
|
|
152
|
+
- ✅ existing clones are allowed,
|
|
153
|
+
- ❌ build fails if *new* clones appear,
|
|
154
|
+
- ✅ refactoring that removes duplication is always allowed.
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## Using with pre-commit
|
|
159
|
+
|
|
160
|
+
```yaml
|
|
161
|
+
repos:
|
|
162
|
+
- repo: local
|
|
163
|
+
hooks:
|
|
164
|
+
- id: codeclone
|
|
165
|
+
name: CodeClone
|
|
166
|
+
entry: codeclone
|
|
167
|
+
language: python
|
|
168
|
+
args: [".", "--fail-on-new"]
|
|
169
|
+
types: [python]
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
## What CodeClone Is (and Is Not)
|
|
175
|
+
|
|
176
|
+
### CodeClone **is**
|
|
177
|
+
|
|
178
|
+
- an architectural analysis tool,
|
|
179
|
+
- a duplication radar,
|
|
180
|
+
- a CI guard against copy-paste,
|
|
181
|
+
- a control-flow-aware clone detector.
|
|
182
|
+
|
|
183
|
+
### CodeClone **is not**
|
|
184
|
+
|
|
185
|
+
- a linter,
|
|
186
|
+
- a formatter,
|
|
187
|
+
- a semantic equivalence prover,
|
|
188
|
+
- a runtime analyzer.
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## How It Works (High Level)
|
|
193
|
+
|
|
194
|
+
1. Parse Python source into AST.
|
|
195
|
+
2. Normalize AST (names, constants, attributes, annotations).
|
|
196
|
+
3. Build a **Control Flow Graph (CFG)** per function.
|
|
197
|
+
4. Compute stable CFG fingerprints.
|
|
198
|
+
5. Detect function-level and block-level clones.
|
|
199
|
+
6. Apply conservative filters to suppress noise.
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## Control Flow Graph (CFG)
|
|
204
|
+
|
|
205
|
+
Starting from **version 1.1.0**, CodeClone uses a **Control Flow Graph (CFG)**
|
|
206
|
+
to improve structural clone detection robustness.
|
|
207
|
+
|
|
208
|
+
The CFG is a **structural abstraction**, not a runtime execution model.
|
|
209
|
+
|
|
210
|
+
See full design and semantics:
|
|
211
|
+
- [docs/cfg.md](docs/cfg.md)
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## License
|
|
216
|
+
|
|
217
|
+
MIT License
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CodeClone — AST and CFG-based code clone detector for Python
|
|
3
|
+
focused on architectural duplication.
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2026 Den Rozhnovskiy
|
|
6
|
+
Licensed under the MIT License.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from importlib.metadata import version, PackageNotFoundError
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
__version__ = version("codeclone")
|
|
13
|
+
except PackageNotFoundError:
|
|
14
|
+
__version__ = "dev"
|
|
15
|
+
|
|
16
|
+
__all__ = ["__version__"]
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CodeClone — AST and CFG-based code clone detector for Python
|
|
3
|
+
focused on architectural duplication.
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2026 Den Rozhnovskiy
|
|
6
|
+
Licensed under the MIT License.
|
|
7
|
+
"""
|
|
8
|
+
|
|
1
9
|
from __future__ import annotations
|
|
2
10
|
|
|
3
11
|
import ast
|
|
@@ -5,8 +13,9 @@ import hashlib
|
|
|
5
13
|
|
|
6
14
|
from .normalize import NormalizationConfig, AstNormalizer
|
|
7
15
|
|
|
16
|
+
|
|
8
17
|
def stmt_hash(stmt: ast.stmt, cfg: NormalizationConfig) -> str:
|
|
9
18
|
normalizer = AstNormalizer(cfg)
|
|
10
19
|
stmt = ast.fix_missing_locations(normalizer.visit(stmt))
|
|
11
20
|
dump = ast.dump(stmt, annotate_fields=True, include_attributes=False)
|
|
12
|
-
return hashlib.sha1(dump.encode("utf-8")).hexdigest()
|
|
21
|
+
return hashlib.sha1(dump.encode("utf-8")).hexdigest()
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CodeClone — AST and CFG-based code clone detector for Python
|
|
3
|
+
focused on architectural duplication.
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2026 Den Rozhnovskiy
|
|
6
|
+
Licensed under the MIT License.
|
|
7
|
+
"""
|
|
8
|
+
|
|
1
9
|
from __future__ import annotations
|
|
2
10
|
|
|
3
11
|
import ast
|
|
@@ -18,13 +26,13 @@ class BlockUnit:
|
|
|
18
26
|
|
|
19
27
|
|
|
20
28
|
def extract_blocks(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
func_node: ast.AST,
|
|
30
|
+
*,
|
|
31
|
+
filepath: str,
|
|
32
|
+
qualname: str,
|
|
33
|
+
cfg: NormalizationConfig,
|
|
34
|
+
block_size: int,
|
|
35
|
+
max_blocks: int,
|
|
28
36
|
) -> list[BlockUnit]:
|
|
29
37
|
body = getattr(func_node, "body", None)
|
|
30
38
|
if not isinstance(body, list) or len(body) < block_size:
|
|
@@ -45,16 +53,18 @@ def extract_blocks(
|
|
|
45
53
|
if last_start is not None and start - last_start < MIN_LINE_DISTANCE:
|
|
46
54
|
continue
|
|
47
55
|
|
|
48
|
-
bh = "|".join(stmt_hashes[i:i + block_size])
|
|
56
|
+
bh = "|".join(stmt_hashes[i : i + block_size])
|
|
49
57
|
|
|
50
|
-
blocks.append(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
blocks.append(
|
|
59
|
+
BlockUnit(
|
|
60
|
+
block_hash=bh,
|
|
61
|
+
filepath=filepath,
|
|
62
|
+
qualname=qualname,
|
|
63
|
+
start_line=start,
|
|
64
|
+
end_line=end,
|
|
65
|
+
size=block_size,
|
|
66
|
+
)
|
|
67
|
+
)
|
|
58
68
|
|
|
59
69
|
last_start = start
|
|
60
70
|
if len(blocks) >= max_blocks:
|