configmanager

 抓取文件

import shutil, tempfile, subprocess

def _fetch_file(host, user, filename):
    """Function to fetch a file from the server and copy it to the
    local machine. A temporary file name is created for
    从远程服务器或者本地本地拷贝文件到临时文件中
     """
    
    handle, tmpfile = tempfile.mkstemp(text=True)
    os.close(handle)

    if host != "localhost":
        source = user + "@" + host + ":" + filename
        #scp -qB  批处理模式,不输入密码,所以需要无密码能够登陆。
        subprocess.check_call(["scp", "-qB", source, tmpfile])
    else:
        #本机直接拷贝
        shutil.copyfile(filename, tmpfile)
    return tmpfile

  

 替换文件

def _replace_file(host, user, filename, source):
    if host != "localhost":
        target = user + "@" + host + ":" + filename
        subprocess.check_call(["scp", "-qB", source, target])
    else:
        shutil.copyfile(source, filename)

  

 清理ConfigParser不能处理的值

        def _clean_config_file(self, fname):
            infile = file(fname, 'r')
            lines = infile.readlines()
            infile.close()

            output = file(fname, 'w')
            for line in lines:
                if re.match("#.*|[w+]|[wd_-]+s*=s*.*", line):
                    pass
                elif re.match("s*[wd_-]+s*", line):
                    line = "%s = %s
" % (line.rstrip("
"), _NONE_MARKER)
                else:
                    line = "#!#" + line
                output.write(line)
            output.close()

  

恢复清理后的文件

        def _unclean_config_file(self, filename):
            infile = file(filename, 'r')
            lines = infile.readlines()
            infile.close()

            output = file(filename, 'w')
            for line in lines:
                mobj = re.match("([wd_-]+)s*=s*(.*)", line)
                if mobj and mobj.group(2) == _NONE_MARKER:
                    output.write(mobj.group(1) + "
")
                    continue
                if re.match("#!#.*", line):
                    output.write(line[3:])
                    continue
                output.write(line)
            output.close()

  

读取配置文件

        def read(self, path):
            """Read configuration from a file."""
            # We use ConfigParser, but since it cannot read
            # configuration files we options without values, we have
            # to clean the output once it is fetched before calling
            # ConfigParser

            handle, tmpfile = tempfile.mkstemp(text=True)
            os.close(handle)
            shutil.copy(path, tmpfile)
            self._clean_config_file(tmpfile)

            self.__config = ConfigParser.SafeConfigParser()
            self.__config.read(tmpfile)

  

写配置文件

        def write(self, path):
            """Write the configuration to a file."""
            output = open(path, 'w')
            self.__config.write(output)
            # Since ConfigParser cannot handle options without values
            # (yet), we have to unclean the file before replacing it.
            output.close()
            self._unclean_config_file(path)

  

原文地址:https://www.cnblogs.com/huanhuanang/p/4942995.html