Esempio n. 1
0
def Main(argv):
    set_profile = set_home = False
    for arg in argv:
        if arg.startswith('--profile='):
            os.environ['MAILPILE_PROFILE'] = arg.split('=', 1)[-1]
            if 'MAILPILE_HOME' in os.environ:
                del os.environ['MAILPILE_HOME']
            set_profile = True
        elif arg.startswith('--home='):
            os.environ['MAILPILE_HOME'] = arg.split('=', 1)[-1]
            if 'MAILPILE_PROFILE' in os.environ:
                del os.environ['MAILPILE_PROFILE']
            set_home = True
    if set_home and set_profile:
        raise ValueError('Please only use one of --home and --profile')

    state = MailpileState().discover(argv)
    ActivateTranslation(None, state.pub_config, None)

    script = [GenerateConfig(state), GenerateBootstrap(state)]

    if '--script' in argv:
        print '\n'.join(script)

    else:
        # FIXME: We shouldn't need to do this, refactoring upstream
        #        to pull in less weird stuff would make sense.
        from mailpile.safe_popen import MakePopenUnsafe
        MakePopenUnsafe()

        from gui_o_matic.control import GUIPipeControl
        gpc = GUIPipeControl(StringIO('\n'.join(script) + '\n'))
        gpc.bootstrap(dry_run=('--compile' in argv))
Esempio n. 2
0
 def configure_language(self, session, config, language, save=True):
     try:
         i18n = lambda: ActivateTranslation(session, config, language)
         if not self._testing_yes(i18n):
             raise ValueError('Failed to configure i18n')
         config.prefs.language = language
         if save and not self._testing():
             self._background_save(config=True)
         return True
     except ValueError:
         return self._error(_('Invalid language: %s') % language)
Esempio n. 3
0
    def setup_command(self, session):
        config = session.config
        if self.data.get('_method') == 'POST' or self._testing():
            language = self.data.get('language', [''])[0]
            if language:
                try:
                    i18n = lambda: ActivateTranslation(session, config,
                                                       language)
                    if not self._testing_yes(i18n):
                        raise ValueError('Failed to configure i18n')
                    config.prefs.language = language
                    if not self._testing():
                        self._background_save(config=True)
                except ValueError:
                    return self._error(_('Invalid language: %s') % language)

            config.slow_worker.add_unique_task(
                session, 'Setup, Stage 1', lambda: self.bg_setup_stage_1())

        results = {
            'languages': ListTranslations(config),
            'language': config.prefs.language
        }
        return self._success(_('Welcome to Mailpile!'), results)
Esempio n. 4
0
    def command(self):
        global AVAILABLE_BACKUPS
        session, config = self.session, self.session.config
        message, results = '', {}

        if config.prefs.gpg_recipient or os.path.exists(config.conf_key):
            raise UrlRedirectException('/' + (config.sys.http_path or ''))

        if 'lang' in self.data:
            ActivateTranslation(session, config, self.data['lang'][0])

        password = ''
        if self.args and '_method' not in self.data:
            try:
                if self.args[0] in AVAILABLE_BACKUPS:
                    backup_data = AVAILABLE_BACKUPS[self.args[0]]
                    self.data['restore'] = [self.args[0]]
                    password = session.ui.get_password(_("Your password: "))
                else:
                    with open(self.args[0], 'r') as fd:
                        backup_data = fd.read()
            except (IOError, OSError):
                return self._error('Failed to read: %s' % self.args[0])
        elif self.data.get('_method') == 'POST':
            if 'restore' in self.data:
                backup_data = AVAILABLE_BACKUPS[self.data['restore'][0]]
                password = self.data.get('password', [''])[0]
            else:
                backup_data = self.data.get('file-data', [None])[0]
        else:
            backup_data = None

        if backup_data is not None:
            try:
                if isinstance(backup_data, str):
                    backup_data = cStringIO.StringIO(backup_data)
                backup_zip = zipfile.ZipFile(backup_data, 'r')

                # Load and validate metadata (from README.txt)
                results['metadata'] = metadata = json.loads(
                    backup_zip.read('README.txt').split('-- ')[1])
                results['metadata']['contents'] = backup_zip.namelist()
                backup_date = metadata['backup_date']
                if metadata['backup_version'] != 1.0:
                    raise ValueError('Unrecognized backup version')

                # If we get this far, the backup looks good. Restore?
                if (password
                        and backup_date == self.data.get('restore', [''])[0]):
                    # This should be safe: we are in the setup phase where
                    # almost no background stuff is running, so it should be
                    # fine to just overwrite files and reload.
                    config.stop_workers()
                    backup_zip.extractall(config.workdir)
                    VerifyAndStorePassphrase(config, password)

                    os_gpg_home = config.sys.gpg_home
                    os_gpg_binary = config.sys.gpg_binary
                    os_http_port = config.sys.http_port
                    os_minfree_mb = config.sys.minfree_mb
                    try:
                        config.load(session)
                    except IOError:
                        pass

                    B = ['backup']
                    if 'keep' == self.data.get('os_settings', B)[0]:
                        config.sys.gpg_home = os_gpg_home
                        config.sys.gpg_binary = os_gpg_binary
                        config.sys.http_port = os_http_port
                        config.sys.minfree_mb = os_minfree_mb

                    self._restore_PGP_keys(
                        config, backup_zip,
                        self.data.get('keychain', ['shared'])[0])

                    self._adjust_paths(config)

                    config.prepare_workers(session, daemons=True)
                    message = _('Backup restored')
                    results['restored'] = True
                    AVAILABLE_BACKUPS = {}
                else:
                    message = _('Backup validated, restoration is possible')
                    AVAILABLE_BACKUPS[backup_date] = backup_data

            except (ValueError, KeyError, zipfile.BadZipfile, IOError):
                traceback.print_exc()
                return self._error('Incomplete, invalid or corrupt backup')
        else:
            message = _('Restore from backup')

        results['available'] = AVAILABLE_BACKUPS.keys()
        return self._success(message, result=results)