Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specifying a non-empty output dir erases the entire contents of that dir without warning #4

Open
luckman212 opened this issue Jun 23, 2023 · 2 comments

Comments

@luckman212
Copy link

luckman212 commented Jun 23, 2023

I just had an oops moment when I misunderstood that the --output parameter requires a non-existing or empty folder.

I ran the following command:

poetry run icons-asset-generator \
--path ~/Downloads/affinity/svg \
--image-name-remove c sq \
--output ~/Desktop \
diagrams.net

I got an error message that the dir wasn't empty, and then the program aborted. To my surprise, my entire ~/Desktop directory had been nuked. Luckily I had a backup, but this could easily be catastrophic.

I think this is the problematic code:

import os
import shutil
def create_output_dir(dir_name) -> None:
shutil.rmtree(dir_name, ignore_errors=True)
os.mkdir(dir_name)

Maybe a check for empty dir could be used instead (this is just an example)

import os
import shutil

from icons_asset_generator.util.logger import get_logger
logger = get_logger(__name__)

def is_directory_empty(path):
    if not os.path.isdir(path):
        raise ValueError("Invalid directory path")
    with os.scandir(path) as entries:
        for entry in entries:
            if not entry.name.startswith('.'):
                return False
    return True

def create_output_dir(dir_name) -> None:
    try:
        os.makedirs(dir_name, exist_ok=True)
    except Exception as e:
        logger.error(repr(e))
        exit(1) 
    try:
        assert(is_directory_empty(dir_name))
    except AssertionError:
        logger.error(f'directory {dir_name} is not empty!')
        exit(1)
@m-radzikowski
Copy link
Owner

To my surprise, my entire ~/Desktop directory had been nuked. Luckily I had a backup, but this could easily be catastrophic.

Oh, that's bad! Sorry for that!

Yes, as you noted, the script is trying to remove and re-create the output dir. This is useful if you are fiddling with the parameters and want to have a fresh output every time, but in retrospective, I agree that's dangerous. And you can do rm -rf ./library && poetry run icons-asset-generator ... to clean the output dir consciously.

I think requiring a nonexisting or empty output location is reasonable. If you can put this into a PR, I'll be happy to change this.

@luckman212
Copy link
Author

Sure, let me put that together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants