|
| 1 | + |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import site |
| 5 | +import sys |
| 6 | + |
| 7 | +from plover.plugins_manager import global_registry,local_registry |
| 8 | +from plover.plugins_manager.utils import running_under_virtualenv |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +def pip(args, stdin=None, stdout=None, stderr=None, **kwargs): |
| 13 | + cmd = [sys.executable, '-m', |
| 14 | + 'plover.plugins_manager.pip_wrapper', |
| 15 | + '--disable-pip-version-check'] |
| 16 | + env = dict(os.environ) |
| 17 | + # Make sure user plugins are handled |
| 18 | + # even if user site is not enabled. |
| 19 | + if not running_under_virtualenv() and not site.ENABLE_USER_SITE: |
| 20 | + pypath = env.get('PYTHONPATH') |
| 21 | + if pypath is None: |
| 22 | + pypath = [] |
| 23 | + else: |
| 24 | + pypath = pypath.split(os.pathsep) |
| 25 | + pypath.insert(0, site.USER_SITE) |
| 26 | + env['PYTHONPATH'] = os.pathsep.join(pypath) |
| 27 | + command = args.pop(0) |
| 28 | + if command == 'check': |
| 29 | + cmd.append('check') |
| 30 | + elif command == 'install': |
| 31 | + cmd.extend(( |
| 32 | + 'install', |
| 33 | + '--upgrade-strategy=only-if-needed', |
| 34 | + )) |
| 35 | + if not running_under_virtualenv(): |
| 36 | + cmd.append('--user') |
| 37 | + elif command == 'uninstall': |
| 38 | + cmd.append('uninstall') |
| 39 | + elif command == 'list': |
| 40 | + cmd.extend(( |
| 41 | + 'list', |
| 42 | + '--format=columns', |
| 43 | + )) |
| 44 | + else: |
| 45 | + raise ValueError('invalid command: %s' % command) |
| 46 | + cmd.extend(args) |
| 47 | + return subprocess.Popen(cmd, env=env, stdin=stdin, |
| 48 | + stdout=stdout, stderr=stderr, |
| 49 | + **kwargs) |
| 50 | + |
| 51 | +def list_plugins(freeze=False): |
| 52 | + installed_plugins = local_registry.list_plugins() |
| 53 | + if freeze: |
| 54 | + available_plugins = {} |
| 55 | + else: |
| 56 | + available_plugins = global_registry.list_plugins() |
| 57 | + for name, installed, available in sorted( |
| 58 | + (name, |
| 59 | + installed_plugins.get(name, []), |
| 60 | + available_plugins.get(name, [])) |
| 61 | + for name in set(itertools.chain(installed_plugins, |
| 62 | + available_plugins)) |
| 63 | + ): |
| 64 | + latest = available[-1] if available else None |
| 65 | + current = installed[-1] if installed else None |
| 66 | + info = latest or current |
| 67 | + if freeze: |
| 68 | + if current: |
| 69 | + print('%s==%s' % (current.name, current.version)) |
| 70 | + continue |
| 71 | + print('%s (%s) - %s' % (info.name, info.version, info.summary)) |
| 72 | + if current: |
| 73 | + print(' INSTALLED: %s' % current.version) |
| 74 | + if latest: |
| 75 | + print(' LATEST: %s' % latest.version) |
| 76 | + |
| 77 | + |
| 78 | +def main(args=None): |
| 79 | + if args is None: |
| 80 | + args = sys.argv[1:] |
| 81 | + if args[0] == 'list_plugins': |
| 82 | + assert len(args) <= 2 |
| 83 | + if len(args) > 1: |
| 84 | + assert args[1] == '--freeze' |
| 85 | + freeze = True |
| 86 | + else: |
| 87 | + freeze = False |
| 88 | + sys.exit(list_plugins(freeze=freeze)) |
| 89 | + proc = pip(args) |
| 90 | + sys.exit(proc.wait()) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
0 commit comments