49
49
"""Directory where all spkg sources are found."""
50
50
51
51
52
- def uninstall (spkg_name , sage_local , keep_files = False , verbose = False ):
52
+ def uninstall (spkg_name , sage_local , keep_files = False ,
53
+ verbose = False , log_directory = None ):
53
54
"""
54
55
Given a package name and path to an installation tree (SAGE_LOCAL or SAGE_VENV),
55
56
uninstall that package from that tree if it is currently installed.
@@ -85,51 +86,44 @@ def uninstall(spkg_name, sage_local, keep_files=False, verbose=False):
85
86
86
87
if 'files' not in spkg_meta :
87
88
if stamp_file :
88
- print ("Old-style or corrupt stamp file ' {0}' "
89
+ print ("Old-style or corrupt stamp file {0}"
89
90
.format (stamp_file ), file = sys .stderr )
90
91
else :
91
92
print ("Package '{0}' is currently not installed"
92
93
.format (spkg_name ), file = sys .stderr )
93
94
94
95
# Run legacy uninstaller even if there is no stamp file: the
95
96
# package may be partially installed without a stamp file
96
- legacy_uninstall (spkg_name , verbose = verbose )
97
+ legacy_uninstall (spkg_name ,
98
+ verbose = verbose , log_directory = log_directory )
97
99
else :
98
100
files = spkg_meta ['files' ]
99
101
if not files :
100
102
print ("Warning: No files to uninstall for "
101
103
"'{0}'" .format (spkg_name ), file = sys .stderr )
102
104
103
- modern_uninstall (spkg_name , sage_local , files , verbose = verbose )
105
+ modern_uninstall (spkg_name , sage_local , files ,
106
+ verbose = verbose , log_directory = log_directory )
104
107
105
108
remove_stamp_files (stamp_files , verbose = verbose )
106
109
107
110
108
- def legacy_uninstall (spkg_name , verbose = False ):
111
+ def legacy_uninstall (spkg_name ,
112
+ verbose = False , log_directory = None ):
109
113
"""
110
114
Run the spkg's legacy uninstall script, if one exists; otherwise do
111
115
nothing.
112
116
"""
113
-
114
117
spkg_dir = pth .join (PKGS , spkg_name )
115
- legacy_uninstall = pth .join (spkg_dir , 'spkg-legacy-uninstall' )
116
-
117
- if not pth .isfile (legacy_uninstall ):
118
- print ("No legacy uninstaller found for '{0}'; nothing to "
119
- "do" .format (spkg_name ), file = sys .stderr )
120
- return
121
-
122
- print ("Uninstalling '{0}' with legacy uninstaller" .format (spkg_name ))
123
- if verbose :
124
- with open (legacy_uninstall ) as f :
125
- print (f .read ())
126
118
127
119
# Any errors from this, including a non-zero return code will
128
120
# bubble up and exit the uninstaller
129
- subprocess .check_call ([legacy_uninstall ])
121
+ run_spkg_script (spkg_name , spkg_dir , 'legacy-uninstall' ,
122
+ if_does_not_exist = 'log' , log_directory = log_directory )
130
123
131
124
132
- def modern_uninstall (spkg_name , sage_local , files , verbose = False ):
125
+ def modern_uninstall (spkg_name , sage_local , files ,
126
+ verbose = False , log_directory = None ):
133
127
"""
134
128
Remove all listed files from the given installation tree (SAGE_LOCAL or SAGE_VENV).
135
129
@@ -150,15 +144,17 @@ def modern_uninstall(spkg_name, sage_local, files, verbose=False):
150
144
# so that we can easily remove a directory once it's been emptied
151
145
files .sort (key = lambda f : (- f .count (os .sep ), f ))
152
146
153
- print ("Uninstalling existing '{0}'" .format (spkg_name ))
147
+ if verbose :
148
+ print ("Uninstalling existing '{0}'" .format (spkg_name ))
154
149
155
150
# Run the package's prerm script, if it exists.
156
151
# If an error occurs here we abort the uninstallation for now.
157
152
# This means a prerm script actually has the ability to abort an
158
153
# uninstallation, for example, if some manual intervention is needed
159
154
# to proceed.
160
155
try :
161
- run_spkg_script (spkg_name , spkg_scripts , 'prerm' , 'pre-uninstall' )
156
+ run_spkg_script (spkg_name , spkg_scripts , 'prerm' ,
157
+ log_directory = log_directory )
162
158
except Exception as exc :
163
159
script_path = pth .join (spkg_scripts , 'spkg-prerm' )
164
160
print ("Error: The pre-uninstall script for '{0}' failed; the "
@@ -177,7 +173,7 @@ def modern_uninstall(spkg_name, sage_local, files, verbose=False):
177
173
# manifest, so this step has to happen before removing the files.
178
174
try :
179
175
run_spkg_script (spkg_name , spkg_scripts , 'piprm' ,
180
- 'pip-uninstall' )
176
+ log_directory = log_directory )
181
177
except Exception :
182
178
print ("Warning: Error running the pip-uninstall script for "
183
179
"'{0}'; uninstallation may have left behind some files" .format (
@@ -190,7 +186,7 @@ def rmdir(dirname):
190
186
print ('rmdir "{}"' .format (dirname ))
191
187
os .rmdir (dirname )
192
188
else :
193
- print ("Warning: Directory ' {0}' not found" .format (
189
+ print ("Warning: Directory {0} not found" .format (
194
190
dirname ), file = sys .stderr )
195
191
196
192
# Remove the files; if a directory is empty after removing a file
@@ -205,7 +201,7 @@ def rmdir(dirname):
205
201
print ('rm "{}"' .format (filename ))
206
202
os .remove (filename )
207
203
else :
208
- print ("Warning: File ' {0}' not found" .format (filename ),
204
+ print ("Warning: File {0} not found" .format (filename ),
209
205
file = sys .stderr )
210
206
211
207
# Remove file's directory if it is now empty
@@ -218,7 +214,7 @@ def rmdir(dirname):
218
214
# files removed.
219
215
try :
220
216
run_spkg_script (spkg_name , spkg_scripts , 'postrm' ,
221
- 'post-uninstall' )
217
+ log_directory = log_directory )
222
218
except Exception :
223
219
print ("Warning: Error running the post-uninstall script for "
224
220
"'{0}'; the package will still be uninstalled, but "
@@ -234,20 +230,30 @@ def rmdir(dirname):
234
230
def remove_stamp_files (stamp_files , verbose = False ):
235
231
# Finally, if all went well, delete all the stamp files.
236
232
for stamp_file in stamp_files :
237
- print ("Removing stamp file ' {0}' " .format (stamp_file ))
233
+ print ("Removing stamp file {0}" .format (stamp_file ))
238
234
os .remove (stamp_file )
239
235
240
236
241
- def run_spkg_script (spkg_name , path , script_name , script_descr ):
237
+ def run_spkg_script (spkg_name , path , script_name ,
238
+ if_does_not_exist = 'ignore' , log_directory = None ):
242
239
"""
243
240
Runs the specified ``spkg-<foo>`` script under the given ``path``,
244
241
if it exists.
245
242
"""
246
-
247
- script = pth .join (path , 'spkg-{0}' . format ( script_name ) )
243
+ script_name = 'spkg-{0}' . format ( script_name )
244
+ script = pth .join (path , script_name )
248
245
if pth .exists (script ):
249
- print ("Running {0} script for '{1}'" .format (script_descr , spkg_name ))
250
- subprocess .check_call ([script ])
246
+ if log_directory :
247
+ log_file = pth .join (log_directory , script + '.log' )
248
+ subprocess .check_call (['sage-logger' , '-p' , script , log_file ])
249
+ else :
250
+ subprocess .check_call ([script ])
251
+ elif if_does_not_exist == 'ignore' :
252
+ pass
253
+ elif if_does_not_exist == 'log' :
254
+ print ('No {0} script; nothing to do' .format (script_name ), file = sys .stderr )
255
+ else :
256
+ raise ValueError ('unknown if_does_not_exist value: {0}' .format (if_does_not_exist ))
251
257
252
258
253
259
def dir_type (path ):
@@ -257,7 +263,7 @@ def dir_type(path):
257
263
258
264
if path and not pth .isdir (path ):
259
265
raise argparse .ArgumentTypeError (
260
- "' {0}' is not a directory" .format (path ))
266
+ "{0} is not a directory" .format (path ))
261
267
262
268
return path
263
269
@@ -284,6 +290,8 @@ def make_parser():
284
290
"but do not remove files installed by the "
285
291
"package" )
286
292
parser .add_argument ('--debug' , action = 'store_true' , help = argparse .SUPPRESS )
293
+ parser .add_argument ('--log-directory' , type = str ,
294
+ help = "directory where to create log files (default: none)" )
287
295
288
296
return parser
289
297
@@ -301,7 +309,7 @@ def run(argv=None):
301
309
302
310
try :
303
311
uninstall (args .spkg , args .sage_local , keep_files = args .keep_files ,
304
- verbose = args .verbose )
312
+ verbose = args .verbose , log_directory = args . log_directory )
305
313
except Exception as exc :
306
314
print ("Error during uninstallation of '{0}': {1}" .format (
307
315
args .spkg , exc ), file = sys .stderr )
0 commit comments