Repository: pymedusa/Medusa

Issue:
ValueError: Cannot use unrar directly on memory buffer
### Before submitting your issue:

Enable debug logging in SickRage settings, reproduce the error (be sure to disable after the bug is fixed)

Branch/Commit: develop@fd8acdd
OS: Linux hostname 3.10.77 #8451 SMP Wed Aug 17 05:13:40 CST 2016 x86_64 GNU/Linux synology_braswell_716+
What you did: Automatic FIND SUBTITLES thread searching missing subtitles
What happened: Legendas.tv provider was discarded for once.upon.a.time.s01e12
What you expected: The subtitle search for once.upon.a.time.s01e12 to happen without any issue
Logs: https://gist.github.com/ratoaq2/cf98bd956245fd53a0e83b77afa1fb37

```
2016-09-09 01:16:34 WARNING  FINDSUBTITLES :: [fd8acdd] Unexpected error in provider 'legendastv', discarding it
Traceback (most recent call last):
  File "/usr/local/sickbeard-custom/var/SickBeard/lib/subliminal/core.py", line 171, in download_subtitle
    self[subtitle.provider_name].download_subtitle(subtitle)
  File "/usr/local/sickbeard-custom/var/SickBeard/lib/subliminal/providers/legendastv.py", line 448, in download_subtitle
    subtitle.content = fix_line_ending(subtitle.archive.content.read(subtitle.name))
  File "/usr/local/sickbeard-custom/var/SickBeard/lib/rarfile/__init__.py", line 657, in read
  File "/usr/local/sickbeard-custom/var/SickBeard/lib/rarfile/__init__.py", line 642, in open
  File "/usr/local/sickbeard-custom/var/SickBeard/lib/rarfile/__init__.py", line 1189, in _open_unrar
ValueError: Cannot use unrar directly on memory buffer
```

Probably related to https://github.com/Diaoul/subliminal/issues/644



Discussion:
The current rarfile version doesn't have that exception anymore. I couldn't figure out exactly when that got changed, but maybe it was a bug that got fixed: https://github.com/markokr/rarfile/blob/master/rarfile.py#L1164

Let's update rarfile and see if the exception persists?

What's the magic?

This file -> `SickBeard/lib/rarfile/__init__.py` doesn't exist in the source code, but it exists on my installation

Found the issue:

```
~/SickBeard$ ls -ltha lib/rarfile
total 60K
drwxrwxr-x 1 sickbeard-custom users 1.8K Aug 11 22:13 ..
drwxrwxr-x 1 sickbeard-custom users   24 Jul 13 17:55 .
-rwxrwxr-x 1 sickbeard-custom users  60K Jun 27 20:19 __init__.pyc
```

The compiled file is still there, it wasn't removed. Probably the folder structure changed and these *.pyc files were left behind.

We need to ensure that all these files are removed when performing version upgrade.

Executing this command:

```
/SickBeard$ git clean -f -d
Removing lib/fanart/
Removing lib/libtrakt/
Removing lib/rarfile/
Removing lib/simplejson/
```

Solved the issue. Probably that should be executed in our update process

https://github.com/pymedusa/SickRage/blame/develop/sickbeard/versionChecker.py#L663

We should execute git clean only in sickbeard, sickrage and lib folders. That way we can ensure users are running the correct code for the correct version of the application and we avoid deleting user's additional files/folders in the root folder 

We used to have data folders likd backup in .gitignore

Wondering if this will solve thraxis issue


Resolving patch:
diff --git a/sickbeard/versionChecker.py b/sickbeard/versionChecker.py
--- a/sickbeard/versionChecker.py
+++ b/sickbeard/versionChecker.py
@@ -660,7 +660,7 @@ def update(self):
 
         # remove untracked files and performs a hard reset on git branch to avoid update issues
         if self._is_hard_reset_allowed():
-            # self.clean() # This is removing user data and backups
+            self.clean()
             self.reset()
 
         if self.branch == self._find_installed_branch():
@@ -692,13 +692,20 @@ def _is_hard_reset_allowed():
                                         sickbeard.BRANCH in sickbeard.GIT_RESET_BRANCHES)
 
     def clean(self):
+        """Call git clean to remove all untracked files.
+
+        It only affects source folders (sickbeard, sickrage) and the lib folder,
+        to prevent deleting untracked user data not known by .gitignore
+
+        :return:
+        :rtype: bool
         """
-        Calls git clean to remove all untracked files. Returns a bool depending
-        on the call's success.
-        """
-        _, _, exit_status = self._run_git(self._git_path, 'clean -df ""')  # @UnusedVariable
-        if exit_status == 0:
-            return True
+        for folder in ('lib', 'sickbeard', 'sickrage'):
+            _, _, exit_status = self._run_git(self._git_path, 'clean -d -f {0}'.format(folder))
+            if exit_status != 0:
+                return False
+
+        return True
 
     def reset(self):
         """


Test patch: