Skip to content

Instantly share code, notes, and snippets.

@iMerica
Forked from serafeimgr/upgrade_pip_packages.py
Last active October 9, 2019 17:17
Show Gist options
  • Select an option

  • Save iMerica/70f3ac88484a7fc02dca68de765be89d to your computer and use it in GitHub Desktop.

Select an option

Save iMerica/70f3ac88484a7fc02dca68de765be89d to your computer and use it in GitHub Desktop.

Revisions

  1. iMerica revised this gist Oct 9, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions upgrade_pip_packages.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    """
    This script upgrades all outdated python packages.
    """
    __author__ = "serafeimgr"

    from multiprocessing import Pool, cpu_count
    from subprocess import PIPE, Popen
    @@ -27,7 +26,7 @@ def upgrade_package(package):
    """
    upgrade_command = "pip install --upgrade {}".format(package)
    stdout, _ = run_command(upgrade_command)
    print package, stdout
    print(package, stdout)


    def collect_packages():
  2. @serafeimgr serafeimgr created this gist Sep 5, 2017.
    55 changes: 55 additions & 0 deletions upgrade_pip_packages.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    """
    This script upgrades all outdated python packages.
    """
    __author__ = "serafeimgr"

    from multiprocessing import Pool, cpu_count
    from subprocess import PIPE, Popen


    def run_command(command):
    """
    Executes a command.
    @param: command
    """
    stdout, stderror = Popen(command,
    stdout=PIPE,
    stderr=PIPE,
    shell=True).communicate()
    return stdout, stderror


    def upgrade_package(package):
    """
    Upgrade a package.
    @param: package
    """
    upgrade_command = "pip install --upgrade {}".format(package)
    stdout, _ = run_command(upgrade_command)
    print package, stdout


    def collect_packages():
    """
    Collect outdated packages.
    @returns : packages
    """
    outdated_command = "pip list --outdated"
    stdout, _ = run_command(outdated_command)

    return [p.split(' ')[0] for p in stdout.split('\n') if p != ""]


    def main():
    """Upgrade outdated python packages."""
    packages = collect_packages()
    pool = Pool(cpu_count())
    pool.map(upgrade_package, packages)
    pool.close()
    pool.join()


    if __name__ == '__main__':
    main()