8
8
"""
9
9
import importlib .metadata
10
10
import logging
11
- from typing import Iterable , Optional
11
+ from typing import Collection
12
12
13
- from .abstract import DEFAULT_VERSION , Source , TargetSession , Version , VersionNotFound
13
+ from .abstract import DEFAULT_VERSION , Source , TargetSession , Version , VersionNotFound , get_redirect_mechanisms
14
14
from .versions import DeploymentAlias
15
15
16
16
_logger = logging .getLogger (__name__ )
@@ -71,6 +71,8 @@ def delete_version(target: TargetSession, version_id: str) -> None:
71
71
:param version_id:
72
72
"""
73
73
deployment_spec = target .deployment_spec
74
+ if deployment_spec .default_version is not None and deployment_spec .default_version .version_id == version_id :
75
+ delete_alias (target , DEFAULT_VERSION )
74
76
for alias_id in deployment_spec .aliases_for_version (version_id ):
75
77
delete_alias (target , alias_id )
76
78
_logger .info ("Deleting version %s" , version_id )
@@ -82,7 +84,7 @@ def delete_version(target: TargetSession, version_id: str) -> None:
82
84
83
85
84
86
def create_alias (
85
- target : TargetSession , alias_id : Version , version : str , mechanisms : Iterable [str ] | None = None
87
+ target : TargetSession , alias_id : Version , version : str , mechanisms : Collection [str ] | None = None
86
88
) -> None :
87
89
"""
88
90
Create a new alias for a version.
@@ -93,66 +95,68 @@ def create_alias(
93
95
:param target: The target session to create the alias on
94
96
:param alias_id: The new alias id
95
97
:param version: The version_id to point to
96
- :param mechanisms: The named mechanisms to use. If None then 'html' target will choose the mechanism .
98
+ :param mechanisms: The named mechanisms to use. If None then all available mechanisms will be used .
97
99
"""
98
100
# Check if the given mechanisms can be implemented by this target
99
- available_redirect_mechanisms = target . available_redirect_mechanisms
101
+ available_redirect_mechanisms = get_redirect_mechanisms ( target )
100
102
if mechanisms is not None :
101
103
for mechanism in mechanisms :
102
104
if mechanism not in available_redirect_mechanisms :
103
- raise ValueError (f"LocalFileTreeTarget does not support redirect mechanism: { mechanism } " )
105
+ raise ValueError (f"{ type ( TargetSession ). __name__ } does not support redirect mechanism: { mechanism } " )
104
106
105
107
# Check if the alias already exists ...
106
108
# If mechanisms wasn't specified use whatever is on the existing one.
107
109
deployment_spec = target .deployment_spec
108
110
if alias_id in deployment_spec .versions :
109
111
raise ValueError (f"Cannot create an alias with the same name as an existing version. "
110
112
f"Delete the version first! Alias name: { alias_id } " )
111
- if alias_id is ... and deployment_spec .default_version is not None :
113
+ if alias_id is DEFAULT_VERSION and deployment_spec .default_version is not None :
112
114
# This is the "default" alias
113
115
alias = deployment_spec .default_version
114
- if mechanisms is None :
115
- mechanisms = alias .redirect_mechanisms
116
- if alias_id in deployment_spec .aliases :
116
+ elif alias_id in deployment_spec .aliases :
117
117
alias = deployment_spec .aliases [alias_id ]
118
- if mechanisms is None :
119
- mechanisms = alias .redirect_mechanisms
120
118
else :
121
119
# No existing alias was found. Make a new one.
122
120
alias = DeploymentAlias (version_id = version , redirect_mechanisms = set ())
123
121
target .set_alias (alias_id , alias )
124
- # Must set the alias first or creating the mechanism will fail.
125
- if mechanisms is None :
126
- mechanisms = [ "html" ]
122
+
123
+ if mechanisms is None :
124
+ mechanisms = available_redirect_mechanisms . keys ()
127
125
128
126
_logger .info ("Creating %s alias redirect %s to %s" , ", " .join (mechanisms ), alias_id , version )
129
127
# Remove any redirect mechanisms to a different version that we are not going to replace
130
- if alias .version_id != version :
131
- for mechanism in alias .redirect_mechanisms .copy ():
132
- if mechanism not in mechanisms :
133
- try :
134
- available_redirect_mechanisms [mechanism ].delete_redirect (target , alias_id )
135
- except KeyError :
136
- raise ValueError (f"LocalFileTreeTarget does not support redirect mechanism: { mechanism } . "
137
- f"Unable to remove redirect for { alias_id } -->{ alias .version_id } " )
138
- alias .redirect_mechanisms .discard (mechanism )
139
- alias .version_id = version
128
+ for mechanism in alias .redirect_mechanisms .copy ():
129
+ if mechanism not in mechanisms :
130
+ try :
131
+ _logger .warning (
132
+ "Implicitly deleting redirect %s mechanism %s to %s" , alias_id , mechanism , alias .version_id
133
+ )
134
+ available_redirect_mechanisms [mechanism ].delete_redirect (target , alias_id )
135
+ except KeyError :
136
+ raise ValueError (
137
+ f"{ type (TargetSession ).__name__ } does not support redirect mechanism: { mechanism } . "
138
+ f"Unable to remove redirect for { alias_id } -->{ alias .version_id } "
139
+ )
140
+ alias .redirect_mechanisms .discard (mechanism )
140
141
141
142
# Create the redirects or refresh them to their new location.
142
143
for mechanism in mechanisms :
143
144
if mechanism in alias .redirect_mechanisms :
144
145
if alias .version_id != version :
146
+ _logger .debug ("Modifying %s mechanism %s from %s to %s" , alias_id , mechanism , alias .version_id , version )
145
147
available_redirect_mechanisms [mechanism ].refresh_redirect (target , alias_id , version )
146
148
else :
147
149
_logger .debug ("mechanism %s already in place, skipping" , mechanism )
148
150
else :
151
+ _logger .debug ("Creating %s mechanism %s to %s" , alias_id , mechanism , version )
149
152
available_redirect_mechanisms [mechanism ].create_redirect (target , alias_id , version )
150
153
alias .redirect_mechanisms .add (mechanism )
151
154
155
+ alias .version_id = version
152
156
target .set_alias (alias_id , alias )
153
157
154
158
155
- def delete_alias (target : TargetSession , alias_id : Version , mechanisms : Iterable [str ] | None = None ) -> None :
159
+ def delete_alias (target : TargetSession , alias_id : Version , mechanisms : Collection [str ] | None = None ) -> None :
156
160
"""
157
161
Delete an alias.
158
162
@@ -167,20 +171,20 @@ def delete_alias(target: TargetSession, alias_id: Version, mechanisms: Iterable[
167
171
if alias_id is DEFAULT_VERSION :
168
172
alias = target .deployment_spec .default_version
169
173
if alias is None :
170
- _logger .debug ( "Default alias not set" )
174
+ _logger .warning ( "Cannot delete default alias as it is not set" )
171
175
return
172
176
else :
173
177
try :
174
178
alias = target .deployment_spec .aliases [alias_id ]
175
179
except KeyError :
176
- _logger .debug ( "Alias %s not set, skipping " , alias_id )
180
+ _logger .warning ( "Cannot delete alias %s not set, it has not been set " , alias_id )
177
181
return
178
182
179
183
if mechanisms is not None :
180
- to_delete = [mechanism for mechanism in mechanisms if mechanism in alias .redirect_mechanisms ]
184
+ to_delete : list | set = [mechanism for mechanism in mechanisms if mechanism in alias .redirect_mechanisms ]
181
185
else :
182
186
to_delete = alias .redirect_mechanisms .copy ()
183
- available_mechanisms = target . available_redirect_mechanisms
187
+ available_mechanisms = get_redirect_mechanisms ( target )
184
188
for mechanism in to_delete :
185
189
try :
186
190
available_mechanisms [mechanism ].delete_redirect (
@@ -196,7 +200,7 @@ def delete_alias(target: TargetSession, alias_id: Version, mechanisms: Iterable[
196
200
target .set_alias (alias_id , None )
197
201
198
202
199
- def refresh_alias (target : TargetSession , alias_id : Version , mechanisms : Iterable [str ] | None = None ) -> None :
203
+ def refresh_alias (target : TargetSession , alias_id : Version , mechanisms : Collection [str ] | None = None ) -> None :
200
204
"""
201
205
Refresh redirects.
202
206
@@ -218,7 +222,7 @@ def refresh_alias(target: TargetSession, alias_id: Version, mechanisms: Iterable
218
222
to_refresh = {mechanism for mechanism in mechanisms if mechanism in alias .redirect_mechanisms }
219
223
else :
220
224
to_refresh = alias .redirect_mechanisms
221
- available_mechanisms = target . available_redirect_mechanisms
225
+ available_mechanisms = get_redirect_mechanisms ( target )
222
226
for mechanism in to_refresh :
223
227
available_mechanisms [mechanism ].refresh_redirect (
224
228
session = target ,
0 commit comments