Improve generate_devices_table.py

This commit is contained in:
Arszilla
2022-08-18 23:20:36 +03:00
parent 478ebb3a1c
commit 771a13c824

View File

@@ -3,36 +3,50 @@ import re
import sys
from datetime import datetime
import yaml # python3 -m pip install pyyaml --user
import yaml # python3 -m pip install pyyaml --user
OUTPUT_FILE = './devices.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_devices = 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)
# https://stackoverflow.com/a/11150413
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
def convert(text): return int(text) if text.isdigit() else text.lower()
def alphanum_key(key): return [convert(c)
for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
def generate_table(data):
global qty_devices
default = ""
table = "| Vendor | Board | CPU | CPU Cores | GPU | RAM | RAM Size (MB) | Ethernet | Ethernet Speed (MB) | Wi-Fi | Bluetooth | USB2 | USB3 | Storage | Notes |\n"
table = "| Vendor | Board | CPU | CPU Cores | GPU | RAM | RAM Size (MB) | Ethernet | Ethernet Speed (MB) | Wi-Fi | Bluetooth | USB2 | USB3 | Storage | Notes |\n"
table += "|--------|-------|-----|-----------|-----|-----|---------------|----------|---------------------|-------|-----------|------|------|---------|---------------------|\n"
# Iterate over per input (depth 1)
@@ -42,67 +56,100 @@ def generate_table(data):
# Iterate over board (depth 2)
for board in yaml[vendor]:
qty_devices += 1
ram_size = ""
storage = ""
i = 0
for f in natural_sort(board.get('ram-size', default)):
if i > 0:
ram_size += ", "
ram_size += f
i += 1
i = 0
for f in natural_sort(board.get('storage', default)):
if i > 0:
storage += ", "
storage += f
i += 1
table += "| {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} |\n".format(vendor,
board.get('name', default),
board.get('cpu', default),
board.get('cpu-cores', default),
board.get('gpu', default),
board.get('ram', default),
ram_size,
board.get('ethernet', default),
board.get('ethernet-speed', default),
board.get('wifi', default),
board.get('bluetooth', default),
board.get('usb2', default),
board.get('usb3', default),
storage,
board.get('notes', default))
board.get(
'name', default),
board.get(
'cpu', default),
board.get(
'cpu-cores', default),
board.get(
'gpu', default),
board.get(
'ram', default),
ram_size,
board.get(
'ethernet', default),
board.get(
'ethernet-speed', default),
board.get(
'wifi', default),
board.get(
'bluetooth', default),
board.get(
'usb2', default),
board.get(
'usb3', default),
storage,
board.get('notes', default))
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 Devices\n'
meta += '---\n\n'
stats = "- The official [Kali ARM repository](https://gitlab.com/kalilinux/build-scripts/kali-arm) contains build-scripts to support [**{}** Kali ARM devices](device-stats.html)\n".format(str(str(qty_devices)))
stats = "- The official [Kali ARM repository](https://gitlab.com/kalilinux/build-scripts/kali-arm) contains build-scripts to support [**{}** Kali ARM devices](device-stats.html)\n".format(
str(str(qty_devices)))
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('Devices: {}'.format(qty_devices))
def main(argv):
# Assign variables
data = read_file(INPUT_FILE)
@@ -120,5 +167,6 @@ def main(argv):
# Exit
exit(0)
if __name__ == "__main__":
main(sys.argv[1:])