11import logging
22from datetime import datetime
3-
43import requests
4+
55import pdcupdater .services
66
77log = logging .getLogger (__name__ )
@@ -39,11 +39,7 @@ def handle(self, pdc, msg):
3939 branch = msg ['msg' ]['commit' ]['branch' ]
4040 repo = msg ['msg' ]['commit' ]['repo' ]
4141 namespace = msg ['msg' ]['commit' ]['namespace' ]
42- # The dist-git namespaces are plural but the types are singular in PDC
43- if namespace .endswith ('s' ):
44- component_type = namespace [:- 1 ]
45- else :
46- component_type = namespace
42+ component_type = self ._namespace_to_pdc (namespace )
4743 # This query guarantees a unique component branch, so a count of 1 is
4844 # expected
4945 branch_query_rv = pdc ['component-branches' ]._ (
@@ -63,22 +59,93 @@ def handle(self, pdc, msg):
6359 @staticmethod
6460 def _retire_branch (pdc , branch ):
6561 """ Internal method for retiring a branch in PDC. """
66- log .info ("Retiring {type}/{global_component}#{name}" .format (** branch ))
6762 today = datetime .utcnow ().date ()
6863 for sla in branch ['slas' ]:
6964 sla_eol = datetime .strptime (sla ['eol' ], '%Y-%m-%d' ).date ()
7065 if sla_eol > today :
7166 pdc ['component-branch-slas' ][sla ['id' ]]._ \
7267 += {'eol' : str (today )}
7368
69+ @staticmethod
70+ def _namespace_to_pdc (namespace ):
71+ namespace_to_pdc = {
72+ 'rpms' : 'rpm' ,
73+ 'modules' : 'module' ,
74+ 'container' : 'container' ,
75+ }
76+ if namespace not in namespace_to_pdc :
77+ raise ValueError ('The namespace "{0}" is not supported'
78+ .format (namespace ))
79+ else :
80+ return namespace_to_pdc [namespace ]
81+
82+ @staticmethod
83+ def _pdc_to_namespace (pdc_type ):
84+ pdc_to_namespace = {
85+ 'rpm' : 'rpms' ,
86+ 'module' : 'modules' ,
87+ 'container' : 'container' ,
88+ }
89+ if pdc_type not in pdc_to_namespace :
90+ raise ValueError ('The PDC type "{0}" is not supported'
91+ .format (pdc_type ))
92+ else :
93+ return pdc_to_namespace [pdc_type ]
94+
95+ @staticmethod
96+ def _is_retired_in_cgit (namespace , repo , branch , requests_session = None ):
97+ if requests_session is None :
98+ requests_session = requests .Session ()
99+
100+ cgit_url = 'https://src.fedoraproject.org/cgit'
101+ # Check to see if they have a dead.package file in dist-git
102+ url = '{base}/{namespace}/{repo}.git/plain/dead.package?h={branch}'
103+ response = requests_session .head (url .format (
104+ base = cgit_url ,
105+ namespace = namespace ,
106+ repo = repo ,
107+ branch = branch ,
108+ ))
109+
110+ # If there is a dead.package, then the branch is retired in cgit
111+ if response .status_code in [200 , 404 ]:
112+ return response .status_code == 200
113+ else :
114+ raise ValueError (
115+ 'The connection to cgit failed. Retirement status could not '
116+ 'be determined. The status code was: {0}. The content was: '
117+ '{1}' .format (response .status_code , response .content ))
118+
74119 def audit (self , pdc ):
75- """ Not Implemented .
120+ """ Returns the difference in retirement status in PDC and dist-git .
76121
77- This function (if it were implemented) should compare the status in PDC
78- and the status in the "real world" (i.e., in dist-git) and return the
79- difference.
122+ This function compares the status in PDC and the status in the
123+ "real world" (i.e., in dist-git) and return the difference.
80124 """
81- pass
125+ branches_retired_in_distgit = set ()
126+ branches_retired_in_pdc = set ()
127+ session = requests .Session ()
128+
129+ log .info ('Looking up all branches from PDC.' )
130+ for branch in pdc .get_paged (pdc ['component-branches' ]._ ):
131+ branch_str = '{type}/{global_component}#{name}' .format (** branch )
132+ log .debug ('Considering {0}' .format (branch_str ))
133+ retired_in_cgit = self ._is_retired_in_cgit (
134+ namespace = self ._pdc_to_namespace (branch ['type' ]),
135+ repo = branch ['global_component' ],
136+ branch = branch ['name' ],
137+ requests_session = session
138+ )
139+
140+ if retired_in_cgit :
141+ branches_retired_in_distgit .add (branch_str )
142+ if not branch ['active' ]:
143+ branches_retired_in_pdc .add (branch_str )
144+
145+ present = branches_retired_in_pdc - branches_retired_in_distgit
146+ absent = branches_retired_in_distgit - branches_retired_in_pdc
147+
148+ return present , absent
82149
83150 def initialize (self , pdc ):
84151 """ Initialize PDC retirement status from analyzing dist-git.
@@ -87,28 +154,19 @@ def initialize(self, pdc):
87154 in PDC that have a dead.package file in dist-git.
88155 """
89156 session = requests .Session ()
90- cgit_url = "https://src.fedoraproject.org/cgit"
91- pdc2namespace = {
92- 'rpm' : 'rpms' ,
93- 'module' : 'modules' ,
94- 'container' : 'container' ,
95- }
96157
97158 # Look up all non-retired branches from PDC
98- log .info ("Looking up active branches from PDC." )
99- branches = pdc .get_paged (pdc ['component-branches' ], active = True )
100-
101- for branch in branches :
102- log .debug ("Considering {type}/{global_component}#{name}" .format (** branch ))
103- # Check to see if they have a dead.package file in dist-git
104- url = "{base}/{type}/{repo}.git/plain/dead.package?h={branch}"
105- response = session .head (url .format (
106- base = cgit_url ,
107- type = pdc2namespace [branch ['type' ]],
159+ log .info ('Looking up active branches from PDC.' )
160+
161+ for branch in pdc .get_paged (pdc ['component-branches' ]._ , active = True ):
162+ log .debug ('Considering {type}/{global_component}#{name}'
163+ .format (** branch ))
164+ retired_in_cgit = self ._is_retired_in_cgit (
165+ namespace = self ._pdc_to_namespace (branch ['type' ]),
108166 repo = branch ['global_component' ],
109167 branch = branch ['name' ],
110- ))
168+ requests_session = session
169+ )
111170
112- # If so, then we need to retire them.
113- if bool (response ):
171+ if retired_in_cgit :
114172 self ._retire_branch (pdc , branch )
0 commit comments