...
- commit_message = soup.find('div', id='commit-message').find('div', id='edit-commit_message') \
- .find('div', 'yui3-editable_text-text').p
- merge_info['commit_message'] = commit_message.string
- # Find all tr-tags with this class. Makes it possible to get bug numbers.
- #
Merge ... into...
- div_branches = soup.find('div', class_='context-publication')
- branches = div_branches.h1.contents[0]
- merge_info['target_branch'] = '+branch/' + branches[(branches.find(' into lp:') + 9):]
- # Find the authors email address. It is hidden in a javascript line like this:
- # conf = {"status_value": "Needs review", "source_revid": "tomasgroth@yahoo.dk-20160921204550-gxduegmcmty9rljf",
- # "user_can_edit_status": false, ...
- script_tag = soup.find('script', attrs={"id": "codereview-script"})
- content = script_tag.contents[0]
- start_pos = content.find('source_revid') + 16
- pattern = re.compile(r'.*\w-\d\d\d\d\d+')
- match = pattern.match(content[start_pos:])
- merge_info['author_email'] = match.group()[:-15]
- # Launchpad doesn't supply the author's true name, so we'll just grab whatever they use for display on LP
- a_person = soup.find('div', id='registration').find('a', 'person')
- merge_info['author_name'] = a_person.contents[0]
- return merge_info
-
-
-def do_merge(merge_info, yes_to_all=False):
- """
- Do the merge
- """
- # Check that we are in the right branch
- bzr_info_output = subprocess.check_output(['bzr', 'info'])
- if merge_info['target_branch'] not in bzr_info_output.decode():
- print('ERROR: It seems you are not in the right folder...')
- return False
- # Merge the branch
- if yes_to_all:
- can_merge = True
- else:
- user_choice = input('Merge ' + merge_info['branch_url'] + ' into local branch? (y/N/q): ').lower()
- if user_choice == 'q':
- return False
- can_merge = user_choice == 'y'
- if can_merge:
- print('Merging...')
- subprocess.call(['bzr', 'merge', merge_info['branch_url']])
- return True
-
-
-def qcommit(commit_message, author_name, author_email, bugs=[]):
- """
- Use qcommit to make the commit
- """
- # Setup QT workaround to make qbzr look right on my box
- my_env = os.environ.copy()
- my_env['QT_GRAPHICSSYSTEM'] = 'native'
- # Print stuff that kan be copy/pasted into qbzr GUI
- if bugs:
- print('These bugs can be copy/pasted in: ' + ' '.join(['lp:{}'.format(bug) for bug in bugs]))
- print('The authors email is: {} <{}>'.format(author_name, author_email))
- # Run qcommit
- subprocess.call(['bzr', 'qcommit', '-m', commit_message], env=my_env)
-
-
-def do_commit(merge_info, yes_to_all=False, use_qcommit=False):
- """
- Actually do the commit
- """
- if use_qcommit:
- qcommit(merge_info['commit_message'], merge_info['author_name'], merge_info['author_email'],
- merge_info.get('bugs'))
- return True
- # Create commit command
- commit_command = ['bzr', 'commit']
- if 'bugs' in merge_info:
- commit_command.extend(['--fixes=lp:{}'.format(bug) for bug in merge_info['bugs']])
- commit_command.extend(['-m', merge_info['commit_message'],
- '--author', '{author_name} <{author_email}>'.format(**merge_info)])
- if yes_to_all:
- can_commit = True
- else:
- print('About to run the bzr command below:\n')
- print(' '.join(commit_command))
- user_choice = input('Run the command (y), use qcommit (q) or cancel (C): ').lower()
- if user_choice == 'q':
- qcommit(merge_info['commit_message'], merge_info['author_name'], merge_info['author_email'],
- merge_info.get('bugs'))
- return True
- can_commit = user_choice == 'y'
- if can_commit:
- print('Committing...')
- subprocess.call(commit_command)
- return True
- else:
- return False
-
-
-def main():
- """
- Run the script
- """
- args = parse_args()
- if not is_merge_url_valid(args.url):
- exit(1)
- merge_info = get_merge_info(args.url)
- if not merge_info:
- exit(2)
- if not do_merge(merge_info, args.yes_to_all):
- exit(3)
- if not do_commit(merge_info, args.yes_to_all, args.use_qcommit):
- exit(4)
-
-
-if __name__ == '__main__':
- main()
|