# PySourcePack

Source code packaging tool for projects defined in `projects.json`.

## Overview

PySourcePack is a command-line tool that packages source code and resource files for Python projects. It reads project metadata from a `projects.json` file (generated by `pyprojectparse`) and copies the relevant files to a `dist/src` directory within each project.

## Features

- **Project-based packaging**: Package specific projects or all projects at once
- **Selective inclusion**: Control which file types to include (Python files, README, etc.)
- **Exclusion patterns**: Exclude unnecessary files and directories (tests, cache, etc.)
- **Flexible output**: Custom output directory or use default project-specific location
- **Debug mode**: Enable detailed logging for troubleshooting

## Installation

```bash
pip install pysfi
```

Or install from source:

```bash
pip install -e .
```

## Usage

### Basic Usage

List all available projects:

```bash
pysourcepack -l
```

Pack a specific project:

```bash
pysourcepack -p projectname
```

Pack all projects:

```bash
pysourcepack
```

### Advanced Usage

Specify custom projects.json file:

```bash
pysourcepack -i path/to/projects.json
```

Specify custom base directory:

```bash
pysourcepack -d /path/to/projects
```

Specify custom output directory:

```bash
pysourcepack -o /custom/output/dir
```

Control which files to include:

```bash
pysourcepack --include "*.py" "README.md" "LICENSE"
```

Exclude specific patterns:

```bash
pysourcepack --exclude "*.pyc" "__pycache__" "tests"
```

Enable debug mode:

```bash
pysourcepack --debug
```

### Command-Line Options

```
-h, --help            Show help message and exit
-i, --input INPUT     Input projects.json file path (default: projects.json)
-d, --directory DIR   Base directory containing project folders (default: current directory)
-o, --output-dir DIR  Output directory for packed files (default: <project>/dist/src)
-p, --project NAME    Project name to pack (default: pack all projects)
-l, --list            List all available projects
--include PATTERN     File patterns to include (default: *.py, README.md, pyproject.toml)
--exclude PATTERN     File patterns to exclude (default: __pycache__, *.pyc, tests, etc.)
--debug               Enable debug mode
```

## Default Behavior

### Included Files (Default)

- All Python source files (`*.py`)
- `README.md`
- `pyproject.toml`
- `LICENSE` (if exists)

### Excluded Files/Directories (Default)

- `__pycache__/` directory and subdirectories
- `*.pyc`, `*.pyo` (compiled Python files)
- `.pytest_cache/` directory
- `.benchmarks/` directory
- `tests/` directory
- `.git/` directory
- `.gitignore` file
- `dist/` directory
- `build/` directory
- `*.egg-info/` directory

**Note**: When copying directories recursively, files matching exclusion patterns in subdirectories are automatically excluded using `shutil.copytree` with ignore patterns.

## Output Structure

When packing a project, files are copied to:

```
<project_path>/dist/src/
├── __init__.py
├── main.py
├── README.md
├── pyproject.toml
└── resources/ (if included)
```

## Examples

### Example 1: Pack the `docscan` project

```bash
pysourcepack -p docscan
```

Output:

```
Packing project 'docscan' to /path/to/docscan/dist/src
Copied file: docscan.py
Copied file: docscan_gui.py
Copied file: pyloader.py
Copied file: README.md
Copied directory: lang
Successfully packed docscan: 4 files, 1 directories
```

### Example 2: Pack all projects with custom output

```bash
pysourcepack -o /tmp/packed_projects
```

### Example 3: Pack specific files only

```bash
pysourcepack -p bumpversion --include "*.py" "README.md"
```

### Example 4: Pack with debug output

```bash
pysourcepack -p filedate --debug
```

## Integration with pyprojectparse

`pysourcepack` works seamlessly with `pyprojectparse`:

```bash
# First, generate projects.json
pyprojectparse -o projects.json

# Then, pack projects
pysourcepack -i projects.json -p projectname
```

## Requirements

- Python >= 3.8
- `tomli` >= 2.4.0 (for Python < 3.11)

## License

See LICENSE file for details.

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.
