#!/usr/bin/env python2 from __future__ import unicode_literals import datetime import os import subprocess from contextlib import contextmanager import frontmatter PROJECT_DIR = os.path.abspath(os.path.dirname(__file__)) CHANGELOG_SITE_DIR = os.path.join(PROJECT_DIR, "site/changelog/") CONTENT_DIR = os.path.join(CHANGELOG_SITE_DIR, "content/") RELEASED_DIR = os.path.join(CONTENT_DIR, "released/") EXPERIMENTAL_DIR = os.path.join(CONTENT_DIR, "experimental/") INTRO_SAMPLE = "" @contextmanager def project_dir(): owd = os.getcwd() try: os.chdir(PROJECT_DIR) yield PROJECT_DIR finally: os.chdir(owd) def main(): changes = "" for item in os.listdir(EXPERIMENTAL_DIR): if item.endswith(".md"): changes = changes + process_change(os.path.join(EXPERIMENTAL_DIR, item)) elif item == ".gitignore": pass else: print("Unexpected file: %s" % item) if changes: human_version = release(changes) commit_release(human_version) else: print("No changes") def process_change(path): """ Returns all changes from the file """ print("Processing %s..." % path) with open(path, "rb") as md_file: data = frontmatter.load(md_file) os.remove(path) print("Removed: %s" % path) return data.content.replace(INTRO_SAMPLE, "") def release(changes): """ Release version - move from experimental/ to released/. Returns human version - the one suitable for tagging and displaying in changelog """ print("Releasing changes...") current_internal_version = get_current_internal_version() human_version = increase_human_version(current_internal_version) data = frontmatter.loads(changes) data["weight"] = current_internal_version + 1 data["subtitle"] = datetime.datetime.now().strftime("released on %Y-%m-%d") data["draft"] = False data["date"] = datetime.datetime.now() # 2018-10-02T09:29:50Z data["version"] = human_version print("Assigning new version: %s / %s" % (data["weight"], data["version"])) released_file_path = os.path.join(RELEASED_DIR, "%s.md" % data["weight"]) with open(released_file_path, "wb") as released_file: frontmatter.dump(data, released_file) print("Created: %s" % released_file_path) return human_version def commit_release(human_version): """ Autogenerates commit and tag """ with project_dir(): subprocess.call(["git", "add", CONTENT_DIR]) subprocess.call(["git", "commit", "-m", "Generated by release script"]) subprocess.call(["git", "fetch", "--tags"]) subprocess.call(["git", "tag", "-d", "%s" % human_version]) subprocess.call(["git", "tag", "%s" % human_version]) def get_current_internal_version(): """ Get current version from files in released/ folder (weight/filename) """ version = 0 files = os.listdir(RELEASED_DIR) files.sort(reverse=True, key=natural_sort) for item in files: if item.endswith(".md"): version = int(item[:-3]) break return version def increase_human_version(internal_version): """ Increase human version from previous file """ current_human_version = get_current_human_version(internal_version) ma, mi = current_human_version.split(".") return "%s.%s" % (ma, int(mi) + 1) def get_current_human_version(internal_version): """ Returns version from frontmatter (used for tagging and displaying) """ md_path = os.path.join(RELEASED_DIR, "%s.md" % internal_version) with open(md_path, "rb") as md_file: data = frontmatter.load(md_file) return data["version"].strip() def print_release_version(): """ Returns release version """ version = get_current_human_version(get_current_internal_version()) is_experimental = len([x for x in os.listdir(EXPERIMENTAL_DIR) if x.endswith(".md")]) > 0 print("%s%s" % (version, "+" if is_experimental else "")) def natural_sort(el): try: v = int(el[:-3]) except ValueError: v = 0 return v if __name__ == "__main__": main()