def insert_msg_into_history(msg): vcs = zest.releaser.choose.version_control() filename = vcs.history_file() if not filename: return lines = open(filename).read().splitlines() headings = utils.extract_headings_from_history(lines) if not headings: return # Hardcoded zest assumptions. target_line = headings[0]['line'] + 3 if len(lines) < target_line: return if 'nothing changed yet' in lines[target_line].lower(): del lines[target_line] # Msg formatting: msg = msg[:] msg = [' %s' % line for line in msg] msg += [''] firstline = msg[0] firstline = '-' + firstline[1:] msg[0] = firstline lines[target_line:target_line] = msg contents = '\n'.join(lines) # Add newline at end of file: contents += '\n' open(filename, 'w').write(contents) logger.debug("Written change to history file")
def _update_history(self): """Update the history file""" version = self.data['new_version'] history = self.vcs.history_file() if not history: logger.warn("No history file found") return history_lines = read_text_file(history).split('\n') headings = utils.extract_headings_from_history(history_lines) if not len(headings): logger.warn("No detectable existing version headings in the " "history file.") inject_location = 0 underline_char = '-' else: 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 = '-' header = '%s (unreleased)' % version inject = [header, underline_char * len(header), '', self.data['nothing_changed_yet'], '', ''] history_lines[inject_location:inject_location] = inject contents = '\n'.join(history_lines) write_text_file(history, contents) logger.info("Injected new section into the history: %r", header)
def _update_history(self): """Update the history file""" version = self.data['new_version'] history = self.vcs.history_file() if not history: logger.warn("No history file found") return history_lines, history_encoding = read_text_file(history) history_lines = history_lines.split('\n') headings = utils.extract_headings_from_history(history_lines) if not len(headings): logger.warn("No detectable existing version headings in the " "history file.") inject_location = 0 underline_char = '-' else: 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 = '-' header = '%s (unreleased)' % version inject = [header, underline_char * len(header), '', self.data['nothing_changed_yet'], '', ''] history_lines[inject_location:inject_location] = inject contents = '\n'.join(history_lines) write_text_file(history, contents, history_encoding) logger.info("Injected new section into the history: %r", header)
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. """ self.data['history_lines'] = [] self.data['history_file'] = None self.data['history_encoding'] = None self.data['headings'] = [] self.data['history_last_release'] = '' self.data['history_insert_line_here'] = 0 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) self.data['history_file'] = history_file if not history_file: logger.warn("No history file found") 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 headings: logger.warn( "No detectable version heading in the history " "file %s", history_file) return self.data['history_lines'] = history_lines self.data['history_encoding'] = history_encoding self.data['headings'] = headings # Grab last header. start = headings[0]['line'] if len(headings) > 1: # Include the next header plus underline, as this is nice # to show in the history_last_release. end = headings[1]['line'] + 2 else: end = len(history_lines) history_last_release = '\n'.join(history_lines[start:end]) self.data['history_last_release'] = history_last_release # Add line number where an extra changelog entry can be inserted. Can # be useful for entry points. 'start' is the header, +1 is the # underline, +2 is probably an empty line, so then we should take +3. # Or rather: the first non-empty line. insert = start + 2 while insert < end: if history_lines[insert].strip(): break insert += 1 self.data['history_insert_line_here'] = insert
def extractHeadings(history_lines): if not history_lines: logger.warn("No history file found") return headings = utils.extract_headings_from_history(history_lines) if not len(headings): logger.warn("No detectable existing version headings in the " "history file.") return return headings
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. """ self.data['history_lines'] = [] self.data['history_file'] = None self.data['history_encoding'] = None self.data['headings'] = [] self.data['history_last_release'] = '' self.data['history_insert_line_here'] = 0 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) self.data['history_file'] = history_file if not history_file: logger.warn("No history file found") 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 headings: logger.warn("No detectable version heading in the history " "file %s", history_file) return self.data['history_lines'] = history_lines self.data['history_encoding'] = history_encoding self.data['headings'] = headings # Grab last header. start = headings[0]['line'] if len(headings) > 1: # Include the next header plus underline, as this is nice # to show in the history_last_release. end = headings[1]['line'] + 2 else: end = len(history_lines) history_last_release = '\n'.join(history_lines[start:end]) self.data['history_last_release'] = history_last_release # Add line number where an extra changelog entry can be inserted. Can # be useful for entry points. 'start' is the header, +1 is the # underline, +2 is probably an empty line, so then we should take +3. # Or rather: the first non-empty line. insert = start + 2 while insert < end: if history_lines[insert].strip(): break insert += 1 self.data['history_insert_line_here'] = insert
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 _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. """ self.data['history_lines'] = [] self.data['history_file'] = None self.data['history_encoding'] = None self.data['headings'] = [] self.data['history_last_release'] = '' self.data['history_insert_line_here'] = 0 default_location = self.pypiconfig.history_file() fallback_encoding = self.pypiconfig.encoding() history_file = self.vcs.history_file(location=default_location) self.data['history_file'] = history_file if not history_file: logger.warning("No history file found") return logger.debug("Checking %s", history_file) history_lines, history_encoding = read_text_file( history_file, fallback_encoding=fallback_encoding, ) headings = utils.extract_headings_from_history(history_lines) if not headings: logger.warning( "No detectable version heading in the history " "file %s", history_file) return self.data['history_lines'] = history_lines self.data['history_encoding'] = history_encoding self.data['headings'] = headings # Grab last header. start = headings[0]['line'] if len(headings) > 1: # Include the next header plus underline, as this is nice # to show in the history_last_release. end = headings[1]['line'] + 2 else: end = len(history_lines) history_last_release = '\n'.join(history_lines[start:end]) self.data['history_last_release'] = history_last_release # Does the latest release header have a release date in it? # This is useful to know, especially when using tools like towncrier # to handle the changelog. released = DATE_PATTERN.match(headings[0]['date']) self.data['has_released_header'] = released # Add line number where an extra changelog entry can be inserted. Can # be useful for entry points. 'start' is the header, +1 is the # underline, +2 is probably an empty line, so then we should take +3. # Or rather: the first non-empty line. insert = start + 2 while insert < end: if history_lines[insert].strip(): break insert += 1 self.data['history_insert_line_here'] = insert