pip-user-upgrade (1442B)
1 #!/usr/bin/env python3 2 3 import json 4 import pathlib 5 import subprocess 6 import sys 7 import logging 8 9 logging.basicConfig(level='DEBUG') 10 logger = logging.getLogger(__name__) 11 12 REQUIREMENTS_FILE = pathlib.Path("~/foo/dotfiles/requirements.txt").expanduser() 13 14 UPGRADE_PIP_COMMAND = "python3 -m pip install --user --upgrade pip".split(" ") 15 LIST_OUTDATED_PACKAGES_IN_JSON_COMMAND = "python3 -m pip list --format=json -o --user".split(" ") 16 UPGRADE_PACKAGES_COMMAND = "python3 -m pip install -U --user -r /dev/stdin".split(" ") 17 INSTALL_WANTED_PACKAGES_COMMAND = ( 18 "python3 -m pip install -U --user -r {REQUIREMENTS_FILE}" 19 .format(REQUIREMENTS_FILE=REQUIREMENTS_FILE) 20 .split(" ") 21 ) 22 23 def main(): 24 subprocess.Popen(UPGRADE_PIP_COMMAND).wait() 25 26 pip_list_json = subprocess.Popen( 27 LIST_OUTDATED_PACKAGES_IN_JSON_COMMAND, 28 stdout=subprocess.PIPE, 29 ).stdout.read() 30 31 package_names = [n["name"] for n in json.loads(pip_list_json.decode("UTF-8"))] 32 33 logger.debug("Outdated package list: %s", package_names) 34 35 upgrade_packages_process = subprocess.Popen( 36 UPGRADE_PACKAGES_COMMAND, 37 stdin=subprocess.PIPE, 38 ) 39 40 upgrade_packages_process.stdin.writelines(package_names) 41 42 upgrade_packages_process.stdin.close() 43 upgrade_packages_process.wait() 44 45 46 install_wanted_packages_process = subprocess.Popen( 47 INSTALL_WANTED_PACKAGES_COMMAND, 48 ).wait() 49 50 51 if __name__ == "__main__": 52 main()