output-dir option

This commit is contained in:
Kirill Markin 2024-06-08 11:58:13 +02:00
parent f7b0e7df8e
commit a836beb856
2 changed files with 40 additions and 2 deletions

View file

@ -81,7 +81,7 @@ def remove_empty_dirs(tree_output: str, path='.') -> str:
logging.debug('Empty directory removal complete') logging.debug('Empty directory removal complete')
return '\n'.join(final_lines) return '\n'.join(final_lines)
def save_repo_to_text(path='.') -> str: def save_repo_to_text(path='.', output_dir=None) -> str:
logging.debug(f'Starting to save repo structure to text for path: {path}') logging.debug(f'Starting to save repo structure to text for path: {path}')
gitignore_spec = load_gitignore(path) gitignore_spec = load_gitignore(path)
tree_structure = get_tree_structure(path, gitignore_spec) tree_structure = get_tree_structure(path, gitignore_spec)
@ -91,6 +91,12 @@ def save_repo_to_text(path='.') -> str:
timestamp = datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S-UTC') timestamp = datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S-UTC')
output_file = f'repo_snapshot_{timestamp}.txt' output_file = f'repo_snapshot_{timestamp}.txt'
# Determine the full path to the output file
if output_dir:
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_file = os.path.join(output_dir, output_file)
with open(output_file, 'w') as file: with open(output_file, 'w') as file:
project_name = os.path.basename(os.path.abspath(path)) project_name = os.path.basename(os.path.abspath(path))
file.write(f'Directory: {project_name}\n\n') file.write(f'Directory: {project_name}\n\n')
@ -140,11 +146,12 @@ def save_repo_to_text(path='.') -> str:
def main(): def main():
parser = argparse.ArgumentParser(description='Convert repository structure and contents to text') parser = argparse.ArgumentParser(description='Convert repository structure and contents to text')
parser.add_argument('--debug', action='store_true', help='Enable debug logging') parser.add_argument('--debug', action='store_true', help='Enable debug logging')
parser.add_argument('--output-dir', type=str, help='Directory to save the output file')
args = parser.parse_args() args = parser.parse_args()
setup_logging(debug=args.debug) setup_logging(debug=args.debug)
logging.debug('repo-to-text script started') logging.debug('repo-to-text script started')
save_repo_to_text() save_repo_to_text(output_dir=args.output_dir)
logging.debug('repo-to-text script finished') logging.debug('repo-to-text script finished')
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -0,0 +1,31 @@
import os
import subprocess
import pytest
import time
def test_repo_to_text():
# Remove any existing snapshot files to avoid conflicts
for file in os.listdir('.'):
if file.startswith('repo_snapshot_') and file.endswith('.txt'):
os.remove(file)
# Run the repo-to-text command
result = subprocess.run(['repo-to-text'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Assert that the command ran without errors
assert result.returncode == 0, f"Command failed with error: {result.stderr.decode('utf-8')}"
# Check for the existence of the new snapshot file
snapshot_files = [f for f in os.listdir('.') if f.startswith('repo_snapshot_') and f.endswith('.txt')]
assert len(snapshot_files) == 1, "No snapshot file created or multiple files created"
# Verify that the snapshot file is not empty
with open(snapshot_files[0], 'r') as f:
content = f.read()
assert len(content) > 0, "Snapshot file is empty"
# Clean up the generated snapshot file
os.remove(snapshot_files[0])
if __name__ == "__main__":
pytest.main()