"""System Configuration - For holding global system variables""" # Copyright 2005, 2006 EIAO Consoritum # This program is distributed under the terms of the GNU General # Public License. # # This file is part of the European Internet Accessibility Observatory # (EIAO) # # EIAO is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # EIAO is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with EIAO; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, # MA 02110-1301 USA __author__ = "Morten Goodwin Olsen" __version__ = "0.2" __maintainer__ = "Nils Ulltveit-Moe" import md5 import sys import os import RDF from scerror import * from RDFreaderwriter import * class SystemConfiguration: """System specification class """ def __init__(self, deletemodel=False, rdfmodel=None, dboptions = (None, None, None)): """Create connection to initial observatory If no specification exsists, create new Keyword arguments: deletemodel -- [Optional] Forcing recreating of system configuration repository. Do nothing as default rdfmodel -- [Optional] rdfmodel to connect to. EIAO_SC as default. dboptions -- [Optional] (username, password, database). Retrieves from dbsetup-file if left empty. """ self.rw = None if dboptions == (None, None, None) or rdfmodel == None or deletemodel: model = RDF.Model() foundinitialfile = False for file in ['/etc/eiao/initial.rdf','initial.rdf', os.path.abspath(sys.prefix + '/initial.rdf')]: if not foundinitialfile: try: f = open(file,'r') foundinitialfile = True except IOError: pass if not foundinitialfile: raise InitialRDFNotFoundError(['initial.rdf',os.path.abspath(sys.prefix + '/initial.rdf')]) self.initialrdf = ''.join(f.readlines()) f.close() RDF.RDFXMLParser().parse_string_into_model(model, self.initialrdf, base_uri="http://www.eiao.net/rdf/systemconfiguration") if dboptions == (None, None, None): self.dbusername = [str(stat.object) for stat in model.find_statements(RDF.Statement(subject = RDF.Uri('http://www.eiao.net/rdf/systemconfiguration') , predicate = RDF.Uri('http://www.eiao.net/rdf/dbusername'), object = None))][0] self.dbpassword = [str(stat.object) for stat in model.find_statements(RDF.Statement(subject = RDF.Uri('http://www.eiao.net/rdf/systemconfiguration') , predicate = RDF.Uri('http://www.eiao.net/rdf/dbpassword'), object = None))][0] self.dbdatabase = [str(stat.object) for stat in model.find_statements(RDF.Statement(subject = RDF.Uri('http://www.eiao.net/rdf/systemconfiguration') , predicate = RDF.Uri('http://www.eiao.net/rdf/dbdatabase'), object = None))][0] else: self.dbusername = dboptions[0] self.dbpassword = dboptions[1] self.dbdatabase = dboptions[2] if not rdfmodel: self.rw = None self.rdfmodel = [str(stat.object) for stat in model.find_statements(RDF.Statement(subject = RDF.Uri('http://www.eiao.net/rdf/systemconfiguration') , predicate = RDF.Uri('http://www.eiao.net/rdf/systemconfigrdfmodel'), object = None))][0] else: self.rdfmodel = rdfmodel if not self.rdfmodel: raise NoRDFModelPresentError() rdftriples = [dict((('subject',str(stat.subject).lstrip('[').rstrip(']')),('predicate',str(stat.predicate).lstrip('[').rstrip(']')),('object',str(stat.object)))) for stat in model.find_statements(RDF.Statement(subject = RDF.Uri('http://www.eiao.net/rdf/systemconfiguration') , predicate = None , object = None))] self.syncSystemConfiguration(rdftriples) def stop(self): if self.rw and self.rw.DBServer: self.rw.DBServer.stop() self.rw.DBServer.join() def syncSystemConfiguration(self,rdftriples): """Extracting system configuration information from RDF and putting values to the SystemConfiguration instance Returns None """ self.sc = {} if not rdftriples: rdftriples = self.rw.readRDF('http://www.eiao.net/rdf/systemconfiguration', None, None) for triple in rdftriples: self.sc[self.removeStartUrl(triple['predicate'],'http://www.eiao.net/rdf/')] = triple['object'] for key,value in self.sc.items(): exec('self.'+key.strip() + ' = "' + value.strip() + '"') def getProductInformation(self): """Returns available information about available products Returns a list of all products and product-information """ rdftriples = self.rw.readRDF('http://www.eiao.net/rdf/systemconfiguration','http://www.eiao.net/rdf/product', None) allproducts = [] for product in rdftriples: producturl = product['object'] producttriples = self.rw.readRDF(producturl, None, None) productdict = {} for triple in producttriples: productdict[self.removeStartUrl(triple['predicate'],'http://www.eiao.net/rdf/')] = triple['object'] allproducts.append(productdict) return allproducts def removeStartUrl(self, URL, startURL): """ Removes the start of a URL. Keywords arguments: URL -- URL to be stripped startURL -- Start of the URL to be removed Returns striped variable name as string Examples: >>> sc = SystemConfiguration() >>> sc.removeStartUrl('http://www.eiao.net/rdf/configdirectory', 'http://www.eiao.net/rdf/') 'configdirectory' >>> sc.removeStartUrl('http://www.eiao.net/rdf/dbusername', 'http://www.eiao.net/rdf/') 'dbusername' """ if startURL not in URL: raise InvalidURLError(URL) if 'http://' not in URL: raise InvalidURLError(URL) if 'http://' not in startURL: raise InvalidURLError(startURL) lentocut = 0 if URL.startswith(startURL): lentocut = len(startURL) return URL[lentocut:] def createInitialRepository(self): """Creates the initial system configuration repository. This should only happen if the repository is empty (e.g. with a new installation) Returns None """ self.rw.writeRDF(self.initialrdf,addnamespace=False) self.syncSystemConfiguration() def removeProduct(self, productname = None, version = None, hashvalue = None): """Removes a product from the system configuration. You must supply either hashvalue or productname and version. Keyword arguments: productname -- [Optional] Name of the product version -- [Optional] Version of the current product hashvalue -- [Optional] Hash-value of the product Returns None """ if productname and version: producturl = 'http://www.eiao.net/rdf/main#' + md5.new(productname + ' ' + str(version)).hexdigest() elif hashvalue: producturl = 'http://www.eiao.net/rdf/main#' + hashvalue else: raise(NotEnoughInformationError(['version', 'productname', 'hashvalue'])) return self.dropVariable('http://www.eiao.net/rdf/systemconfiguration', 'http://www.eiao.net/rdf/product', producturl) self.dropVariable(producturl, None, None) self.syncSystemConfiguration() def addProduct(self, productname, version, downloadURL, localsvn, location, startprocedure, hashvalue = ''): """Add a product from the system configuration Keyword arguments: productname -- Name of the product version -- Version of the product downloadURL -- URL of location of the product, such as homepage, cvs/svn location location -- Position of the product on local disk localsvn -- Location of the product in the local svn startprocedure -- How to start the application hashvalue -- [Optional] Hash-value identifying a unique version and product. If not provided name and version will be hashed Returns None """ if not hashvalue: hashvalue = md5.new(productname + ' ' + str(version)).hexdigest() producturl = 'http://www.eiao.net/rdf/main#' + hashvalue self.rw.writeRDFTriple('http://www.eiao.net/rdf/systemconfiguration', 'http://www.eiao.net/rdf/product', producturl) self.rw.writeRDFTriple(producturl, 'http://www.eiao.net/rdf/productname', productname, objectliteral = True) self.rw.writeRDFTriple(producturl, 'http://www.eiao.net/rdf/version', str(version), objectliteral = True) self.rw.writeRDFTriple(producturl, 'http://www.eiao.net/rdf/downloadURL', downloadURL) self.rw.writeRDFTriple(producturl, 'http://www.eiao.net/rdf/localsvn', localsvn, objectliteral = True) self.rw.writeRDFTriple(producturl, 'http://www.eiao.net/rdf/location', location, objectliteral = True) self.rw.writeRDFTriple(producturl, 'http://www.eiao.net/rdf/hashvalue', hashvalue, objectliteral = True) self.rw.writeRDFTriple(producturl, 'http://www.eiao.net/rdf/startprocedure', startprocedure, objectliteral = True) self.syncSystemConfiguration() def dropVariable(self, subject, predicate, object): """Deletes a variale Keyword arguments: subject -- Subject to be deleted, None if all predicate -- Predicte to be deleted, None if all object -- Object to be deleted, None if all Returns None """ self.rw.dt.deletetriple(subject, predicate, object, self.rdfmodel) if predicate: exec ('self.'+self.removeStartUrl(predicate,'http://www.eiao.net/rdf/') + ' = None') if __name__ == "__main__": sc = SystemConfiguration(deletemodel=True, rdfmodel='EIAO_SC')