示例#1
0
 def run(self, args):
     super(PopulateCommand, self).run(args)
     out("LOADING ENVIRONMENT")
     self.load_app()
     out("BUILDING SCHEMA")
     try:
         out("STARTING A TRANSACTION...")
         models.start()
         models.Base.metadata.create_all(conf.sqlalchemy.engine)
     except:
         models.rollback()
         out("ROLLING BACK... ")
         raise
     else:
         out("COMMITING... ")
         models.commit()
示例#2
0
def connection(app, request):
    """Session-wide test database."""
    # Connect and create the temporary database
    print "=" * 80
    print "CREATING TEMPORARY DATABASE FOR TESTS"
    print "=" * 80
    subprocess.call(['dropdb', DBNAME])
    subprocess.call(['createdb', DBNAME])

    # Bind and create the database tables
    _db.clear()
    engine_url = '%s/%s' % (BIND, DBNAME)

    db_engine = create_engine(
        engine_url,
        encoding='utf-8',
        poolclass=NullPool)

    # AKA models.start()
    _db.Session.bind = db_engine
    _db.metadata.bind = _db.Session.bind

    _db.Base.metadata.create_all(db_engine)
    _db.commit()
    _db.clear()

    #connection = db_engine.connect()

    def teardown():
        _db.Base.metadata.drop_all(db_engine)

    request.addfinalizer(teardown)

    # Slap our test app on it
    _db.app = app
    return _db
示例#3
0
文件: generate.py 项目: dmick/ayni
    def run(self, args):
        super(GenerateMapCommand, self).run(args)
        out("LOADING ENVIRONMENT")
        self.load_app()
        models.init_model()
        try:
            out("STARTING A TRANSACTION...")
            models.start()

            template = map_template.format(timestamp=timestamp())
            for project in conf.projects:
                p_query = models.Project.query.filter_by(name=project['name'])
                p = p_query.first()
                if p:  # lets update
                    p_kw = dict((k, v) for k, v in project.items() if k != 'docs')
                    p_query.update(p_kw)
                else:
                    p = models.Project(name=project['name'], fqdn=project['fqdn'])
                template = template + '\n# redirects for %s\n' % p.name
                redirects = []
                for doc in project.get('docs', []):
                    d = p.get_doc(doc['name'])
                    if d:
                        for k, v in doc.items():
                            if k == 'redirect':
                                pass
                            setattr(d, k, v)
                    if not d:
                        d = models.Doc(p, **doc)

                    if doc.get('redirect'):
                        line = "{prefix} {redirect};\n".format(
                            prefix=d.prefix_regex or d.url_prefix, redirect=d.redirect_to
                        )
                        redirects.append((d.weight, line))

                lines = ''.join([v for k, v in sorted(redirects, reverse=True)])
                template += lines

                # Create the JS
                here = os.path.abspath(os.path.dirname(__file__))
                top_path = os.path.abspath(os.path.dirname(os.path.dirname(here)))
                public_path = os.path.join(top_path, 'public')
                js_path = os.path.join(public_path, 'js')
                project_js = os.path.join(js_path, "%s.js" % project['name'])
                with open(project_js, 'w') as js_file:
                    project_url_part = "/projects/%s/" % project['name']
                    project_url = "%s%s" % (conf.ayni_fqdn.strip('/'), project_url_part)
                    t = Template(templates.js)
                    contents = t.substitute({
                        'ayni_css_file': conf.ayni_css_file,
                        'project_url': project_url,
                        })
                    js_file.write(contents)

            models.commit()

            with open(conf.get('map_path', 'ayni.map'), 'w') as f:
                f.write(template)
                f.write(extra_rules(conf.get('extra_redirect_rules', '')))

        except:
            models.rollback()
            out("ROLLING BACK... ")
            raise
        else:
            out("COMMITING... ")
            models.commit()