Created
April 7, 2015 14:59
-
-
Save cmbaughman/9a98dc326c847ff073c3 to your computer and use it in GitHub Desktop.
Automatically generate setup.py install_requires and dependency_links from a requirements.txt file.
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
import os | |
import re | |
def which(program): | |
""" | |
Detect whether or not a program is installed. | |
Thanks to http://stackoverflow.com/a/377028/70191 | |
""" | |
def is_exe(fpath): | |
return os.path.exists(fpath) and os.access(fpath, os.X_OK) | |
fpath, _ = os.path.split(program) | |
if fpath: | |
if is_exe(program): | |
return program | |
else: | |
for path in os.environ['PATH'].split(os.pathsep): | |
exe_file = os.path.join(path, program) | |
if is_exe(exe_file): | |
return exe_file | |
return None | |
EDITABLE_REQUIREMENT = re.compile(r'^-e (?P<link>(?P<vcs>git|svn|hg|bzr).+#egg=(?P<package>.+)-(?P<version>\d(?:\.\d)*))$') | |
install_requires = [] | |
dependency_links = [] | |
for requirement in (l.strip() for l in open('requirements.txt')): | |
match = EDITABLE_REQUIREMENT.match(requirement) | |
if match: | |
assert which(match.group('vcs')) is not None, \ | |
"VCS '%(vcs)s' must be installed in order to install %(link)s" % match.groupdict() | |
install_requires.append("%(package)s==%(version)s" % match.groupdict()) | |
dependency_links.append(match.group('link')) | |
else: | |
install_requires.append(requirement) | |
print install_requires | |
print dependency_links |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Saved me a lot of time searching the internet for a solution to
install_requires
anddependency_links
. Thanks a lot!