Repository: pymedusa/Medusa

Issue:
PostProcessing Error With _unpack And NZBGet
Good afternoon,

I have moved over to Medusa and also back to NZBGet and picked up an issue when the post processor starts whilst NZBGet is busy unpacking a download.  (See logs below)

I found [this commit](https://github.com/SickRage/SickRage/commit/30e3902cdb0f56b6e4deef92a1e7b6a40fffea86) from SickRage (hope it's OK to reference it here) which adds a _or folder_name.upper().endswith(u'_UNPACK'):_  in processTV.py (I'm assuming process_tv.py in Medusa line 227)

- If I look at my logs it looks like created a subfolder called _unpack which should then be picked up with the code as is or is it because it is a subfolder inside a subfolder of the folder being postprocessed?


**Branch/Commit:** Master
**OS:** Raspberry Pi 3 OSMC 2017.10-1
**What you did:** PostProcessing started whilst NZBGet is unpacking
**What happened:** Errors in the logs
**What you expected:** "Being Unpacked" warning
**Logs:** (Copied from /errorlogs/viewlog/, so read from bottom to top. Did not have debug logs enabled. I can try and reproduce if needed?)
```
2017-11-02 14:52:53 WARNING  POSTPROCESSOR :: [b16e0e5] /mnt/External/Downloads/completed/Series/Mr.Robot.S03e04.720p.Hdtv.Hevc.X265.Rmteam/_unpack/mr.robot.s03e04.720p.hdtv.hevc.x265.rmteam.mkv: Processing failed: Unable to move the files to their new home
2017-11-02 14:52:53 WARNING  POSTPROCESSOR :: [b16e0e5] Problem(s) during processing, failed for the following files/folders: 
2017-11-02 14:52:53 WARNING  POSTPROCESSOR :: [b16e0e5] Processing failed for /mnt/External/Downloads/completed/Series/Mr.Robot.S03e04.720p.Hdtv.Hevc.X265.Rmteam/_unpack/mr.robot.s03e04.720p.hdtv.hevc.x265.rmteam.mkv: Unable to move the files to their new home
2017-11-02 14:52:53 ERROR    POSTPROCESSOR :: [b16e0e5] Unable to move file /mnt/External/Downloads/completed/Series/Mr.Robot.S03e04.720p.Hdtv.Hevc.X265.Rmteam/_unpack/mr.robot.s03e04.720p.hdtv.hevc.x265.rmteam.mkv to /mnt/External/Series/Mr. Robot/Season 3/Mr. Robot - S03E04 - eps3.3_m3tadata.par2.mkv: OSError(2, 'No such file or directory')
Traceback (most recent call last):
  File "/opt/medusa/medusa/post_processor.py", line 449, in move
    helpers.move_file(cur_file_path, new_file_path)
  File "/opt/medusa/medusa/helpers/__init__.py", line 316, in move_file
    os.unlink(src_file)
  File "/opt/medusa/medusa/init/filesystem.py", line 85, in <lambda>
    return lambda *args, **kwargs: handle_output(f(*[handle_arg(arg) for arg in args], **{k: handle_arg(arg) for k, arg in kwargs.items()}))
OSError: [Errno 2] No such file or directory: '/mnt/External/Downloads/completed/Series/Mr.Robot.S03e04.720p.Hdtv.Hevc.X265.Rmteam/_unpack/mr.robot.s03e04.720p.hdtv.hevc.x265.rmteam.mkv'
2017-11-02 14:50:05 INFO     POSTPROCESSOR :: [b16e0e5] This download is marked a priority download so I'm going to replace an existing file if I find one
2017-11-02 14:50:05 INFO     POSTPROCESSOR :: [b16e0e5] New size: 52.00 MB
2017-11-02 14:50:05 INFO     POSTPROCESSOR :: [b16e0e5] New file: /mnt/External/Downloads/completed/Series/Mr.Robot.S03e04.720p.Hdtv.Hevc.X265.Rmteam/_unpack/mr.robot.s03e04.720p.hdtv.hevc.x265.rmteam.mkv
2017-11-02 14:50:05 INFO     POSTPROCESSOR :: [b16e0e5] Processing /mnt/External/Downloads/completed/Series/Mr.Robot.S03e04.720p.Hdtv.Hevc.X265.Rmteam/_unpack/mr.robot.s03e04.720p.hdtv.hevc.x265.rmteam.mkv
```



Discussion:

Resolving patch:
diff --git a/medusa/process_tv.py b/medusa/process_tv.py
--- a/medusa/process_tv.py
+++ b/medusa/process_tv.py
@@ -177,7 +177,7 @@ def process(self, resource_name=None, force=False, is_priority=None, delete_on=F
 
                 else:
                     self.log('Found temporary sync files in folder: {0}'.format(dir_path))
-                    self.log('Skipping post processing for folder: {0}'.format(dir_path))
+                    self.log('Skipping post-processing for folder: {0}'.format(dir_path))
                     self.missedfiles.append('{0}: Sync files found'.format(dir_path))
 
         if self.succeeded:
@@ -213,6 +213,25 @@ def should_process(self, path, failed=False):
         :return: True if the directory is valid for processing, otherwise False
         :rtype: Boolean
         """
+        if not self._is_valid_folder(path, failed):
+            return False
+
+        for root, dirs, files in os.walk(path):
+            for folder in dirs:
+                if not self._is_valid_folder(os.path.join(root, folder), failed):
+                    return False
+            for each_file in files:
+                if helpers.is_media_file(each_file) or helpers.is_rar_file(each_file):
+                    return True
+            # Stop at first subdirectories if post-processing path
+            if self.directory == path:
+                break
+
+        self.log('No processable items found in folder: {0}'.format(path), logger.DEBUG)
+        return False
+
+    def _is_valid_folder(self, path, failed):
+        """Verify folder validity based on the checks below."""
         folder = os.path.basename(path)
         if folder in self.IGNORED_FOLDERS:
             return False
@@ -224,31 +243,24 @@ def should_process(self, path, failed=False):
             self.log('The directory name indicates that it was previously rejected for being undersized.',
                      logger.DEBUG)
             failed = True
-        elif folder.upper().startswith('_UNPACK'):
-            self.log('The directory name indicates that this release is in the process of being unpacked.',
-                     logger.DEBUG)
-            self.missedfiles.append('{0}: Being unpacked'.format(folder))
-            return False
 
         if failed:
             self.process_failed(path)
-            self.missedfiles.append('{0}: Failed download'.format(folder))
+            self.missedfiles.append('{0}: Failed download'.format(path))
+            return False
+
+        if folder.startswith('_unpack'):
+            self.log('The directory name indicates that this release is in the process of being unpacked.',
+                     logger.DEBUG)
+            self.missedfiles.append('{0}: Being unpacked'.format(path))
             return False
 
         if helpers.is_hidden_folder(path):
             self.log('Ignoring hidden folder: {0}'.format(folder), logger.DEBUG)
-            self.missedfiles.append('{0}: Hidden folder'.format(folder))
+            self.missedfiles.append('{0}: Hidden folder'.format(path))
             return False
 
-        for root, dirs, files in os.walk(path):
-            for each_file in files:
-                if helpers.is_media_file(each_file) or helpers.is_rar_file(each_file):
-                    return True
-            del root  # unused variable
-            del dirs  # unused variable
-
-        self.log('No processable items found in folder: {0}'.format(path), logger.DEBUG)
-        return False
+        return True
 
     def _get_files(self, path):
         """Return the path to a folder and its contents as a tuple."""


Test patch: