# Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
#
# This file is part of Iro.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# #Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# -*- test-case-name: iro.tests.test_install -*-
from twisted.python import log
import logging
from sqlalchemy import create_engine
from sqlalchemy.exc import DatabaseError
from sets import Set
import os
from .config import configParser, confFiles, main
from .error import NeededOption, ValidateException
from .offer.provider import providers, getProvider
from .model.schema import Base, Offer
from .model.utils import WithSession
from . import config
[docs]def checkConfig():
"""check configuration file syntax.
:return: boolean value.
"""
try:
l = configParser.read(confFiles)
if len(l) > 0:
return True
return False
except (NeededOption,ValidateException) as e:
log.msg("Error while processing config file: %s"%e,logLevel=logging.ERROR)
return False
[docs]def checkDatabase():
"""Checks, if all tables are created.
:return: boolean value
"""
engine = create_engine(config.main.dburl)
for t in Base.metadata.sorted_tables:
if not t.exists(engine):
return False
return True
[docs]def checkDatabaseConnection():
"""Checks, if database can be connected.
:return: boolean value
"""
try:
engine = create_engine(config.main.dburl)
con = engine.connect()
con.close()
return True
except DatabaseError as e:
log.msg("Error while trying to connect to database\n%s"%e,logLevel=logging.ERROR)
return False
[docs]def createDatabase():
"""Create all database tables or only missing."""
engine = create_engine(config.main.dburl)
Base.metadata.create_all(engine)
[docs]def createSampleConfig():
"""create a sample configuration file 'iro.conf' with all possible provider sections."""
if not os.path.exists("iro.conf"):
with open("iro.conf",'w') as fp:
fp.write("\n".join(main.sampleConf()))
fp.write("\n")
k = providers.keys()
k.sort()
for p in k:
fp.write("\n".join(providers[p](p).sampleConf()))
fp.write("\n")
else:
log.msg("iro.conf exists and will not be overwritten.")
[docs]def getAllRoutes(providers,write=False):
"""Checks and update offer list.
:param boolean write: check or update list
:return dict:
- **"orphand"** (Set) -- a set of orphand offers
- **"added"** (Set) -- a set of new offers. The new name have a schema provider_typ_route
"""
engine = create_engine(config.main.dburl)
ret={"orphand":Set(),"added":Set()}
with WithSession(engine,write) as session:
ret["orphand"]=Set([i[0] for i in session.query(Offer.name).all()])
for provider in providers:
p=getProvider(provider,configParser.get(provider,"typ"),configParser.items(provider))
for t in p.typs:
for r in p.typs[t]:
try:
ret["orphand"].remove(Offer.get(session, provider, r, t).name)
except:
if write:
session.add(Offer(provider=provider,route=r,typ=t,name='%s_%s_%s'%(provider,t,r)))
ret["added"].add("%s_%s_%s"%(provider,t,r))
return ret