|
1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net> |
|
2 # |
|
3 # This file is part of Iro. |
|
4 # |
|
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6 # this software and associated documentation files (the "Software"), to deal in |
|
7 # the Software without restriction, including without limitation the rights to use, |
|
8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
9 # #Software, and to permit persons to whom the Software is furnished to do so, |
|
10 # subject to the following conditions: |
|
11 # |
|
12 # The above copyright notice and this permission notice shall be included in |
|
13 # all copies or substantial portions of the Software. |
|
14 # |
|
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|
17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
21 |
|
22 # -*- test-case-name: iro.tests.test_install -*- |
|
23 |
|
24 from twisted.python import log |
|
25 import logging |
|
26 from sqlalchemy import create_engine |
|
27 from sqlalchemy.exc import DatabaseError |
|
28 from sets import Set |
|
29 import os |
|
30 |
|
31 from .config import configParser, confFiles, main |
|
32 from .error import NeededOption, ValidateException |
|
33 from .offer.provider import providers, getProvider |
|
34 |
|
35 from .model.schema import Base, Offer |
|
36 from .model.utils import WithSession |
|
37 |
|
38 from . import config |
|
39 |
|
40 def checkConfig(): |
|
41 """check configuration file syntax. |
|
42 |
|
43 :return: boolean value. |
|
44 """ |
|
45 |
|
46 try: |
|
47 l = configParser.read(confFiles) |
|
48 if len(l) > 0: |
|
49 return True |
|
50 return False |
|
51 except (NeededOption,ValidateException) as e: |
|
52 log.msg("Error while processing config file: %s"%e,logLevel=logging.ERROR) |
|
53 return False |
|
54 |
|
55 def checkDatabase(): |
|
56 """Checks, if all tables are created. |
|
57 |
|
58 :return: boolean value |
|
59 """ |
|
60 engine = create_engine(config.main.dburl) |
|
61 for t in Base.metadata.sorted_tables: |
|
62 if not t.exists(engine): |
|
63 return False |
|
64 return True |
|
65 |
|
66 def checkDatabaseConnection(): |
|
67 """Checks, if database can be connected. |
|
68 |
|
69 :return: boolean value |
|
70 """ |
|
71 try: |
|
72 engine = create_engine(config.main.dburl) |
|
73 con = engine.connect() |
|
74 con.close() |
|
75 return True |
|
76 except DatabaseError as e: |
|
77 log.msg("Error while trying to connect to database\n%s"%e,logLevel=logging.ERROR) |
|
78 return False |
|
79 |
|
80 def createDatabase(): |
|
81 """Create all database tables or only missing.""" |
|
82 engine = create_engine(config.main.dburl) |
|
83 Base.metadata.create_all(engine) |
|
84 |
|
85 def createSampleConfig(): |
|
86 """create a sample configuration file 'iro.conf' with all possible provider sections.""" |
|
87 if not os.path.exists("iro.conf"): |
|
88 with open("iro.conf",'w') as fp: |
|
89 fp.write("\n".join(main.sampleConf())) |
|
90 fp.write("\n") |
|
91 k = providers.keys() |
|
92 k.sort() |
|
93 for p in k: |
|
94 fp.write("\n".join(providers[p](p).sampleConf())) |
|
95 fp.write("\n") |
|
96 else: |
|
97 log.msg("iro.conf exists and will not be overwritten.") |
|
98 |
|
99 def getAllRoutes(providers,write=False): |
|
100 """Checks and update offer list. |
|
101 |
|
102 :param boolean write: check or update list |
|
103 :return dict: |
|
104 - **"orphand"** (Set) -- a set of orphand offers |
|
105 - **"added"** (Set) -- a set of new offers. The new name have a schema provider_typ_route |
|
106 |
|
107 """ |
|
108 engine = create_engine(config.main.dburl) |
|
109 ret={"orphand":Set(),"added":Set()} |
|
110 with WithSession(engine,write) as session: |
|
111 ret["orphand"]=Set([i[0] for i in session.query(Offer.name).all()]) |
|
112 for provider in providers: |
|
113 p=getProvider(provider,configParser.get(provider,"typ"),configParser.items(provider)) |
|
114 for t in p.typs: |
|
115 for r in p.typs[t]: |
|
116 try: |
|
117 ret["orphand"].remove(Offer.get(session, provider, r, t).name) |
|
118 except: |
|
119 if write: |
|
120 session.add(Offer(provider=provider,route=r,typ=t,name='%s_%s_%s'%(provider,t,r))) |
|
121 ret["added"].add("%s_%s_%s"%(provider,t,r)) |
|
122 return ret |
|
123 |