Un-nest some sections of code

Reversing the logic in a few of the booleans and returning earlier
allows us to not have to be nested quite so deep.

Change-Id: Ia0dad183563381e9eb8c790ba61d0b350d74421d
This commit is contained in:
Monty Taylor 2014-05-26 11:32:12 -07:00
parent cb1c34ce42
commit bdb01910cb
1 changed files with 82 additions and 76 deletions

View File

@ -253,95 +253,101 @@ def write_git_changelog(git_dir=None, dest_dir=os.path.curdir,
"""Write a changelog based on the git changelog."""
should_skip = get_boolean_option(option_dict, 'skip_changelog',
'SKIP_WRITE_GIT_CHANGELOG')
if not should_skip:
new_changelog = os.path.join(dest_dir, 'ChangeLog')
# If there's already a ChangeLog and it's not writable, just use it
if (os.path.exists(new_changelog)
and not os.access(new_changelog, os.W_OK)):
return
log.info('[pbr] Writing ChangeLog')
if git_dir is None:
git_dir = _get_git_directory()
if git_dir:
log_cmd = ['log', '--oneline', '--decorate']
changelog = _run_git_command(log_cmd, git_dir)
first_line = True
with io.open(new_changelog, "w",
encoding="utf-8") as changelog_file:
changelog_file.write("CHANGES\n=======\n\n")
for line in changelog.split('\n'):
line_parts = line.split()
if len(line_parts) < 2:
continue
# Tags are in a list contained in ()'s. If a commit
# subject that is tagged happens to have ()'s in it
# this will fail
if line_parts[1].startswith('(') and ')' in line:
msg = line.split(')')[1].strip()
else:
msg = " ".join(line_parts[1:])
if should_skip:
return
if "tag:" in line:
tags = [
tag.split(",")[0]
for tag in line.split(")")[0].split("tag: ")[1:]]
tag = _get_highest_tag(tags)
new_changelog = os.path.join(dest_dir, 'ChangeLog')
# If there's already a ChangeLog and it's not writable, just use it
if (os.path.exists(new_changelog)
and not os.access(new_changelog, os.W_OK)):
return
log.info('[pbr] Writing ChangeLog')
if git_dir is None:
git_dir = _get_git_directory()
if not git_dir:
return
underline = len(tag) * '-'
if not first_line:
changelog_file.write('\n')
changelog_file.write(
("%(tag)s\n%(underline)s\n\n" %
dict(tag=tag,
underline=underline)))
log_cmd = ['log', '--oneline', '--decorate']
changelog = _run_git_command(log_cmd, git_dir)
first_line = True
with io.open(new_changelog, "w",
encoding="utf-8") as changelog_file:
changelog_file.write("CHANGES\n=======\n\n")
for line in changelog.split('\n'):
line_parts = line.split()
if len(line_parts) < 2:
continue
# Tags are in a list contained in ()'s. If a commit
# subject that is tagged happens to have ()'s in it
# this will fail
if line_parts[1].startswith('(') and ')' in line:
msg = line.split(')')[1].strip()
else:
msg = " ".join(line_parts[1:])
if not msg.startswith("Merge "):
if msg.endswith("."):
msg = msg[:-1]
changelog_file.write(
("* %(msg)s\n" % dict(msg=msg)))
first_line = False
if "tag:" in line:
tags = [
tag.split(",")[0]
for tag in line.split(")")[0].split("tag: ")[1:]]
tag = _get_highest_tag(tags)
underline = len(tag) * '-'
if not first_line:
changelog_file.write('\n')
changelog_file.write(
("%(tag)s\n%(underline)s\n\n" %
dict(tag=tag,
underline=underline)))
if not msg.startswith("Merge "):
if msg.endswith("."):
msg = msg[:-1]
changelog_file.write(
("* %(msg)s\n" % dict(msg=msg)))
first_line = False
def generate_authors(git_dir=None, dest_dir='.', option_dict=dict()):
"""Create AUTHORS file using git commits."""
should_skip = get_boolean_option(option_dict, 'skip_authors',
'SKIP_GENERATE_AUTHORS')
if not should_skip:
old_authors = os.path.join(dest_dir, 'AUTHORS.in')
new_authors = os.path.join(dest_dir, 'AUTHORS')
# If there's already an AUTHORS file and it's not writable, just use it
if (os.path.exists(new_authors)
and not os.access(new_authors, os.W_OK)):
return
log.info('[pbr] Generating AUTHORS')
ignore_emails = '(jenkins@review|infra@lists|jenkins@openstack)'
if git_dir is None:
git_dir = _get_git_directory()
if git_dir:
authors = []
if should_skip:
return
# don't include jenkins email address in AUTHORS file
git_log_cmd = ['log', '--format=%aN <%aE>']
authors += _run_git_command(git_log_cmd, git_dir).split('\n')
authors = [a for a in authors if not re.search(ignore_emails, a)]
old_authors = os.path.join(dest_dir, 'AUTHORS.in')
new_authors = os.path.join(dest_dir, 'AUTHORS')
# If there's already an AUTHORS file and it's not writable, just use it
if (os.path.exists(new_authors)
and not os.access(new_authors, os.W_OK)):
return
log.info('[pbr] Generating AUTHORS')
ignore_emails = '(jenkins@review|infra@lists|jenkins@openstack)'
if git_dir is None:
git_dir = _get_git_directory()
if git_dir:
authors = []
# get all co-authors from commit messages
co_authors_out = _run_git_command('log', git_dir)
co_authors = re.findall('Co-authored-by:.+', co_authors_out,
re.MULTILINE)
co_authors = [signed.split(":", 1)[1].strip()
for signed in co_authors if signed]
# don't include jenkins email address in AUTHORS file
git_log_cmd = ['log', '--format=%aN <%aE>']
authors += _run_git_command(git_log_cmd, git_dir).split('\n')
authors = [a for a in authors if not re.search(ignore_emails, a)]
authors += co_authors
authors = sorted(set(authors))
# get all co-authors from commit messages
co_authors_out = _run_git_command('log', git_dir)
co_authors = re.findall('Co-authored-by:.+', co_authors_out,
re.MULTILINE)
co_authors = [signed.split(":", 1)[1].strip()
for signed in co_authors if signed]
with open(new_authors, 'wb') as new_authors_fh:
if os.path.exists(old_authors):
with open(old_authors, "rb") as old_authors_fh:
new_authors_fh.write(old_authors_fh.read())
new_authors_fh.write(('\n'.join(authors) + '\n')
.encode('utf-8'))
authors += co_authors
authors = sorted(set(authors))
with open(new_authors, 'wb') as new_authors_fh:
if os.path.exists(old_authors):
with open(old_authors, "rb") as old_authors_fh:
new_authors_fh.write(old_authors_fh.read())
new_authors_fh.write(('\n'.join(authors) + '\n')
.encode('utf-8'))
def _find_git_files(dirname='', git_dir=None):