Python替換多個tomcat端口號

批量替換tomcat端口號:
tomcat_port.py:
# -*- coding: utf-8 -*-
import os

from famiover.util.os_util import is_windows


def get_tomcats_config():
    """獲取tomcat端口修改配置"""
    configs = []
    tomcat_info_8081 = {
        "is_delete_comments": True,
        "path": "/Users/famiover/Downloads/apache-tomcat-8081",
        "p8005": "8006",
        "p8080": "8081",
        "p8009": "8010",
    }
    tomcat_info_8082 = {
        "is_delete_comments": False,
        "path": "/Users/famiover/Downloads/apache-tomcat-8082",
        "p8005": "8007",
        "p8080": "8082",
        "p8009": "8011",
    }
    configs.append(tomcat_info_8081)
    configs.append(tomcat_info_8082)
    return configs


def get_file_path(file_path):
    if not is_windows():
        if file_path.endswith("/"):
            file_path += "conf/server.xml"
        else:
            file_path += "/conf/server.xml"
    else:
        if file_path.endswith("\\"):
            file_path += "conf\\server.xml"
        else:
            file_path += "\\conf\\server.xml"

    return file_path


def delete_comments(config):
    """去掉註釋、空行"""
    file_path = get_file_path(config.get("path"))
    if not os.path.exists(file_path):
        print(file_path, "不存在!")
        exit(0)
    with open(file_path, 'r') as r:
        lines = r.readlines()
        r.close()
    is_enable_write = False
    with open(file_path, 'w') as w:
        for index, line in enumerate(lines):
            strip_line = line.strip()
            if len(strip_line) == 0:
                continue
            switch1 = strip_line[0:4] == "<!--" and strip_line.endswith("-->")
            if switch1:
                continue
            switch2 = strip_line[0:4] == "<!--" and not strip_line.endswith("-->")
            if switch2:
                is_enable_write = False
                continue
            else:
                if strip_line.endswith("-->"):
                    is_enable_write = True
                    continue
                else:
                    if is_enable_write or index == 0:
                        w.write(line)
                    else:
                        continue
    w.close()


def validate_port():
    """驗證是否有重複端口號"""
    configs = get_tomcats_config()
    config_ports = ["8005", "8080", "8009"]
    for config in configs:
        config_ports.append(config.get("p8005"))
        config_ports.append(config.get("p8080"))
        config_ports.append(config.get("p8009"))
    for config_port in config_ports:
        if config_ports.count(config_port) > 1:
            print("端口配置有重複的,請更正!")
            exit(0)


def replace_port(config):
    file_path = config.get("path")
    if file_path.endswith("/"):
        file_path += "conf/server.xml"
    else:
        file_path += "/conf/server.xml"
    if not os.path.exists(file_path):
        print(file_path, "不存在!")
        exit(0)
    with open(file_path, 'r') as r:
        lines = r.readlines()
        r.close()
    with open(file_path, 'w') as w:
        for line in lines:
            if "8005" in line:
                line = line.replace("8005", config.get("p8005"))
            if "8080" in line:
                line = line.replace("8080", config.get("p8080"))
            if "8009" in line:
                line = line.replace("8009", config.get("p8009"))
            w.write(line)
    w.close()


def replace():
    validate_port()
    configs = get_tomcats_config()
    for config in configs:
        if config.get("is_delete_comments",False):
            delete_comments(config)
        replace_port(config)


if __name__ == '__main__':
    replace()
相關文章
相關標籤/搜索