Skip to main content

A utility to save source code from directories and files into a single output file with smart file filtering, streaming, and progress tracking.

Project description

Python Code Saver

Python Code Saver is a command-line utility that gathers Python code from specified directories and individual files, then consolidates them into one output file. This tool is especially useful for creating a single aggregated file from multiple Python scripts for documentation, analysis, or archiving purposes.


Features

  • Recursive File Gathering: Scans directories recursively for all .py files.
  • Customizable Directory Skipping: Easily exclude specific subdirectories (default: node_modules, dist, build, .git).
  • Git Integration: Collect only files tracked by Git with options for staged or unstaged changes.
  • File Filtering: Automatically skips the script itself to avoid self-inclusion.
  • Flexible Input Options: Combine files from multiple root directories and individual file paths.
  • Multiple File Types: Support for various file extensions beyond Python (e.g., js, toml, html, css).
  • Output Customization: Specify the output file location (defaults to temp.txt in the script's directory).
  • Progress Indicator: Shows a progress bar during file processing for better visibility.
  • Enhanced Output Display: After saving, the tool prints a colored list of all the files that were processed.

Installation

  1. Clone the Repository:

    git clone https://github.com/yourusername/python-code-saver.git
    cd python-code-saver
    
  2. Ensure Python 3 is Installed:

Verify that you have Python 3 installed on your system:

python3 --version

If not, download and install it from python.org.


Usage

Run the script from the command line using Python. Below are the available options:

python cli.py [OPTIONS]

Options

-r or --roots

Specify one or more root directories to search for files.

python -m savecode -r ./src ./lib

-f or --files

Provide one or more individual file paths to include.

python -m savecode -f script1.py script2.py

-o or --output

Define the output file path. If not provided, defaults to temp.txt in the current directory.

python -m savecode -o combined_output.txt

--skip

List subdirectory names to skip during the search (default: node_modules, dist, build, .git).

python -m savecode --skip tests docs

--ext or --extensions

Specify file extensions to collect (without leading period). If not provided, defaults to 'py'.

python -m savecode --ext py js html css

--git

Collect files listed by Git status instead of walking the filesystem.

python -m savecode --git

--staged

With --git: only include staged changes.

python -m savecode --git --staged

--unstaged

With --git: only include unstaged changes.

python -m savecode --git --unstaged

--all-ext

When used with --git, include every file Git reports, ignoring --ext.

python -m savecode --git --all-ext

Example Commands

Basic Usage:

Combine files from the current directory with specific extensions:

python -m savecode --ext py js html css

Every run automatically places the combined output on your clipboard. Set environment variable SAVECODE_NOCOPY=1 if you prefer to disable this.

Multiple Directories:

Combine files from multiple directories, exclude certain subdirectories, and specify an output file:

python -m savecode -r ./project/src ./project/utils -o all_code.txt --skip __pycache__ migrations

Git Integration:

Collect only files tracked by Git with changes (staged or unstaged):

python -m savecode --git

Collect only files with staged changes in Git:

python -m savecode --git --staged

Using the Makefile:

The included Makefile provides convenient shortcuts:

# Run on current directory with default extensions
make run

# Run with custom arguments
make run ARGS="--ext js html css"

Snapshot just what Git says has changed

make git            # staged + unstaged + untracked, every file type
make git-staged     # staged only
make git-unstaged   # unstaged + untracked

Use ARGS="--ext py js" if you want to re-enable filtering.


How It Works

  1. File Discovery: The script recursively walks through the provided root directories, collecting paths of all .py files while ignoring directories specified by the --skip option and excluding itself (cli.py).

  2. File Aggregation: Each discovered Python file is opened, and its content is read and written into the output file. A header indicating the file path is inserted before each file's content to provide clear separation.

  3. Output Confirmation: Once the aggregation is complete, the script prints a summary that includes the number of files processed and a list of file paths, with colored formatting for improved readability.


Contributing

Contributions are welcome! If you have suggestions or improvements, feel free to open an issue or submit a pull request.

  1. Fork the repository.

  2. Create your feature branch: git checkout -b feature/my-new-feature

  3. Commit your changes: git commit -am 'Add some feature'

  4. Push to the branch: git push origin feature/my-new-feature

  5. Open a pull request.


License

Open Source


Acknowledgments

Inspired by the need for a simple yet effective tool to consolidate Python code for documentation and analysis.


Happy coding!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

savecode-2.1.5.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

savecode-2.1.5-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file savecode-2.1.5.tar.gz.

File metadata

  • Download URL: savecode-2.1.5.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for savecode-2.1.5.tar.gz
Algorithm Hash digest
SHA256 657d1ba6895a23e1a088a72ba536e1eda5741a675de68daea35cc1e1b28ed779
MD5 5bb3c65c129348da6cd9ef1d591aba7c
BLAKE2b-256 585b504e2242e671780c5aa3b1eab7b60f55c743fa118d7fecd76692506434b1

See more details on using hashes here.

File details

Details for the file savecode-2.1.5-py3-none-any.whl.

File metadata

  • Download URL: savecode-2.1.5-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for savecode-2.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 155a38b8f5096cf42a212dc81b552455b86d9b4b226da3481f8a98fd30e1c90c
MD5 e01e394664036da445e2d9db88190eb4
BLAKE2b-256 88916d06cf8169ddebcd71cd40981c96d83a633b6c8960bb2329858223b6fe1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page