Skip to main content

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


Download files

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

Source Distribution

tree_to_script-0.1.2.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

tree_to_script-0.1.2-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file tree_to_script-0.1.2.tar.gz.

File metadata

  • Download URL: tree_to_script-0.1.2.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

Hashes for tree_to_script-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9787ef11f51e178f19a07e5a30f8df71c61b8ca1e50ae1b290f36f61cba3245c
MD5 6d4198b65b71b79fb363536e9f407f81
BLAKE2b-256 4367e9f6834ac7f798402483c0cc8a4f0d2365020980394d9d67610a839b093d

See more details on using hashes here.

File details

Details for the file tree_to_script-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tree_to_script-0.1.2-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

Hashes for tree_to_script-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5e32f51a701b27e66e1dedfba6b2d040756d45f560f332f781254f880d8e5ddf
MD5 a7afe13b7beb27a75ac0356479b9d2a0
BLAKE2b-256 24af246539f6cec08a6af9aace2c5d2a280b9deeda49532daeec12b1a4d0439b

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