<s> """<STR_LIT>""" <EOL> import os <EOL> from ganeti import compat <EOL> from ganeti import _constants <EOL> ETC_HOSTS = _constants . V_CLUSTER_ETC_HOSTS <EOL> _VIRT_PATH_PREFIX = _constants . V_CLUSTER_VIRT_PATH_PREFIX <EOL> _ROOTDIR_ENVNAME = _constants . V_CLUSTER_ROOTDIR_ENVNAME <EOL> _HOSTNAME_ENVNAME = _constants . V_CLUSTER_HOSTNAME_ENVNAME <EOL> _VPATH_WHITELIST = _constants . V_CLUSTER_VPATH_WHITELIST <EOL> def _GetRootDirectory ( envname ) : <EOL> """<STR_LIT>""" <EOL> path = os . getenv ( envname ) <EOL> if path : <EOL> if not os . path . isabs ( path ) : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( envname , path ) ) <EOL> return os . path . normpath ( path ) <EOL> return "<STR_LIT>" <EOL> def _GetHostname ( envname ) : <EOL> """<STR_LIT>""" <EOL> return os . getenv ( envname , default = "<STR_LIT>" ) <EOL> def _CheckHostname ( hostname ) : <EOL> """<STR_LIT>""" <EOL> if os . path . basename ( hostname ) != hostname : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % hostname ) <EOL> def _PreparePaths ( rootdir , hostname ) : <EOL> """<STR_LIT>""" <EOL> if bool ( rootdir ) ^ bool ( hostname ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( _ROOTDIR_ENVNAME , _HOSTNAME_ENVNAME ) ) <EOL> if rootdir : <EOL> assert rootdir == os . path . normpath ( rootdir ) , "<STR_LIT>" + rootdir <EOL> _CheckHostname ( hostname ) <EOL> if os . path . basename ( rootdir ) != hostname : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( rootdir , hostname ) ) <EOL> return ( os . path . dirname ( rootdir ) , rootdir , hostname ) <EOL> else : <EOL> return ( "<STR_LIT>" , "<STR_LIT>" , None ) <EOL> ( _VIRT_BASEDIR , _VIRT_NODEROOT , _VIRT_HOSTNAME ) = _PreparePaths ( _GetRootDirectory ( _ROOTDIR_ENVNAME ) , <EOL> _GetHostname ( _HOSTNAME_ENVNAME ) ) <EOL> assert ( compat . all ( [ _VIRT_BASEDIR , _VIRT_NODEROOT , _VIRT_HOSTNAME ] ) or <EOL> not compat . any ( [ _VIRT_BASEDIR , _VIRT_NODEROOT , _VIRT_HOSTNAME ] ) ) <EOL> def GetVirtualHostname ( ) : <EOL> """<STR_LIT>""" <EOL> return _VIRT_HOSTNAME <EOL> def MakeNodeRoot ( base , node_name ) : <EOL> """<STR_LIT>""" <EOL> _CheckHostname ( node_name ) <EOL> return os . path . normpath ( "<STR_LIT>" % ( base , node_name ) ) <EOL> def ExchangeNodeRoot ( node_name , filename , <EOL> _basedir = _VIRT_BASEDIR , _noderoot = _VIRT_NODEROOT ) : <EOL> """<STR_LIT>""" <EOL> if _basedir : <EOL> pure = _RemoveNodePrefix ( filename , _noderoot = _noderoot ) <EOL> result = "<STR_LIT>" % ( MakeNodeRoot ( _basedir , node_name ) , pure ) <EOL> else : <EOL> result = filename <EOL> return os . path . normpath ( result ) <EOL> def EnvironmentForHost ( hostname , _basedir = _VIRT_BASEDIR ) : <EOL> """<STR_LIT>""" <EOL> if _basedir : <EOL> return { <EOL> _ROOTDIR_ENVNAME : MakeNodeRoot ( _basedir , hostname ) , <EOL> _HOSTNAME_ENVNAME : hostname , <EOL> } <EOL> else : <EOL> return { } <EOL> def AddNodePrefix ( path , _noderoot = _VIRT_NODEROOT ) : <EOL> """<STR_LIT>""" <EOL> assert os . path . isabs ( path ) , "<STR_LIT>" + path <EOL> if _noderoot : <EOL> result = "<STR_LIT>" % ( _noderoot , path ) <EOL> else : <EOL> result = path <EOL> assert os . path . isabs ( result ) , "<STR_LIT>" + path <EOL> return os . path . normpath ( result ) <EOL> def _RemoveNodePrefix ( path , _noderoot = _VIRT_NODEROOT ) : <EOL> """<STR_LIT>""" <EOL> assert os . path . isabs ( path ) , "<STR_LIT>" + path <EOL> norm_path = os . path . normpath ( path ) <EOL> if _noderoot : <EOL> norm_root = os . path . normpath ( _noderoot ) <EOL> root_with_sep = "<STR_LIT>" % ( norm_root , os . sep ) <EOL> prefix = os . path . commonprefix ( [ root_with_sep , norm_path ] ) <EOL> if prefix == root_with_sep : <EOL> result = norm_path [ len ( norm_root ) : ] <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( path , _noderoot ) ) <EOL> else : <EOL> result = norm_path <EOL> assert os . path . isabs ( result ) , "<STR_LIT>" + path <EOL> return result <EOL> def MakeVirtualPath ( path , _noderoot = _VIRT_NODEROOT ) : <EOL> """<STR_LIT>""" <EOL> assert os . path . isabs ( path ) , "<STR_LIT>" + path <EOL> if _noderoot and path not in _VPATH_WHITELIST : <EOL> return _VIRT_PATH_PREFIX + _RemoveNodePrefix ( path , _noderoot = _noderoot ) <EOL> else : <EOL> return path <EOL> def LocalizeVirtualPath ( path , _noderoot = _VIRT_NODEROOT ) : <EOL> """<STR_LIT>""" <EOL> assert os . path . isabs ( path ) , "<STR_LIT>" + path <EOL> if _noderoot and path not in _VPATH_WHITELIST : <EOL> if path . startswith ( _VIRT_PATH_PREFIX ) : <EOL> return AddNodePrefix ( path [ len ( _VIRT_PATH_PREFIX ) : ] , _noderoot = _noderoot ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % path ) <EOL> else : <EOL> return path </s>
