A utility for transforming directory tree structures from text into actionable shell commands or executing them directly
Project description
TreeToScript
Welcome to TreeToScript, a utility for transforming directory tree structures from text into actionable shell commands or executing them directly. This tool is perfect for setting up project file structures, automating file system operations, or documenting directory layouts.
Features
- Tree Structure Parsing: Converts text representations of directory structures into commands.
- Command Generation: Generates shell commands (
mkdir,touch) to replicate the given structure. - Validation: Ensures the tree structure is valid, checking for proper nesting and syntax.
- Special Character Support: Handles file and directory names with spaces, special characters, and Unicode.
- Comment Support: Ignores comments within the tree structure text.
- Serialization: Outputs commands in various formats like Bash arrays or YAML.
- Command Execution: Provides options to either generate commands or execute them directly with caution.
Installation
Python Requirement
Python 3.7 or higher.
From PyPI
pip install tree-to-script
From Source
git clone <repository-url>
cd tree-to-script
python setup.py install
Usage
Input Format
Your input should be a text file or stdin describing a directory tree:
project/
├── src/
│ ├── main.py
│ └── util/
│ └── helper.py
├── tests/
│ └── test_main.py
└── README.md
Running TreeToScript
To Generate Commands:
python3 -m tree_to_script.main example_tree.txt
To Execute Commands Directly:
Add the --no-dry flag to run commands immediately:
python3 -m tree_to_script.main example_tree.txt --no-dry
For stdin Input:
echo "project/
├── src/
│ ├── main.py
└── README.md" | python3 -m tree_to_script.main --no-dry
Output
By default, the output is a Bash array of commands:
commands=(
'mkdir -p project'
'mkdir -p project/src'
'touch project/src/main.py'
'mkdir -p project/src/util'
'touch project/src/util/helper.py'
'mkdir -p project/tests'
'touch project/tests/test_main.py'
'touch project/README.md'
)
Serialization Formats
Choose your output format:
python3 -m tree_to_script.main example_tree.txt --serializer yaml
Output in YAML:
commands:
- mkdir -p project
- mkdir -p project/src
- touch project/src/main.py
- mkdir -p project/src/util
- touch project/src/util/helper.py
- mkdir -p project/tests
- touch project/tests/test_main.py
- touch project/README.md
Command Execution
Warning: When using --no-dry, commands are executed directly. Be cautious about:
- Permissions: Ensure you have the necessary permissions to create directories and files.
- Environment: Avoid running in sensitive environments where unintended changes could be harmful.
- Error Handling: The tool will attempt to handle errors gracefully, but always check the output for any issues.
Debugging
Debug the parsed tree:
from tree_to_script.debug import print_tree
from tree_to_script.tree_parser import parse_tree_to_nodes
nodes = parse_tree_to_nodes(tree_lines)
print_tree(nodes)
Testing
Unit Tests
python3 -m unittest tree_to_script.test_tree_parser
python3 -m unittest tree_to_script.test_complex_structure
Integration Tests
python3 -m unittest tree_to_script.test_integration
Examples
Example 1: Basic Tree
Input (example_tree.txt):
project/
├── src/
│ ├── main.py
│ └── util/
│ └── helper.py
Command:
python3 -m tree_to_script.main example_tree.txt
Output:
commands=(
'mkdir -p project'
'mkdir -p project/src'
'touch project/src/main.py'
'mkdir -p project/src/util'
'touch project/src/util/helper.py'
)
To Execute:
python3 -m tree_to_script.main example_tree.txt --no-dry
Example 2: Special Characters
Input (special_chars.txt):
documents/
├── my report.pdf
└── notes 2023.txt
Command:
python3 -m tree_to_script.main special_chars.txt
Output:
commands=(
'mkdir -p documents'
"touch 'documents/my report.pdf'"
"touch 'documents/notes 2023.txt'"
)
To Execute:
python3 -m tree_to_script.main special_chars.txt --no-dry
License
This project is licensed under the MIT License - see the LICENSE file for details.
Version
Current version: 0.0.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tree_to_script-0.1.1.tar.gz.
File metadata
- Download URL: tree_to_script-0.1.1.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e535c743001c6974713751eb96a436d1e668f1f3d52557357ff414d2ff19fea
|
|
| MD5 |
b439d34caf9468fcd66b34891d25c630
|
|
| BLAKE2b-256 |
a1fc424660c7e2e391fe8d2394d87f5bf0ef17b686255336237a9aa1a499fa60
|
File details
Details for the file tree_to_script-0.1.1-py3-none-any.whl.
File metadata
- Download URL: tree_to_script-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f150b054c05685419917ddaecd804f980fcb12254c3fb0a5b49ce339804c7dd5
|
|
| MD5 |
46fe8c7a6c76233e13d22263f5a07756
|
|
| BLAKE2b-256 |
a35641ae41bfdf336901d7bb1898a1ae93a6cbcc6cc0d1bfbf020397ff91ed95
|