Skip to content

Move strategy will delete empty dirs. #32

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

Closed
wants to merge 2 commits into from

Conversation

mdujava
Copy link
Contributor

@mdujava mdujava commented Dec 28, 2017

Hello,

This change adds feature mentioned in #20. After processing all files, it will walk dirs in reverse order and delete empty dirs.

@ivandokov
Copy link
Owner

I see you've added two test files in test_files directory, but I don't see new tests.
Actually here is my test for the same functionality that I've wrote some time ago but pushed just few hours ago.

phockup/tests.py

Lines 195 to 210 in 183e423

def test_handle_move_removes_empty_dirs():
tempdir = tempfile.mkdtemp(suffix='first')
firstnesteddir = os.path.join(tempdir, 'one')
nesteddir = os.path.join(firstnesteddir, 'two', 'three')
os.makedirs(nesteddir, exist_ok=True)
open(os.path.join(nesteddir, "move.txt"), "w").close()
handle_file(os.path.join(nesteddir, "move.txt"), tempdir, "%Y/%m/%d", True)
assert not os.path.isfile(os.path.join(nesteddir, "move.txt"))
assert os.path.isfile(os.path.join(tempdir, "unknown/move.txt"))
# shutil.rmtree(tempdir)
assert not os.path.isdir(firstnesteddir)

The code that I wrote is not working exactly as expected. It deletes only the currently processed directory, but leaves the parent empty directory.

Ideally the directories should be deleted right after all files are processed because in case of error in the process the processed directories will be already deleted. Do you have an idea how to implement it?

…ving

It is using function os.removedirs, which will remove as mach as it can
from right side of path (even whole path, if its only one path in source dir).

Signed-off-by: Matej Dujava <[email protected]>
@mdujava
Copy link
Contributor Author

mdujava commented Jan 1, 2018

Hello,
I updated this PR, can You check it?
Basicly after procesing all files in some dir, it will try to remove current dir and if it is successful it will try to remove all parent dirs inside of INPUTDIR. It is default behaivor of os.removedirs function.

@ivandokov
Copy link
Owner

To be honest that os.removedirs bothers me a bit.
What if the user has the following directory structure:

Photos/
├── MyCamera/
│   ├── photo1.jpg
│   ├── photo2.jpg
│   └── ...

and the input path is Photos/MyCamera. Since MyCamera is the only directory in Photos from os.removedirs docs I assume the folder Photos will be deleted too. Am I right?

If I am I think the code should be changed to manually look for parent directory and stop if it reached the input directory.

@mdujava
Copy link
Contributor Author

mdujava commented Jan 8, 2018

os.removedirs by definition will delete as far as input dir, but not further (meaning up in hierarhy). In your example tree, os.removedirs can recieve path MyCamera/some/path/inside/ and it will not delete Photos, it will stop at MyCamera (if you run inside of Photos folder).

But if we run it like

~$ phockup.py Photos/MyCamera output/sorted --move

it will delete Photos folder.

We can truncate INPUTDIR path to only last portion (MyCamera and cd to its parrent dir Photos) and then it will only delete desaired dir. I will update this PR.

@ivandokov ivandokov self-requested a review January 8, 2018 11:47
@mdujava
Copy link
Contributor Author

mdujava commented Jan 10, 2018

I have some problems. On linux we can use dir_fd to act like we are in inputdir and then remove everything inside it (in unix philosophy we souldn't change working dir, in case of kill, our process should create core dump inside original cwd). But this dir_fd is not supported on Windows, so we probably need to change dir or do removing manualy in some loop.

Is it ok to separate code if os supports dir_fd or should I create one universal code?

@ivandokov
Copy link
Owner

I think one universal function is more predictable for cross platform.

@ivandokov ivandokov removed their request for review January 21, 2018 09:24
@ivandokov
Copy link
Owner

@mdujava do you have any updates?

@ivandokov
Copy link
Owner

@mdujava, the code is obsolete and I will be closing this PR. If you want to update it to work with the latest release please submit a new PR and I will be more than happy to take a look.

@ivandokov ivandokov closed this Sep 17, 2018
@mdujava mdujava deleted the delte-empty-folders branch September 17, 2018 21:03
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

Successfully merging this pull request may close these issues.

2 participants