编写自己的python3安装脚本

发布时间:2019-09-27 07:07:17编辑:auto阅读(1806)

    python在Linux集群运维时需要统一python版本,简单的编写python安装脚本,以便统一安装,需要机器有安装包源环境(网络源/本地源)python源码包(下载到本地或在线),源码包国内推荐sohu python源包。
    此脚本无任何技术可言,用os.syetem 调用Linux命令,根据返回值判断是否成功执行 仅input_timeout_python 问题 在stackoverflow上寻求的答案。

    判断系统版本用platform (不同版本的依赖和安装方式不同)

    if "redhat" in os_platform:
        print color_green("正在安装依赖包...")
        res = os.system(
            "yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel epel-release git > /dev/null  2>&1")
    elif "Ubuntu" in os_platform:
        res = os.system(
            " apt-get -y install wget libkrb5-dev libsqlite3-dev gcc make automake libssl-dev zlib1g-dev libmysqlclient-dev libffi-dev git > /dev/null  2>&1")
    else:
        print color_red("此脚本暂不支持此系统,{}".format(os_platform))
        sys.exit(1)

    input_timeout_on_python 问题(这是一个自己给自己挖的坑,应该安装python的同仁都要安装ssl支持吧)
    编写自己的python3安装脚本
    自己修改代码如下(引用忽略):

    action = "yes" #默认值
    timeout = 5
    print color_green("请选择是否支持ssl,默认支持(yes/no)"),
    rlist, _, _ = select([sys.stdin], [], [], timeout)
    if rlist:
        action = sys.stdin.readline() #获取新的输入值(如果有)

    python3 支持ssl需要修改Modules/Setup文件,调用sed命令进行修改(命令不熟)

        os.system("sed -i '205s/^#//g' ./Python-3.6.2/Modules/Setup")
        os.system("sed -i '210s/^#//g' ./Python-3.6.2/Modules/Setup")
        os.system("sed -i '211s/^#//g' ./Python-3.6.2/Modules/Setup")
        os.system("sed -i '212s/^#//g' ./Python-3.6.2/Modules/Setup")

    用函数设置终端字体颜色

    
    def color_red(context):
        return '\033[1;31;40m{}\033[0m'.format(context)
    
    def color_green(context):
        return '\033[1;32;40m{}\033[0m'.format(context)

    参考:stackoverflow

关键字