1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
"""\ Tool for setting the index-url of pip
Reference: - https://pip.pypa.io/en/latest/ - https://docs.python.org/3/installing/index.html - https://docs.python.org/3/library/ensurepip.html """
assert __name__ == "__main__", "Can only be used as __main__ module"
__author__ = "ChenyangGao <https://chenyanggao.github.io/>" __version__ = (0, 1)
from argparse import ArgumentParser, RawTextHelpFormatter
parser = ArgumentParser( description="Python pip 源配置工具", formatter_class=RawTextHelpFormatter, ) subparsers = parser.add_subparsers(dest="command", help="Available commands")
parser_list = subparsers.add_parser( "list", formatter_class=RawTextHelpFormatter, help="罗列所有可用的源", description="""罗列所有可用的源,格式形如 {key}: index-url: {index_url!r} trusted-host: {trusted_host!r} comment: {comment!r}""")
parser_use = subparsers.add_parser( "use", formatter_class=RawTextHelpFormatter, help="使用 key 指定要设置的源", description="使用 key 指定要设置的源") parser_use.add_argument( "key", default="pypi", nargs="?", help="key,对应某个源,默认值 pypi") parser_use.add_argument( "-k", "--kind", default="user", choices=("global", "user", "site"), help="""类型,默认值 user - global Use the system-wide configuration file only - user Use the user configuration file only - site Use the current environment configuration file only""", ) parser_use.add_argument( "-i", "--isolated", action="store_true", help="Run pip in an isolated mode, ignoring environment " "variables and user configuration.") parser_use.add_argument( "-c", "--cmd-only", dest="cmd_only", action="store_true", help="并不设置配置文件,只是打印一个 pip install 命令" )
args = parser.parse_args()
from pip._internal.configuration import Configuration from urllib.parse import urlsplit
__import__("logging").basicConfig(level=20, format="%(message)s")
INDEXES = { "pypi": { "index-url": "https://pypi.python.org/pypi", "trusted-host": "pypi.python.org", "comment": "官方源" }, "douban": { "index-url": "http://pypi.douban.com/simple/", "trusted-host": "pypi.douban.com", "comment": "豆瓣" }, "aliyun": { "index-url": "http://mirrors.aliyun.com/pypi/simple/", "trusted-host": "mirrors.aliyun.com", "comment": "阿里云" }, "tsinghua": { "index-url": "https://pypi.tuna.tsinghua.edu.cn/simple/", "trusted-host": "pypi.tuna.tsinghua.edu.cn", "comment": "清华大学" }, "ustc": { "index-url": "https://pypi.mirrors.ustc.edu.cn/simple/", "trusted-host": "pypi.mirrors.ustc.edu.cn", "comment": "中国科学技术大学" }, "hustunique": { "index-url": "http://pypi.hustunique.com/simple/", "trusted-host": "pypi.hustunique.com", "comment": "华中理工大学" }, "sdutlinux": { "index-url": "http://pypi.sdutlinux.org/simple/", "trusted-host": "pypi.sdutlinux.org", "comment": "山东理工大学" }, "tencent": { "index-url": "http://mirrors.cloud.tencent.com/pypi/simple/", "trusted-host": "mirrors.cloud.tencent.com", "comment": "腾讯云" }, }
def pipi_list(): "罗列所有可用的源" for key, index in INDEXES.items(): print("%s: %s" % (key, "".join("\n %s: %r" % e for e in index.items())))
def pipi_use(key="pypi", kind="user", isolated=False): """使用 key 指定要设置的源
:param key: key,对应某个源,默认值 pypi :param kind: 类型,默认值 user - global Use the system-wide configuration file only - user Use the user configuration file only - site Use the current environment configuration file only :param isolated: Run pip in an isolated mode, ignoring environment variables and user configuration. """ index = INDEXES[key] index_url = index["index-url"] trusted_host = index.get("trusted-host") or urlsplit(index_url).netloc conf = Configuration(isolated, kind) conf.load() conf.set_value("global.index-url", index_url) conf.set_value("global.trusted-host", trusted_host) conf.save()
command = args.command if command == "list": pipi_list() elif command == "use": if args.cmd_only: from sys import executable
index = INDEXES[args.key] index_url = index["index-url"] trusted_host = index.get("trusted-host") or urlsplit(index_url).netloc print(f"'{executable}' -m pip install --index-url " f"{index_url} --trusted-host {trusted_host} ") else: pipi_use(args.key, args.kind, args.isolated) else: raise NotImplementedError(f"Command {command!r} is not implemented!")
|