Skip to content

Instantly share code, notes, and snippets.

@tyrelsouza
Forked from dmpayton/pypi-versions.py
Last active August 29, 2015 14:21
Show Gist options
  • Save tyrelsouza/0f21fbad60b851af9106 to your computer and use it in GitHub Desktop.
Save tyrelsouza/0f21fbad60b851af9106 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
''' Get a list of out-of-date packages from a pip requirements.txt file. '''
import itertools
import json
import requests
import sys
import yaml
def make_rainbow_string(input):
# http://www.codepost.io/snippets/13
color_template, min_color, max_color, output = "\033[{}m", 31, 37, ""
current_color = min_color
for index in range(0, len(input)):
output += color_template.format(current_color) + input[index]
if input[index] != " ":
current_color += 1
if current_color > max_color:
current_color = min_color
return output + color_template.format("05")
# TODO: Rewrite this using requirements-parser
# https://github.com/davidfischer/requirements-parser
class ProgressIndicator(object):
chars = itertools.cycle(('|', '/', '-', '\\'))
def update(self, message):
self.clear()
message = make_rainbow_string(message)
sys.stderr.write('{0} {1}'.format(next(self.chars), message))
sys.stderr.flush()
def clear(self):
# For more ANSI control characters, check out fish.ANSIControl
sys.stderr.write("\033[0m\x1b[2K\r")
sys.stderr.flush()
def main():
try:
requirements_txt = sys.argv[1]
except IndexError:
requirements_txt = 'requirements.txt'
status = ProgressIndicator()
packages = {}
requirements = open(requirements_txt, 'r').read()
for line in requirements.splitlines():
line = line.strip()
if not line or line.count('==') != 1 or line.startswith('#'):
continue
package, version = line.split('#')[0].split('==')
status.update(package)
response = requests.get('http://pypi.python.org/pypi/{0}/json'.format(package))
try:
data = json.loads(response.content)
except Exception, err:
status.update('ERROR [{0}]: {1}'.format(package, err))
continue
current_version = data['info']['version'].strip()
if version != current_version:
packages[package] = {
'installed_version': version,
'current_version': current_version
}
status.clear()
print yaml.dump(packages,default_flow_style=False)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment