55
66import os
77import re
8+ import shutil
89import subprocess as sp
9- import sys
1010
1111import requests
1212
1616from .provider import Provider , Result
1717
1818
19+ def get_module_config_url (registry , module_name , branch = "main" ):
20+ """
21+ Get the raw address of the config (container.yaml)
22+ """
23+ registry_bare = registry .split (".com" )[- 1 ]
24+ raw = (
25+ "https://gitlab.com/%s/-/raw/%s/%s/container.yaml"
26+ if "gitlab" in registry
27+ else "https://raw.githubusercontent.com/%s/%s/%s/container.yaml"
28+ )
29+ return raw % (
30+ registry_bare ,
31+ branch ,
32+ module_name ,
33+ )
34+
35+
1936class RemoteResult (Result ):
2037 """
2138 A remote result provides courtesy functions for interacting with
@@ -117,6 +134,9 @@ def exists(self, name):
117134 """
118135 Determine if a module exists in the registry.
119136 """
137+ name = name .split (":" )[0 ]
138+ if self ._cache and name in self ._cache :
139+ return True
120140 dirname = self .source
121141 if self .subdir :
122142 dirname = os .path .join (dirname , self .subdir )
@@ -158,7 +178,8 @@ def find(self, name):
158178 """
159179 Find a particular entry in a registry
160180 """
161- self ._update_cache ()
181+ if not self ._cache :
182+ self ._update_cache ()
162183 if name in self ._cache :
163184 return RemoteResult (name , self ._cache [name ])
164185
@@ -172,12 +193,29 @@ def _update_cache(self, force=False):
172193 # Check for exposed library API on GitHub or GitLab pages
173194 response = requests .get (self .web_url )
174195 if response .status_code != 200 :
175- sys .exit (
176- "Remote %s is not deploying a Registry API (%s). Open a GitHub issue to ask for help."
177- % (self .source , self .web_url )
178- )
196+ return self ._update_clone_cache ()
179197 self ._cache = response .json ()
180198
199+ def _update_clone_cache (self ):
200+ """
201+ Given a remote that does not expose a library.json, handle via clone.
202+ """
203+ logger .warning (
204+ "Remote %s is not deploying a Registry API, falling back to clone."
205+ % self .source
206+ )
207+ tmpdir = self .clone ()
208+ for dirname , module in self .iter_modules ():
209+ # Minimum amount of metadata to function here
210+ config_url = get_module_config_url (self .source , module )
211+ self ._cache [module ] = {
212+ "config" : shpc .utils .read_yaml (
213+ os .path .join (dirname , module , "container.yaml" )
214+ ),
215+ "config_url" : config_url ,
216+ }
217+ shutil .rmtree (tmpdir )
218+
181219 def iter_registry (self , filter_string = None ):
182220 """
183221 Yield metadata about containers in a remote registry.
0 commit comments