def update_changelog(inplace_file: str, marker: str, version_regex: str) -> None: """ Update the given changelog file in place. Arguments: inplace_file: The file to update in-place. marker: The line after which to insert new contents. version_regex: A regular expression to find currently documented versions in the file. """ env = SandboxedEnvironment(autoescape=True) template = env.from_string(httpx.get(TEMPLATE_URL).text) changelog = Changelog(".", style=COMMIT_STYLE) if len(changelog.versions_list) == 1: last_version = changelog.versions_list[0] if last_version.planned_tag is None: planned_tag = "v0.1.0" last_version.tag = planned_tag last_version.url += planned_tag last_version.compare_url = last_version.compare_url.replace( "HEAD", planned_tag) lines = read_changelog(inplace_file) last_released = latest(lines, re.compile(version_regex)) if last_released: changelog.versions_list = unreleased(changelog.versions_list, last_released) rendered = template.render(changelog=changelog, inplace=True) lines[lines.index(marker)] = rendered write_changelog(inplace_file, lines)
def update_changelog( inplace_file: str, marker: str, version_regex: str, template_url: str, commit_style: str, ) -> None: """Update the given changelog file in place. Arguments: inplace_file: The file to update in-place. marker: The line after which to insert new contents. version_regex: A regular expression to find currently documented versions in the file. template_url: The URL to the Jinja template used to render contents. commit_style: The style of commit messages to parse. """ env = SandboxedEnvironment(autoescape=False) template = env.from_string(httpx.get(template_url).text) changelog = Changelog(".", style=commit_style) # noqa: W0621 (shadowing changelog) if len(changelog.versions_list) == 1: last_version = changelog.versions_list[0] if last_version.planned_tag is None: planned_tag = "0.1.0" last_version.tag = planned_tag last_version.url += planned_tag last_version.compare_url = last_version.compare_url.replace("HEAD", planned_tag) lines = read_changelog(inplace_file) last_released = latest(lines, re.compile(version_regex)) if last_released: changelog.versions_list = unreleased(changelog.versions_list, last_released) rendered = template.render(changelog=changelog, inplace=True) lines[lines.index(marker)] = rendered write_changelog(inplace_file, lines)
def update_changelog( inplace_file: str, marker: str = MARKER, version_regex: str = VERSION_REGEX, template_filepath: str = CHANGELOG_TEMPLATE, commit_style: str = "angular", ): env = SandboxedEnvironment(autoescape=False) changelog = Changelog(".", style=commit_style) template = env.from_string(read_template(template_filepath)) if len(changelog.versions_list) == 1: last_version = changelog.versions_list[0] if last_version.planned_tag is None: planned_tag = "0.1.0" last_version.tag = planned_tag last_version.url += planned_tag last_version.compare_url = last_version.compare_url.replace( "HEAD", planned_tag) lines = read_changelog(inplace_file) last_released = latest_version(lines, re.compile(version_regex)) if last_released: changelog.versions_list = unreleased_versions(changelog.versions_list, last_released) rendered = template.render(changelog=changelog, inplace=True) lines[lines.index(marker)] = rendered write_changelog(inplace_file, lines)
def update_changelog( inplace_file: str, marker: str, version_regex: str, template_url: str, ) -> None: """ Update the given changelog file in place. Arguments: inplace_file: The file to update in-place. marker: The line after which to insert new contents. version_regex: A regular expression to find currently documented versions in the file. template_url: The URL to the Jinja template used to render contents. """ from git_changelog.build import Changelog from git_changelog.commit import AngularStyle from jinja2.sandbox import SandboxedEnvironment AngularStyle.DEFAULT_RENDER.insert(0, AngularStyle.TYPES["build"]) env = SandboxedEnvironment(autoescape=False) template_text = urlopen(template_url).read().decode("utf8") # noqa: S310 template = env.from_string(template_text) changelog = Changelog(".", style="angular") if len(changelog.versions_list) == 1: last_version = changelog.versions_list[0] if last_version.planned_tag is None: planned_tag = "0.1.0" last_version.tag = planned_tag last_version.url += planned_tag last_version.compare_url = last_version.compare_url.replace( "HEAD", planned_tag) with open(inplace_file, "r") as changelog_file: lines = changelog_file.read().splitlines() last_released = _latest(lines, re.compile(version_regex)) if last_released: changelog.versions_list = _unreleased(changelog.versions_list, last_released) rendered = template.render(changelog=changelog, inplace=True) lines[lines.index(marker)] = rendered with open(inplace_file, "w") as changelog_file: # noqa: WPS440 changelog_file.write("\n".join(lines).rstrip("\n") + "\n")
def main(args: Optional[List[str]] = None) -> int: """ Run the main program. This function is executed when you type `git-changelog` or `python -m git_changelog`. Arguments: args: Arguments passed from the command line. Returns: An exit code. """ parser = get_parser() args = parser.parse_args(args=args) # get template if args.template.startswith("path:"): path = args.template.replace("path:", "", 1) try: template = templates.get_custom_template(path) except FileNotFoundError: print("git-changelog: no such directory, " "or missing changelog.md: %s" % path, file=sys.stderr) return 1 else: template = templates.get_template(args.template) # build data changelog = Changelog(args.repository, style=args.style) # get rendered contents rendered = template.render(changelog=changelog) # write result in specified output if args.output is sys.stdout: sys.stdout.write(rendered) else: with open(args.output, "w") as stream: stream.write(rendered) return 0
import requests from git_changelog.build import Changelog from jinja2.sandbox import SandboxedEnvironment TEMPLATE_URL = "https://raw.githubusercontent.com/pawamoy/jinja-templates/master/keepachangelog.md" COMMIT_STYLE = "angular" if __name__ == "__main__": if len(sys.argv) != 4: print("usage: update_changelog.py <FILE> <MARKER> <VERSION_REGEX>", file=sys.stderr) sys.exit(1) env = SandboxedEnvironment(autoescape=True) template = env.from_string(requests.get(TEMPLATE_URL).text) changelog = Changelog(".", style=COMMIT_STYLE) inplace_file, marker, version_regex = sys.argv[1:] with open(inplace_file, "r") as fd: old_lines = fd.read().splitlines(keepends=False) # get last version version_re = re.compile(version_regex) last_released = None for line in old_lines: match = version_re.search(line) if match: last_released = match.groupdict()["version"] break # only keep more recent versions versions = [] for version in changelog.versions_list: if version.tag == last_released: