Created
January 3, 2023 02:31
-
-
Save dmattera/b97a4c4f114850d41877ec493170d88d to your computer and use it in GitHub Desktop.
man_page_parser.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def parse_man_file(man_filepath): | |
with open(man_filepath, "r") as man_file: | |
lines = man_file.read().split("\n") | |
formatted_lines = [] | |
for line in lines: | |
# remove Apple developer comments included on the same line and strip off trailing white space | |
formatted_lines.append(line.split('\\"')[0].strip()) | |
sections = {} | |
for n, line in enumerate(formatted_lines): | |
section_header = "" | |
section_body = "" | |
if line.startswith(".Sh "): | |
section_header = line.replace(".Sh ", "") | |
section_body_line = n + 1 | |
while True: | |
if section_body_line >= len(formatted_lines): | |
break | |
else: | |
body_line = formatted_lines[section_body_line] | |
if not body_line.startswith(".Sh "): | |
formatted_body_line = "" | |
if body_line.startswith(".Nd"): | |
formatted_body_line = body_line.replace(".Nd", "-") | |
else: | |
for word in body_line.split(" "): | |
if not word.startswith("."): | |
replace_list = ["\\fB", "\\fR", "Pa ", "", "", "", ""] | |
word = word.replace("\\fB", "") | |
word = word.replace("\\fR", "") | |
formatted_body_line += word + " " | |
else: | |
formatted_body_line += " " | |
section_body += formatted_body_line.replace(" ", " ") | |
section_body_line += 1 | |
else: | |
break | |
# print(current_section_header) | |
# print(current_section_body) | |
sections[section_header] = section_body.strip().replace(" ", " ") | |
# print(sections) | |
# print(n, ":", line) | |
return sections | |
man_dir = "/usr/share/man/man8/" | |
service_dirs = [ | |
os.getenv("HOME") + '/Library/LaunchAgents', # Per-user agents provided by the user. | |
'/Library/LaunchAgents', # Per-user agents provided by the administrator. | |
'/Library/LaunchDaemons', # System wide daemons provided by the administrator. | |
'/System/Library/LaunchAgents', # OS X Per-user agents. | |
'/System/Library/LaunchDaemons' # OS X System wide daemons. | |
] | |
services = [] | |
for service_dir in service_dirs: | |
for service_filename in os.listdir(service_dir): | |
s = {} | |
if service_filename.startswith("com.apple."): | |
s["name"] = service_filename.replace(".plist", "") | |
s["path"] = service_dir + os.sep + service_filename | |
s['has_man_page'] = False | |
s["man_page_path"] = None | |
possible_man_page_paths = [ | |
man_dir + s["name"].replace("com.apple.", "") + ".8", | |
man_dir + s["name"].split(".")[-1] + ".8" | |
] | |
for man_path in possible_man_page_paths: | |
if os.path.isfile(man_path) and s['has_man_page'] is False: | |
s['has_man_page'] = True | |
s["man_page_path"] = man_path | |
if s['has_man_page']: | |
s['man_page'] = parse_man_file(s["man_page_path"]) | |
else: | |
s['man_page'] = None | |
services.append(s) | |
output = "" | |
output += "|" + "|".join(services[0].keys()) + "|\n" | |
for keys in services[0].keys(): | |
output += '|---' | |
output += "|\n" | |
for service in services: | |
row = "|" | |
for k,v in service.items(): | |
cell = "" | |
if isinstance(v, dict): | |
for k2, v2 in v.items(): | |
cell += f"{k2}: {str(v2)}<br>" | |
print("\t", k2, ":", v2) | |
else: | |
cell = str(v) | |
row += cell + "|" | |
output += row + "\n" | |
with open('output.txt', 'w') as outfile: | |
outfile.write(output) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment