diff options
Diffstat (limited to 'module/plugins')
| -rw-r--r-- | module/plugins/hooks/ExternalScripts.py | 1 | ||||
| -rw-r--r-- | module/plugins/hooks/ExtractArchive.py | 132 | 
2 files changed, 68 insertions, 65 deletions
| diff --git a/module/plugins/hooks/ExternalScripts.py b/module/plugins/hooks/ExternalScripts.py index 435ab618c..16049ae9b 100644 --- a/module/plugins/hooks/ExternalScripts.py +++ b/module/plugins/hooks/ExternalScripts.py @@ -96,6 +96,7 @@ class ExternalScripts(Hook):              except Exception:                  self.logError(_("Runtime error: %s") % os.path.abspath(script), _("Unknown error")) +      def beforeReconnecting(self, ip):          for script in self.scripts['before_reconnect']:              self.callScript(script, ip) diff --git a/module/plugins/hooks/ExtractArchive.py b/module/plugins/hooks/ExtractArchive.py index edf25b8c7..0c163078c 100644 --- a/module/plugins/hooks/ExtractArchive.py +++ b/module/plugins/hooks/ExtractArchive.py @@ -12,8 +12,7 @@ from traceback import print_exc  # http://bugs.python.org/issue6122 , http://bugs.python.org/issue1236 , http://bugs.python.org/issue1731717  if sys.version_info < (2, 7) and os.name != "nt":      import errno - -    from subprocess import Popen +    import subprocess      def _eintr_retry_call(func, *args):          while True: @@ -44,13 +43,13 @@ if sys.version_info < (2, 7) and os.name != "nt":              self._handle_exitstatus(sts)          return self.returncode -    Popen.wait = wait +    subprocess.Popen.wait = wait  if os.name != "nt":      from grp import getgrnam      from pwd import getpwnam -from module.plugins.Hook import Hook, threaded, Expose +from module.plugins.Hook import Hook, Expose, threaded  from module.plugins.internal.Extractor import ArchiveError, CRCError, PasswordError  from module.plugins.internal.SimpleHoster import replace_patterns  from module.utils import fs_encode, save_join, uniqify @@ -104,28 +103,27 @@ class ArchiveQueue(object):          return self.set(queue) -  class ExtractArchive(Hook):      __name__    = "ExtractArchive"      __type__    = "hook" -    __version__ = "1.33" - -    __config__ = [("activated"      , "bool"  , "Activated"                                 , True                                                                     ), -                  ("fullpath"       , "bool"  , "Extract with full paths"                   , True                                                                     ), -                  ("overwrite"      , "bool"  , "Overwrite files"                           , False                                                                    ), -                  ("keepbroken"     , "bool"  , "Try to extract broken archives"            , False                                                                    ), -                  ("repair"         , "bool"  , "Repair broken archives (rar required)"     , False                                                                    ), -                  ("test"           , "bool"  , "Test archive before extracting"            , False                                                                    ), -                  ("usepasswordfile", "bool"  , "Use password file"                         , True                                                                     ), -                  ("passwordfile"   , "file"  , "Password file"                             , "archive_password.txt"                                                   ), -                  ("delete"         , "bool"  , "Delete archive when successfully extracted", False                                                                    ), -                  ("subfolder"      , "bool"  , "Create subfolder for each package"         , False                                                                    ), -                  ("destination"    , "folder", "Extract files to folder"                   , ""                                                                       ), -                  ("extensions"     , "str"   , "Extract the following extensions"          , "7z,bz2,bzip2,gz,gzip,lha,lzh,lzma,rar,tar,taz,tbz,tbz2,tgz,xar,xz,z,zip"), -                  ("excludefiles"   , "str"   , "Don't extract the following files"         , "*.nfo,*.DS_Store,index.dat,thumb.db"                                    ), -                  ("recursive"      , "bool"  , "Extract archives in archives"              , True                                                                     ), -                  ("waitall"        , "bool"  , "Wait for all downloads to be finished"     , False                                                                    ), -                  ("renice"         , "int"   , "CPU priority"                              , 0                                                                        )] +    __version__ = "1.34" + +    __config__ = [("activated"      , "bool"  , "Activated"                             , True                                                                     ), +                  ("fullpath"       , "bool"  , "Extract with full paths"               , True                                                                     ), +                  ("overwrite"      , "bool"  , "Overwrite files"                       , False                                                                    ), +                  ("keepbroken"     , "bool"  , "Try to extract broken archives"        , False                                                                    ), +                  ("repair"         , "bool"  , "Repair broken archives (RAR required)" , False                                                                    ), +                  ("test"           , "bool"  , "Test archive before extracting"        , False                                                                    ), +                  ("usepasswordfile", "bool"  , "Use password file"                     , True                                                                     ), +                  ("passwordfile"   , "file"  , "Password file"                         , "archive_password.txt"                                                   ), +                  ("delete"         , "bool"  , "Delete archive after extraction"       , False                                                                    ), +                  ("subfolder"      , "bool"  , "Create subfolder for each package"     , False                                                                    ), +                  ("destination"    , "folder", "Extract files to folder"               , ""                                                                       ), +                  ("extensions"     , "str"   , "Extract archives ending with extension", "7z,bz2,bzip2,gz,gzip,lha,lzh,lzma,rar,tar,taz,tbz,tbz2,tgz,xar,xz,z,zip"), +                  ("excludefiles"   , "str"   , "Don't extract the following files"     , "*.nfo,*.DS_Store,index.dat,thumb.db"                                    ), +                  ("recursive"      , "bool"  , "Extract archives in archives"          , True                                                                     ), +                  ("waitall"        , "bool"  , "Run after all downloads was processed" , False                                                                    ), +                  ("renice"         , "int"   , "CPU priority"                          , 0                                                                        )]      __description__ = """Extract different kind of archives"""      __license__     = "GPLv3" @@ -139,6 +137,8 @@ class ExtractArchive(Hook):      def setup(self): +        self.info = {}  #@TODO: Remove in 0.4.10 +          self.queue  = ArchiveQueue(self, "Queue")          self.failed = ArchiveQueue(self, "Failed") @@ -158,7 +158,7 @@ class ExtractArchive(Hook):                  if klass.isUsable():                      self.extractors.append(klass)                  if klass.REPAIR: -                    self.repair = self.getConfig("repair") +                    self.repair = self.getConfig('repair')              except OSError, e:                  if e.errno == 2: @@ -181,10 +181,10 @@ class ExtractArchive(Hook):      @threaded -    def extractQueued(self,thread): +    def extractQueued(self, thread):          packages = self.queue.get()          while packages: -            if self.lastPackage: # called from allDownloadsProcessed +            if self.lastPackage:  #: called from allDownloadsProcessed                  self.lastPackage = False                  if self.extract(packages, thread):  #@NOTE: check only if all gone fine, no failed reporting for now                      self.manager.dispatchEvent("all_archives_extracted") @@ -193,7 +193,7 @@ class ExtractArchive(Hook):                  if self.extract(packages, thread):  #@NOTE: check only if all gone fine, no failed reporting for now                      pass -            packages = self.queue.get() # check for packages added during extraction +            packages = self.queue.get()  #: check for packages added during extraction      @Expose @@ -201,7 +201,7 @@ class ExtractArchive(Hook):          """ Extract packages with given id"""          for id in ids:              self.queue.add(id) -        if not self.getConfig("waitall") and not self.extracting: +        if not self.getConfig('waitall') and not self.extracting:              self.extractQueued() @@ -211,7 +211,7 @@ class ExtractArchive(Hook):      def packageFinished(self, pypack):          self.queue.add(pypack.id) -        if not self.getConfig("waitall") and not self.extracting: +        if not self.getConfig('waitall') and not self.extracting:              self.extractQueued() @@ -221,7 +221,8 @@ class ExtractArchive(Hook):              self.extractQueued() -    def extract(self, ids, thread=None): +    @Expose +    def extract(self, ids, thread=None):  #@TODO: Use pypack, not pid to improve method usability          if not ids:              return False @@ -233,17 +234,17 @@ class ExtractArchive(Hook):          toList = lambda string: string.replace(' ', '').replace(',', '|').replace(';', '|').split('|') -        destination  = self.getConfig("destination") -        subfolder    = self.getConfig("subfolder") -        fullpath     = self.getConfig("fullpath") -        overwrite    = self.getConfig("overwrite") -        renice       = self.getConfig("renice") -        recursive    = self.getConfig("recursive") -        delete       = self.getConfig("delete") -        keepbroken   = self.getConfig("keepbroken") +        destination  = self.getConfig('destination') +        subfolder    = self.getConfig('subfolder') +        fullpath     = self.getConfig('fullpath') +        overwrite    = self.getConfig('overwrite') +        renice       = self.getConfig('renice') +        recursive    = self.getConfig('recursive') +        delete       = self.getConfig('delete') +        keepbroken   = self.getConfig('keepbroken') -        extensions   = [x.lstrip('.').lower() for x in toList(self.getConfig("extensions"))] -        excludefiles = toList(self.getConfig("excludefiles")) +        extensions   = [x.lstrip('.').lower() for x in toList(self.getConfig('extensions'))] +        excludefiles = toList(self.getConfig('excludefiles'))          if extensions:              self.logDebug("Use for extensions: %s" % "|.".join(extensions)) @@ -254,7 +255,7 @@ class ExtractArchive(Hook):          # dl folder          dl = self.config['general']['download_folder'] -        #iterate packages -> extractors -> targets +        # iterate packages -> extractors -> targets          for pid in ids:              pypack = self.core.files.getPackage(pid) @@ -276,7 +277,7 @@ class ExtractArchive(Hook):              matched   = False              success   = True              files_ids = dict((pylink['name'],((save_join(dl, pypack.folder, pylink['name'])), pylink['id'], out)) for pylink \ -                        in sorted(pypack.getChildren().itervalues(), key=lambda k: k['name'])).values()    # remove duplicates +                        in sorted(pypack.getChildren().itervalues(), key=lambda k: k['name'])).values()  #: remove duplicates              # check as long there are unseen files              while files_ids: @@ -301,6 +302,7 @@ class ExtractArchive(Hook):                          self.logInfo(name, _("Extract to: %s") % fout)                          try: +                            pyfile  = self.core.files.getFile(fid)                              archive = Extractor(self,                                                  fname,                                                  fout, @@ -311,16 +313,22 @@ class ExtractArchive(Hook):                                                  delete,                                                  keepbroken,                                                  fid) + +                            thread.addActive(pyfile)                              archive.init() -                            new_files = self._extract(archive, fid, pypack.password, thread) +                            try: +                                new_files = self._extract(pyfile, archive, pypack.password) + +                            finally: +                                thread.finishFile(pyfile)                          except Exception, e:                              self.logError(name, e)                              success = False                              continue -                        files_ids.remove((fname, fid, fout)) # don't let other extractors spam log +                        files_ids.remove((fname, fid, fout))  #: don't let other extractors spam log                          self.logDebug("Extracted files: %s" % new_files)                          self.setPermissions(new_files) @@ -331,12 +339,11 @@ class ExtractArchive(Hook):                                  continue                              if recursive and os.path.isfile(file): -                                new_files_ids.append((filename, fid, os.path.dirname(filename)))  # append as new target +                                new_files_ids.append((filename, fid, os.path.dirname(filename)))  #: append as new target -                        pyfile = self.core.files.getFile(fid) -                        self.manager.dispatchEvent("archive_extracted", pyfile, archive.out, archive.filename, new_files) +                        self.manager.dispatchEvent("archive_extracted", pyfile, archive) -                files_ids = new_files_ids  # also check extracted files +                files_ids = new_files_ids  #: also check extracted files              if matched:                  if success: @@ -364,21 +371,19 @@ class ExtractArchive(Hook):          return True if not failed else False -    def _extract(self, archive, fid, password, thread): -        pyfile = self.core.files.getFile(fid) +    def _extract(self, pyfile, archive, password):          name   = os.path.basename(archive.filename) -        thread.addActive(pyfile)          pyfile.setStatus("processing")          encrypted = False          try:              self.logDebug("Password: %s" % (password or "None provided")) -            passwords = uniqify([password] + self.getPasswords(False)) if self.getConfig("usepasswordfile") else [password] +            passwords = uniqify([password] + self.getPasswords(False)) if self.getConfig('usepasswordfile') else [password]              for pw in passwords:                  try: -                    if self.getConfig("test") or self.repair: -                        pyfile.setCustomStatus(_("testing")) +                    if self.getConfig('test') or self.repair: +                        pyfile.setCustomStatus(_("archive testing"))                          if pw:                              self.logDebug("Testing with password: %s" % pw)                          pyfile.setProgress(0) @@ -402,12 +407,12 @@ class ExtractArchive(Hook):                      if self.repair:                          self.logWarning(name, _("Repairing...")) -                        pyfile.setCustomStatus(_("repairing")) +                        pyfile.setCustomStatus(_("archive repairing"))                          pyfile.setProgress(0)                          repaired = archive.repair()                          pyfile.setProgress(100) -                        if not repaired and not self.getConfig("keepbroken"): +                        if not repaired and not self.getConfig('keepbroken'):                              raise CRCError("Archive damaged")                          self.addPassword(pw) @@ -421,7 +426,7 @@ class ExtractArchive(Hook):              pyfile.setCustomStatus(_("extracting"))              pyfile.setProgress(0) -            if not encrypted or not self.getConfig("usepasswordfile"): +            if not encrypted or not self.getConfig('usepasswordfile'):                  self.logDebug("Extracting using password: %s" % (password or "None"))                  archive.extract(password)              else: @@ -439,12 +444,12 @@ class ExtractArchive(Hook):                      raise PasswordError              pyfile.setProgress(100) -            pyfile.setCustomStatus(_("finalizing")) +            pyfile.setStatus("processing")              if self.core.debug:                  self.logDebug("Would delete: %s" % ", ".join(archive.getDeleteFiles())) -            if self.getConfig("delete"): +            if self.getConfig('delete'):                  files = archive.getDeleteFiles()                  self.logInfo(_("Deleting %s files") % len(files))                  for f in files: @@ -473,10 +478,7 @@ class ExtractArchive(Hook):              if self.core.debug:                  print_exc() -        finally: -            thread.finishFile(pyfile) - -        self.manager.dispatchEvent("archive_extract_failed", pyfile) +        self.manager.dispatchEvent("archive_extract_failed", pyfile, archive)          raise Exception(_("Extract failed")) @@ -494,7 +496,7 @@ class ExtractArchive(Hook):          try:              passwords = [] -            file = fs_encode(self.getConfig("passwordfile")) +            file = fs_encode(self.getConfig('passwordfile'))              with open(file) as f:                  for pw in f.read().splitlines():                      passwords.append(pw) @@ -512,7 +514,7 @@ class ExtractArchive(Hook):          try:              self.passwords = uniqify([password] + self.passwords) -            file = fs_encode(self.getConfig("passwordfile")) +            file = fs_encode(self.getConfig('passwordfile'))              with open(file, "wb") as f:                  for pw in self.passwords:                      f.write(pw + '\n') | 
