forked from packit/packit
/
package_config.py
353 lines (304 loc) · 13.3 KB
/
package_config.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# MIT License
#
# Copyright (c) 2018-2019 Red Hat, Inc.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import json
import logging
import os
from glob import glob
from pathlib import Path
from typing import Optional, List, Dict, Union
from yaml import safe_load
from ogr.abstract import GitProject
from packit.actions import ActionName
from packit.constants import CONFIG_FILE_NAMES, PROD_DISTGIT_URL
from packit.config.job_config import JobConfig, default_jobs
from packit.config.sync_files_config import SyncFilesConfig, SyncFilesItem
from packit.exceptions import PackitConfigException
logger = logging.getLogger(__name__)
class PullRequestNotificationsConfig:
""" Configuration of commenting on pull requests. """
def __init__(self, successful_build: bool = True):
self.successful_build = successful_build
class NotificationsConfig:
""" Configuration of notifications. """
def __init__(self, pull_request: PullRequestNotificationsConfig):
self.pull_request = pull_request
class PackageConfig:
"""
Config class for upstream/downstream packages;
this is the config people put in their repos
"""
def __init__(
self,
config_file_path: Optional[str] = None,
specfile_path: Optional[str] = None,
synced_files: Optional[SyncFilesConfig] = None,
jobs: Optional[List[JobConfig]] = None,
dist_git_namespace: str = None,
upstream_project_url: str = None, # can be URL or path
upstream_package_name: str = None,
downstream_project_url: str = None,
downstream_package_name: str = None,
dist_git_base_url: str = None,
create_tarball_command: List[str] = None,
current_version_command: List[str] = None,
actions: Dict[ActionName, Union[str, List[str]]] = None,
upstream_ref: Optional[str] = None,
allowed_gpg_keys: Optional[List[str]] = None,
create_pr: bool = True,
spec_source_id: str = "Source0",
upstream_tag_template: str = "{version}",
patch_generation_ignore_paths: List[str] = None,
notifications: Optional[NotificationsConfig] = None,
**kwargs,
):
self.config_file_path: Optional[str] = config_file_path
self.specfile_path: Optional[str] = specfile_path
self.synced_files: SyncFilesConfig = synced_files or SyncFilesConfig([])
self.patch_generation_ignore_paths = patch_generation_ignore_paths or []
self.jobs: List[JobConfig] = jobs or []
self.dist_git_namespace: str = dist_git_namespace or "rpms"
self.upstream_project_url: Optional[str] = upstream_project_url
self.upstream_package_name: Optional[str] = upstream_package_name
# this is generated by us
self.downstream_package_name: Optional[str] = downstream_package_name
self.dist_git_base_url: str = dist_git_base_url or PROD_DISTGIT_URL
self._downstream_project_url: str = downstream_project_url
# path to a local git clone of the dist-git repo; None means to clone in a tmpdir
self.dist_git_clone_path: Optional[str] = None
self.actions = actions or {}
self.upstream_ref: Optional[str] = upstream_ref
self.allowed_gpg_keys = allowed_gpg_keys
self.create_pr: bool = create_pr
self.spec_source_id: str = spec_source_id
self.notifications = notifications or NotificationsConfig(
pull_request=PullRequestNotificationsConfig()
)
# command to generate a tarball from the upstream repo
# uncommitted changes will not be present in the archive
self.create_tarball_command: List[str] = create_tarball_command
# command to get current version of the project
self.current_version_command: List[str] = current_version_command or [
"git",
"describe",
"--abbrev=0",
"--tags",
"--match",
"*",
]
# template to create an upstream tag name (upstream may use different tagging scheme)
self.upstream_tag_template = upstream_tag_template
if kwargs:
logger.warning(f"Following kwargs were not processed:" f"{kwargs}")
@property
def downstream_project_url(self) -> str:
if not self._downstream_project_url:
self._downstream_project_url = self.dist_git_package_url
return self._downstream_project_url
@property
def dist_git_package_url(self):
return (
f"{self.dist_git_base_url}{self.dist_git_namespace}/"
f"{self.downstream_package_name}.git"
)
@classmethod
def get_from_dict(
cls,
raw_dict: dict,
config_file_path: str = None,
repo_name: str = None,
spec_file_path: str = None,
) -> "PackageConfig":
# required to avoid cyclical imports
from packit.schema import PackageConfigSchema
if config_file_path and not raw_dict.get("config_file_path", None):
raw_dict.update(config_file_path=config_file_path)
package_config = PackageConfigSchema(strict=True).load(raw_dict).data
if not getattr(package_config, "specfile_path", None):
if spec_file_path:
package_config.specfile_path = spec_file_path
else:
raise PackitConfigException("Spec file was not found!")
if not getattr(package_config, "upstream_package_name", None) and repo_name:
package_config.upstream_package_name = repo_name
if not getattr(package_config, "downstream_package_name", None) and repo_name:
package_config.downstream_package_name = repo_name
if "jobs" not in raw_dict:
package_config.jobs = default_jobs
return package_config
def get_all_files_to_sync(self):
"""
Adds the default files (config file, spec file) to synced files when doing propose-update.
:return: SyncFilesConfig with default files
"""
files = self.synced_files.files_to_sync
if self.specfile_path not in (item.src for item in files):
files.append(SyncFilesItem(src=self.specfile_path, dest=self.specfile_path))
if self.config_file_path and self.config_file_path not in (
item.src for item in files
):
files.append(
SyncFilesItem(src=self.config_file_path, dest=self.config_file_path)
)
return SyncFilesConfig(files)
def __eq__(self, other: object):
if not isinstance(other, self.__class__):
return NotImplemented
logger.debug(f"our configuration:\n{self.__dict__}")
logger.debug(f"the other configuration:\n{other.__dict__}")
return (
self.specfile_path == other.specfile_path
and self.synced_files == other.synced_files
and self.jobs == other.jobs
and self.dist_git_namespace == other.dist_git_namespace
and self.upstream_project_url == other.upstream_project_url
and self.upstream_package_name == other.upstream_package_name
and self.downstream_project_url == other.downstream_project_url
and self.downstream_package_name == other.downstream_package_name
and self.dist_git_base_url == other.dist_git_base_url
and self.current_version_command == other.current_version_command
and self.create_tarball_command == other.create_tarball_command
and self.actions == other.actions
and self.allowed_gpg_keys == other.allowed_gpg_keys
and self.create_pr == other.create_pr
and self.spec_source_id == other.spec_source_id
and self.upstream_tag_template == other.upstream_tag_template
)
def get_local_package_config(
*directory,
repo_name: str = None,
try_local_dir_first=False,
try_local_dir_last=False,
) -> PackageConfig:
"""
:return: local PackageConfig if present
"""
directories = [Path(config_dir) for config_dir in directory]
cwd = Path.cwd()
if try_local_dir_first and try_local_dir_last:
logger.error("Ambiguous usage of try_local_dir_first and try_local_dir_last")
if try_local_dir_first:
if cwd in directories:
directories.remove(cwd)
directories.insert(0, cwd)
if try_local_dir_last:
if cwd in directories:
directories.remove(cwd)
directories.append(cwd)
for config_dir in directories:
for config_file_name in CONFIG_FILE_NAMES:
config_file_name_full = config_dir / config_file_name
if config_file_name_full.is_file():
logger.debug(f"Local package config found: {config_file_name_full}")
try:
loaded_config = safe_load(open(config_file_name_full))
except Exception as ex:
logger.error(
f"Cannot load package config '{config_file_name_full}'."
)
raise PackitConfigException(f"Cannot load package config: {ex}.")
return parse_loaded_config(
loaded_config=loaded_config,
config_file_path=str(config_file_name),
repo_name=repo_name,
spec_file_path=get_local_specfile_path(directories),
)
logger.debug(f"The local config file '{config_file_name_full}' not found.")
raise PackitConfigException("No packit config found.")
def get_package_config_from_repo(
sourcegit_project: GitProject, ref: str
) -> Optional[PackageConfig]:
for config_file_name in CONFIG_FILE_NAMES:
try:
config_file_content = sourcegit_project.get_file_content(
path=config_file_name, ref=ref
)
except FileNotFoundError:
# do nothing
pass
else:
logger.debug(
f"Found a config file '{config_file_name}' "
f"on ref '{ref}' "
f"of the {sourcegit_project.full_repo_name} repository."
)
break
else:
logger.warning(
f"No config file ({CONFIG_FILE_NAMES}) found on ref '{ref}' "
f"of the {sourcegit_project.full_repo_name} repository."
)
return None
try:
loaded_config = safe_load(config_file_content)
except Exception as ex:
logger.error(f"Cannot load package config {config_file_name!r}. {ex}")
raise PackitConfigException(
f"Cannot load package config {config_file_name!r}. {ex}"
)
return parse_loaded_config(
loaded_config=loaded_config,
config_file_path=config_file_name,
repo_name=sourcegit_project.repo,
spec_file_path=get_specfile_path_from_repo(sourcegit_project),
)
def parse_loaded_config(
loaded_config: dict,
config_file_path: str = None,
repo_name: str = None,
spec_file_path: str = None,
) -> PackageConfig:
"""Tries to parse the config to PackageConfig."""
logger.debug(f"Package config:\n{json.dumps(loaded_config, indent=4)}")
try:
package_config = PackageConfig.get_from_dict(
raw_dict=loaded_config,
config_file_path=config_file_path,
repo_name=repo_name,
spec_file_path=spec_file_path,
)
return package_config
except Exception as ex:
logger.error(f"Cannot parse package config. {ex}.")
raise PackitConfigException(f"Cannot parse package config: {ex}.")
def get_local_specfile_path(directories: Union[List[str], List[Path]]) -> Optional[str]:
"""
Get the relative path of the local spec file if present.
:param directories: dirs to find the spec file
:return: str relative path of the spec file
"""
for dir in directories:
files = [
os.path.relpath(path, dir) for path in glob(os.path.join(dir, "*.spec"))
]
if len(files) > 0:
return files[0]
return None
def get_specfile_path_from_repo(project: GitProject) -> Optional[str]:
"""
Get the path of the spec file in the given repo if present.
:param project: GitProject
:return: str path of the spec file or None
"""
spec_files = project.get_files(filter_regex=r".+\.spec$")
if not spec_files:
logger.debug(f"No spec file found in {project.full_repo_name}")
return None
return spec_files[0]