-
Notifications
You must be signed in to change notification settings - Fork 1
/
InstallPlace.m
432 lines (389 loc) · 13.8 KB
/
InstallPlace.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
## Copyright (C) 2019 Andrew Janke
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; If not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Class Constructor} {obj =} InstallPlace (tag, prefix, arch_prefix, index_file)
##
## A local directory where pkg package installations reside.
##
## An InstallPlace is a local filesystem directory hierarchy where pkg-installed
## packages reside.
##
## An InstallPlace is actually multiple paths, including a base prefix
## and an arch-specific prefix, and possibly a custom arbitrary index file
## location.
##
## @end deftypefn
classdef InstallPlace < handle
properties
% A tag or name identifying this install dir
tag
% The main directory under which to install packages
prefix
% The architecture-dependent directory. May be the same as prefix.
arch_prefix
% The index file that holds a list of packages
index_file
% The variable to save in the "octave_packages" file
package_list_var_name = "octave_packages"
endproperties
methods
function this = InstallPlace (tag, prefix, arch_prefix, index_file)
if nargin == 0
return
endif
narginchk (2, 4);
mustBeNonempty (tag);
mustBeNonempty (prefix);
if nargin < 3 || isempty (arch_prefix)
arch_prefix = prefix;
endif
if nargin < 4 || isempty (index_file)
index_file = fullfile (prefix, 'octave_packages');
endif
this.tag = tag;
this.prefix = prefix;
this.arch_prefix = arch_prefix;
this.index_file = index_file;
endfunction
function out = actual_package_list_var_name (this)
if ! packajoozle.internal.Util.isfile (this.index_file)
out = this.package_list_var_name;
return
endif
s = load (this.index_file);
vars = fieldnames (s);
if numel (vars) == 1
out = vars{1};
elseif isempty (vars)
out = this.package_list_var_name;
else
error ('pkj:MultipleVarsInIndexFile', ['pkj: found multiple variables ' ...
'in index file %s; cannot determine which to use.'], this.index_file);
endif
endfunction
function out = install_paths_for_pkg (this, pkgver)
ver = char (pkgver.version);
name_ver = [pkgver.name "-" ver];
out.dir = fullfile (this.prefix, name_ver);
out.arch_dir = fullfile (this.arch_prefix, name_ver);
endfunction
function disp (this)
disp (dispstr (this));
endfunction
function out = dispstr (this)
if isscalar (this)
strs = dispstrs (this);
out = strs{1};
else
out = sprintf ("%s %s", size2str (size (this)), class (this));
endif
endfunction
function out = dispstrs (this)
out = cell (size (this));
for i = 1:numel (this)
t = this(i);
out{i} = sprintf([...
"InstallPlace: %s\n" ...
"prefix: %s\n" ...
"arch_prefix: %s\n" ...
"index_file: %s\n" ...
"package_list_var_name: %s (actual = %s)"], ...
t.tag, t.prefix, t.arch_prefix, t.index_file, t.package_list_var_name, ...
t.actual_package_list_var_name);
endfor
endfunction
function out = get_package_list_descs (this)
# Gets list of instlled packages as "descs"
out = this.get_package_list ("desc");
endfunction
function out = get_package_list (this, format = "pkgver")
# Gets list of instlled packages
if ! packajoozle.internal.Util.isfile (this.index_file)
out = [];
return
endif
# Now, for some reason, a 0-byte octave_packages file is appearing, and I don't
# know what's writing it there. Octave, not Packajoozle, seems to be dropping
# them. But regardless, it breaks load().
if file_is_zero_bytes (this.index_file)
out = [];
return
endif
s = load (this.index_file);
# Hack: take any field
fields = fieldnames (s);
if numel (fields) > 1
error ("Multiple fields in package list file: %s", this.index_file);
endif
descs = s.(fields{1});
# Convert to output format
switch format
case "desc"
out = descs;
case "pkgver"
out = descs_to_pkgvers (descs);
otherwise
error ("InstallPlace.get_package_list: Invalid format: '%s'", format);
endswitch
endfunction
function out = get_installed_package_desc (this, pkgver)
descs = this.get_package_list_descs;
for i = 1:numel (descs)
pkgver_i = packajoozle.internal.PkgVer (descs{i}.name, descs{i}.version);
if pkgver_i == pkgver
out = descs{i};
return
endif
endfor
error ("Installed package not found: %s", char (pkgver));
endfunction
function record_installed_package (this, desc, target)
desc.dir = target.dir;
desc.archprefix = target.arch_dir;
list = this.get_package_list_descs;
if isempty (list)
new_list = {desc};
else
new_list = normalize_desc_save_order ([list {desc}]);
endif
this.save_pkg_list_to_file (new_list);
endfunction
function record_uninstalled_package (this, pkgver)
list = this.get_package_list_descs;
ix_to_delete = [];
for i = 1:numel (list)
ref = packajoozle.internal.PkgVer (list{i}.name, list{i}.version);
if ref == pkgver
ix_to_delete(end+1) = i;
endif
endfor
if ! isempty (ix_to_delete)
list(ix_to_delete) = [];
this.save_pkg_list_to_file (list);
endif
endfunction
function save_pkg_list_to_file (this, list)
eval (sprintf ("%s = list;", this.package_list_var_name));
packajoozle.internal.Util.mkdir (fileparts (this.index_file));
save (this.index_file, this.package_list_var_name);
endfunction
function out = is_installed (this, pkgver)
pkgver = makeItBeA (pkgver, "packajoozle.internal.PkgVer");
mustBeScalar (pkgver);
installed = this.get_package_list;
out = ismember (pkgver, installed);
endfunction
function out = list_packages_matching (this, pkgreqs)
all_pkgs = this.get_package_list ("pkgver");
tf = false (size (all_pkgs));
for i = 1:numel (pkgreqs)
tf = tf | pkgreqs(i).matches (all_pkgs);
endfor
out = all_pkgs(tf);
endfunction
function out = loaded_packages (this)
installed = this.get_package_list_descs;
out = installed(this.isloaded(installed));
endfunction
function out = is_loaded (this, pkgvers)
pkgvers = makeItBeA (pkgvers, "packajoozle.internal.PkgVer");
load_path = strsplit (path, pathsep);
out = false (size (pkgvers));
for i = 1:numel (pkgvers)
if ! this.is_installed (pkgvers(i))
continue
endif
desc = this.get_installed_package_desc (pkgvers(i));
out(i) = any (ismember ({desc.dir desc.archprefix}, load_path));
endfor
endfunction
function out = load_package (this, pkgver)
if this.is_installed (pkgver)
desc = this.get_installed_package_desc (pkgver);
if exist (desc.dir)
this.addpath_safe (desc.dir);
endif
if exist (desc.archprefix)
this.addpath_safe (desc.archprefix);
endif
return
else
error ("pkj: package not installed in %s: %s\n", this.tag, char (pkgver));
endif
endfunction
function addpath_safe (this, varargin)
% We have to do this becase PKG_ADD might raise errors
try
addpath (varargin{:});
catch err
# TODO: Decide if this should be an error instead of a warning. If it happens,
# it probably means the package was not loaded correctly, and we shoud error
# to indicate that, and caller can deal with it.
warning (["pkj: error (probably from PKG_ADD) when adding directory to path:\n" ...
" Dir: %s\n" ...
" Error: %s\n"], ...
strjoin (varargin, ", "), err.message);
end_try_catch
endfunction
function unload_packages (this, pkgvers)
pkgvers = makeItBeA (pkgvers, "packajoozle.internal.PkgVer");
to_unload = pkgvers(this.is_loaded (pkgvers));
if ! isempty (to_unload)
printf("pkj: unloading: %s\n", dispstr (to_unload));
endif
for i = 1:numel (to_unload)
this.unload_package (to_unload(i));
endfor
endfunction
function out = unload_package (this, pkgver)
out.status = true;
out.message = [];
pkgver = makeItBeA (pkgver, "packajoozle.internal.PkgVer");
mustBeScalar (pkgver);
if this.is_loaded (pkgver)
desc = this.get_installed_package_desc (pkgver);
for the_dir = {desc.dir desc.archprefix}
the_dir = the_dir{1};
if is_on_octave_load_path (the_dir)
# try/catch because PKG_DEL might misbehave
try
rmpath (the_dir);
catch err
error ("pkj: failed unloading package %s from %s: %s", ...
char (pkgver), the_dir, err.message);
end_try_catch
endif
endfor
endif
endfunction
function uninstall_packages (this, pkgspecs)
# Find packages to uninstall
if isa (pkgspecs, "packajoozle.internal.PkgVer")
pkgvers = pkgspecs;
elseif isa (pkgspecs, "packajoozle.internal.PkgVerReq")
pkgreqs = pkgspecs;
pkgvers = this.list_packages_matching (pkgreqs);
if isempty (pkgvers)
printf ("pkj: no matching packages found in %s for uninstallation\n", this.tag);
return
endif
else
error (["InstallPlace.uninstall_packages: invalid input: pkgspecs must be" ...
"a PkgVer or PkgVerReq; got a %s"], class (pkgspecs));
endif
printf ("pkj: uninstalling from %s: %s\n", this.tag, dispstr (pkgvers));
# TODO: Check dependencies
# Calculate remaining installed packages and see that their deps are still
# satisfied. This may actually have to be done by World, since packages
# can have dependencies across multiple install places (e.g. a user package
# depending on a global package)
this.unload_packages (pkgvers);
for i = 1:numel (pkgvers)
this.uninstall_one_package (pkgvers(i));
endfor
printf ("pkj: packages uninstalled OK\n");
endfunction
function uninstall_one_package (this, pkgver)
%UNINSTALL_ONE Uninstall a package
mustBeA (pkgver, "packajoozle.internal.PkgVer");
mustBeScalar (pkgver);
found = false;
if ! this.is_installed (pkgver)
error ("pkj: package %s is not installed in %s\n", char (pkgver), this.tag);
endif
#TODO: Get desc for installed package
target = this.install_paths_for_pkg (pkgver);
# Run pre-uninstall hooks
uninstall_hook_file = fullfile (target.dir, "packinfo", "on_uninstall.m");
if packajoozle.internal.Util.isfile (uninstall_hook_file)
orig_pwd = pwd;
try
cd (fullfile (target.dir, "packinfo"));
on_uninstall (desc);
cd (orig_pwd);
catch err
cd (orig_pwd);
error ("pkj: error while running on_uninstall hook for %s: %s\n", ...
char (pkgver), err.message);
end_try_catch
endif
# Delete package installation directories
if ! packajoozle.internal.Util.isfolder (target.dir)
warning ("pkj: directory %s previously removed; marking %s as uninstalled\n", ...
target.dir, char (pkgver));
endif
dirs = {target.arch_dir target.dir};
for i = 1:numel (dirs)
if packajoozle.internal.Util.isfileorfolder (dirs{i})
packajoozle.internal.Util.rm_rf (dirs{i});
endif
endfor
# Update package index
this.record_uninstalled_package (pkgver);
endfunction
endmethods
endclassdef
function out = is_on_octave_load_path (dir)
out = ismember (dir, strsplit (path, pathsep));
endfunction
function out = descs_to_pkgvers (descs)
c = cell (size (descs));
for i = 1:numel (descs)
c{i} = packajoozle.internal.PkgVer (descs{i}.name, descs{i}.version);
endfor
out = objvcat (c{:});
endfunction
function out = normalize_desc_save_order (descs)
newdesc = {};
for i = 1 : length (descs)
desc = descs{i};
deps = desc.depends;
if (isempty (deps)
|| (length (deps) == 1 && strcmp (deps{1}.package, "octave")))
newdesc{end + 1} = desc;
else
tmpdesc = {};
for k = 1 : length (deps)
for j = 1 : length (descs)
if (strcmp (descs{j}.name, deps{k}.package))
tmpdesc{end+1} = descs{j};
break;
endif
endfor
endfor
if (! isempty (tmpdesc))
newdesc = {newdesc{:}, normalize_desc_save_order(tmpdesc){:}, desc};
else
newdesc{end+1} = desc;
endif
endif
endfor
## Eliminate the duplicates.
idx = [];
for i = 1 : length (newdesc)
for j = (i + 1) : length (newdesc)
if isequal (packajoozle.internal.PkgVer (newdesc{i}.name, newdesc{i}.version), ...
packajoozle.internal.PkgVer (newdesc{j}.name, newdesc{j}.version))
idx(end + 1) = j;
endif
endfor
endfor
newdesc(idx) = [];
out = newdesc;
endfunction
function out = file_is_zero_bytes (file)
st = stat (file);
out = st.blocks == 0;
endfunction