def makeSharedDir(path, writable=True): """creates a directory with group ownership [general]group. There's much that can to wrong; we try to raise useful error messages. """ if not os.path.isdir(path): try: os.makedirs(path) except os.error, err: raise utils.ReportableError( "Could not create directory %s" % path, hint="The operating system reported: %s" % err) except Exception, msg: raise utils.ReportableError("Could not create directory %s (%s)" % (path, msg))
def query(self, query, data={}, timeout=None): """runs a single query in a new cursor and returns that cursor. You will see all exceptions, no transaction management is done here. query will try to re-establish broken connections. """ if self.connection is None: raise utils.ReportableError( "SimpleQuerier connection is None.", hint="This ususally is because an AdhocQuerier's query method" " was used outside of a with block.") cursor = self.connection.cursor() try: if timeout is not None: oldTimeout = self.getTimeout() self.setTimeout(timeout) try: cursor.execute(query, data) finally: if timeout is not None: self.setTimeout(oldTimeout) except DBError, ex: if isinstance(ex, OperationalError) and self.connection.fileno() == -1: if not self._reconnecting: return self._queryReconnecting(query, data) raise
def getDBConnection(profile, debug=debug, autocommitted=False): """returns a slightly instrumented connection through profile. For the standard table connection, there's a pool of those below. """ if profile is None: profile = "trustedquery" if isinstance(profile, basestring): profile = config.getDBProfile(profile) if debug: conn = psycopg2.connect(connection_factory=DebugConnection, **profile.getArgs()) print "NEW CONN using %s (%s)" % (profile.name, conn.getPID()), id(conn) def closer(): print "CONNECTION CLOSE", id(conn) return DebugConnection.close(conn) conn.close = closer else: try: conn = psycopg2.connect(connection_factory=GAVOConnection, **profile.getArgs()) except OperationalError, msg: raise utils.ReportableError( "Cannot connect to the database server." " The database library reported:\n\n%s" % msg, hint= "This usually means you must adapt either the access profiles" " in $GAVO_DIR/etc or your database config (in particular," " pg_hba.conf).")
def stanFactory(tag, **kwargs): """returns a factory for ModelBasedBuilder built from a stan-like "tag". Do *not* pass in instanciated tags -- they will just keep accumulating children on every model run. """ if isinstance(tag, stanxml.Element): raise utils.ReportableError( "Do not use instanciated stanxml element" " in stanFactories. Instead, return them from a zero-argument" " function.") def factory(args, localattrs=None): if localattrs: localattrs.update(kwargs) attrs = localattrs else: attrs = kwargs if isinstance(tag, type): el = tag else: # assume it's a function if it's not an element type. el = tag() return el(**attrs)[args] return factory
def getGroupId(): gavoGroup = config.get("group") try: return grp.getgrnam(gavoGroup)[2] except KeyError, ex: raise utils.ReportableError( "Group %s does not exist" % str(ex), hint="You should have created this (unix) group when you" " created the server user (usually, 'gavo'). Just do it" " now and re-run this program.")
def getFieldInfo(self, colName, tableName): """returns the (colName, fieldInfo) for colName within tableName in the current context. """ res = self.colResolvers[-1](colName, tableName) if res is None: raise utils.ReportableError( "Internal Error: resolver returned NULL for" " %s.%s. Please report this to the [email protected]" " together with the failed query." % (tableName, colName)) return res
def _build(self, constructors, metaContainer, macroPackage): result = [] for item in constructors: if isinstance(item, basestring): result.append(item) else: try: result.extend( self._getItemsForConstructor(metaContainer, macroPackage, *item)) except utils.Error: raise except: raise utils.logOldExc( utils.ReportableError( "Invalid constructor func in %s, meta container active %s" % (repr(item), repr(metaContainer)))) return result
(path, msg)) gavoGroup = getGroupId() stats = os.stat(path) if stats.st_mode & 0060 != 060 or stats.st_gid != gavoGroup: try: os.chown(path, -1, gavoGroup) if writable: os.chmod(path, stats.st_mode | 0060) except Exception, msg: raise utils.ReportableError( "Cannot set %s to group ownership %s, group writable" % (path, gavoGroup), hint="Certain directories must be writable by multiple user ids." " They must therefore belong to the group %s and be group" " writeable. The attempt to make sure that's so just failed" " with the error message %s." " Either grant the directory in question to yourself, or" " fix permissions manually. If you own the directory and" " sill see permission errors, try 'newgrp %s'" % (config.get("group"), msg, config.get("group"))) @utils.document def makeSitePath(path): """returns a rooted local part for a server-internal URL. uri itself needs to be server-absolute; a leading slash is recommended for clarity but not mandatory. """ return str(config.get("web", "nevowRoot") + path.lstrip("/"))