Exemple #1
0
 def save(self, prefix=None, compact=True, mode='wb'):
     with self.lock:
         prefix = prefix or self.filename
         output = self._fmt_file(prefix)
         if compact:
             prefix, output = self._compact(prefix, output)
         try:
             outfile = self.SaveFile(self.session, prefix)
             self.session.ui.mark('Writing %d bytes to %s' %
                                  (len(output), outfile))
             if output:
                 if self.config.prefs.encrypt_index:
                     encryption_key = self.config.master_key
                     with EncryptingStreamer(encryption_key,
                                             delimited=True,
                                             name='PostingList') as efd:
                         efd.write(output)
                         efd.save(outfile, mode=mode)
                 else:
                     with open(outfile, mode) as fd:
                         fd.write(output)
                 return len(output)
             elif os.path.exists(outfile):
                 os.remove(outfile)
         except:
             self.session.ui.warning('%s=>%s' % (
                 outfile,
                 sys.exc_info(),
             ))
         return 0
Exemple #2
0
    def _open_log(self):
        if self._log_fd:
            self._log_fd.close()

        if not os.path.exists(self.logdir):
            os.mkdir(self.logdir)

        self._log_start_id = NewEventId()
        enc_key = self.encryption_key_func()
        if enc_key:
            self._log_fd = EncryptingStreamer(enc_key,
                                              dir=self.logdir,
                                              name='EventLog/ES',
                                              use_filter=False,
                                              long_running=True)
            self._log_fd.save(self._save_filename(), finish=False)
            self._log_write = self._log_fd.write_pad_and_flush
        else:
            self._log_fd = open(self._save_filename(), 'wb', 0)
            self._log_write = self._log_fd.write

        # Write any incomplete events to the new file
        for e in self.incomplete():
            self._log_write('%s\n')

        # We're starting over, incomplete events don't count
        self._logged = 0
Exemple #3
0
 def _encrypt_and_add_data(filename, data):
     tempfile = os.path.join(config.tempfile_dir(), filename)
     with EncryptingStreamer(config.get_master_key(),
                             dir=config.tempfile_dir()) as fd:
         fd.write(data)
         fd.save(tempfile)
     _add_file(tempfile, filename)
     safe_remove(tempfile)
Exemple #4
0
 def save_pickle(self, obj, pfn):
     if self.prefs.obfuscate_index:
         from mailpile.crypto.streamer import EncryptingStreamer
         fd = EncryptingStreamer(self.prefs.obfuscate_index,
                                 dir=self.workdir)
         cPickle.dump(obj, fd, protocol=0)
         fd.save(os.path.join(self.workdir, pfn))
     else:
         fd = open(os.path.join(self.workdir, pfn), 'wb')
         cPickle.dump(obj, fd, protocol=0)
         fd.close()
    def save(self, split=True):
        if not self.changes:
            return

        if split and len(self.words) > 1:
            with self.lock:
                for plc in self._splits():
                    plc.save(split=False)
            return

        t = [time.time()]
        encryption_key = self.config.master_key
        outfile = self._SaveFile(self.config, self.sig)
        with self.lock:
            # Optimizing for fast loads, so deletion only happens on save.
            del_set = self._deleted_set()
            output = '\n'.join(
                '\t'.join(l)
                for l in ([sig] + [str(v) for v in (values - del_set)]
                          for sig, values in self.words.iteritems())
                if len(l) > 1)
            t.append(time.time())

            if not output:
                try:
                    os.remove(outfile)
                except OSError:
                    pass
            elif self.config.prefs.encrypt_index and encryption_key:
                subj = self.config.mailpile_path(outfile)
                with EncryptingStreamer(encryption_key,
                                        delimited=False,
                                        dir=self.config.tempfile_dir(),
                                        header_data={'subject': subj},
                                        name='PLC/%s' % self.sig) as fd:
                    fd.write(output)
                    fd.save(outfile)
            else:
                with open(outfile, 'wb') as fd:
                    fd.write(output)

            t.append(time.time())
            self.changes = 0

        if len(t) == 3:
            TIMERS['render'] += t[1] - t[0]
            TIMERS['save'] += t[2] - t[1]
            TIMERS['save_count'] += 1
Exemple #6
0
 def save(self, filename=None, gpg_recipient=None, encryption_key=None):
     filename = filename or self.filename
     if filename:
         gpg_recipient = gpg_recipient or self.gpg_recipient()
         encryption_key = encryption_key or self.encryption_key()
         if encryption_key:
             from mailpile.crypto.streamer import EncryptingStreamer
             with EncryptingStreamer(encryption_key,
                                     dir=os.path.dirname(filename)) as es:
                 es.write(self.as_vCard())
                 es.save(filename)
         else:
             with gpg_open(filename, gpg_recipient, 'wb') as gpg:
                 gpg.write(self.as_vCard())
         return self
     else:
         raise ValueError('Save to what file?')
Exemple #7
0
 def save(self, filename=None):
     filename = filename or self.filename
     if filename:
         encryption_key = self.encryption_key_func()
         if encryption_key:
             from mailpile.crypto.streamer import EncryptingStreamer
             subj = self.config.mailpile_path(filename)
             with EncryptingStreamer(encryption_key,
                                     delimited=False,
                                     dir=self.config.tempfile_dir(),
                                     header_data={'subject': subj},
                                     name='VCard/save') as es:
                 es.write(self.as_vCard())
                 es.save(filename)
         else:
             with open(filename, 'wb') as fd:
                 fd.write(self.as_vCard())
         return self
     else:
         raise ValueError('Save to what file?')
Exemple #8
0
    def _open_log(self):
        if self._log_fd:
            self._log_fd.close()

        if not os.path.exists(self.logdir):
            os.mkdir(self.logdir)

        self._log_start_id = NewEventId()
        enc_key = self.encryption_key_func()
        if enc_key:
            self._log_fd = EncryptingStreamer(enc_key, dir=self.logdir)
            self._log_fd.save(self._save_filename(), finish=False)
        else:
            self._log_fd = open(self._save_filename(), 'w', 0)

        # Write any incomplete events to the new file
        for e in self.incomplete():
            self._log_fd.write('%s\n' % e)

        # We're starting over, incomplete events don't count
        self._logged = 0