-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmpi.m
1251 lines (1142 loc) · 41.2 KB
/
mpi.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function mpi(action, varargin)
%MPI Matlab Package Installer
% function mpi(ACTION, varargin)
%
% ACTION can be any of the following:
% 'init' add all installed packages in default install directory to path
% 'search' finds a url for a package by name (searches Github and File Exchange)
% 'install' installs a package by name
% 'uninstall' installs a package by name
% 'freeze' list all installed packages (optional: in install-dir)
% 'set' change options for an already installed package
%
% If ACTION is one of 'search', 'install', or 'uninstall', then you must
% provide a package NAME as the next argument (e.g., 'matlab2tikz')
%
%
% Examples:
%
% % Add all installed packages to the path (e.g. to be run at startup)
% mpi init
%
% % Search for a package called 'test' on Matlab File Exchange
% mpi search test
%
% % Install a package called 'test'
% mpi install test
%
% % Uninstall a package called 'test'
% mpi uninstall test
%
% % List all installed packages
% mpi freeze
%
% % Change the folder added to the path in an already installed package
% mpi set test -n folder_name_to_add
%
% To modify the default behavior of the above commands,
% the following optional arguments are available:
%
% name-value arguments:
% url (-u): optional; if does not exist, must search
% infile (-i): if set, will run mpi on all packages listed in file
% install-dir (-d): where to install package
% query (-q): if name is different than query
% release-tag (-t): if url is found on github, this lets user set release tag
% internal-dir (-n): lets user set which directories inside package to add to path
% collection (-c): override mpi's default package collection ("default")
% by specifying a custom collection name
%
% arguments that are true if passed (otherwise they are false):
% --github-first (-g): check github for url before matlab fileexchange
% --force (-f): install package even if name already exists in InstallDir
% --approve: when using -i, auto-approve the installation without confirming
% --debug: do not install anything or update paths; just pretend
% --no-paths: no paths are added after installing (default if -c is specified)
% --all-paths: add path to all subfolders in package
% --local: url is a path to a local directory to install (add '-e' to not copy)
% --use-local (-e): skip copy operation during local install
%
% For more help, or to report an issue, see <a href="matlab:
% web('https://github.com/mobeets/mpi')">the mpi Github page</a>.
%
% print help info if no arguments were provided
if nargin < 1
help mpi;
return;
end
% parse and validate command line args
[package, opts] = setDefaultOpts();
[package, opts] = parseArgs(package, opts, action, varargin);
validateArgs(package, opts);
if opts.debug
warning(i18n('debug_message'));
end
disp(i18n('setup_option', opts.collection));
% installing from requirements
if ~isempty(opts.inFile)
% read filename, and call mpi for all lines in this file
readRequirementsFile(opts.inFile, opts);
return;
end
% load metadata
[opts.metadata, opts.metafile] = getMetadata(opts);
% mpi init
if strcmpi(opts.action, 'init')
opts.updateMpiPaths = true;
updatePaths(opts);
return;
end
% mpi freeze
if strcmpi(opts.action, 'freeze')
listPackages(opts);
return;
end
% mpi set
if strcmpi(opts.action, 'set')
changePackageOptions(package, opts);
return;
end
% mpi uninstall
if strcmpi(opts.action, 'uninstall')
removePackage(package, opts);
return;
end
% mpi search OR mpi install
findAndSetupPackage(package, opts);
end
function success = findAndSetupPackage(package, opts)
success = true;
package.installDir = fullfile(opts.installDir, package.name);
disp(i18n('setup_start', package.name));
% check if exists
if ( ...
~opts.force ...
&& ~strcmpi(opts.action, 'search') ...
&& ~isempty(indexInMetadata(package, opts.metadata.packages)) ...
)
warning(i18n('package_collision'));
success = false;
return;
end
% find url if not set
if isempty(package.url)
package.url = findUrl(package, opts);
end
% download package and add to metadata
if ~isempty(package.url) && strcmpi(opts.action, 'install')
if ~opts.localInstall
disp(i18n('setup_download', package.url));
else
disp(i18n('setup_install'));
end
[package, isOk] = installPackage(package, opts);
if ~isempty(package) && isOk
opts = addToMetadata(package, opts);
if ~opts.noPaths
disp(i18n('setup_updating'));
updatePath(package, opts);
end
end
end
end
function removePackage(package, opts)
packages = opts.metadata.packages;
[~, ix] = indexInMetadata(package, packages);
if ~any(ix)
disp(i18n('remove_404', package.name));
return;
end
% delete package directories if they exist
removalQueue = packages(ix);
disp(i18n('remove_start', num2str(sum(ix)), package.name));
if ~opts.force
reply = input(i18n('confirm'), 's');
if isempty(reply)
reply = i18n('confirm_yes');
end
if ~strcmpi(reply(1), i18n('confirm_yes'))
disp(i18n('confirm_nvm'));
return;
end
end
for ii = 1:numel(removalQueue)
package = removalQueue(ii);
% check for uninstall file
dirPath = fullfile(package.installDir, package.mdir);
checkForFileAndRun(dirPath, 'uninstall.m', opts);
if exist(package.installDir, 'dir')
% remove old directory
if ~package.noRmdirOnUninstall
rmdir(package.installDir, 's');
else
installDir = package.installDir;
disp(i18n('remove_preexist', installDir));
end
end
end
% write new metadata to file
packages = packages(~ix);
if ~opts.debug
save(opts.metafile, 'packages');
end
disp(i18n('remove_complete'));
end
function dispTree(name)
folderNames = dir(name);
folderNames = {folderNames([folderNames.isdir] == 1).name};
for i = 1:length(folderNames)
if i == length(folderNames)
disp([char(9492) char(9472) char(9472) char(9472) folderNames{i}])
elseif ~endsWith(folderNames{i}, '.')
disp([char(9500) char(9472) char(9472) char(9472) folderNames{i}])
end
end
end
function changePackageOptions(package, opts)
% find existing package
packageMetadata = opts.metadata.packages;
[~, ix] = indexInMetadata(package, packageMetadata);
if ~any(ix)
warning(i18n('update_404', package.name));
return;
end
assert(sum(ix) == 1, i18n('options_conflict'));
oldPackage = packageMetadata(ix);
% update options
if opts.noPaths
disp(i18n('update_nopaths', package.name));
oldPackage.addPath = false;
end
if package.addAllDirsToPath
disp(i18n('update_pathdirs', package.name));
oldPackage.addAllDirsToPath = true;
if ~oldPackage.addPath
disp(i18n('update_addpath'));
oldPackage.addPath = true;
end
end
if ~isempty(package.internalDir)
if exist(fullfile(oldPackage.installDir, package.internalDir), 'dir')
oldPackage.mdir = package.internalDir;
oldPackage.internalDir = package.internalDir;
disp(i18n('update_package', package.name, package.internalDir));
if ~oldPackage.addPath
disp(i18n('update_addpath'));
oldPackage.addPath = true;
end
else
if numel(folderNames) == 0
warning(i18n('internal_no_dirs'));
else
warning(i18n('internal_nosuchdir'));
dispTree(oldPackage.installDir);
end
end
end
% write new metadata to file
packageMetadata(ix) = oldPackage;
packages = packageMetadata;
if ~opts.debug
save(opts.metafile, 'packages');
end
end
function listPackages(opts)
packages = opts.metadata.packages;
if isempty(packages)
disp(i18n('list_404', opts.installDir));
return;
end
disp(i18n('list_current', opts.installDir));
for ii = 1:numel(packages)
package = packages(ii);
packageName = package.name;
if ~isempty(package.releaseTag)
packageName = [packageName '==' package.releaseTag]; %#ok<*AGROW>
end
out = [' - ' packageName ' (' package.downloadDate ')'];
cdir = fileparts(package.installDir);
if ~strcmpi(cdir, opts.installDir)
out = [out ' : ' package.installDir];
end
disp(out);
end
end
function [package, opts] = setDefaultOpts()
% load opts from config file, and then set additional defaults
% empty package
package.name = '';
package.url = '';
package.internalDir = '';
package.releaseTag = '';
package.addPath = true;
package.localInstall = false;
package.noRmdirOnUninstall = false;
package.addAllDirsToPath = false;
package.collection = 'default';
opts = mpi_config(); % load default opts from config file
opts.installDir = opts.DEFAULT_INSTALL_DIR;
opts.metadir = opts.DEFAULT_INSTALL_DIR;
opts.searchGithubFirst = opts.DEFAULT_CHECK_GITHUB_FIRST;
opts.updateMpiPaths = false;
opts.updateAllPaths = false;
opts.localInstall = false;
opts.localInstallUseLocal = false;
opts.addAllDirsToPath = false;
opts.installDirOverride = false; % true if user sets using -d
opts.inFile = '';
opts.force = false;
opts.approve = false;
opts.debug = false;
opts.noPaths = false;
opts.collection = package.collection;
end
function url = handleCustomUrl(url, releaseTag)
% if .git url, must remove and add /zipball/master
inds = strfind(url, '.git'); % want to match this
inds = setdiff(inds, strfind(url, '.github')); % ignore matches to '.github'
if isempty(inds)
inds = strfind(url, '?download=true');
if isempty(inds) %#ok<*STREMP>
url = [url '?download=true'];
end
return;
end
ind = inds(end);
if ~isempty(releaseTag)
release = ['/zipball/', releaseTag];
else
release = '/zipball/master';
end
url = [url(1:ind-1) release url(ind+4:end)];
end
function url = findUrl(package, opts)
% find url by searching matlab fileexchange and github given opts.name
if ~isempty(package.releaseTag) % tag set, so search github only
url = findUrlOnGithub(package);
elseif opts.searchGithubFirst
url = findUrlOnGithub(package);
if isempty(url) % if nothing found, try file exchange
url = findUrlOnFileExchange(package);
end
else
url = findUrlOnFileExchange(package);
if isempty(url) % if nothing found, try github
url = findUrlOnGithub(package);
end
end
if isempty(url)
disp(i18n('url_404'));
else
disp(i18n('url_found', url));
end
end
function url = findUrlOnFileExchange(package)
% search file exchange, and return first search result
query = package.query;
if isempty(query)
query = package.name;
end
% query file exchange
apiUrl = 'https://api.mathworks.com/community/v1/search';
response = webread(apiUrl, 'query', query, 'scope', 'file-exchange');
% if any packages contain package name exactly, return that one
for ii = 1:numel(response.items)
item = response.items(ii);
if ~isempty(strfind(lower(query), lower(item.title)))
url = [item.url '?download=true'];
return;
end
end
% return first result
if ~isempty(response.items)
url = response.items(1).url;
url = [url '?download=true'];
% urlFormat = @(aid, ver) [ ...
% 'https://www.mathworks.com/' ...
% 'matlabcentral/mlc-downloads/downloads/submissions/' aid ...
% '/versions/' version '/download/zip' ...
% ];
% url = urlFormat(aid, '101'); % 101 works for all? we'll see
else
url = '';
end
end
function url = findUrlOnGithub(package)
% searches github for matlab repositories
% - if releaseTag is set, get url of release that matches
% - otherwise, get url of most recent release
% - and if no releases exist, get url of most recent commit
%
url = '';
query = package.query;
if isempty(query)
query = package.name;
end
% query github for matlab repositories
% https://developer.github.com/v3/search/#search-repositories
% ' ' will be replaced by '+', which seems necessary
% ':' for search qualifiers can be sent encoded on the other hand
qUrl = 'https://api.github.com/search/repositories';
qReq = [query, ' language:matlab'];
html = webread(qUrl, 'q', qReq);
if isempty(html) || ~isfield(html, 'items') || isempty(html.items)
return;
end
% take first repo
item = html.items(1);
if ~isempty(package.releaseTag)
% if release tag set, return the release matching this tag
url = [item.url '/zipball/' package.releaseTag];
else
relUrl = [item.url '/releases/latest'];
try
res = webread(relUrl);
catch
url = [item.html_url '/zipball/master'];
return;
end
if ~isempty(res) && isfield(res, 'zipball_url')
url = res.zipball_url;
else
url = [item.html_url '/zipball/master']; % if no releases found
end
end
end
function [package, isOk] = installPackage(package, opts)
% install package by downloading url, unzipping, and finding paths to add
if opts.debug
isOk = false;
return;
end
isOk = true;
% check for previous package
if exist(package.installDir, 'dir') && ~opts.force
warning(i18n('install_conflict'));
isOk = false;
return;
elseif exist(package.installDir, 'dir')
% remove old directory
disp(i18n('install_remove_previous'));
rmdir(package.installDir, 's');
end
if ( ...
~opts.localInstall ...
&& ~isempty(strfind(package.url, '.git')) ...
&& isempty(strfind(package.url, 'github.com')) ...
)
% install with git clone because not on github
isOk = checkoutFromUrl(package);
if ~isOk
warning(i18n('install_git_error'));
end
elseif ~opts.localInstall
% download zip
package.url = handleCustomUrl(package.url, package.releaseTag);
[isOk, package] = unzipFromUrl(package);
if ( ...
~isOk ...
&& ~isempty(strfind(package.url, 'github.com')) ...
&& isempty(strfind(package.url, '.git')) ...
)
warning(i18n('install_add_git_ext'));
elseif ~isOk
warning(i18n('install_download_error'));
end
else % local install (using pre-existing local directory)
% make sure path exists
if ~exist(package.url, 'dir')
warning(i18n('install_nosuchdir', package.url));
isOk = false; return;
end
% copy directory to installDir
if ~opts.localInstallUseLocal
if ~exist(package.url, 'dir')
warning(i18n('install_404', package.url));
isOk = false; return;
end
mkdir(package.installDir);
isOk = copyfile(package.url, package.installDir);
if ~isOk
warning(i18n('install_error_local'));
end
else % no copy; just track the provided path
% make sure we have absolute path
if ~isempty(strfind(package.url, pwd))
absPath = package.url;
else % try making it ourselves
absPath = fullfile(pwd, package.url);
end
if ~exist(absPath, 'dir')
warning(i18n('install_404', absPath));
isOk = false; return;
else
package.installDir = absPath;
end
end
end
if ~isOk
warning(i18n('install_error', package.name));
return;
end
package.downloadDate = datestr(datetime);
package.mdir = findMDirOfPackage(package);
if isOk
% check for install.m and run after confirming
dirPath = fullfile(package.installDir, package.mdir);
checkForFileAndRun(dirPath, 'install.m', opts);
end
end
function isOk = checkoutFromUrl(package)
% git checkout from url to installDir
isOk = true;
if ispc, quote = '"'; else, quote = ''''; end
if ~isempty(package.releaseTag)
flag = system(['git clone --depth 1 --branch ', package.releaseTag, ' ', package.url, ' ', quote, package.installDir, quote]);
else
flag = system(['git clone --depth 1 ', package.url, ' ', quote, package.installDir, quote]);
end
if (flag ~= 0)
isOk = false;
warning(i18n('checkout_error', package.url));
end
end
function [isOk, package] = unzipFromUrl(package)
% download from url to installDir
isOk = true;
zipFileName = [tempname '.zip'];
try
zipFileName = websave(zipFileName, package.url);
catch ME
% handle 404 from File Exchange for getting updated download url
ps = strsplit(ME.message, 'for URL');
if numel(ps) < 2
isOk = false; return;
end
ps = strsplit(ps{2}, 'github_repo.zip');
package.url = ps{1}(2:end);
zipFileName = websave(zipFileName, package.url);
end
try
unzip(zipFileName, package.installDir);
catch
isOk = false; return;
end
folderNames = dir(package.installDir);
numFolderNames = numel(folderNames);
ndirs = sum([folderNames.isdir]);
if ...
((numFolderNames == 3) && (ndirs == 3)) ...
|| ((numFolderNames == 4) && (ndirs == 3) ...
&& strcmpi(folderNames(~[folderNames.isdir]).name, 'license.txt'))
% only folders are '.', '..', and package folder (call it dirName)
% and then maybe a license file,
% so copy the subtree of dirName and place inside installDir
folderNames = folderNames([folderNames.isdir]);
fldr = folderNames(end).name;
dirName = fullfile(package.installDir, fldr);
try
movefile(fullfile(dirName, '*'), package.installDir);
catch % hack for handling packages like cbrewer 34087
movefile(fullfile(dirName, package.name, '*'), package.installDir);
end
rmdir(dirName, 's');
end
end
function mdir = findMDirOfPackage(package)
% find mdir (folder containing .m files that we will add to path)
if ~package.addPath
mdir = '';
return;
end
if ~isempty(package.internalDir)
if exist(fullfile(package.installDir, package.internalDir), 'dir')
mdir = package.internalDir;
return;
else
warning(i18n('internal_nosuchdir'));
dispTree(package.installDir);
end
end
folderNames = dir(fullfile(package.installDir, '*.m'));
if ~isempty(folderNames)
mdir = ''; % all is well; *.m files exist in base directory
return;
else
M_DIR_ORDER = {'bin', 'src', 'lib', 'code'};
for ii = 1:numel(M_DIR_ORDER)
folderNames = dir(fullfile(package.installDir, M_DIR_ORDER{ii}, '*.m'));
if ~isempty(folderNames)
mdir = M_DIR_ORDER{ii};
return;
end
end
end
warning(i18n('mdir_404'));
disp(i18n('mdir_help', package.name));
dispTree(package.installDir);
% tree
mdir = '';
end
function [m, metafile] = getMetadata(opts)
metafile = fullfile(opts.metadir, 'mpi.mat');
if exist(metafile, 'file')
m = load(metafile);
packages = [];
for ii = 1:numel(m.packages)
pkg = m.packages(ii);
if isfield(pkg, 'internaldir')
pkg.internalDir = pkg.internaldir;
pkg.releaseTag = pkg.release_tag;
pkg.addPath = pkg.addpath;
pkg.localInstall = pkg.local_install;
pkg.noRmdirOnUninstall = pkg.no_rmdir_on_uninstall;
pkg.addAllDirsToPath = pkg.add_all_dirs_to_path;
pkg.installDir = pkg.installdir;
pkg.downloadDate = pkg.date_downloaded;
pkg = rmfield(pkg, { ...
'internaldir', ...
'release_tag', ...
'addpath', ...
'local_install', ...
'no_rmdir_on_uninstall', ...
'add_all_dirs_to_path', ...
'installdir', ...
'date_downloaded' ...
});
end
packages = [packages pkg];
end
save(metafile, 'packages');
m.packages = packages;
else
m = struct();
end
if ~isfield(m, 'packages')
m.packages = [];
end
packages = m.packages;
defaultPkg = setDefaultOpts();
allFieldNames = fieldnames(defaultPkg);
cleanPackages = [];
for ii = 1:numel(packages)
package = packages(ii);
% set any missing fields to default value
missingFields = setdiff(allFieldNames, fieldnames(package));
for jj = 1:numel(missingFields)
cfld = missingFields{jj};
package.(cfld) = defaultPkg.(cfld);
end
% handle manually-deleted packages by skipping if dir doesn't exist
dirPath = fullfile(package.installDir, package.mdir);
if exist(dirPath, 'dir')
cleanPackages = [cleanPackages package];
end
end
m.packages = cleanPackages;
end
function [ind, ix] = indexInMetadata(package, packageMetadata)
if isempty(packageMetadata)
ind = []; ix = [];
return;
end
ix = ismember({packageMetadata.name}, package.name);
ind = find(ix, 1, 'first');
end
function opts = addToMetadata(package, opts)
% update metadata file to track all packages installed
packageMetadata = opts.metadata.packages;
[~, ix] = indexInMetadata(package, packageMetadata);
if any(ix)
packageMetadata = packageMetadata(~ix);
disp(i18n('metadata_replace_op', opts.metafile));
else
disp(i18n('metadata_add_op', opts.metafile));
end
packageMetadata = [packageMetadata package];
% write to file
packages = packageMetadata;
opts.metadata.packages = packages;
if ~opts.debug
save(opts.metafile, 'packages');
end
end
function updatePaths(opts)
% read metadata file and add all paths listed
% add mdir to path for each package in metadata (optional)
namesAdded = {};
if opts.updateMpiPaths
packages = opts.metadata.packages;
for ii = 1:numel(packages)
success = updatePath(packages(ii), opts);
if success
namesAdded = [namesAdded packages(ii).name];
end
end
end
if numel(packages) == 0
disp(i18n('updatepaths_404'));
else
disp(i18n('updatepaths_success', num2str(numel(packages))));
end
% also add all folders listed in install-dir (optional)
if opts.updateAllPaths
c = updateAllPaths(opts, namesAdded);
disp(i18n('updatepaths_all', num2str(c)));
end
end
function success = updatePath(package, opts)
success = false;
if ~package.addPath
return;
end
dirPath = fullfile(package.installDir, package.mdir);
if exist(dirPath, 'dir')
success = true;
if ~opts.debug
disp(i18n('updatepath_op', dirPath));
addpath(dirPath);
end
% add all folders to path
if package.addAllDirsToPath
disp(i18n('updatepath_all'));
addpath(genpath(dirPath));
else % check for pathList.m file
pathfile = fullfile(dirPath, 'pathList.m');
pathsToAdd = checkForPathlistAndGenpath(pathfile, dirPath);
if numel(pathsToAdd) > 0 && ~opts.debug
disp(i18n('updatepath_pathlist'));
addpath(pathsToAdd);
end
end
else
warning(i18n('updatepath_404', dirPath));
return;
end
end
function c = updateAllPaths(opts, namesAlreadyAdded)
% adds all directories inside installDir to path
% ignoring those already added
%
c = 0;
fs = dir(opts.installDir); % get names of everything in install dir
fs = {fs([fs.isdir]).name}; % keep directories only
fs = fs(~strcmp(fs, '.') & ~strcmp(fs, '..')); % ignore '.' and '..'
for ii = 1:numel(fs)
f = fs{ii};
if ~ismember(f, namesAlreadyAdded)
if ~opts.debug
dirPath = fullfile(opts.installDir, f);
disp(mpi_config('updatepath_op', dirPath));
addpath(dirPath);
end
c = c + 1;
end
end
end
function [package, opts] = parseArgs(package, opts, action, varargin)
% function p = parseArgs(action, varargin)
%
% init matlab's input parser and read action
q = inputParser;
actions = { ...
'init', ...
'search', ...
'install', ...
'uninstall', ...
'freeze', ...
'set' ...
};
checkAction = @(x) any(validatestring(x, actions));
addRequired(q, 'action', checkAction);
defaultName = '';
addOptional(q, 'remainingargs', defaultName);
parse(q, action, varargin{:});
opts.action = q.Results.action;
remainingArgs = q.Results.remainingargs;
params = { ...
'collection', '-c', ...
'infile', '-i', ...
'install-dir', '-d', ...
'internal-dir', '-n', ...
'release-tag', '-t', ...
'url', '-u', ...
'--all-paths', ...
'--approve' ...
'--debug', ...
'--use-local', '-e', ...
'--force', '-f', ...
'--no-paths', ...
'--github-first', '-gh', ...
'--local', ...
'--query', '-q', ...
};
% no additional args
if numel(remainingArgs) == 0
if strcmpi(opts.action, 'freeze') || strcmpi(opts.action, 'init')
package.query = '';
return;
else
error(i18n('parseargs_noargin'));
end
end
% if first arg is not a param name, it's the package name
nextArg = remainingArgs{1};
if ~ismember(lower(nextArg), lower(params))
package.name = nextArg;
package.query = '';
remainingArgs = remainingArgs(2:end);
else
package.name = '';
package.query = '';
end
% check for parameters, passed as name-value pairs
usedNextArg = false;
for ii = 1:numel(remainingArgs)
curArg = remainingArgs{ii};
if usedNextArg
usedNextArg = false;
continue;
end
usedNextArg = false;
if strcmpi(curArg, 'url') || strcmpi(curArg, '-u')
nextArg = getNextArg(remainingArgs, ii, curArg);
package.url = nextArg;
usedNextArg = true;
elseif strcmpi(curArg, 'query') || strcmpi(curArg, '-q')
nextArg = getNextArg(remainingArgs, ii, curArg);
package.query = nextArg;
usedNextArg = true;
elseif strcmpi(curArg, 'infile') || strcmpi(curArg, '-i')
nextArg = getNextArg(remainingArgs, ii, curArg);
opts.inFile = nextArg;
usedNextArg = true;
elseif strcmpi(curArg, 'install-dir') || strcmpi(curArg, '-d')
nextArg = getNextArg(remainingArgs, ii, curArg);
opts.installDir = nextArg;
opts.installDirOverride = true;
usedNextArg = true;
elseif strcmpi(curArg, 'collection') || strcmpi(curArg, '-c')
nextArg = getNextArg(remainingArgs, ii, curArg);
opts.collection = nextArg;
package.collection = nextArg;
usedNextArg = true;
elseif strcmpi(curArg, 'internal-dir') || strcmpi(curArg, '-n')
nextArg = getNextArg(remainingArgs, ii, curArg);
package.internalDir = nextArg;
usedNextArg = true;
elseif strcmpi(curArg, 'release-tag') || strcmpi(curArg, '-t')
nextArg = getNextArg(remainingArgs, ii, curArg);
package.releaseTag = nextArg;
usedNextArg = true;
elseif strcmpi(curArg, '--github-first') || ...
strcmpi(curArg, '-g')
opts.searchGithubFirst = true;
elseif strcmpi(curArg, '--force') || strcmpi(curArg, '-f')
opts.force = true;
elseif strcmpi(curArg, '--approve')
opts.approve = true;
elseif strcmpi(curArg, '--debug')
opts.debug = true;
elseif strcmpi(curArg, '--no-paths')
package.addPath = false;
opts.noPaths = true;
elseif strcmpi(curArg, '--all-paths')
package.addAllDirsToPath = true;
opts.addAllDirsToPath = true;
elseif strcmpi(curArg, '--local')
opts.localInstall = true;
package.localInstall = true;
elseif strcmpi(curArg, '--use-local') || strcmpi(curArg, '-e')
opts.localInstallUseLocal = true;
package.noRmdirOnUninstall = true;
else
error(['Did not recognize argument ''' curArg '''.']);
end
end
% update metadir, if collection was set
if ~strcmpi(opts.collection, 'default')
opts.metadir = fullfile(opts.metadir, 'mpi-collections', ...
opts.collection);
opts.installDir = opts.metadir;
if strcmpi(opts.action, 'install')
opts.noPaths = true;
end
end
end
function nextArg = getNextArg(remainingArgs, ii, curArg)
if numel(remainingArgs) <= ii
error(i18n('getnextarg_noargin', curArg));
end
nextArg = remainingArgs{ii+1};
end
function isOk = validateArgs(package, opts)
isOk = true;
if strcmpi(opts.action, 'init')
return;
end
if isempty(package.name) && isempty(opts.inFile)
if ~strcmpi(opts.action, 'freeze')
error(i18n('parseargs_noargin'));
end
end
if ~isempty(opts.inFile)
assert(isempty(package.name), i18n('validateargs_infile_name'));
assert(isempty(package.url), i18n('validateargs_infile_url'));
assert(isempty(package.internalDir), i18n('validateargs_infile_internal_dir'));
assert(isempty(package.releaseTag), i18n('validateargs_infile_release_tag'));
assert(~opts.searchGithubFirst, i18n('validateargs_infile_github_first'));
else
assert(~opts.approve, i18n('validateargs_infile_approve'));
end
if strcmpi(opts.action, 'uninstall')
assert(isempty(package.url), i18n('validateargs_url', 'uninstall'));
assert(isempty(package.query), i18n('validateargs_query', 'uninstall'));
assert(isempty(package.internalDir), i18n('validateargs_internal_dir', 'uninstall'));
assert(isempty(package.releaseTag), i18n('validateargs_release_tag', 'uninstall'));
assert(~opts.searchGithubFirst, i18n('validateargs_github_first', 'uninstall'));
end
if strcmpi(opts.action, 'search')
assert(~opts.force, i18n('validateargs_force_conflict', 'search'));
end
if strcmpi(opts.action, 'freeze')
assert(~opts.force, i18n('validateargs_force_conflict', 'freeze'));
assert(isempty(package.url), i18n('validateargs_url', 'freeze'));
assert(isempty(package.query), i18n('validateargs_query', 'freeze'));
assert(isempty(package.internalDir), i18n('validateargs_internal_dir', 'freeze'));
assert(isempty(package.releaseTag), i18n('validateargs_release_tag', 'freeze'));
assert(~opts.searchGithubFirst, i18n('validateargs_github_first', 'freeze'));
end
if strcmpi(opts.action, 'set')
assert(~opts.force, i18n('validateargs_force_conflict', 'set'));
assert(isempty(package.url), i18n('validateargs_url', 'set'));
assert(isempty(package.query), i18n('validateargs_query', 'set'));
assert(isempty(package.releaseTag), i18n('validateargs_release_tag', 'set'));
assert(~opts.searchGithubFirst, i18n('validateargs_github_first', 'set'));
end
if opts.localInstall