[Python源码]自动修改pip源

在国内使用python自带的pip源实在太慢,而根据系统环境不断重复设置过程太过烦琐,因此创建这个自动化脚本,实现自动替换为国内pip源的操作。

最新版本请移步https://github.com/echosun1996/Python-pip-source-changer 获取。

#!/usr/bin/env python
# coding=utf-8
import platform
import os


def mkdir(path):
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        print(path + ' Create finish.')
        return True
    else:
        print(path + ' Directory already exists.')
        return False


def mkfile(filePath):
    pipfile = "[global]\ntrusted-host=mirrors.aliyun.com\nindex-url=http://mirrors.aliyun.com/pypi/simple/"
    if os.path.exists(filePath):
        if str(input("File exist!Cover?(Y/N))")).upper() == 'N':
            print("Not Cover.")
            return
    with open(filePath, 'w') as fp:
        fp.write(pipfile)
    print("Write finish.")


def platformSelect():
    systype = platform.system()
    print("System type: " + systype)
    if systype == "Windows":
        path = os.getenv('HOMEPATH') + "\\pip"
        mkdir(path)
        mkfile(path + '\\pip.ini')

    elif systype == "Linux" or systype == "Darwin":
        path =os.path.expandvars('$HOME')+"/.pip"
        mkdir(path)
        mkfile(path + '/pip.conf')
    else:
        print("System type: " + systype + " Not Support!")


if __name__ == "__main__":
    platformSelect()

你可能感兴趣的:(杂七杂八)