forked from SUSE/osc-tiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_release_obs.py
153 lines (140 loc) · 4.86 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
OBS auto release helper
----------------------
"""
import argparse
import os
import tempfile
import typing
from datetime import datetime
from pathlib import Path
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 get_latest_release() -> typing.Tuple[str, str]:
"""
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()
return (releases[0]['tag_name'].strip('v'), releases[0]['body'])
def find_file(directory: str, suffix: str) -> str:
"""
Find file name in directory
"""
for _, _, files in os.walk(directory):
for item in files:
if item.endswith(suffix):
return item
return ""
def read_file(filename: str) -> str:
"""
Read file content
"""
with open(filename, 'r') as fh:
content = fh.readlines()
return content
class Obs:
"""
OBS instance
"""
pproject = "openSUSE:Factory"
package = "python-osc-tiny"
project = None
release_name = None
release_body = None
def __init__(self, **kwargs):
"""
Initialize the obs instance
"""
self.username = kwargs.get("username", "")
self.osc = Osc(
url="https://api.opensuse.org",
username=self.username,
password=kwargs.get("password", ""),
)
self.project = f"home:{self.username}:branches:devel:languages:python"
self.destdir = Path(tempfile.mkdtemp())
def branchco(self):
"""
Branch from parent project and checkout paclage
"""
params = {"force": 1,
"noservice": 1,
"autocleanup": 1,
"target_project": self.project}
# Branch from parent project
self.osc.packages.cmd(self.pproject, self.package, "branch", **params)
self.osc.packages.checkout(self.project, self.package, self.destdir)
self.release_name, self.release_body = get_latest_release()
def modify_spec_file(self) -> None:
"""
Modify spec file
"""
lines = read_file(f"{self.destdir}/{self.package}.spec")
data = ""
for line in lines:
if line.startswith("Version:"):
data += f"Version: {self.release_name}\n"
else:
data += line.replace("osc-tiny-%{version}", "osc_tiny-%{version}")
self.osc.packages.push_file(self.project, self.package, f"{self.package}.spec", data)
def modify_changes_file(self) -> None:
"""
Modify change log
"""
lines = read_file(f"{self.destdir}/{self.package}.changes")
content = f"- Release {self.release_name}\n"
for line in self.release_body.split("\r\n"):
content += f" {line}\n"
entry = Entry(
packager=self.username,
content=content,
timestamp=datetime.now(tz=_UTC())
)
data = str(entry)
for line in lines:
data += line
self.osc.packages.push_file(self.project, self.package,
f"{self.package}.changes", data)
def replace_source_file(self) -> None:
"""
Replace source file
"""
self.osc.packages.delete_file(self.project, self.package,
find_file(self.destdir, ".tar.gz"))
new_file = find_file("./dist", ".tar.gz")
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.pproject, package=self.package)
target=Target(project="home:ChHuang", package=self.package)
)
]
self.osc.requests.create(actions=actions)
def __call__(self) -> None:
self.branchco()
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')
args = parser.parse_args()
obs = Obs(username=args.username, password=args.password)
obs()