|
| 1 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | + |
| 3 | +# Copyright 2011 OpenStack LLC. |
| 4 | +# All Rights Reserved. |
| 5 | +# |
| 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 |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 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 |
| 16 | +# under the License. |
| 17 | + |
| 18 | +""" |
| 19 | +Utilities with minimum-depends for use in setup.py |
| 20 | +""" |
| 21 | + |
| 22 | +import os |
| 23 | +import re |
| 24 | +import subprocess |
| 25 | + |
| 26 | + |
| 27 | +def parse_mailmap(mailmap='.mailmap'): |
| 28 | + mapping = {} |
| 29 | + if os.path.exists(mailmap): |
| 30 | + fp = open(mailmap, 'r') |
| 31 | + for l in fp: |
| 32 | + l = l.strip() |
| 33 | + if not l.startswith('#') and ' ' in l: |
| 34 | + canonical_email, alias = l.split(' ') |
| 35 | + mapping[alias] = canonical_email |
| 36 | + return mapping |
| 37 | + |
| 38 | + |
| 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 |
| 42 | + """ |
| 43 | + for alias, email in mapping.iteritems(): |
| 44 | + changelog = changelog.replace(alias, email) |
| 45 | + return changelog |
| 46 | + |
| 47 | + |
| 48 | +# Get requirements from the first file that exists |
| 49 | +def get_reqs_from_files(requirements_files): |
| 50 | + reqs_in = [] |
| 51 | + for requirements_file in requirements_files: |
| 52 | + if os.path.exists(requirements_file): |
| 53 | + return open(requirements_file, 'r').read().split('\n') |
| 54 | + return [] |
| 55 | + |
| 56 | + |
| 57 | +def parse_requirements(requirements_files=['requirements.txt', |
| 58 | + 'tools/pip-requires']): |
| 59 | + requirements = [] |
| 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', |
| 63 | + line)) |
| 64 | + elif re.match(r'\s*-f\s+', line): |
| 65 | + pass |
| 66 | + else: |
| 67 | + requirements.append(line) |
| 68 | + |
| 69 | + return requirements |
| 70 | + |
| 71 | + |
| 72 | +def parse_dependency_links(requirements_files=['requirements.txt', |
| 73 | + 'tools/pip-requires']): |
| 74 | + dependency_links = [] |
| 75 | + for line in get_reqs_from_files(requirements_files): |
| 76 | + if re.match(r'(\s*#)|(\s*$)', line): |
| 77 | + continue |
| 78 | + if re.match(r'\s*-[ef]\s+', line): |
| 79 | + dependency_links.append(re.sub(r'\s*-[ef]\s+', '', line)) |
| 80 | + return dependency_links |
| 81 | + |
| 82 | + |
| 83 | +def write_requirements(): |
| 84 | + venv = os.environ.get('VIRTUAL_ENV', None) |
| 85 | + if venv is not 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) |
| 91 | + |
| 92 | + |
| 93 | +def _run_shell_command(cmd): |
| 94 | + output = subprocess.Popen(["/bin/sh", "-c", cmd], |
| 95 | + stdout=subprocess.PIPE) |
| 96 | + return output.communicate()[0].strip() |
| 97 | + |
| 98 | + |
| 99 | +def write_vcsversion(location): |
| 100 | + """ Produce a vcsversion dict that mimics the old one produced by bzr |
| 101 | + """ |
| 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. :) |
| 112 | +version_info = { |
| 113 | + 'branch_nick': '%s', |
| 114 | + 'revision_id': '%s', |
| 115 | + 'revno': %s |
| 116 | +} |
| 117 | +""" % (branch_nick, revid, revno)) |
| 118 | + |
| 119 | + |
| 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)) |
| 128 | + |
| 129 | + |
| 130 | +def generate_authors(): |
| 131 | + """Create AUTHORS file using git commits""" |
| 132 | + jenkins_email = 'jenkins@review.openstack.org' |
| 133 | + if os.path.isdir('.git'): |
| 134 | + # don't include jenkins email address in AUTHORS file |
| 135 | + git_log_cmd = "git log --format='%aN <%aE>' | sort -u | " \ |
| 136 | + "grep -v " + jenkins_email |
| 137 | + changelog = _run_shell_command(git_log_cmd) |
| 138 | + mailmap = parse_mailmap() |
| 139 | + with open("AUTHORS", "w") as authors_file: |
| 140 | + authors_file.write(canonicalize_emails(changelog, mailmap)) |
0 commit comments