zine-main / scripts / reset-instance

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    Reset the Instance
    ~~~~~~~~~~~~~~~~~~

    This script resets the development instance.

    :copyright: (c) 2010 by the Zine Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""
import sys
from os import mkdir, path, listdir, unlink
from shutil import rmtree
from optparse import OptionParser


sys.path.append(path.dirname(__file__))
from _init_zine import find_instance


def main():
    parser = OptionParser(usage='%prog [path]')
    parser.add_option('--username', '-u', dest='username', default='admin')
    parser.add_option('--password', '-p', dest='password', default='default')
    parser.add_option('--blog-url', dest='blog_url', default='http://localhost:4000/')
    parser.add_option('--database', dest='database_uri', default='sqlite:///zine.db')
    parser.add_option('--language', dest='lang', default='en')
    parser.add_option('--keep-plugins', default=False, action='store_true')

    options, args = parser.parse_args()
    if not args:
        instance = find_instance()
        if instance is None:
            parser.error('instance not found.  Specify path to instance')
    elif len(args) == 1:
        instance = args[0]
    else:
        parser.error('incorrect number of arguments')

    print 'Resetting instance', instance

    if not options.keep_plugins:
        rmtree(instance)
        mkdir(instance)
    else:
        for file in listdir(instance):
            fpath = path.join(instance, file)
            if path.isfile(fpath):
                unlink(fpath)
            elif path.isdir(fpath) and file != 'plugins':
                rmtree(fpath)

    import zine.application
    from zine import setup
    from zine.database import users, init_database, db
    from zine.utils.crypto import gen_pwhash, gen_secret_key
    from zine.config import Configuration
    e = db.create_engine(options.database_uri, instance)
    init_database(e)

    cfg = Configuration(path.join(instance, 'zine.ini'))
    t = cfg.edit()
    t.update(
        maintenance_mode=False,
        blog_url=options.blog_url,
        secret_key=gen_secret_key(),
        database_uri=options.database_uri,
        language=options.lang,
        plugins='vessel_theme',
        theme='vessel'
    )
    t.commit()

    from zine.models import User
    from zine.privileges import BLOG_ADMIN
    app = setup(instance)
    user = User(options.username, options.password, 'admin@localhost',
                is_author=True)
    user.own_privileges.add(BLOG_ADMIN)
    db.commit()

    print 'All done.'


if __name__ == '__main__':
    main()
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.