-
Notifications
You must be signed in to change notification settings - Fork 34
/
minify
executable file
·152 lines (127 loc) · 5.52 KB
/
minify
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
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
## SPDX-License-Identifier: BSD-2-Clause
## Copyright (c) 2021, Konrad Weihmann
import argparse
import logging
import os
import shutil
import tempfile
import textwrap
import git
from git.exc import GitCommandError
from packaging import version
MINIMUM_VERSION = "v1.24.0"
_new_publish = []
def cleanup_version(a):
return version.parse(a.split('_')[-1].lstrip("v"))
_ignore_branches = ['krogoth', 'pyro', 'rocko', 'sumo', 'thud']
_minimum_version = cleanup_version(MINIMUM_VERSION)
def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument("--excludes", default=os.path.join(os.path.dirname(
__file__), "..", ".minifyexcludes"), help="Source checkout of meta-sca")
parser.add_argument("source", help="Source checkout of meta-sca")
parser.add_argument("target", help="Target checkout of meta-sca-minified")
return parser.parse_args()
def get_excludes(excludefile):
_cnt = []
with open(excludefile) as i:
_cnt = [x.strip("\n") for x in i.readlines() if x]
return _cnt
def create_release_map(source, target, excludefile):
_list = {}
for branch in source.references:
if isinstance(branch, git.refs.remote.RemoteReference):
if branch.name in _ignore_branches:
continue
_tags_on_branch = source.git.tag(
'--sort=v:refname', '--merged', branch.name).split('\n')
for i, v in enumerate(_tags_on_branch):
if i >= len(_tags_on_branch) - 1:
continue
if cleanup_version(v) < _minimum_version:
continue
if cleanup_version(v) == _minimum_version:
v = MINIMUM_VERSION
_this_tag = v
_next_tag = _tags_on_branch[i+1]
_excludes = [':(exclude){}'.format(x)
for x in get_excludes(excludefile) if x]
_args = [_this_tag, _next_tag, '--binary']
if _excludes:
_args += ["--", *_excludes]
patch = source.git.diff(*_args)
if not patch.endswith("\n"):
patch += "\n"
_branch = [x.strip() for x in source.git.branch(
'--contains', _next_tag).split(' ') if x]
if len(_branch) > 1:
_branch = 'master'
elif any(_branch):
_branch = _branch[0]
else:
_branch = branch.name
_branch = _branch.replace("origin/", "", 1)
if _next_tag not in _list:
print("Adding new diff {}..{} on {}".format(_this_tag, _next_tag, _branch))
_list[_next_tag] = {
'patch': patch,
'branch': _branch,
'before': _this_tag
}
return _list
def apply_branch(branch, list, source, target):
global _new_publish
_releases = sorted(list.keys(), key=lambda k: cleanup_version(k))
if not any(_releases):
return
_initial_fork = [v['before']
for k, v in list.items() if k == _releases[0]][0]
if not any(x.name == branch for x in target.branches):
target.git.branch(branch, 'tags/' + _initial_fork)
_new_publish.append(branch)
target.git.checkout(branch)
for release in _releases:
_obj = [v for k, v in list.items() if k == release]
if any(_obj):
_obj = _obj[0]
else:
continue
_summary = "Release {rel}".format(rel=release)
_body = textwrap.dedent("""
{summary}
See https://github.com/priv-kweihmann/meta-sca/releases/tag/{rel} for details
""".format(summary=_summary, rel=release))
_commits = target.iter_commits(branch)
if not any(x.summary == _summary for x in _commits):
print("Applying release {} [{}..]".format(release, _obj['before']))
with tempfile.NamedTemporaryFile(mode="w") as o:
o.write(_obj["patch"])
o.flush()
shutil.copy(o.name, "/tmp/last.patch")
try:
target.git.apply('--whitespace=nowarn', o.name)
target.git.add("*")
target.git.commit('-m', _body, author="kweihmann@outlook.com")
target.git.tag("-f", "-a", release, "-m", _summary)
_new_publish.append(release)
except GitCommandError as e:
print('Something went wrong: {}\n\nPatch saved at /tmp/last.patch'.format(e))
try:
_args = create_parser()
_source_repo = git.Repo(path=_args.source)
_target_repo = git.Repo(path=_args.target)
print("Create release map")
_list = create_release_map(_source_repo, _target_repo, _args.excludes)
_branches = ['master'] + list(set([v['branch']
for x, v in _list.items() if v['branch'] != 'master']))
for branch in _branches:
print("Update branch {}".format(branch))
apply_branch(branch, {k: v for k, v in _list.items()
if v['branch'] == branch}, _source_repo, _target_repo)
print("Publish new: {}".format(",".join(sorted(_new_publish))))
print("Update branches: {}".format(",".join(sorted(_branches))))
_target_repo.git.push('origin', *_new_publish)
_target_repo.git.push('origin', *_branches)
except Exception as e:
logging.exception(msg="Something went wrong", exc_info=e)