8000 Add the directory workflow in the action folder (#15) · TheAlgorithms/scripts@8c4dc10 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c4dc10

Browse files
authored
Add the directory workflow in the action folder (#15)
1 parent a3d6183 commit 8c4dc10

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

directory_md/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ runs:
3535
- name: Running the formatter
3636
shell: bash
3737
run: |
38-
python ./build_directory_md.py ${{ inputs.language }} ${{ inputs.working-directory }} ${{ inputs.filetypes }} ${{ inputs.ignored-directories }} ${{ inputs.ignore-folders-children }} > DIRECTORY.md
38+
python build_directory_md.py ${{ inputs.language }} ${{ inputs.working-directory }} ${{ inputs.filetypes }} ${{ inputs.ignored-directories }} ${{ inputs.ignore-folders-children }} > DIRECTORY.md
3939
- name: Committing changes
4040
shell: bash
4141
run: |

directory_md/build_directory_md.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import sys
5+
from typing import Iterator
6+
7+
if ".py" in sys.argv[0]:
8+
sys.argv.pop(0)
9+
10+
11+
if len(sys.argv) not in (3, 4, 5):
12+
print(
13+
"Arguments:\n"
14+
"[0] - Language\n"
15+
"[1] - Base path\n"
16+
"[2] - Allowed filenames\n"
17+
"[3] - Files or folders to ignore (optional)\n"
18+
"[4] - Folders to ignore, but include children (optional)"
19+
)
20+
sys.exit()
21+
22+
ignore = []
23+
skip = []
24+
if len(sys.argv) == 4:
25+
ignore = sys.argv[3].split(",")
26+
if len(sys.argv) == 5:
27+
skip = sys.argv[4].split(",")
28+
29+
URL_BASE = f"https://github.com/TheAlgorithms/{sys.argv[0]}/blob/HEAD"
30+
31+
32+
def good_file_paths(top_dir: str = ".") -> Iterator[str]:
33+
for dir_path, dir_names, filenames in os.walk(top_dir):
34+
dir_names[:] = [d for d in dir_names if d != "scripts" and d[0] not in "._"]
35+
for filename in filenames:
36+
if filename == "__init__.py":
37+
continue
38+
if any(
39+
e.lower() in os.path.join(dir_path, filename).lower() for e in ignore
40+
):
41+
continue
42+
if os.path.splitext(filename)[1] in sys.argv[2].split(","):
43+
path = os.path.join(dir_path, filename).lstrip(".").lstrip("/")
44+
for e in skip:
45+
path = path.replace(e + "/", "")
46+
path = path.replace(e + "\\", "")
47+
yield path
48+
49+
50+
def md_prefix(i):
51+
return f"{i * ' '}*" if i else "\n##"
52+
53+
54+
def print_path(old_path: str, new_path: str) -> str:
55+
old_parts = old_path.split(os.sep)
56+
for i, new_part in enumerate(new_path.split(os.sep)):
57+
if i + 1 > len(old_parts) or old_parts[i] != new_part:
58+
if new_part:
59+
print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}")
60+
return new_path
61+
62+
63+
def print_directory_md(top_dir: str = ".") -> None:
64+
old_path = ""
65+
for filepath in sorted(good_file_paths(top_dir)):
66+
filepath, filename = os.path.split(filepath)
67+
if filepath != old_path:
68+
old_path = print_path(old_path, filepath)
69+
indent = (filepath.count(os.sep) + 1) if filepath else 0
70+
url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20")
71+
filename = os.path.splitext(filename.replace("_", " ").title())[0]
72+
print(f"{md_prefix(indent)} [{filename}]({url})")
73+
74+
75+
if __name__ == "__main__":
76+
print_directory_md(sys.argv[1])

0 commit comments

Comments
 (0)
0