Last active
August 29, 2015 13:56
-
-
Save alexsavio/8808115 to your computer and use it in GitHub Desktop.
Python script to use pip and upgrade all packages in the currently activated virtualenv.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import subprocess | |
env_root = os.getenv('VIRTUAL_ENV') | |
if sys.platform == 'win32': | |
bin = 'Scripts' | |
else: | |
bin = 'bin' | |
class Piper(object): | |
def __init__(self, pip_path): | |
self._pip_path = pip_path | |
@staticmethod | |
def _check_output(args=[]): | |
return subprocess.check_output(args).split() | |
@staticmethod | |
def _exec_comm(args=[]): | |
return subprocess.call(args) | |
@staticmethod | |
def _append_to_comm(comm, args): | |
if isinstance(comm, str): | |
comm = [comm] | |
if isinstance(args, list): | |
comm.extend(args) | |
elif isinstance(args, str): | |
comm.append(args) | |
return comm | |
def _pip_exec(self, args, modules=[]): | |
pip_comm = self._pip_path | |
pip_comm = self._append_to_comm(pip_comm, args) | |
pip_comm = self._append_to_comm(pip_comm, modules) | |
print('Executing: ' + ' '.join(pip_comm)) | |
self._exec_comm(pip_comm) | |
def install(self, modules=[]): | |
self._pip_exec('install', modules) | |
def upgrade(self, modules=[]): | |
self._pip_exec(['install', '-U'], modules) | |
def freeze(self): | |
return self._check_output([self._pip_path, 'freeze']) | |
if __name__ == '__main__': | |
mypip = Piper(os.path.join(env_root, bin, 'pip')) | |
env_packages = mypip.freeze() | |
for pck in env_packages: | |
if sys.version > '3': | |
pck = pck.decode() | |
pck_name = pck.split('==')[0] | |
mypip.upgrade(pck_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment