方便更新第三方库脚本。
直接在命令行运行 pipx.py
x表示对pip的功能扩展。
亮点:输入s进入sudo模式,一次输入密码多次使用。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Upgrade packages
just run `pipx.py` in clt
Menu:
q: quit
s: sudo mode
u: usual mode
l: list the packages
m: install the previous packages again
"""
import sh
import collections
MIRROR = 'https://pypi.tuna.tsinghua.edu.cn/simple'
PARAMS = '--upgrade', '-i', MIRROR
Package = collections.namedtuple('package', ['name', 'version', 'latest', 'type_'])
outdate = sh.pip.list('--format', 'columns', '--outdate', '-i', MIRROR)
packages = [Package._make(package.split()) for package in outdate.split('\n')[2:-1] if package]
# cmd = sh.pip3.install()
parallel = False
from threading import Semaphore
pool = Semaphore(10)
def done(cmd, success, exit_code):
pool.release()
def _install(current, packages):
cmd = sh.pip.install
if sudo:
if 'password' not in globals():
globals()['password'] = input('Your password:')
cmd = sh.sudo.bake("-S", _in=password).pip.install
# cmd = sh.contrib.sudo(password="hack2020", _with=True).pip.install
successful = []
if parallel:
def do(p):
pool.acquire()
try:
cmd(*PARAMS, p, _bg=True, _done=done)
successful.append(p)
print(f'{p} has been installed.')
except Exception as e:
print(e)
for p in current:
do(p).wait()
else:
for p in current:
try:
outdate = cmd(*PARAMS, p)
successful.append(p)
print(f'{p} has been installed.')
except Exception as e:
print(e)
return [package for package in packages if package.name not in successful]
def display(packages):
print('''
Name Version Latest
----------------------
''')
for package in packages:
print(' ', package.name, package.version, package.latest)
display(packages)
sudo = False
current = []
def install(source='tsinghua'):
global packages, sudo, current
print('''====Type one or some names of packages wantted.====
q: quit
s: sudo mode
u: usual mode
l: list the packages
m: install the previous packages again
-----------------------
''')
while True:
ps = input('>=> ')
# if ps.startswith('%'):
# a, _, b = ps.lstrip('%').partition('=')
# a = a.strip()
# b = b.strip()
# if b.isdigit():
# b = int(b)
# globals()[a] = b
if ps == 'q':
print('Bye.')
break
elif ps == 's':
sudo = True
print('in sudo mode')
elif ps == 'u':
sudo = False
print('not in sudo mode')
elif ps == 'l':
display(packages)
elif ps == 'm':
if not current:
print('m could not be used now!')
continue
packages = _install(current, packages)
else:
current = ps.split()
for c in current.copy():
if not any(c == package.name for package in packages):
print(f'{c} is not in the list of packages!')
current.remove(c)
else:
print(f'{c} will be installed.')
packages = _install(current, packages)
install()