vim/update.py: fix handling of redirects

This commit is contained in:
Matthieu Coudron 2022-05-01 01:10:49 +02:00 committed by Matthieu Coudron
parent 6fa768884c
commit 1b8e123255

View File

@ -87,7 +87,8 @@ def make_request(url: str, token=None) -> urllib.request.Request:
return urllib.request.Request(url, headers=headers) return urllib.request.Request(url, headers=headers)
Redirects = Dict['Repo', 'Repo'] # a dictionary of plugins and their new repositories
Redirects = Dict['PluginDesc', 'Repo']
class Repo: class Repo:
def __init__( def __init__(
@ -96,8 +97,8 @@ class Repo:
self.uri = uri self.uri = uri
'''Url to the repo''' '''Url to the repo'''
self._branch = branch self._branch = branch
# {old_uri: new_uri} # Redirect is the new Repo to use
self.redirect: Redirects = {} self.redirect: Optional['Repo'] = None
self.token = "dummy_token" self.token = "dummy_token"
@property @property
@ -207,7 +208,7 @@ class RepoGitHub(Repo):
) )
new_repo = RepoGitHub(owner=new_owner, repo=new_name, branch=self.branch) new_repo = RepoGitHub(owner=new_owner, repo=new_name, branch=self.branch)
self.redirect[self] = new_repo self.redirect = new_repo
def prefetch(self, commit: str) -> str: def prefetch(self, commit: str) -> str:
@ -237,7 +238,7 @@ class RepoGitHub(Repo):
}}''' }}'''
@dataclass @dataclass(frozen=True)
class PluginDesc: class PluginDesc:
repo: Repo repo: Repo
branch: str branch: str
@ -332,9 +333,19 @@ class Editor:
self.deprecated = deprecated or root.joinpath("deprecated.json") self.deprecated = deprecated or root.joinpath("deprecated.json")
self.cache_file = cache_file or f"{name}-plugin-cache.json" self.cache_file = cache_file or f"{name}-plugin-cache.json"
def get_current_plugins(self): def get_current_plugins(editor) -> List[Plugin]:
"""To fill the cache""" """To fill the cache"""
return get_current_plugins(self) with CleanEnvironment():
cmd = ["nix", "eval", "--extra-experimental-features", "nix-command", "--impure", "--json", "--expr", editor.get_plugins]
log.debug("Running command %s", cmd)
out = subprocess.check_output(cmd)
data = json.loads(out)
plugins = []
for name, attr in data.items():
print("get_current_plugins: name %s" % name)
p = Plugin(name, attr["rev"], attr["submodules"], attr["sha256"])
plugins.append(p)
return plugins
def load_plugin_spec(self, config: FetchConfig, plugin_file) -> List[PluginDesc]: def load_plugin_spec(self, config: FetchConfig, plugin_file) -> List[PluginDesc]:
'''CSV spec''' '''CSV spec'''
@ -448,24 +459,10 @@ class CleanEnvironment(object):
self.empty_config.close() self.empty_config.close()
def get_current_plugins(editor: Editor) -> List[Plugin]:
with CleanEnvironment():
cmd = ["nix", "eval", "--extra-experimental-features", "nix-command", "--impure", "--json", "--expr", editor.get_plugins]
log.debug("Running command %s", cmd)
out = subprocess.check_output(cmd)
data = json.loads(out)
plugins = []
for name, attr in data.items():
print("get_current_plugins: name %s" % name)
p = Plugin(name, attr["rev"], attr["submodules"], attr["sha256"])
plugins.append(p)
return plugins
def prefetch_plugin( def prefetch_plugin(
p: PluginDesc, p: PluginDesc,
cache: "Optional[Cache]" = None, cache: "Optional[Cache]" = None,
) -> Tuple[Plugin, Redirects]: ) -> Tuple[Plugin, Optional[Repo]]:
repo, branch, alias = p.repo, p.branch, p.alias repo, branch, alias = p.repo, p.branch, p.alias
name = alias or p.repo.name name = alias or p.repo.name
commit = None commit = None
@ -479,7 +476,7 @@ def prefetch_plugin(
return cached_plugin, repo.redirect return cached_plugin, repo.redirect
has_submodules = repo.has_submodules() has_submodules = repo.has_submodules()
print(f"prefetch {name}") log.debug(f"prefetch {name}")
sha256 = repo.prefetch(commit) sha256 = repo.prefetch(commit)
return ( return (
@ -488,7 +485,7 @@ def prefetch_plugin(
) )
def print_download_error(plugin: str, ex: Exception): def print_download_error(plugin: PluginDesc, ex: Exception):
print(f"{plugin}: {ex}", file=sys.stderr) print(f"{plugin}: {ex}", file=sys.stderr)
ex_traceback = ex.__traceback__ ex_traceback = ex.__traceback__
tb_lines = [ tb_lines = [
@ -498,19 +495,21 @@ def print_download_error(plugin: str, ex: Exception):
print("\n".join(tb_lines)) print("\n".join(tb_lines))
def check_results( def check_results(
results: List[Tuple[PluginDesc, Union[Exception, Plugin], Redirects]] results: List[Tuple[PluginDesc, Union[Exception, Plugin], Optional[Repo]]]
) -> Tuple[List[Tuple[PluginDesc, Plugin]], Redirects]: ) -> Tuple[List[Tuple[PluginDesc, Plugin]], Redirects]:
''' ''' ''' '''
failures: List[Tuple[str, Exception]] = [] failures: List[Tuple[PluginDesc, Exception]] = []
plugins = [] plugins = []
# {old: new} plugindesc redirects: Redirects = {}
redirects: Dict[Repo, Repo] = {}
for (pdesc, result, redirect) in results: for (pdesc, result, redirect) in results:
if isinstance(result, Exception): if isinstance(result, Exception):
failures.append((pdesc.name, result)) failures.append((pdesc, result))
else: else:
plugins.append((pdesc, result)) new_pdesc = pdesc
redirects.update(redirect) if redirect is not None:
redirects.update({pdesc: redirect})
new_pdesc = PluginDesc(redirect, pdesc.branch, pdesc.alias)
plugins.append((new_pdesc, result))
print(f"{len(results) - len(failures)} plugins were checked", end="") print(f"{len(results) - len(failures)} plugins were checked", end="")
if len(failures) == 0: if len(failures) == 0:
@ -591,13 +590,13 @@ class Cache:
def prefetch( def prefetch(
pluginDesc: PluginDesc, cache: Cache pluginDesc: PluginDesc, cache: Cache
) -> Tuple[PluginDesc, Union[Exception, Plugin], dict]: ) -> Tuple[PluginDesc, Union[Exception, Plugin], Optional[Repo]]:
try: try:
plugin, redirect = prefetch_plugin(pluginDesc, cache) plugin, redirect = prefetch_plugin(pluginDesc, cache)
cache[plugin.commit] = plugin cache[plugin.commit] = plugin
return (pluginDesc, plugin, redirect) return (pluginDesc, plugin, redirect)
except Exception as e: except Exception as e:
return (pluginDesc, e, {}) return (pluginDesc, e, None)
@ -606,7 +605,7 @@ def rewrite_input(
input_file: Path, input_file: Path,
deprecated: Path, deprecated: Path,
# old pluginDesc and the new # old pluginDesc and the new
redirects: Dict[PluginDesc, PluginDesc] = {}, redirects: Redirects = {},
append: List[PluginDesc] = [], append: List[PluginDesc] = [],
): ):
plugins = load_plugins_from_csv(config, input_file,) plugins = load_plugins_from_csv(config, input_file,)
@ -618,9 +617,10 @@ def rewrite_input(
cur_date_iso = datetime.now().strftime("%Y-%m-%d") cur_date_iso = datetime.now().strftime("%Y-%m-%d")
with open(deprecated, "r") as f: with open(deprecated, "r") as f:
deprecations = json.load(f) deprecations = json.load(f)
for old, new in redirects.items(): for pdesc, new_repo in redirects.items():
old_plugin, _ = prefetch_plugin(old) new_pdesc = PluginDesc(new_repo, pdesc.branch, pdesc.alias)
new_plugin, _ = prefetch_plugin(new) old_plugin, _ = prefetch_plugin(pdesc)
new_plugin, _ = prefetch_plugin(new_pdesc)
if old_plugin.normalized_name != new_plugin.normalized_name: if old_plugin.normalized_name != new_plugin.normalized_name:
deprecations[old_plugin.normalized_name] = { deprecations[old_plugin.normalized_name] = {
"new": new_plugin.normalized_name, "new": new_plugin.normalized_name,