Improve generate_kernel_stats.py
This commit is contained in:
@@ -3,33 +3,42 @@
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
import yaml # python3 -m pip install pyyaml --user
|
||||
import yaml # python3 -m pip install pyyaml --user
|
||||
|
||||
OUTPUT_FILE = './kernel-stats.md'
|
||||
|
||||
INPUT_FILE = './devices.yml'
|
||||
repo_msg = "\n_This table was [generated automatically](https://gitlab.com/kalilinux/build-scripts/kali-arm/-/blob/master/devices.yml) on {} from the [Kali ARM GitLab repository](https://gitlab.com/kalilinux/build-scripts/kali-arm)_\n".format(datetime.now().strftime("%Y-%B-%d %H:%M:%S"))
|
||||
|
||||
repo_msg = "\n_This table was [generated automatically](https://gitlab.com/kalilinux/build-scripts/kali-arm/-/blob/master/devices.yml) on {} from the [Kali ARM GitLab repository](https://gitlab.com/kalilinux/build-scripts/kali-arm)_\n".format(
|
||||
datetime.now().strftime("%Y-%B-%d %H:%M:%S"))
|
||||
|
||||
qty_kernels = 0
|
||||
qty_versions = {
|
||||
'custom': 0,
|
||||
'kali': 0,
|
||||
'vendor': 0
|
||||
}
|
||||
'custom': 0,
|
||||
'kali': 0,
|
||||
'vendor': 0
|
||||
}
|
||||
|
||||
# Input:
|
||||
# ------------------------------------------------------------
|
||||
# See: ./devices.yml
|
||||
# https://gitlab.com/kalilinux/build-scripts/kali-arm/-/blob/master/devices.yml
|
||||
|
||||
## Input:
|
||||
## ------------------------------------------------------------ ##
|
||||
## See: ./devices.yml
|
||||
## https://gitlab.com/kalilinux/build-scripts/kali-arm/-/blob/master/devices.yml
|
||||
|
||||
def yaml_parse(content):
|
||||
result = ""
|
||||
lines = content.split('\n')
|
||||
|
||||
for line in lines:
|
||||
if line.strip() and not line.strip().startswith('#'):
|
||||
result += line + "\n"
|
||||
|
||||
return yaml.safe_load(result)
|
||||
|
||||
|
||||
def generate_table(data):
|
||||
global qty_kernels, qty_versions
|
||||
|
||||
images = []
|
||||
default = "unknown"
|
||||
|
||||
@@ -46,53 +55,71 @@ def generate_table(data):
|
||||
# Iterate over image (depth 3)
|
||||
for image in board[key]:
|
||||
if image['name'] not in images:
|
||||
images.append(image['name']) # ALT: images.append(image['image'])
|
||||
qty_kernels += 1
|
||||
qty_versions[(image.get('kernel', default))] += 1
|
||||
#else:
|
||||
# print('DUP {} / {}'.format(image['name'], image['image']))
|
||||
if 'images' not in board.keys():
|
||||
print("[i] Possible issue with: " + board.get('board', default) + " (no images)")
|
||||
# ALT: images.append(image['image'])
|
||||
images.append(image['name'])
|
||||
|
||||
table = "| Kernel | Qty |\n"
|
||||
qty_kernels += 1
|
||||
qty_versions[(
|
||||
image.get('kernel', default))] += 1
|
||||
|
||||
# else:
|
||||
# print('DUP {} / {}'.format(image['name'], image['image']))
|
||||
|
||||
if 'images' not in board.keys():
|
||||
print("[i] Possible issue with: " +
|
||||
board.get('board', default) + " (no images)")
|
||||
|
||||
table = "| Kernel | Qty |\n"
|
||||
table += "|--------|-----|\n"
|
||||
|
||||
# iterate over all the devices
|
||||
for v in qty_versions:
|
||||
table += "| {} | {} |\n".format(v.capitalize(),
|
||||
str(qty_versions[v]))
|
||||
|
||||
return table
|
||||
|
||||
|
||||
def read_file(file):
|
||||
try:
|
||||
with open(file) as f:
|
||||
data = f.read()
|
||||
f.close()
|
||||
|
||||
except Exception as e:
|
||||
print("[-] Cannot open input file: {} - {}".format(file, e))
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def write_file(data, file):
|
||||
try:
|
||||
with open(file, 'w') as f:
|
||||
meta = '---\n'
|
||||
meta = '---\n'
|
||||
meta += 'title: Kali ARM Kernel Statistics\n'
|
||||
meta += '---\n\n'
|
||||
stats = "- The official [Kali ARM repository](https://gitlab.com/kalilinux/build-scripts/kali-arm) contains [build-scripts]((https://gitlab.com/kalilinux/build-scripts/kali-arm)) to create [**{}** unique Kali ARM images](images.html)\n".format(str(qty_kernels))
|
||||
|
||||
stats = "- The official [Kali ARM repository](https://gitlab.com/kalilinux/build-scripts/kali-arm) contains [build-scripts]((https://gitlab.com/kalilinux/build-scripts/kali-arm)) to create [**{}** unique Kali ARM images](images.html)\n".format(str(qty_kernels))
|
||||
stats += "- [Kali ARM Statistics](index.html)\n\n"
|
||||
|
||||
f.write(str(meta))
|
||||
f.write(str(stats))
|
||||
f.write(str(data))
|
||||
f.write(str(repo_msg))
|
||||
f.close()
|
||||
|
||||
print('[+] File: {} successfully written'.format(OUTPUT_FILE))
|
||||
|
||||
except Exception as e:
|
||||
print("[-] Cannot write to output file: {} - {}".format(file, e))
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def print_summary():
|
||||
print('Kernels: {}'.format(qty_kernels))
|
||||
|
||||
|
||||
def main(argv):
|
||||
# Assign variables
|
||||
data = read_file(INPUT_FILE)
|
||||
@@ -110,5 +137,6 @@ def main(argv):
|
||||
# Exit
|
||||
exit(0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
|
||||
Reference in New Issue
Block a user