def set_reviewer(self, reviewer): latest_entry = self.latest_entry() latest_entry_contents = latest_entry.contents() reviewer_text = latest_entry.reviewer() found_nobody = re.search("NOBODY\s*\(OOPS!\)", latest_entry_contents, re.MULTILINE) found_reviewer_or_unreviewed = latest_entry.has_valid_reviewer() if not found_nobody and not found_reviewer_or_unreviewed and not reviewer_text: bug_url_number_of_items = len( re.findall(config_urls.bug_url_long, latest_entry_contents, re.MULTILINE)) bug_url_number_of_items += len( re.findall(config_urls.bug_url_short, latest_entry_contents, re.MULTILINE)) result = StringIO() with self._filesystem.open_text_file_for_reading( self.path) as file: for line in file: found_bug_url = re.search(config_urls.bug_url_long, line) if not found_bug_url: found_bug_url = re.search(config_urls.bug_url_short, line) result.write(line) if found_bug_url: if bug_url_number_of_items == 1: result.write("\n Reviewed by %s.\n" % reviewer) bug_url_number_of_items -= 1 self._filesystem.write_text_file(self.path, result.getvalue()) else: data = self._filesystem.read_text_file(self.path) newdata = data.replace("NOBODY (OOPS!)", reviewer) self._filesystem.write_text_file(self.path, newdata)
class _CaptureAndPassThroughStream(object): def __init__(self, stream): self._buffer = StringIO() self._stream = stream def write(self, msg): self._stream.write(msg) # Note that we don't want to capture any output generated by the debugger # because that could cause the results of capture_output() to be invalid. if not self._message_is_from_pdb(): self._buffer.write(msg) def _message_is_from_pdb(self): # We will assume that if the pdb module is in the stack then the output # is being generated by the python debugger (or the user calling something # from inside the debugger). import inspect import pdb stack = inspect.stack() return any(frame[1] == pdb.__file__.replace('.pyc', '.py') for frame in stack) def flush(self): self._stream.flush() def getvalue(self): return self._buffer.getvalue()
def set_short_description_and_bug_url(self, short_description, bug_url): result = StringIO() with self._filesystem.open_text_file_for_reading(self.path) as file: short_description_placeholder = "Need a short description (OOPS!)." bug_url_placeholder = "Need the bug URL (OOPS!)." for line in file: stripped = line.strip() if stripped == short_description_placeholder: line = self._changelog_indent + short_description + "\n" if stripped == bug_url_placeholder: line = self._changelog_indent + bug_url + "\n" result.write(line) self._filesystem.write_text_file(self.path, result.getvalue())
def delete_entries(self, num_entries): date_line_regexp = re.compile(ChangeLogEntry.date_line_regexp) rolled_over_regexp = re.compile(ChangeLogEntry.rolled_over_regexp) entries = 0 result = StringIO() with self._filesystem.open_text_file_for_reading(self.path) as file: for line in file: if date_line_regexp.match(line): entries += 1 elif rolled_over_regexp.match(line): entries = num_entries + 1 if entries > num_entries: result.write(line) self._filesystem.write_text_file(self.path, result.getvalue())
def update_with_unreviewed_message(self, message): first_boilerplate_line_regexp = re.compile( "%sNeed a short description \(OOPS!\)\." % self._changelog_indent) removing_boilerplate = False result = StringIO() with self._filesystem.open_text_file_for_reading(self.path) as file: for line in file: if first_boilerplate_line_regexp.search(line): message_lines = self._wrap_lines(message) result.write(first_boilerplate_line_regexp.sub(message_lines, line)) # Remove all the ChangeLog boilerplate, except the first line (date, name, e-mail). removing_boilerplate = True elif removing_boilerplate: if re.search("^[1-9]", line): # each changelog entry is preceded by a date removing_boilerplate = False if not removing_boilerplate: result.write(line) self._filesystem.write_text_file(self.path, result.getvalue())
def write(self, msg=''): if not self._filter(msg): StringIO.write(self, msg)
def filter_branch(self, range, identifier_template=None, environment_shell=None): # We can't effectively mock the bash script in the command, but we can mock the python code that # script calls, which is where the program logic is. head, start = range.split('...') head = self.find(head) start = self.find(start) commits_to_edit = [] for commit in reversed(self.commits[head.branch]): if commit.branch == start.branch and commit.identifier <= start.identifier: break commits_to_edit.insert(0, commit) if head.branch != self.default_branch: for commit in reversed( self.commits[self.default_branch][:head.branch_point]): if commit.identifier <= start.identifier: break commits_to_edit.insert(0, commit) stdout = StringIO() original_env = { key: os.environ.get('OLDPWD') for key in [ 'OLDPWD', 'GIT_COMMIT', 'GIT_AUTHOR_NAME', 'GIT_AUTHOR_EMAIL', 'GIT_COMMITTER_NAME', 'GIT_COMMITTER_EMAIL', ] } try: count = 0 os.environ['OLDPWD'] = self.path for commit in commits_to_edit: count += 1 os.environ['GIT_COMMIT'] = commit.hash os.environ['GIT_AUTHOR_NAME'] = commit.author.name os.environ['GIT_AUTHOR_EMAIL'] = commit.author.email os.environ['GIT_COMMITTER_NAME'] = commit.author.name os.environ['GIT_COMMITTER_EMAIL'] = commit.author.email stdout.write( 'Rewrite {hash} ({count}/{total}) (--- seconds passed, remaining --- predicted)\n' .format( hash=commit.hash, count=count, total=len(commits_to_edit), )) if identifier_template: messagefile = StringIO() messagefile.write(commit.message) messagefile.seek(0) with OutputCapture() as captured: message_main(messagefile, identifier_template) lines = captured.stdout.getvalue().splitlines() if lines[-1].startswith('git-svn-id: https://svn'): lines.pop(-1) commit.message = '\n'.join(lines) if not environment_shell: continue if re.search(r'echo "Overwriting', environment_shell): stdout.write('Overwriting {}\n'.format(commit.hash)) match = re.search(r'(?P<json>\S+\.json)', environment_shell) if match: with OutputCapture() as captured: committer_main(match.group('json')) captured.stdout.seek(0) for line in captured.stdout.readlines(): line = line.rstrip() os.environ[line.split(' ')[0]] = ' '.join( line.split(' ')[1:]) commit.author = Contributor( name=os.environ['GIT_AUTHOR_NAME'], emails=[os.environ['GIT_AUTHOR_EMAIL']]) if re.search(r'echo "\s+', environment_shell): for key in [ 'GIT_AUTHOR_NAME', 'GIT_AUTHOR_EMAIL', 'GIT_COMMITTER_NAME', 'GIT_COMMITTER_EMAIL' ]: stdout.write(' {}={}\n'.format( key, os.environ[key])) finally: for key, value in original_env.items(): if value is not None: os.environ[key] = value else: del os.environ[key] return mocks.ProcessCompletion( returncode=0, stdout=stdout.getvalue(), )
def test_stringio(self): stream = StringIO() stream.write('string data') self.assertEqual(stream.getvalue(), 'string data')