Skip to content

Instantly share code, notes, and snippets.

@asfaltboy
Created June 21, 2012 11:15
Show Gist options
  • Save asfaltboy/2965177 to your computer and use it in GitHub Desktop.
Save asfaltboy/2965177 to your computer and use it in GitHub Desktop.
returns current requirements.txt versions
import pkg_resources, os
def requirements_versions(req_file=None):
path = req_file
if path and not os.path.exists(path) or not path:
path = 'base_requirements.txt'
elif not os.path.exists(path):
path = 'req.txt'
elif not os.path.exists(path):
return None
packages = []
req_file = open(path, 'r')
for line in req_file.readlines():
if line.startswith('-i'):
continue
try:
line = line[:line.index('==')]
except ValueError:
try:
line = line[:line.index('\n')]
except:
pass
packages.append(line)
reqs = []
for pkg in packages:
reqs.append("{0}=={1}\n".format(pkg, pkg_resources.get_distribution(pkg).version))
return "".join(reqs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment