python脚本修改httpd.conf文件

本文介绍如何利用Python脚本读取并修改httpd.conf配置文件,涉及文件操作和文本处理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python脚本修改httpd.conf文件

# -*- coding: UTF-8 -*-
import sublime, sublime_plugin, os, shutil

class SaveEvent(sublime_plugin.EventListener):
    def on_post_save(self, view):
        curfile = view.file_name()       

        ehrParam = self.load_ehr_param()
        ehrDir = ehrParam["ehr_dir"];
        tomcatDir = ehrParam["tomcat_dir"]
        print ehrParam
        #jsp文件直接拷到tomcat相应目录下
        isEhrJsp = curfile.startswith(ehrDir + "\\hrms")
        if isEhrJsp:
            tomcatEhrJsp = curfile.upper().replace(ehrDir, tomcatDir + "\\webapps")
            shutil.copyfile(curfile, tomcatEhrJsp)
            return
        #java文件需要先编译成class,然后拷贝到tomcat相应目录下
        isEhrJava = curfile.startswith(ehrDir + "\\src")


        print os.path.splitext(curfile)

    def load_ehr_param(self):
        s = sublime.load_settings("eventtest.sublime-settings")
        return {"ehr_dir": s.get("ehr_dir"), "tomcat_dir": s.get("tomcat_dir")}

#!/usr/bin/python
 
from cStringIO import StringIO
import re
 
vhost_start = re.compile(r'<VirtualHost\s+(.*?)>')
vhost_end = re.compile(r'</VirtualHost>')
docroot_re = re.compile(r'(DocumentRoot\s+)(\S+)')
 
def replace_docroot(conf_string, vhost, new_docroot):
  '''yield new lines of an httpd.conf file where docroot lines matching
      the specified vhost are replaced with the new_docroot
  '''
  conf_file = StringIO(conf_string)
  in_vhost = False
  curr_vhost = None
  for line in conf_file:
    vhost_start_match = vhost_start.search(line)
    if vhost_start_match:
      curr_vhost = vhost_start_match.groups()[0]
      in_vhost = True
    if in_vhost and (curr_vhost == vhost):
      docroot_match = docroot_re.search(line)
      if docroot_match:
        sub_line = docroot_re.sub(r'\1%s' % new_docroot, line)
        line = sub_line
      vhost_end_match = vhost_end.search(line)
      if vhost_end_match:
        in_vhost = False
      yield line
         
if __name__ == '__main__':
  import sys
  conf_file = sys.argv[1]
  vhost = sys.argv[2]
  docroot = sys.argv[3]
  conf_string = open(conf_file).read()
  for line in replace_docroot(conf_string, vhost, docroot):
    print line,


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值