-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparse.py
More file actions
90 lines (74 loc) · 2.61 KB
/
parse.py
File metadata and controls
90 lines (74 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import os
def get_tool_url(tool_path):
git_config_path = os.path.join(tool_path, '.git', 'config')
if os.path.exists(git_config_path):
with open(git_config_path, 'r') as f:
for line in f:
if 'url' in line:
return line.split('=')[1].strip()
#print("Git URL not found: {}".format(tool_path))
return ''
def generate_md(tool_path):
tool_name = os.path.basename(tool_path)
category_name = os.path.basename(os.path.dirname(tool_path))
tool_url = get_tool_url(tool_path)
md_content = f"""---
description: |
TODO
command: |
{tool_name} {tool_url}
code: |
TODO
items:
- {category_name}
"""
if tool_url:
md_content += f"""
references:
- {tool_url}
"""
md_content += "---"
return md_content
def process_directory(directory):
for item in os.listdir(directory):
item_path = os.path.join(directory, item)
if os.path.isdir(item_path):
#if os.path.isfile(os.path.join(item_path, ".git/config")):
item_name = str(item).lower()
output_path = os.path.join('/tmp/tools', f'{item_name}.md')
if os.path.exists(output_path):
print(f"\n====================\nWarning: File {output_path} already exists. Duplicate found in: {os.path.abspath(item_path)}")
print("\n\n")
os.system(f"cat {output_path} | grep -A1 command | tr -d '\n' ")
print(" ")
os.system(f"cat {output_path} | grep -A1 items | tr -d '\n' ")
print("\n------\n")
print(get_tool_url(item_path))
print(" ")
print(os.path.basename(os.path.dirname(item_path)))
else:
md_content = generate_md(item_path)
with open(output_path, 'w') as f:
f.write(md_content)
elif os.path.isdir(item_path):
process_directory(item_path)
def main():
# tmp dir for processing
if not os.path.exists("/tmp/tools"):
os.mkdir("/tmp/tools")
# tools dir
tools_dir = "/mnt/Data/Tools"
url_dict = {}
#current_dir = os.getcwd()
for item in os.listdir(tools_dir):
item_path = os.path.join(tools_dir, item)
if os.path.isdir(item_path):
process_directory(item_path)
for url, directories in url_dict.items():
if len(directories) > 1:
print(f"Multiple directories have the same URL: {url}")
for directory in directories:
print(f"- {os.path.abspath(directory)}")
if __name__ == "__main__":
main()