-
Notifications
You must be signed in to change notification settings - Fork 11
/
auto_release_obs.py
137 lines (125 loc) · 4.73 KB
/
auto_release_obs.py
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
"""
OBS auto release helper
----------------------
"""
import argparse
import glob
import os
import re
import typing
from datetime import datetime
from pytz import _UTC
import requests
from osctiny import Osc
from osctiny.models.request import Action, ActionType, Source, Target
from osctiny.utils.changelog import Entry
def find_source(directory: str) -> typing.Union[str, None]:
"""
Find file name in directory
"""
file_path = glob.glob(f'{directory}/osc[-_]tiny-*.tar.gz')
if file_path:
return os.path.basename(file_path[0])
return None
class Obs:
"""
OBS instance
"""
project = "devel:languages:python"
target = "openSUSE:Factory"
package = "python-osc-tiny"
release_name = None
release_body = None
def __init__(self, **kwargs):
"""
Initialize the obs instance
"""
self.username = kwargs.get("username", "")
self.contact = kwargs.get("contact", "")
self.osc = Osc(
url="https://api.opensuse.org",
username=self.username,
password=kwargs.get("password", ""),
)
def get_latest_release(self) -> None:
"""
Get latest release name and body
"""
response = requests.get(
"https://api.github.com/repos/suse/osc-tiny/releases?per_page=1",
timeout=10)
releases = response.json()
self.release_name = releases[0]['tag_name'].strip('v')
self.release_body = releases[0]['body']
def modify_spec_file(self) -> None:
"""
Modify spec file
"""
version_pattern = r"\nVersion:\s+[\d\.]+\n"
response = self.osc.packages.get_file(self.project, self.package,
f"{self.package}.spec")
response.encoding = 'utf-8'
data = response.text.replace("osc-tiny-%{version}", "osc_tiny-%{version}")
data = re.sub(version_pattern, f"\nVersion: {self.release_name}\n", data)
self.osc.packages.push_file(self.project, self.package, f"{self.package}.spec", data)
def modify_changes_file(self) -> None:
"""
Modify change log
"""
response = self.osc.packages.get_file(self.project, self.package,
f"{self.package}.changes")
response.encoding = 'utf-8'
lines = [f" {line}" for line in self.release_body.strip().split("\r\n") if line]
content = f"- Release {self.release_name}\n" + "\n".join(lines)
entry = Entry(
packager=f"{self.username} <{self.contact}>",
content=content,
timestamp=datetime.now(tz=_UTC())
)
data = str(entry) + response.text
self.osc.packages.push_file(self.project, self.package,
f"{self.package}.changes", data)
def replace_source_file(self) -> None:
"""
Replace source file
"""
source_pattern = re.compile(r"osc[-_]tiny-[\d\.]+.tar.gz")
entries = self.osc.packages.get_files(self.project, self.package, expand=True)
files = [e.get("name") for e in entries.findall("entry")
if source_pattern.match(e.get("name"))]
old_file = files[0] if files else None
new_file = find_source("./dist")
if old_file and new_file:
self.osc.packages.delete_file(self.project, self.package, old_file)
with open(f"./dist/{new_file}", 'rb') as file:
self.osc.packages.push_file(self.project, self.package, new_file, data=file)
def commit(self) -> None:
"""
Commit and create submit request
"""
self.osc.packages.cmd(self.project, self.package, "commit",
comment=f"Release {self.release_name}")
actions = [
Action(
type=ActionType.SUBMIT,
source=Source(project=self.project, package=self.package),
target=Target(project=self.target, package=self.package)
)
]
self.osc.requests.create(actions=actions)
def __call__(self) -> None:
self.get_latest_release()
self.modify_spec_file()
self.modify_changes_file()
self.replace_source_file()
self.commit()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Auto release package to OBS.')
parser.add_argument('--username', required=True, help='Username for login')
parser.add_argument('--password', required=True, help='Password for login')
parser.add_argument('--contact', required=True, help='Email address of user')
args = parser.parse_args()
obs = Obs(username=args.username,
password=args.password,
contact=args.contact)
obs()