简介
- 有时候直接使用pip不方便,我就想做个简单的餐单
- 同时实现加速、sudo切换、失败后轻松重新下载
- 基于sh. 觉得subprocess有点麻烦
代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sh
import collections
out = sh.pip3.list('--format', 'columns', '--outdate', '-i', 'https://pypi.tuna.tsinghua.edu.cn/simple')
Package = collections.namedtuple('package', ['name', 'version', 'latest', 'type_'])
packages = [Package._make(package.split()) for package in out.split('\n')[2:-1] if package]
display(packages)
def display(packages):
print('''
Name Version Latest
----------------------
''')
for package in packages:
print(' ', package.name, package.version, package.latest)
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 == 'q':
print('Bye.')
break
elif ps == 's':
sudo = 1
print('in sudo mode')
elif ps == 'u':
sudo = 0
print('not in sudo mode')
elif ps == 'l':
display(packages)
elif ps == 'm':
if not current:
print('m could not be used now!')
continue
params = 'install', '--upgrade', '-i', 'https://pypi.tuna.tsinghua.edu.cn/simple'
cmd = sh.pip3
if sudo:
cmd = sh.contrib.sudo.pip3
for p in current:
try:
out = cmd(*params, p)
except Exception as e:
print(e)
else:
packages = [package for package in packages if package.name not in ps.split()]
else:
current = ps.split()
for c in current:
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.')
params = 'install', '--upgrade', '-i', 'https://pypi.tuna.tsinghua.edu.cn/simple'
cmd = sh.pip3
if sudo:
cmd = sh.contrib.sudo.pip3
for p in current:
try:
out = cmd(*params, p)
print(f'{p} has been installed.')
except Exception as e:
print(e)
else:
packages = [package for package in packages if package.name not in ps.split()]
install()
脚本运行效果
...
pymongo 3.9.0 3.10.1
PyOpenGL 3.1.4 3.1.5
pyrsistent 0.15.6 0.15.7
...
zope.interface 4.6.0 4.7.1
====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
>=>
shell 版本(DEPRECATED)
#!/bin/sh
:<<EOF
update the packages of python
s: sudo, u: not sudo -- udo, l: show list again, m: install last module
h: help
EOF
list=`pip3 list --format columns --outdate` # execute pip, and get the output
echo "$list"
last_module=""
mode=u
echo "Input q to quit, input h for help."
while true
do
read -p "please input module name: " module
if [ $module == m -o -z $module ]
# upgrade the last module again
then
if [ -n $last_module ]
then
echo "Upgrade $last_module again."
pip3 install --upgrade $last_module -i https://pypi.tuna.tsinghua.edu.cn/simple
else
echo "Please input a valid string."
fi
elif [ $module == q ] # to quit
then
echo "Finished. Have a good time ^_^"
break
elif [ $module == s ]
then
echo "sudo mode on"
mode=s
elif [ $module == u ]
then
echo "sudo mode off"
mode=u
elif [ $module == l ]
then
echo "$list"
elif [ $module == h ]
then
echo "s: sudo, u: not sudo -- udo, l: show list again, m: install last module, h: help"
else
last_module=$module
echo "Start to upgrade $last_module."
if [ $mode == u ]
then
pip3 install --upgrade $module -i https://pypi.tuna.tsinghua.edu.cn/simple
else
sudo pip3 install --upgrade $module -i https://pypi.tuna.tsinghua.edu.cn/simple
fi
fi
done