def _grab_history(self): """Calculate the needed history/changelog changes Every history heading looks like '1.0 b4 (1972-12-25)'. Extract them, check if the first one matches the version and whether it has a the current date. """ history_file = self.vcs.history_file() if not history_file: logger.warn("No history file found") self.data['history_lines'] = None self.data['history_file'] = None return logger.debug("Checking %s", history_file) history_lines = open(history_file).read().split('\n') # ^^^ TODO: .readlines()? headings = utils.extract_headings_from_history(history_lines) if not len(headings): logger.error("No detectable version heading in the history " "file %s", history_file) sys.exit() good_heading = self.data['history_header'] % self.data # ^^^ history_header is a string with %(abc)s replacements. line = headings[0]['line'] previous = history_lines[line] history_lines[line] = good_heading logger.debug("Set heading from %r to %r.", previous, good_heading) history_lines[line + 1] = utils.fix_rst_heading( heading=good_heading, below=history_lines[line + 1]) logger.debug("Set line below heading to %r", history_lines[line + 1]) self.data['history_lines'] = history_lines self.data['history_file'] = history_file
def _grab_history(self): """Calculate the needed history/changelog changes Every history heading looks like '1.0 b4 (1972-12-25)'. Extract them, check if the first one matches the version and whether it has a the current date. """ default_location = None config = self.setup_cfg.config if config and config.has_option('zest.releaser', 'history_file'): default_location = config.get('zest.releaser', 'history_file') history_file = self.vcs.history_file(location=default_location) if not history_file: logger.warn("No history file found") self.data['history_lines'] = None self.data['history_file'] = None self.data['history_encoding'] = None return logger.debug("Checking %s", history_file) history_lines, history_encoding = read_text_file(history_file) history_lines = history_lines.split('\n') headings = utils.extract_headings_from_history(history_lines) if not len(headings): logger.error("No detectable version heading in the history " "file %s", history_file) sys.exit(1) good_heading = self.data['history_header'] % self.data # ^^^ history_header is a string with %(abc)s replacements. line = headings[0]['line'] previous = history_lines[line] history_lines[line] = good_heading logger.debug("Set heading from %r to %r.", previous, good_heading) history_lines[line + 1] = utils.fix_rst_heading( heading=good_heading, below=history_lines[line + 1]) logger.debug("Set line below heading to %r", history_lines[line + 1]) self.data['history_lines'] = history_lines self.data['history_file'] = history_file self.data['history_encoding'] = history_encoding # TODO: add line number where an extra changelog entry can be # inserted. # Look for 'Nothing changed yet' under the latest header. Not # nice if this text ends up in the changelog. Did nothing happen? start = headings[0]['line'] if len(headings) > 1: end = headings[1]['line'] else: end = -1 for line in history_lines[start:end]: if self.data['nothing_changed_yet'] in line: if not utils.ask( "WARNING: Changelog contains %r. Are you sure you " "want to release?" % self.data['nothing_changed_yet'], default=False): logger.info("You can use the 'lasttaglog' command to " "see the commits since the last tag.") sys.exit(0) break
def _grab_history(self): """Calculate the needed history/changelog changes Every history heading looks like '1.0 b4 (1972-12-25)'. Extract them, check if the first one matches the version and whether it has a the current date. """ default_location = None config = self.setup_cfg.config if config and config.has_option('zest.releaser', 'history_file'): default_location = config.get('zest.releaser', 'history_file') history_file = self.vcs.history_file(location=default_location) if not history_file: logger.warn("No history file found") self.data['history_lines'] = None self.data['history_file'] = None self.data['history_encoding'] = None return logger.debug("Checking %s", history_file) history_lines, history_encoding = read_text_file(history_file) history_lines = history_lines.split('\n') headings = utils.extract_headings_from_history(history_lines) if not len(headings): logger.error( "No detectable version heading in the history " "file %s", history_file) sys.exit(1) good_heading = self.data['history_header'] % self.data # ^^^ history_header is a string with %(abc)s replacements. line = headings[0]['line'] previous = history_lines[line] history_lines[line] = good_heading logger.debug("Set heading from %r to %r.", previous, good_heading) history_lines[line + 1] = utils.fix_rst_heading( heading=good_heading, below=history_lines[line + 1]) logger.debug("Set line below heading to %r", history_lines[line + 1]) self.data['history_lines'] = history_lines self.data['history_file'] = history_file self.data['history_encoding'] = history_encoding # TODO: add line number where an extra changelog entry can be # inserted. # Look for 'Nothing changed yet' under the latest header. Not # nice if this text ends up in the changelog. Did nothing happen? start = headings[0]['line'] if len(headings) > 1: end = headings[1]['line'] else: end = -1 for line in history_lines[start:end]: if self.data['nothing_changed_yet'] in line: if not utils.ask( "WARNING: Changelog contains %r. Are you sure you " "want to release?" % self.data['nothing_changed_yet'], default=False): logger.info("You can use the 'lasttaglog' command to " "see the commits since the last tag.") sys.exit(0) break
def _grab_history(self): """Calculate the needed history/changelog changes Every history heading looks like '1.0 b4 (1972-12-25)'. Extract them, check if the first one matches the version and whether it has a the current date. """ default_location = None config = self.setup_cfg.config if config and config.has_option('zest.releaser', 'history_file'): default_location = config.get('zest.releaser', 'history_file') history_file = self.vcs.history_file(location=default_location) if not history_file: logger.warn("No history file found") self.data['history_lines'] = None self.data['history_file'] = None return logger.debug("Checking %s", history_file) history_lines = open(history_file).read().split('\n') # ^^^ TODO: .readlines()? headings = utils.extract_headings_from_history(history_lines) if not len(headings): logger.error("No detectable version heading in the history " "file %s", history_file) sys.exit() good_heading = self.data['history_header'] % self.data # ^^^ history_header is a string with %(abc)s replacements. line = headings[0]['line'] previous = history_lines[line] history_lines[line] = good_heading logger.debug("Set heading from %r to %r.", previous, good_heading) history_lines[line + 1] = utils.fix_rst_heading( heading=good_heading, below=history_lines[line + 1]) logger.debug("Set line below heading to %r", history_lines[line + 1]) self.data['history_lines'] = history_lines self.data['history_file'] = history_file
def _change_header(self, add=False): """Change the latest header. Change the version number and the release date or development status. @add: - False: edit current header (prerelease) - True: add header (postrelease) """ if self.data['history_file'] is None: return good_heading = self.data['history_header'] % self.data # ^^^ history_header is a string with %(abc)s replacements. headings = self.data['headings'] history_lines = self.data['history_lines'] previous = '' underline_char = '-' empty = False if not history_lines: # Remember that we were empty to start with. empty = True # prepare header line history_lines.append('') if len(history_lines) <= 1: # prepare underline history_lines.append(underline_char) if not headings: # Mock a heading headings = [{'line': 0}] inject_location = 0 first = headings[0] inject_location = first['line'] underline_line = first['line'] + 1 try: underline_char = history_lines[underline_line][0] except IndexError: logger.debug("No character on line below header.") underline_char = '-' previous = history_lines[inject_location] if add: inject = [ good_heading, underline_char * len(good_heading), '', self.data['nothing_changed_yet'], '', '', ] if empty: history_lines = [] history_lines[inject_location:inject_location] = inject else: # edit current line history_lines[inject_location] = good_heading logger.debug("Set heading from %r to %r.", previous, good_heading) history_lines[underline_line] = utils.fix_rst_heading( heading=good_heading, below=history_lines[underline_line]) logger.debug("Set line below heading to %r", history_lines[underline_line]) # Setting history_lines is not needed, except when we have replaced the # original instead of changing it. So just set it. self.data['history_lines'] = history_lines
def _change_header(self, add=False): """Change the latest header. Change the version number and the release date or development status. @add: - False: edit current header (prerelease or bumpversion) - True: add header (postrelease) But in some cases it may not be wanted to change a header, especially when towncrier is used to handle the history. Otherwise we could be changing a date of an already existing release. What we want to avoid: - change a.b.c (1999-12-32) to x.y.z (unreleased) [bumpversion] - change a.b.c (1999-12-32) to x.y.z (today) [prerelease] But this is fine: - change a.b.c (unreleased) to x.y.z (unreleased) [bumpversion] - change a.b.c (unreleased) to a.b.c (today) [prerelease] - change a.b.c (unreleased) to x.y.z (today) [prerelease] """ if self.data['history_file'] is None: return good_heading = self.data['history_header'] % self.data if not add and self.data.get('has_released_header'): # So we are editing a line, but it already has a release date. logger.warning( 'Refused to edit the first history heading, because it looks ' 'to be from an already released version with a date. ' 'Would have wanted to set this header: %s', good_heading) return # ^^^ history_header is a string with %(abc)s replacements. headings = self.data['headings'] history_lines = self.data['history_lines'] previous = '' underline_char = '-' empty = False if not history_lines: # Remember that we were empty to start with. empty = True # prepare header line history_lines.append('') if len(history_lines) <= 1: # prepare underline history_lines.append(underline_char) if not headings: # Mock a heading headings = [{'line': 0}] inject_location = 0 first = headings[0] inject_location = first['line'] underline_line = first['line'] + 1 try: underline_char = history_lines[underline_line][0] except IndexError: logger.debug("No character on line below header.") underline_char = '-' previous = history_lines[inject_location] if add: inject = [ good_heading, underline_char * len(good_heading), '', self.data['nothing_changed_yet'], '', '', ] if empty: history_lines = [] history_lines[inject_location:inject_location] = inject else: # edit current line history_lines[inject_location] = good_heading logger.debug("Set heading from '%s' to '%s'.", previous, good_heading) history_lines[underline_line] = utils.fix_rst_heading( heading=good_heading, below=history_lines[underline_line]) logger.debug("Set line below heading to '%s'", history_lines[underline_line]) # Setting history_lines is not needed, except when we have replaced the # original instead of changing it. So just set it. self.data['history_lines'] = history_lines