def is_local_path(self, path): ''' local_path: '/srv/formulas/base/topd-formula/topd/init.sls' 'file:///srv/formulas/base/topd-formula/topd/init.sls' :param path: ''' try: path = self.get(path, 'abspath') except AttributeError: pass if salt.utils.url.validate(path, ['', 'file']): url = urlparse(path) return not self.is_cache_path(path) and os.path.isabs(url.path) return False
def is_relpath(self, path, saltenv=None): ''' path: 'topd/init.sls' relpath: 'topd/init.sls' :param path: :param saltenv: ''' try: url = urlparse(path) if not salt.utils.url.validate(path, ['']) or os.path.isabs( url.path): return False return not self.is_slspath(url.path, saltenv) except AttributeError: return False
def is_cache_path(self, path): ''' cache_path: '/var/cache/salt/minion/files/base/topd/init.sls' 'file:///var/cache/salt/minion/files/base/topd/init.sls' :param path: ''' try: return os.path.commonprefix( [self.get(path, 'abspath'), self.opts['cachedir']]) == self.opts['cachedir'] except AttributeError: if salt.utils.url.validate(path, ['', 'file']): url = urlparse(path) return os.path.commonprefix([url.path, self.opts['cachedir'] ]) == self.opts['cachedir'] return False
def is_cache_path(self, path): ''' cache_path: '/var/cache/salt/minion/files/base/topd/init.sls' 'file:///var/cache/salt/minion/files/base/topd/init.sls' :param path: ''' try: return os.path.commonprefix( [self.get(path, 'abspath'), self.opts['cachedir']] ) == self.opts['cachedir'] except AttributeError: if salt.utils.url.validate(path, ['', 'file']): url = urlparse(path) return os.path.commonprefix( [url.path, self.opts['cachedir']] ) == self.opts['cachedir'] return False
def is_relpath(self, path, saltenv=None): ''' path: 'topd/init.sls' relpath: 'topd/init.sls' :param path: :param saltenv: ''' try: url = urlparse(path) if not salt.utils.url.validate( path, [ '' ] ) or os.path.isabs(url.path): return False return not self.is_slspath(url.path, saltenv) except AttributeError: return False
def path(self, path, saltenv=None, path_type=None): ''' Relative salt path is the base path that all paths rely on. Any conversions from formats that are not relative will happen here. :param path: :param saltenv: :param path_type: ''' if isinstance(path, list): return [self.path(p, saltenv) for p in path] path = self._normpath(path) url = urlparse(path) path_type = path_type or self.path_type(path, saltenv) if path_type in ['relpath']: return path if path_type in ['slspath']: source = self.client.get_state(url.path, saltenv).get('source') return salt.utils.url.parse(source)[0] if path_type in ['cache_path']: return os.path.relpath(url.path, self.cache_dir(saltenv)) if path_type in ['local_path']: roots = self.file_root(url.path, saltenv) for root in roots: if not self.is_cache_path(root): return os.path.relpath(url.path, root) return '' if path_type in ['salt_path']: return url.netloc + url.path raise SaltRenderError('Could not find relpath for {0}'.format(path))
def _normpath(path): if not urlparse(path).scheme: return os.path.normpath(path) return path