Skip to content

Instantly share code, notes, and snippets.

@dmpayton
Created May 13, 2015 20:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmpayton/7d3556406fe7eaf1bbd7 to your computer and use it in GitHub Desktop.
Save dmpayton/7d3556406fe7eaf1bbd7 to your computer and use it in GitHub Desktop.
Check a requirements.txt for updated packages
#!/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
# TODO: Rewrite this using requirements-parser
# https://github.com/davidfischer/requirements-parser
class ProgressIndicator(object):
chars = itertools.cycle(('|', '/', '-', '\\'))
def update(self, message):
self.clear()
sys.stdout.write('{0} {1}'.format(next(self.chars), message))
sys.stdout.flush()
def clear(self):
# For more ANSI control characters, check out fish.ANSIControl
sys.stdout.write("\x1b[2K\r")
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.append({
'package': package,
'installed_version': version,
'current_version': current_version
})
status.clear()
print json.dumps(packages, indent=4)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment