Welcome to mirror list, hosted at ThFree Co, Russian Federation.

release.py - github.com/jsnjack/hugo-changelog-theme.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0ab50d847cf436427e24b0ac9f1224f4709d0b53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/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 = "<!-- Available tags are: added, changed, deprecated, removed, fixed, performance, security -->"


@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()