-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #808 from yashksaini-coder/main
🐛Bug Fix: Project structure text file
- Loading branch information
Showing
3 changed files
with
760 additions
and
93 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,92 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import github | ||
from github import Github | ||
|
||
def get_repo_structure(path='.', prefix=''): | ||
structure = [] | ||
items = sorted(os.listdir(path)) | ||
for i, item in enumerate(items): | ||
if item.startswith('.'): | ||
continue | ||
item_path = os.path.join(path, item) | ||
if os.path.isdir(item_path): | ||
is_last = i == len(items) - 1 | ||
current_prefix = '└── ' if is_last else '├── ' | ||
structure.append(f"{prefix}{current_prefix}{item}") | ||
next_prefix = prefix + (' ' if is_last else '│ ') | ||
structure.extend(get_repo_structure(item_path, next_prefix)) | ||
return structure | ||
|
||
def update_structure_file(structure): | ||
with open('repo_structure.txt', 'w') as f: | ||
f.write('\n'.join(structure)) | ||
|
||
def update_readme(structure): | ||
with open('Repo-structure.md', 'r') as f: # updated file name | ||
content = f.read() | ||
|
||
start_marker = '<!-- START_STRUCTURE -->' | ||
end_marker = '<!-- END_STRUCTURE -->' | ||
|
||
start_index = content.find(start_marker) | ||
end_index = content.find(end_marker) | ||
|
||
if start_index != -1 and end_index != -1: | ||
new_content = ( | ||
content[:start_index + len(start_marker)] + | ||
'\n```\n' + '\n'.join(structure) + '\n```\n' + | ||
content[end_index:] | ||
) | ||
|
||
with open('Repo-structure.md', 'w') as f: | ||
f.write(new_content) | ||
print("Repo-structure.md updated with new structure.") | ||
else: | ||
print("Markers not found in Repo-structure.md. Structure not updated.") | ||
import argparse | ||
from collections.abc import Iterator | ||
|
||
|
||
def good_file_paths(top_dir: str = ".") -> Iterator[str]: | ||
""" | ||
Generator that yields paths of Python or Jupyter Notebook files, excluding certain directories. | ||
Args: | ||
top_dir (str): The root directory to start searching from. | ||
Yields: | ||
str: File paths relative to the top directory. | ||
""" | ||
for dir_path, dir_names, filenames in os.walk(top_dir): | ||
dir_names[:] = [ | ||
d for d in dir_names | ||
if d != "scripts" and d[0] not in "._" and "venv" not in d | ||
] | ||
for filename in filenames: | ||
if filename != "__init__.py" and os.path.splitext(filename)[1] in (".py", ".ipynb"): | ||
yield os.path.join(dir_path, filename).lstrip("./") | ||
|
||
|
||
def md_prefix(indent_level: int) -> str: | ||
""" | ||
Generate Markdown prefix for directories and files based on indentation level. | ||
Args: | ||
indent_level (int): The level of indentation. | ||
Returns: | ||
str: Markdown prefix string. | ||
""" | ||
return f"{' ' * indent_level}*" if indent_level else "\n##" | ||
|
||
|
||
def print_path(old_path: str, new_path: str) -> str: | ||
""" | ||
Print Markdown formatted directory structure between old_path and new_path. | ||
Args: | ||
old_path (str): The previous directory path. | ||
new_path (str): The current directory path. | ||
Returns: | ||
str: The new directory path. | ||
""" | ||
old_parts = old_path.split(os.sep) | ||
for i, new_part in enumerate(new_path.split(os.sep)): | ||
if i >= len(old_parts) or old_parts[i] != new_part: | ||
print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}") | ||
return new_path | ||
|
||
|
||
def print_directory_md(top_dir: str = ".") -> None: | ||
""" | ||
Print the directory structure in Markdown format, including links to Python and Jupyter Notebook files. | ||
Args: | ||
top_dir (str): The root directory to start searching from. | ||
""" | ||
old_path = "" | ||
for filepath in sorted(good_file_paths(top_dir)): | ||
dirpath, filename = os.path.split(filepath) | ||
if dirpath != old_path: | ||
old_path = print_path(old_path, dirpath) | ||
indent_level = dirpath.count(os.sep) + 1 if dirpath else 0 | ||
url = f"{dirpath}/{filename}".replace(" ", "%20") | ||
file_title = os.path.splitext(filename.replace("_", " ").title())[0] | ||
print(f"{md_prefix(indent_level)} [{file_title}]({url})") | ||
|
||
|
||
def main(): | ||
g = Github(os.environ['GH_TOKEN']) | ||
repo = g.get_repo(os.environ['GITHUB_REPOSITORY']) | ||
|
||
current_structure = get_repo_structure() | ||
|
||
try: | ||
contents = repo.get_contents("repo_structure.txt") | ||
existing_structure = contents.decoded_content.decode().split('\n') | ||
except github.GithubException: | ||
existing_structure = None | ||
|
||
if current_structure != existing_structure: | ||
update_structure_file(current_structure) | ||
update_readme(current_structure) | ||
print("Repository structure updated.") | ||
else: | ||
print("No changes in repository structure.") | ||
# Argument parsing | ||
parser = argparse.ArgumentParser(description="Generate a Markdown-formatted file structure for Python and Jupyter files.") | ||
parser.add_argument("directory", nargs="?", default=".", help="The root directory to generate the Markdown structure for (default: current directory).") | ||
args = parser.parse_args() | ||
|
||
# Check if the directory exists | ||
if not os.path.exists(args.directory): | ||
print(f"Error: Directory '{args.directory}' does not exist.") | ||
return | ||
|
||
print_directory_md(args.directory) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,23 @@ | ||
name: Detect and Update Repo Structure | ||
|
||
on: | ||
schedule: | ||
- cron: '0 * * * *' # Run every hour | ||
workflow_dispatch: # Allow manual triggering | ||
push: | ||
branches: | ||
- main | ||
|
||
name: directory_writer | ||
on: [push] | ||
jobs: | ||
detect-and-update-structure: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
- uses: actions/checkout@v4 | ||
with: | ||
python-version: 3.12 | ||
|
||
- name: Install dependencies | ||
fetch-depth: 0 | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: 3.x | ||
- name: Write Project-Structure.md | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install PyGithub | ||
- name: Run update script | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
run: python .github/scripts/update_dir_structure.py | ||
|
||
- name: Commit and push if changed | ||
python .github/scripts/update_dir_structure.py 2>&1 | tee Project-Structure.md | ||
git config --global user.name "$GITHUB_ACTOR" | ||
git config --global user.email "[email protected]" | ||
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY | ||
- name: Update Project-Structure.md | ||
run: | | ||
git config --local user.email "[email protected]" | ||
git config --local user.name "$GITHUB_ACTOR" | ||
git add . | ||
git diff --quiet && git diff --staged --quiet || (git commit -m "Update repo structure" && git push) | ||
git add Project-Structure.md | ||
git commit -am "updating Project-Structure.md" || true | ||
git push --force origin HEAD:$GITHUB_REF || true |
Oops, something went wrong.