99from typing import Optional
1010
1111import tmt .log
12+ import tmt .utils
13+ from tmt .container import container
1214from tmt .guest import Guest
1315from tmt .steps .prepare .artifact .providers import (
1416 ArtifactInfo ,
1517 ArtifactProvider ,
1618 ArtifactProviderId ,
19+ Repository ,
1720 UnsupportedOperationError ,
1821 provides_artifact_provider ,
1922)
2023from tmt .utils import Path
2124
25+ COPR_REPO_PATTERN = re .compile (r'^(?P<group>@)?(?P<name>[^/]+)/(?P<project>[^/]+)$' )
2226COPR_REPOSITORY_PATTERN = re .compile (r'^(?:@[^/]+/[^/]+|[^@/]+/[^/]+)$' )
2327
2428
29+ @container (frozen = True )
30+ class CoprRepo :
31+ is_group : bool
32+ name : str
33+ project : str
34+
35+
36+ def parse_copr_repo (copr_repo : str ) -> CoprRepo :
37+ """
38+ Parse a COPR repository identifier into its components.
39+ """
40+ matched = COPR_REPO_PATTERN .match (copr_repo )
41+ if not matched :
42+ raise tmt .utils .PrepareError (f"Invalid copr repository '{ copr_repo } '." )
43+ return CoprRepo (
44+ is_group = bool (matched .group ('group' )),
45+ name = matched .group ('name' ),
46+ project = matched .group ('project' ),
47+ )
48+
49+
2550@provides_artifact_provider ('copr.repository' )
2651class CoprRepositoryProvider (ArtifactProvider ):
2752 """
@@ -41,10 +66,12 @@ class CoprRepositoryProvider(ArtifactProvider):
4166 """
4267
4368 copr_repo : str # Parsed Copr repository name (e.g. 'packit/packit-dev')
69+ repository : Optional [Repository ]
4470
4571 def __init__ (self , raw_id : str , repository_priority : int , logger : tmt .log .Logger ):
4672 super ().__init__ (raw_id , repository_priority , logger )
4773 self .copr_repo = self .id
74+ self .repository = None
4875
4976 @classmethod
5077 def _extract_provider_id (cls , raw_id : str ) -> ArtifactProviderId :
@@ -83,11 +110,22 @@ def fetch_contents(
83110 exclude_patterns : Optional [list [Pattern [str ]]] = None ,
84111 ) -> list [Path ]:
85112 """
86- Enable the Copr repository on the guest.
87-
88- :return: Empty list as no files are downloaded.
89- :raises tmt.utils.PrepareError: If the package manager does not support
90- enabling Copr repositories.
113+ Enable the Copr repository on the guest and retrieve the resulting
114+ ``.repo`` file content.
91115 """
92116 guest .package_manager .enable_copr (self .copr_repo )
117+
118+ repo = parse_copr_repo (self .copr_repo )
119+ owner = f'group_{ repo .name } ' if repo .is_group else repo .name
120+ repo_filename = f"_copr:copr.fedorainfracloud.org:{ owner } :{ repo .project } .repo"
121+
122+ # pull from guest to build Repository object with populated repo_ids
123+ guest .pull (
124+ source = Path (f"/etc/yum.repos.d/{ repo_filename } " ),
125+ destination = download_path ,
126+ )
127+
128+ self .repository = Repository .from_file_path (
129+ download_path / repo_filename , self .logger , name = self .copr_repo
130+ )
93131 return []
0 commit comments