Christian Ebert avatar Christian Ebert committed ca16abe

urlcollector: always write stdin to tempfile

Comments (0)

Files changed (1)

muttils/urlcollector.py

             sl.append(s)
         return sl
 
-    def filedeconstructor(self, fp):
+    def filedeconstructor(self, fn):
         '''Checks if given file object is message or mailbox.
         If no, returns text contents of file or empty string if file is binary.
         Parses message/mailbox for relevant headers adding urls to list of items
         and returns text parts for further searching.'''
         # binary check from mercurial.util
-        s = fp.read()
-        if '\0' in s:
-            return ''
-        elif self.ui.text:
-            return s
-        msg = _msgfactory(fp)
-        if not msg:
-            return s
-        # else it's a message or a mailbox
-        if not msg['message-id']:
-            hint = ('make sure input is a raw message,'
-                    ' in mutt: unset pipe_decode')
-            raise util.DeadMan('no message-id found', hint=hint)
-        if not msg.get_unixfrom():
-            sl = self.msgharvest(msg)
-        else: # treat s like a mailbox because it might be one
-            sl = [] # list of strings to search
-            mbox = mailbox.PortableUnixMailbox(fp, _msgfactory)
-            while msg is not None:
-                msg = mbox.next()
-                if msg:
-                    sl = self.msgharvest(msg, sl)
+        fp = open(fn, 'rb')
+        try:
+            s = fp.read()
+            if '\0' in s:
+                return ''
+            elif self.ui.text:
+                return s
+            msg = _msgfactory(fp)
+            if not msg:
+                return s
+            # else it's a message or a mailbox
+            if not msg['message-id']:
+                hint = ('make sure input is a raw message,'
+                        ' in mutt: unset pipe_decode')
+                raise util.DeadMan('no message-id found', hint=hint)
+            if not msg.get_unixfrom():
+                sl = self.msgharvest(msg)
+            else: # treat s like a mailbox because it might be one
+                sl = [] # list of strings to search
+                mbox = mailbox.PortableUnixMailbox(fp, _msgfactory)
+                while msg is not None:
+                    msg = mbox.next()
+                    if msg:
+                        sl = self.msgharvest(msg, sl)
+        finally:
+            fp.close()
         return '\n'.join(sl)
 
     def urlcollect(self):
         '''Harvests urls from stdin or files.'''
-        textlist = []
-        if not self.files: # read from stdin
-            fp = sys.stdin
-            tempname = ''
-            try: # not every stdin file object is seekable
-                fp.seek(0)
-            except IOError:
-                tempname = tempfile.mkstemp(prefix='urlcollector')[1]
-                fp = open(tempname, 'wb')
-                try:
-                    fp.write(sys.stdin.read())
-                finally:
-                    fp.close()
-                fp = open(tempname, 'rb')
-            text = self.filedeconstructor(fp)
-            if tempname:
+        if not self.files: # stdin
+            tempname = tempfile.mkstemp(prefix='urlcollector.')[1]
+            fp = open(tempname, 'wb')
+            try:
+                fp.write(sys.stdin.read())
+            finally:
                 fp.close()
-                os.unlink(tempname)
-            textlist.append(text)
+            text = self.filedeconstructor(tempname)
+            os.unlink(tempname)
         else:
+            textlist = []
             for f in self.files:
-                f = util.absolutepath(f)
-                fp = open(f, 'rb')
-                try:
-                    text = self.filedeconstructor(fp)
-                finally:
-                    fp.close()
-                textlist.append(text)
-        text = '\n'.join(textlist)
+                textlist.append(self.filedeconstructor(util.absolutepath(f)))
+            text = '\n'.join(textlist)
         if text:
             self.findurls(text)
         if self.ui.pat and self.items:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.