1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
Utilities with minimum-depends for use in setup.py
27
def parse_mailmap(mailmap='.mailmap'):
29
if os.path.exists(mailmap):
30
fp = open(mailmap, 'r')
33
if not l.startswith('#') and ' ' in l:
34
canonical_email, alias = l.split(' ')
35
mapping[alias] = canonical_email
39
def canonicalize_emails(changelog, mapping):
40
"""Takes in a string and an email alias mapping and replaces all
41
instances of the aliases in the string with their real email.
43
for alias, email in mapping.iteritems():
44
changelog = changelog.replace(alias, email)
48
# Get requirements from the first file that exists
49
def get_reqs_from_files(requirements_files):
51
for requirements_file in requirements_files:
52
if os.path.exists(requirements_file):
53
return open(requirements_file, 'r').read().split('\n')
57
def parse_requirements(requirements_files=['requirements.txt',
58
'tools/pip-requires']):
60
for line in get_reqs_from_files(requirements_files):
61
if re.match(r'\s*-e\s+', line):
62
requirements.append(re.sub(r'\s*-e\s+.*#egg=(.*)$', r'\1',
64
elif re.match(r'\s*-f\s+', line):
67
requirements.append(line)
72
def parse_dependency_links(requirements_files=['requirements.txt',
73
'tools/pip-requires']):
75
for line in get_reqs_from_files(requirements_files):
76
if re.match(r'(\s*#)|(\s*$)', line):
78
if re.match(r'\s*-[ef]\s+', line):
79
dependency_links.append(re.sub(r'\s*-[ef]\s+', '', line))
80
return dependency_links
83
def write_requirements():
84
venv = os.environ.get('VIRTUAL_ENV', None)
86
with open("requirements.txt", "w") as req_file:
87
output = subprocess.Popen(["pip", "-E", venv, "freeze", "-l"],
88
stdout=subprocess.PIPE)
89
requirements = output.communicate()[0].strip()
90
req_file.write(requirements)
93
def _run_shell_command(cmd):
94
output = subprocess.Popen(["/bin/sh", "-c", cmd],
95
stdout=subprocess.PIPE)
96
return output.communicate()[0].strip()
99
def write_vcsversion(location):
100
"""Produce a vcsversion dict that mimics the old one produced by bzr.
102
if os.path.isdir('.git'):
103
branch_nick_cmd = 'git branch | grep -Ei "\* (.*)" | cut -f2 -d" "'
104
branch_nick = _run_shell_command(branch_nick_cmd)
105
revid_cmd = "git rev-parse HEAD"
106
revid = _run_shell_command(revid_cmd).split()[0]
107
revno_cmd = "git log --oneline | wc -l"
108
revno = _run_shell_command(revno_cmd)
109
with open(location, 'w') as version_file:
110
version_file.write("""
111
# This file is automatically generated by setup.py, So don't edit it. :)
117
""" % (branch_nick, revid, revno))
120
def write_git_changelog():
121
"""Write a changelog based on the git changelog."""
122
if os.path.isdir('.git'):
123
git_log_cmd = 'git log --stat'
124
changelog = _run_shell_command(git_log_cmd)
125
mailmap = parse_mailmap()
126
with open("ChangeLog", "w") as changelog_file:
127
changelog_file.write(canonicalize_emails(changelog, mailmap))
130
def generate_authors():
131
"""Create AUTHORS file using git commits."""
132
jenkins_email = 'jenkins@review.openstack.org'
133
old_authors = 'AUTHORS.in'
134
new_authors = 'AUTHORS'
135
if os.path.isdir('.git'):
136
# don't include jenkins email address in AUTHORS file
137
git_log_cmd = "git log --format='%aN <%aE>' | sort -u | " \
138
"grep -v " + jenkins_email
139
changelog = _run_shell_command(git_log_cmd)
140
mailmap = parse_mailmap()
141
with open(new_authors, 'w') as new_authors_fh:
142
new_authors_fh.write(canonicalize_emails(changelog, mailmap))
143
if os.path.exists(old_authors):
144
with open(old_authors, "r") as old_authors_fh:
145
new_authors_fh.write('\n' + old_authors_fh.read())