比pip更方便的更新第三方库方法

方便更新第三方库脚本。
直接在命令行运行 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值