Skip to content

Instantly share code, notes, and snippets.

@cmbaughman
Created April 7, 2015 14:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmbaughman/9a98dc326c847ff073c3 to your computer and use it in GitHub Desktop.
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.
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
@kinow
Copy link

kinow commented Oct 16, 2018

Saved me a lot of time searching the internet for a solution to install_requires and dependency_links. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment