Created
May 20, 2011 18:54
-
-
Save matthewryanscott/983536 to your computer and use it in GitHub Desktop.
Updates git hashes in requirements.txt files based on current state of adjacent repositories.
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
#!/usr/bin/env python2.6 | |
"""Updates git hashes in requirements.txt files based on current state of adjacent repositories. | |
Expects to be run inside directory containing requirements.txt to be updated. | |
Be sure to commit regularly: Performs an in-place update of requirements.txt. | |
""" | |
import re | |
import sys | |
import os | |
import subprocess | |
GIT_REQUIREMENT_RE = re.compile(r'^\-e git\+(?P<url>.+)@(?P<rev>\w*)\#egg=(?P<egg>[\w\._-]+)') | |
GIT_REQUIREMENT_FORMAT = '-e git+{url:}@{rev:}#egg={egg:}\n' | |
def find_git_revision(egg): | |
egg_path = os.path.join(os.pardir, egg) | |
git_path = os.path.join(egg_path, '.git') | |
p = subprocess.Popen(['git', '--git-dir={0}'.format(git_path), 'rev-parse', 'HEAD'], stdout=subprocess.PIPE) | |
stdout, stderr = p.communicate() | |
return stdout.strip() | |
REQUIREMENT_FORMATS = ( | |
(GIT_REQUIREMENT_RE, GIT_REQUIREMENT_FORMAT, find_git_revision), | |
) | |
def read_requirements(): | |
with open('requirements.txt', 'rU') as f: | |
return f.readlines() | |
def main(args): | |
new_requirements = [] | |
for requirement in read_requirements(): | |
for req_re, req_fmt, find_revision in REQUIREMENT_FORMATS: | |
match = req_re.match(requirement) | |
if match is not None: | |
d = match.groupdict() | |
new_rev = find_revision(d['egg']) | |
if new_rev: | |
d['rev'] = new_rev | |
else: | |
# Keep the same revision. | |
pass | |
requirement = req_fmt.format(**d) | |
break | |
else: | |
continue | |
new_requirements.append(requirement) | |
with open('requirements.txt', 'wb') as f: | |
f.writelines(new_requirements) | |
os.system('git diff requirements.txt') | |
return 0 | |
if __name__ == '__main__': | |
args = sys.argv[1:] | |
sys.exit(main(args)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment