Created
January 25, 2014 21:41
-
-
Save Xion/8624077 to your computer and use it in GitHub Desktop.
requirements.txt parser for setup.py
This file contains hidden or 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
def read_requirements(filename='requirements.txt'): | |
"""Reads the list of requirements from given file. | |
:param filename: Filename to read the requirements from. | |
Uses ``'requirements.txt'`` by default. | |
:return: Requirments as list of strings. | |
""" | |
# allow for some leeway with the argument | |
if not filename.startswith('requirements'): | |
filename = 'requirements-' + filename | |
if not os.path.splitext(filename)[1]: | |
filename += '.txt' # no extension, add default | |
def valid_line(line): | |
line = line.strip() | |
return line and not any(line.startswith(p) for p in ('#', '-')) | |
def extract_requirement(line): | |
egg_eq = '#egg=' | |
if egg_eq in line: | |
_, requirement = line.split(egg_eq, 1) | |
return requirement | |
return line | |
with open(filename) as f: | |
lines = f.readlines() | |
return list(map(extract_requirement, filter(valid_line, lines))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment