#!/usr/bin/python # Copyright (c) 2005 by Mark T. Holder, Florida State University. (see end of file) """Parses the cipres_config.properties file""" import os import sys import getpass import re import time import subprocess from socket import gethostname from PIPRes.util.io import expandPath, getCipresUserDir, getCipresFile from PIPRes.util.cipres import cipresGetLogger _LOG = cipresGetLogger('pipres.util._cipres_properties') def isWindows(): return sys.platform == 'win32' class CipresConfigurationError(AssertionError): def __init__(self, message, file = None): self.file = file if file: message = 'In file %s: %s' % (file, message) AssertionError.__init__(self, message) def getIORFromFilePath(regIORFilePath, svc): """Reads the filepath expecting just an IOR (used for getting the properties and registry services svc is a string (e.g. "registry" or "properties service") to display in error messages""" if not os.path.exists(regIORFilePath): msg = regIORFilePath + ' does not exist' _LOG.debug("Connecting to " + svc + " service: " + msg) raise CipresConfigurationError(msg) regIORFileObj = open(regIORFilePath, 'rU') try: regIORContents = regIORFileObj.readlines() regIOR = regIORContents[0].strip() if not regIOR: msg = regIORFilePath + ' is empty.' _LOG.debug("Connecting to " + svc + " service: " + msg) raise CipresConfigurationError(msg) return regIOR finally: regIORFileObj.close() def getCipresRootDirNoProperties(): """Checks for $CIPRES_ROOT (used when the trying to find the Properties or Registry service so we cannot use the Properties service to get this info.""" cipresRoot = os.environ.get('CIPRES_ROOT') if not cipresRoot: m = 'CIPRES_ROOT environmental variable must be set' raise CipresConfigurationError(m) if not os.path.exists(cipresRoot): raise CipresConfigurationError(cipresRoot + ' does not exist') return cipresRoot def getCipresResouresDirNoProperties(): """Checks for $CIPRES_ROOT/share/cipres/resources (used when the trying to find the Properties service so we cannot use the Properties service to get this info.""" cipresRoot = getCipresRootDirNoProperties() cipresResourcesDir = os.path.join(cipresRoot, 'share', 'cipres', 'resources') if not os.path.exists(cipresResourcesDir): raise CipresConfigurationError(cipresResourcesDir + ' does not exist') return cipresResourcesDir def getCipresPortsDirNoProperties(): pd = os.path.join(getCipresResouresDirNoProperties(), 'ports') if not os.path.exists(pd): raise CipresConfigurationError(pd + ' does not exist') return pd def launch_registry(): cip_root = getCipresRootDirNoProperties() suffix = isWindows() and ".bat" or ".sh" reg_launcher = "user_registry" + suffix path_to_launcher = os.path.join(cip_root, "bin", reg_launcher) _LOG.debug("Launching registry using %s" % path_to_launcher) if not os.path.exists(path_to_launcher): m = path_to_launcher + ' does not exist' _LOG.debug(m) raise CipresConfigurationError(m) return subprocess.Popen(path_to_launcher, shell=True) def getCipresRegistryIOR(launch_reg=True): cipresPortsDir = getCipresPortsDirNoProperties() username = getpass.getuser() host_frag = "_" + gethostname() mids = ["", host_frag] for middle in mids: try: regIORFile = "%s%s_registry.txt" % (username, middle) regIORFilePath = os.path.join(cipresPortsDir, regIORFile) return getIORFromFilePath(regIORFilePath, "registry") except CipresConfigurationError: pass if launch_reg: reg_proc = launch_registry() for i in range(50): try: return getCipresRegistryIOR(launch_reg=False) except: if reg_proc.poll() is not None: m = "Registry could not be launched" raise CipresConfigurationError(m) time.sleep(.5) raise CipresConfigurationError("No registry ports file found does not exist") def getCipresPropertiesIOR(): cipresPortsDir = getCipresPortsDirNoProperties() username = getpass.getuser() propIORFile = username + "_propertyservice.txt" propIORFilePath = os.path.join(cipresPortsDir, propIORFile) return getIORFromFilePath(propIORFilePath, "property service") def launchPropertiesService(): import subprocess cipresRoot = os.environ.get('CIPRES_ROOT') if not cipresRoot: raise CipresConfigurationError('CIPRES_ROOT environmental variable must be set') script = isWindows() and 'user_propsvc.bat' or 'user_propsvc.sh' scriptPath = os.path.join(cipresRoot, 'bin', script) if not os.path.exists(scriptPath): raise CipresConfigurationError(scriptPath + ' does not exist') # temp we should record the pid and redirect output to a file if isWindows(): subprocess.Popen([scriptPath]) # spawns process that is never killed else: subprocess.Popen(['/bin/sh', scriptPath]) # spawns process that is never killed return def testPropertiesService(ior): orb = getCipresORB() sleepSeconds = 0.5 nRetries = 10 for i in range(nRetries): try: propServiceObjRef = orb.string_to_object(ior) isSet, value = propServiceObjRef.getProperty('cipres.root') return propServiceObjRef except: launchPropertiesService() time.sleep(sleepSeconds) raise CipresConfigurationError('Could not contact (or launch) the CipresProperties service on port %d' % propertiesPort) def getCipresPropertiesObjectRef(): # code to find propService ior pIOR = getCipresPropertiesIOR() return testPropertiesService(pIOR) class CipresProperties: def __init__(self): self.propService = getCipresPropertiesObjectRef() def getPlatform(self): return sys.platform platform = property(getPlatform) class FakeProps(CipresProperties): def __init__(self, **kwargs): self.registryRetryCount = 12 self.registryRetryInterval = 0.5 self.applicationSendNexusAsString = False self.__dict__.update(kwargs) def set(self, k, v): self.__dict__[k] = v return v def get(self, k): v = self.__dict__.get(k) if v is None: return False, '' return True, v # This file is part of the PIPRes library # The PIPRes library is free software; you can redistribute it # and/or modify it under the terms of the GNU Lesser General # Public License as published by the Free Software Foundation; # either version 2.1 of the License, or (at your option) any later # version. # # This library 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 Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, # MA 02111-1307, USA