| 1 |
# SPDX-License-Identifier: GPL-2.0-only |
| 2 |
# This file is part of Scapy |
| 3 |
# See https://scapy.net/ for more information |
| 4 |
# Copyright (C) Gabriel Potter <gabriel[]potter[]fr> |
| 5 |
|
| 6 |
""" |
| 7 |
Generate MyPy deployment stats |
| 8 |
""" |
| 9 |
|
| 10 |
import os |
| 11 |
import io |
| 12 |
import glob |
| 13 |
from collections import defaultdict |
| 14 |
|
| 15 |
# Parse config file |
| 16 |
|
| 17 |
localdir = os.path.split(__file__)[0] |
| 18 |
rootpath = os.path.abspath(os.path.join(localdir, '../../')) |
| 19 |
|
| 20 |
with io.open(os.path.join(localdir, "mypy_enabled.txt")) as fd: |
| 21 |
FILES = [l.strip() for l in fd.readlines() if l.strip() and l[0] != "#"] |
| 22 |
|
| 23 |
# Scan Scapy |
| 24 |
|
| 25 |
ALL_FILES = [ |
| 26 |
"".join(x.partition("scapy/")[2:]) for x in |
| 27 |
glob.iglob(os.path.join(rootpath, 'scapy/**/*.py'), recursive=True) |
| 28 |
] |
| 29 |
|
| 30 |
# Process |
| 31 |
|
| 32 |
REMAINING = defaultdict(list) |
| 33 |
MODULES = defaultdict(lambda: (0, 0, 0, 0)) |
| 34 |
|
| 35 |
for f in ALL_FILES: |
| 36 |
with open(os.path.join(rootpath, f)) as fd: |
| 37 |
lines = len(fd.read().split("\n")) |
| 38 |
parts = f.split("/") |
| 39 |
if len(parts) > 2: |
| 40 |
mod = parts[1] |
| 41 |
else: |
| 42 |
mod = "[core]" |
| 43 |
e, l, t, a = MODULES[mod] |
| 44 |
if f in FILES: |
| 45 |
e += lines |
| 46 |
t += 1 |
| 47 |
else: |
| 48 |
REMAINING[mod].append(f) |
| 49 |
l += lines |
| 50 |
a += 1 |
| 51 |
MODULES[mod] = (e, l, t, a) |
| 52 |
|
| 53 |
ENABLED = sum(x[0] for x in MODULES.values()) |
| 54 |
TOTAL = sum(x[1] for x in MODULES.values()) |
| 55 |
|
| 56 |
print("**MyPy Support: %.2f%%**" % (ENABLED / TOTAL * 100)) |
| 57 |
print("| Module | Typed code (lines) | Typed files |") |
| 58 |
print("| --- | --- | --- |") |
| 59 |
for mod, dat in MODULES.items(): |
| 60 |
print("|`%s` | %.2f%% | %s/%s |" % (mod, dat[0] / dat[1] * 100, dat[2], dat[3])) |
| 61 |
|
| 62 |
print() |
| 63 |
COREMODS = REMAINING["[core]"] |
| 64 |
if COREMODS: |
| 65 |
print("Core modules still untypes:") |
| 66 |
for mod in COREMODS: |
| 67 |
print("- `%s`" % mod) |
| 68 |
|