Skip to content

Instantly share code, notes, and snippets.

@Beercow
Created November 1, 2022 13:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Beercow/5e13282541f79580405f96364baca8d5 to your computer and use it in GitHub Desktop.
Save Beercow/5e13282541f79580405f96364baca8d5 to your computer and use it in GitHub Desktop.
Python script to dump all fields in KAPE targets and modules, including documentation
import csv
import yaml
import argparse
import os
import sys
filenames = []
def main():
if args.target:
fieldnames = ['Disabled', 'Local', 'Target', 'Description', 'Author', 'Version', 'Id',
'RecreateDirectories', 'Name', 'Category', 'Path',
'Recursive', 'FileMask', 'SaveAsFileName',
'AlwaysAddToQueue', 'Comment', 'MinSize', 'MaxSize',
'Documentation'
]
output = 'Targets.csv'
else:
fieldnames = ['Disabled', 'Local', 'Module', 'Description', 'Category', 'Author',
'Version', 'Id', 'BinaryUrl', 'ExportFormat',
'WaitTimeout', 'FileMask', 'Executable', 'CommandLine',
'ExportFormat', 'ExportFile', 'Append', 'Documentation'
]
output = 'Modules.csv'
with open(output, 'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames=fieldnames)
csv_output.writeheader()
for filename in filenames:
print(f'Parsing {filename}')
with open(filename) as f_input:
try:
data = yaml.safe_load(f_input)
except Exception as e:
print(f'Unable to parse {f_input}: {e}')
continue
doc = False
f_input.seek(0)
Documentation = []
while True:
line = f_input.readline()
if not line:
break
if doc:
Documentation.append(line)
if '# Documentation' in line:
doc = True
if args.target:
Disabled = 'False'
Local = 'False'
Target = os.path.basename(f_input.name).split('.')[0]
if '!Disabled' in f_input.name:
Disabled = 'True'
if '!Local' in f_input.name:
Local = 'True'
Description = data['Description']
Author = data['Author']
Version = data['Version']
Id = data['Id']
RecreateDirectories = data['RecreateDirectories']
for dic in data['Targets']:
row = {'Disabled': Disabled, 'Local': Local, 'Target': Target, 'Description': Description,
'Author': Author, 'Version': Version, 'Id': Id,
'RecreateDirectories': RecreateDirectories
}
for get in ['Name', 'Category', 'Path', 'Recursive',
'FileMask', 'SaveAsFileName',
'AlwaysAddToQueue', 'Comment', 'MinSize',
'MaxSize'
]:
try:
row[get] = dic[get]
except KeyError:
pass
row['Documentation'] = ''.join(Documentation).replace('# ', '')
csv_output.writerow(row)
else:
Disabled = 'False'
Local = 'False'
Module = os.path.basename(f_input.name).split('.')[0]
if '!Disabled' in f_input.name:
Disabled = 'True'
if '!Local' in f_input.name:
Local = 'True'
Description = data['Description']
Category = data['Category']
Author = data['Author']
Version = data['Version']
Id = data['Id']
try:
BinaryUrl = data['BinaryUrl']
except KeyError:
BinaryUrl = ''
ExportFormat = data['ExportFormat']
try:
WaitTimeout = data['WaitTimeout']
except KeyError:
WaitTimeout = ''
try:
FileMask = data['FileMask']
except KeyError:
FileMask = ''
for dic in data['Processors']:
row = {'Disabled': Disabled, 'Local': Local, 'Module': Module, 'Description': Description,
'Category': Category, 'Author': Author,
'Version': Version, 'Id': Id, 'BinaryUrl': BinaryUrl,
'ExportFormat': ExportFormat,
'WaitTimeout': WaitTimeout, 'FileMask': FileMask
}
for get in ['Executable', 'CommandLine', 'ExportFormat',
'ExportFile', 'Append'
]:
try:
row[get] = dic[get]
except KeyError:
pass
row['Documentation'] = ''.join(Documentation).replace('# ', '')
csv_output.writerow(row)
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dir", help="Directory to be parsed")
parser.add_argument("--target", help="Directory to be parsed", action='store_true')
parser.add_argument("--module", help="Directory to be parsed", action='store_true')
if len(sys.argv) == 1:
parser.print_help()
parser.exit()
args = parser.parse_args()
for path, subdirs, files in os.walk(args.dir):
if 'bin' not in path:
for name in files:
filenames.append(os.path.join(path, name))
if __name__ == "__main__":
main()
@Beercow
Copy link
Author

Beercow commented Nov 1, 2022

Requires PyYAML
pip install PyYAML

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment