reposnap 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ # python generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # venv
10
+ .venv
11
+ .idea
12
+ output.md
@@ -0,0 +1 @@
1
+ 3.12.4
reposnap-0.2.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 agoloborodko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,91 @@
1
+ Metadata-Version: 2.3
2
+ Name: reposnap
3
+ Version: 0.2.0
4
+ Summary: Generate a Markdown file with all contents of your project
5
+ Author: agoloborodko
6
+ License-File: LICENSE
7
+ Requires-Python: >=3.8
8
+ Requires-Dist: gitpython>=3.1.43
9
+ Requires-Dist: pathlib>=1.0.1
10
+ Requires-Dist: pathspec>=0.12.1
11
+ Description-Content-Type: text/markdown
12
+
13
+ # RepoSnap
14
+
15
+ ## Overview
16
+
17
+ `reposnap` is a Python script designed to generate a Markdown file that documents the structure and contents of a git project. This tool is particularly useful for creating a quick overview of a project's file hierarchy, including optional syntax-highlighted code snippets.
18
+
19
+ ## Features
20
+
21
+ - **Recursive Directory Traversal**: Automatically traverses through all subdirectories of the specified project root.
22
+ - **File Exclusion**: Supports excluding specific files and directories using patterns similar to `.gitignore`.
23
+ - **Syntax Highlighting**: Includes syntax highlighting for known file types in the generated Markdown file.
24
+ - **Structure Only Option**: The `--structure-only` flag can be used to generate the Markdown file with just the directory structure, omitting the contents of the files.
25
+
26
+ ## Usage
27
+
28
+ ### Command-Line Interface
29
+
30
+ To use the `collect.py` script, run it with the following options:
31
+
32
+ ```bash
33
+ python -m collect <project_root> [-o OUTPUT] [-e EXCLUDE ...] [-x EXCLUDE_FILE] [--structure-only]
34
+ ```
35
+
36
+ - `<project_root>`: The root directory of the project to document.
37
+ - `-o, --output`: The name of the output Markdown file. Defaults to `repo-content-<timestamp>.md`.
38
+ - `-e, --exclude`: Patterns of files or directories to exclude (supports multiple patterns).
39
+ - `-x, --exclude-file`: A file (e.g., `.gitignore`) containing patterns of files or directories to exclude.
40
+ - `--structure-only`: Generate a Markdown file that includes only the project structure, without file contents.
41
+
42
+ ### Examples
43
+
44
+ 1. **Generate a full project structure with file contents**:
45
+
46
+ ```bash
47
+ python -m collect my_project/
48
+ ```
49
+
50
+ 2. **Generate a project structure only**:
51
+
52
+ ```bash
53
+ python -m collect my_project/ --structure-only
54
+ ```
55
+
56
+ 3. **Generate a Markdown file excluding certain files and directories**:
57
+
58
+ ```bash
59
+ python -m collect my_project/ -e '*.log' -e 'build/' -o output.md
60
+ ```
61
+
62
+ 4. **Use exclusion patterns from a `.gitignore` file**:
63
+
64
+ ```bash
65
+ python -m collect my_project/ -x .gitignore
66
+ ```
67
+
68
+ ## Testing
69
+
70
+ This project includes unit tests to ensure the correctness of the code:
71
+
72
+ - **`test_collect.py`**: Tests the `file_extension_to_language` function, verifying that file extensions are correctly mapped to programming languages for syntax highlighting in the Markdown file.
73
+ - **`test_cli.py`**: Tests the command-line interface of the `collect.py` script, including the `--structure-only` flag.
74
+
75
+ ### Running the Tests
76
+
77
+ To run the tests, use `rye`:
78
+
79
+ ```bash
80
+ rye test
81
+ ```
82
+
83
+ This will execute all test cases and provide a summary of the results.
84
+
85
+ ## Dependencies
86
+
87
+ This project uses standard Python libraries and does not require any external dependencies.
88
+
89
+ ## License
90
+
91
+ This project is licensed under the MIT License.
@@ -0,0 +1,79 @@
1
+ # RepoSnap
2
+
3
+ ## Overview
4
+
5
+ `reposnap` is a Python script designed to generate a Markdown file that documents the structure and contents of a git project. This tool is particularly useful for creating a quick overview of a project's file hierarchy, including optional syntax-highlighted code snippets.
6
+
7
+ ## Features
8
+
9
+ - **Recursive Directory Traversal**: Automatically traverses through all subdirectories of the specified project root.
10
+ - **File Exclusion**: Supports excluding specific files and directories using patterns similar to `.gitignore`.
11
+ - **Syntax Highlighting**: Includes syntax highlighting for known file types in the generated Markdown file.
12
+ - **Structure Only Option**: The `--structure-only` flag can be used to generate the Markdown file with just the directory structure, omitting the contents of the files.
13
+
14
+ ## Usage
15
+
16
+ ### Command-Line Interface
17
+
18
+ To use the `collect.py` script, run it with the following options:
19
+
20
+ ```bash
21
+ python -m collect <project_root> [-o OUTPUT] [-e EXCLUDE ...] [-x EXCLUDE_FILE] [--structure-only]
22
+ ```
23
+
24
+ - `<project_root>`: The root directory of the project to document.
25
+ - `-o, --output`: The name of the output Markdown file. Defaults to `repo-content-<timestamp>.md`.
26
+ - `-e, --exclude`: Patterns of files or directories to exclude (supports multiple patterns).
27
+ - `-x, --exclude-file`: A file (e.g., `.gitignore`) containing patterns of files or directories to exclude.
28
+ - `--structure-only`: Generate a Markdown file that includes only the project structure, without file contents.
29
+
30
+ ### Examples
31
+
32
+ 1. **Generate a full project structure with file contents**:
33
+
34
+ ```bash
35
+ python -m collect my_project/
36
+ ```
37
+
38
+ 2. **Generate a project structure only**:
39
+
40
+ ```bash
41
+ python -m collect my_project/ --structure-only
42
+ ```
43
+
44
+ 3. **Generate a Markdown file excluding certain files and directories**:
45
+
46
+ ```bash
47
+ python -m collect my_project/ -e '*.log' -e 'build/' -o output.md
48
+ ```
49
+
50
+ 4. **Use exclusion patterns from a `.gitignore` file**:
51
+
52
+ ```bash
53
+ python -m collect my_project/ -x .gitignore
54
+ ```
55
+
56
+ ## Testing
57
+
58
+ This project includes unit tests to ensure the correctness of the code:
59
+
60
+ - **`test_collect.py`**: Tests the `file_extension_to_language` function, verifying that file extensions are correctly mapped to programming languages for syntax highlighting in the Markdown file.
61
+ - **`test_cli.py`**: Tests the command-line interface of the `collect.py` script, including the `--structure-only` flag.
62
+
63
+ ### Running the Tests
64
+
65
+ To run the tests, use `rye`:
66
+
67
+ ```bash
68
+ rye test
69
+ ```
70
+
71
+ This will execute all test cases and provide a summary of the results.
72
+
73
+ ## Dependencies
74
+
75
+ This project uses standard Python libraries and does not require any external dependencies.
76
+
77
+ ## License
78
+
79
+ This project is licensed under the MIT License.
@@ -0,0 +1,30 @@
1
+ [project]
2
+ name = "reposnap"
3
+ version = "0.2.0"
4
+ description = "Generate a Markdown file with all contents of your project"
5
+ authors = [
6
+ { name = "agoloborodko" }
7
+ ]
8
+ dependencies = [
9
+ "pathlib>=1.0.1",
10
+ "pathspec>=0.12.1",
11
+ "gitpython>=3.1.43",
12
+ ]
13
+ readme = "README.md"
14
+ requires-python = ">= 3.8"
15
+
16
+ [build-system]
17
+ requires = ["hatchling"]
18
+ build-backend = "hatchling.build"
19
+
20
+ [tool.rye]
21
+ managed = true
22
+ dev-dependencies = [
23
+ "pytest>=8.3.2",
24
+ ]
25
+
26
+ [tool.hatch.metadata]
27
+ allow-direct-references = true
28
+
29
+ [tool.hatch.build.targets.wheel]
30
+ packages = ["src/reposnap"]
@@ -0,0 +1,29 @@
1
+ # generated by rye
2
+ # use `rye lock` or `rye sync` to update this lockfile
3
+ #
4
+ # last locked with the following flags:
5
+ # pre: false
6
+ # features: []
7
+ # all-features: false
8
+ # with-sources: false
9
+ # generate-hashes: false
10
+ # universal: false
11
+
12
+ -e file:.
13
+ gitdb==4.0.11
14
+ # via gitpython
15
+ gitpython==3.1.43
16
+ # via reposnap
17
+ iniconfig==2.0.0
18
+ # via pytest
19
+ packaging==24.1
20
+ # via pytest
21
+ pathlib==1.0.1
22
+ # via reposnap
23
+ pathspec==0.12.1
24
+ # via reposnap
25
+ pluggy==1.5.0
26
+ # via pytest
27
+ pytest==8.3.2
28
+ smmap==5.0.1
29
+ # via gitdb
@@ -0,0 +1,22 @@
1
+ # generated by rye
2
+ # use `rye lock` or `rye sync` to update this lockfile
3
+ #
4
+ # last locked with the following flags:
5
+ # pre: false
6
+ # features: []
7
+ # all-features: false
8
+ # with-sources: false
9
+ # generate-hashes: false
10
+ # universal: false
11
+
12
+ -e file:.
13
+ gitdb==4.0.11
14
+ # via gitpython
15
+ gitpython==3.1.43
16
+ # via reposnap
17
+ pathlib==1.0.1
18
+ # via reposnap
19
+ pathspec==0.12.1
20
+ # via reposnap
21
+ smmap==5.0.1
22
+ # via gitdb
@@ -0,0 +1,4 @@
1
+ from reposnap.interfaces.cli import main
2
+
3
+ if __name__ == "__main__":
4
+ main()
File without changes
File without changes
@@ -0,0 +1,21 @@
1
+ # src/reposnap/core/collector.py
2
+
3
+ from .file_system import FileSystem
4
+ from .git_repo import GitRepo
5
+ from .markdown_generator import MarkdownGenerator
6
+ import pathspec
7
+
8
+ class ProjectContentCollector:
9
+ def __init__(self, root_dir: str, output_file: str, structure_only: bool, gitignore_patterns: list):
10
+ self.root_dir = root_dir
11
+ self.output_file = output_file
12
+ self.structure_only = structure_only
13
+ self.spec = pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, gitignore_patterns)
14
+ self.file_system = FileSystem(root_dir)
15
+ self.git_repo = GitRepo(root_dir)
16
+ self.markdown_generator = MarkdownGenerator()
17
+
18
+ def collect_and_generate(self):
19
+ git_files = self.git_repo.get_git_files()
20
+ tree_structure = self.file_system.build_tree_structure(git_files)
21
+ self.markdown_generator.generate_markdown(self.output_file, tree_structure, git_files, self.spec, self.structure_only)
@@ -0,0 +1,19 @@
1
+ # src/reposnap/core/file_system.py
2
+
3
+ from pathlib import Path
4
+
5
+ class FileSystem:
6
+ def __init__(self, root_dir: str):
7
+ self.root_dir = Path(root_dir).resolve()
8
+
9
+ def build_tree_structure(self, files):
10
+ tree = {}
11
+ for file in files:
12
+ file_path = Path(file).resolve()
13
+ relative_path = file_path.relative_to(self.root_dir).as_posix()
14
+ parts = relative_path.split('/')
15
+ current_level = tree
16
+ for part in parts[:-1]:
17
+ current_level = current_level.setdefault(part, {})
18
+ current_level[parts[-1]] = relative_path
19
+ return tree
@@ -0,0 +1,14 @@
1
+ # src/reposnap/core/git_repo.py
2
+
3
+ from git import Repo, InvalidGitRepositoryError
4
+
5
+ class GitRepo:
6
+ def __init__(self, repo_path: str):
7
+ self.repo_path = repo_path
8
+
9
+ def get_git_files(self):
10
+ try:
11
+ repo = Repo(self.repo_path)
12
+ return repo.git.ls_files().splitlines()
13
+ except InvalidGitRepositoryError:
14
+ return []
@@ -0,0 +1,24 @@
1
+ # src/reposnap/core/markdown_generator.py
2
+
3
+ from pathlib import Path
4
+ from ..utils.path_utils import format_tree
5
+
6
+ class MarkdownGenerator:
7
+ def generate_markdown(self, output_file, tree_structure, git_files, spec, structure_only=False):
8
+ with open(output_file, 'w') as f:
9
+ f.write("# Project Structure\n\n")
10
+ f.write("```\n")
11
+ for line in format_tree(tree_structure):
12
+ f.write(line)
13
+ f.write("```\n\n")
14
+
15
+ if not structure_only:
16
+ for file in git_files:
17
+ relative_path = Path(file).relative_to(Path(output_file).parent).as_posix()
18
+ if spec and not spec.match_file(relative_path):
19
+ # Only read the file if spec is not None and does not match
20
+ with open(Path(output_file).parent / file, 'r') as file_content:
21
+ f.write(f"## {relative_path}\n\n")
22
+ f.write(f"```\n{file_content.read()}\n```\n\n")
23
+
24
+ print(f"Markdown file generated: {output_file}")
File without changes
@@ -0,0 +1,21 @@
1
+ # src/reposnap/interfaces/cli.py
2
+
3
+ import argparse
4
+ from reposnap.core.collector import ProjectContentCollector
5
+
6
+ def main():
7
+ parser = argparse.ArgumentParser(description='Generate a Markdown representation of a Git repository.')
8
+ parser.add_argument('path', help='Path to the Git repository.')
9
+ parser.add_argument('-o', '--output', help='Output Markdown file', default='output.md')
10
+ parser.add_argument('--structure-only', action='store_true',
11
+ help='Only include the file structure without content.')
12
+ args = parser.parse_args()
13
+
14
+ with open(f"{args.path}/.gitignore", 'r') as gitignore:
15
+ patterns = gitignore.readlines()
16
+
17
+ collector = ProjectContentCollector(args.path, args.output, args.structure_only, patterns)
18
+ collector.collect_and_generate()
19
+
20
+ if __name__ == "__main__":
21
+ main()
File without changes
@@ -0,0 +1,9 @@
1
+ # src/reposnap/utils/path_utils.py
2
+
3
+ def format_tree(tree, indent=''):
4
+ for key, value in tree.items():
5
+ if isinstance(value, dict):
6
+ yield f"{indent}{key}/\n"
7
+ yield from format_tree(value, indent + ' ')
8
+ else:
9
+ yield f"{indent}{key}\n"
File without changes
File without changes
@@ -0,0 +1,77 @@
1
+ # src/reposnap/tests/test_cli.py
2
+
3
+ import pytest
4
+ from unittest.mock import patch, MagicMock
5
+ import os
6
+ import tempfile
7
+ from reposnap.interfaces.cli import main
8
+
9
+ def create_file(file_path: str, content: str = ''):
10
+ """Helper function to create a file with the given content."""
11
+ with open(file_path, 'w') as f:
12
+ f.write(content)
13
+
14
+ def create_directory_structure(base_dir: str, structure: dict):
15
+ """
16
+ Helper function to create directories and files based on a given structure.
17
+ """
18
+ for name, content in structure.items():
19
+ path = os.path.join(base_dir, name)
20
+ if isinstance(content, dict):
21
+ os.makedirs(path, exist_ok=True)
22
+ create_directory_structure(path, content)
23
+ else:
24
+ create_file(path, content)
25
+
26
+ @pytest.fixture
27
+ def temp_dir():
28
+ with tempfile.TemporaryDirectory() as temp_dir:
29
+ gitignore_content = \
30
+ """
31
+ *.txt
32
+ *.py[oc]
33
+ .venv
34
+ """
35
+
36
+ structure = {
37
+ 'dir1': {
38
+ 'dir2': {},
39
+ 'file2.py': 'File 2 content',
40
+ },
41
+ 'file1.txt': 'File 1 content',
42
+ '.gitignore': gitignore_content,
43
+ '.venv': {
44
+ 'bin': {
45
+ 'activate': 'source venv/bin/activate',
46
+ 'python': 'python3.8',
47
+ },
48
+ 'lib': {
49
+ 'python3.8': {
50
+ 'site-packages': {
51
+ 'package1': {
52
+ '__init__.py': '',
53
+ 'module1.py': 'def foo(): pass',
54
+ },
55
+ 'package2': {
56
+ '__init__.py': '',
57
+ 'module2.py': 'def bar(): pass',
58
+ },
59
+ },
60
+ },
61
+ },
62
+ 'include': {},
63
+ },
64
+ }
65
+
66
+ create_directory_structure(temp_dir, structure)
67
+ yield temp_dir
68
+
69
+ @patch('reposnap.interfaces.cli.ProjectContentCollector')
70
+ def test_cli(mock_collector, temp_dir):
71
+ mock_collector_instance = MagicMock()
72
+ mock_collector.return_value = mock_collector_instance
73
+
74
+ with patch('sys.argv', ['cli.py', str(temp_dir), '--structure-only']):
75
+ main()
76
+
77
+ mock_collector_instance.collect_and_generate.assert_called_once()
@@ -0,0 +1,27 @@
1
+ # src/reposnap/tests/test_collector.py
2
+
3
+ import pytest # noqa
4
+ from unittest.mock import patch, MagicMock
5
+ from reposnap.core.collector import ProjectContentCollector
6
+
7
+
8
+ @patch('reposnap.core.collector.MarkdownGenerator')
9
+ @patch('reposnap.core.collector.FileSystem')
10
+ @patch('reposnap.core.collector.GitRepo')
11
+ def test_collect_and_generate(mock_git_repo, mock_file_system, mock_markdown_generator):
12
+ # Setup mocks
13
+ mock_git_repo_instance = MagicMock()
14
+ mock_file_system_instance = MagicMock()
15
+ mock_markdown_generator_instance = MagicMock()
16
+
17
+ mock_git_repo.return_value = mock_git_repo_instance
18
+ mock_file_system.return_value = mock_file_system_instance
19
+ mock_markdown_generator.return_value = mock_markdown_generator_instance
20
+
21
+ mock_git_repo_instance.get_git_files.return_value = ['file1.py', 'file2.py']
22
+ mock_file_system_instance.build_tree_structure.return_value = {'dir': {'file1.py': 'file1.py'}}
23
+
24
+ collector = ProjectContentCollector('root_dir', 'output.md', False, ['*.log'])
25
+ collector.collect_and_generate()
26
+
27
+ mock_markdown_generator_instance.generate_markdown.assert_called_once()
@@ -0,0 +1,28 @@
1
+ # src/reposnap/tests/test_file_system.py
2
+
3
+ import pytest
4
+ from reposnap.core.file_system import FileSystem
5
+
6
+ @pytest.fixture
7
+ def file_system(tmp_path):
8
+ # Initialize FileSystem with tmp_path as the root directory
9
+ return FileSystem(str(tmp_path))
10
+
11
+ def test_build_tree_structure(file_system, tmp_path):
12
+ # Ensure intermediate directories are created
13
+ (tmp_path / 'dir1').mkdir(parents=True, exist_ok=True)
14
+ (tmp_path / 'dir2').mkdir(parents=True, exist_ok=True)
15
+
16
+ # Write files into the created directories
17
+ (tmp_path / 'dir1' / 'file1.py').write_text('content')
18
+ (tmp_path / 'dir2' / 'file2.py').write_text('content')
19
+
20
+ # Build tree structure based on the files in tmp_path
21
+ tree_structure = file_system.build_tree_structure([
22
+ str(tmp_path / 'dir1' / 'file1.py'),
23
+ str(tmp_path / 'dir2' / 'file2.py')
24
+ ])
25
+ assert 'dir1' in tree_structure
26
+ assert 'file1.py' in tree_structure['dir1']
27
+ assert 'dir2' in tree_structure
28
+ assert 'file2.py' in tree_structure['dir2']
@@ -0,0 +1,16 @@
1
+ # src/reposnap/tests/test_git_repo.py
2
+
3
+ import pytest # noqa
4
+ from unittest.mock import patch, MagicMock
5
+ from reposnap.core.git_repo import GitRepo
6
+
7
+ @patch('reposnap.core.git_repo.Repo')
8
+ def test_get_git_files(mock_repo):
9
+ mock_repo_instance = MagicMock()
10
+ mock_repo_instance.git.ls_files.return_value = 'file1.py\nfile2.py'
11
+ mock_repo.return_value = mock_repo_instance
12
+
13
+ git_repo = GitRepo('test_repo')
14
+ files = git_repo.get_git_files()
15
+ assert 'file1.py' in files
16
+ assert 'file2.py' in files
@@ -0,0 +1,15 @@
1
+ # src/reposnap/tests/test_markdown_generator.py
2
+
3
+ import pytest # noqa
4
+ from reposnap.core.markdown_generator import MarkdownGenerator
5
+ from unittest.mock import mock_open, patch
6
+
7
+ def test_generate_markdown():
8
+ mock_open_func = mock_open()
9
+ with patch('builtins.open', mock_open_func):
10
+ markdown_generator = MarkdownGenerator()
11
+ markdown_generator.generate_markdown('output.md', {'dir': {'file.py': 'file.py'}}, ['file.py'], None)
12
+
13
+ mock_open_func.assert_called_once_with('output.md', 'w')
14
+ handle = mock_open_func()
15
+ handle.write.assert_called()
@@ -0,0 +1,15 @@
1
+ # src/reposnap/tests/test_path_utils.py
2
+
3
+ import pytest # noqa
4
+ from reposnap.utils.path_utils import format_tree
5
+
6
+ def test_format_tree():
7
+ tree = {
8
+ 'dir1': {
9
+ 'file1.py': 'file1.py'
10
+ },
11
+ 'file2.py': 'file2.py'
12
+ }
13
+ formatted = ''.join(format_tree(tree))
14
+ expected = 'dir1/\n file1.py\nfile2.py\n'
15
+ assert formatted == expected