-
Notifications
You must be signed in to change notification settings - Fork 34
/
cockpit-lib-update
executable file
·93 lines (75 loc) · 3.34 KB
/
cockpit-lib-update
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
#!/usr/bin/env python3
# This file is part of Cockpit.
#
# Copyright (C) 2023 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
# Update COCKPIT_REPO_COMMIT to cockpit HEAD automatically, defaults to
# Makefile as input optionally the full path can be provided. (For example
# Anaconda uses ui/webui/Makefile.am).
import os
import re
import subprocess
import sys
import tempfile
from pathlib import Path
import task
from lib.constants import BASE_DIR
sys.dont_write_bytecode = True
GIT_URL_RE = r'COCKPIT_REPO_URL\s*=\s*(.*)'
GIT_COMMIT_RE = r'COCKPIT_REPO_COMMIT\s*=\s*(.*)'
def run(context, verbose=False, **kwargs):
cockpit_repo_url = 'https://github.com/cockpit-project/cockpit.git'
cockpit_repo_commit = 'HEAD'
makefile = context or 'Makefile'
makefile_path = os.path.join(BASE_DIR, makefile)
with open(makefile_path) as fp:
content = fp.read()
m = re.search(GIT_URL_RE, content)
if m:
cockpit_repo_url = m.group(1)
m = re.search(GIT_COMMIT_RE, content)
if m:
cockpit_repo_commit = m.group(1)
# Figure out latest cockpit tip commit
with tempfile.TemporaryDirectory('cockpit-repo') as tmpdir:
tmpdir = Path(tmpdir)
clone_dir = 'cockpit'
commit = cockpit_repo_commit.partition('#')[0].strip()
subprocess.check_call(['git', 'clone', cockpit_repo_url, clone_dir], cwd=tmpdir)
git_describe = subprocess.check_output(['git', 'describe'], cwd=tmpdir / clone_dir).decode().strip()
git_head = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=tmpdir / clone_dir).decode().strip()
git_shortlog = subprocess.check_output(['git', 'shortlog', f'{commit}...', '--',
'pkg/lib', 'test/common', 'test/static-code', 'tools/node-modules'],
cwd=tmpdir / clone_dir).decode().strip()
try:
# when HEAD is not tagged, this looks like "290-9-g4a6d86f5b"
tag, commits, _ = git_describe.split('-')
comment = f'{git_head} # {tag} + {commits} commits'
except ValueError:
# when HEAD is tagged, use that name
comment = f'{git_head} # {git_describe}'
new_content = content.replace(cockpit_repo_commit, comment)
if content == new_content:
print("COCKPIT_REPO_COMMIT is already up to date, nothing to do")
return
with open(makefile_path, 'w') as fp:
fp.write(new_content)
title = f"Makefile: Update Cockpit lib to {git_head[:32]}"
branch = task.branch('cockpit-lib', title, pathspec=makefile, **kwargs)
kwargs["title"] = title
kwargs["body"] = git_shortlog
task.pull(branch, **kwargs)
if __name__ == '__main__':
task.main(function=run, title="Update COCKPIT_REPO_COMMIT for cockpit projects")