Created
June 21, 2012 11:15
-
-
Save asfaltboy/2965177 to your computer and use it in GitHub Desktop.
returns current requirements.txt versions
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
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