mr-orange/Sick-Beard — pull request #356 by Vhex (2013-10-18 20:42:29+00:00)
Title: Issue #332: @Tolstyak's failed download handling

Sorry, I had some connectivity issues this morning. This is the pull request without the encodingKludge.py change.

**HOWEVER** It's really important that something is done about the os.path.split issue before pulling this in. I copied the notes from the other pull request here:

--------------

You shouldn't put in failed download handling without the fix for the way that the TPB branch currently handles finding the directory. The code is actually really simple and since python short circuits, it shouldn't add too much extra processing to the ek function and it's only after the OS is determined to be NT:
```python
# Fix difference in os.path.split for Windows vs Linux
if func == os.path.split and os.path.isdir(*args, **kwargs) and os.path.normpath(*args, **kwargs)[-1] != os.sep:
result = func(str(*args, **kwargs) + os.sep)
else:
result = func(*args, **kwargs)
```

The issue (and this occurs even in just processing) is that os.path.split on Windows and Linux perform differently when it comes to the trailing slash or backslash. Given the two directories:
```python
dirName = "C:\First\Second\Third" #Windows
dirName = "/first/second/third" #Linux
path, dirs = os.path.split(dirName)
print("path = " + path)
```
On Linux the output is:
path = /first/second/third
On Windows the output is:
path = C:\First\Second

If you add the trailing backslash in Windows then the output is path = C:\First\Second\Third as expected.

This probably doesn't matter much for processing right now, although if you have all of your folders in C:\TVShows\Show Subfolder then when Sab calls SB for processing it actually processes everything under C:\TVShows instead of everything under C:\TVShows\Show Subfolder. You can see this from my output:
```
Processing folder F:\In Progress\Complete\TV\Show.Name.Here
TV_DOWNLOAD_DIR: F:\In Progress\Processed\TV
PostProcessing Path: F:\In Progress\Complete\TV
PostProcessing Dirs: [u'Show.Name.Here']
PostProcessing Files: []
PostProcessing VideoFiles: []
```

Again, it's not a huge deal when you're dealing with just processing shows (even though you end up processing more directories than you intended) but when you start doing failed download and pass the "path" to the failed download, the path on windows is one level up from the path you intend.

Because of the way DELETE_FAILED is written, it's going to delete the directory it was passed (thinking that is the entire directory of the show. In my example above the directory that SHOULD be deleted is "F:\In Progress\Complete\TV\Show.Name.Here" but without the fix, the directory that gets deleted is "F:\In Progress\Complete\TV"

I can also put that code in the check before the function is passed to ek, if you feel more comfortable with that, but the ek code already has code that's specific to Windows in it, so I really think that's the best place for it.