File: //usr/local/aegis/PythonLoaderTemp/third_party/apacheconfig/reader.py
import io
import os
import logging
from os import path
logger = logging.getLogger()
def split_all(file_path):
allparts = []
while True:
parts = path.split(file_path)
if parts[0] == file_path: # sentinel for absolute paths
allparts.insert(0, parts[0])
break
elif parts[1] == file_path: # sentinel for relative paths
allparts.insert(0, parts[1])
break
else:
file_path = parts[0]
allparts.insert(0, parts[1])
return allparts
def get_real_file_path(proc_root, filename):
if proc_root != '/':
filename = get_file_path_in_container(proc_root, filename)
return filename
def get_file_path_in_container(proc_root, file_path):
container_file_path = proc_root
if proc_root in file_path:
_, file_path = file_path.split(proc_root, 1)
parts = split_all(file_path.lstrip('/'))
for part in parts:
container_file_path = path.join(container_file_path, part)
link_count = 0
while True:
if not path.islink(container_file_path):
break
link_count += 1
if link_count > 10:
logger.warning('{} link may be cycle...'.format(container_file_path))
break
link_path = os.readlink(container_file_path)
if path.isabs(link_path):
container_file_path = path.join(proc_root, link_path.lstrip('/'))
else:
# used for /proc/29788/root/usr/bin/redis-server -> redis-check-rdb
container_file_path = path.join(path.dirname(container_file_path), link_path)
return path.normpath(container_file_path)
class LocalHostReader(object):
def __init__(self, procroot):
self._os = os
self._environ = self._os.environ
self._path = self._os.path
self._proc_root = procroot
@property
def environ(self):
return self._environ
def exists(self, filepath):
filepath = get_real_file_path(self._proc_root, filepath)
return self._path.exists(filepath)
def isdir(self, filepath):
return self._path.isdir(filepath)
def listdir(self, filepath):
return self._os.listdir(filepath)
def open(self, filename, mode='r', encoding='utf-8', bufsize=-1):
filename = get_real_file_path(self._proc_root, filename)
return io.open(filename, mode, bufsize, encoding=encoding)