Skip to content

Commit

Permalink
Merge pull request #29 from thomas-vincent/fix_installation
Browse files Browse the repository at this point in the history
Fix installation
  • Loading branch information
thomas-vincent authored May 22, 2018
2 parents 7d8005f + da7db38 commit 8ae8298
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 57 deletions.
7 changes: 6 additions & 1 deletion bst_plugin/nst_request_files.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
bst_progress('start', download_title, 'Checking data on server...', 1, length(relative_fns));
for ifn=1:length(relative_fns)
local_fns{ifn} = fullfile(local_repository, strjoin(relative_fns{ifn}, filesep));
output_dir = dirname(local_fns{ifn});
[output_dir, ignore_bfn, ignore_ext] = fileparts(local_fns{ifn});
if ~exist(output_dir, 'dir')
mkdir(output_dir)
end
Expand Down Expand Up @@ -220,3 +220,8 @@
ssize = sprintf('%1.2f Tb', size / 1e12);
end
end


function dn = dirname(fn)
[dn, tmpf, tmpe] = fileparts(fn);
end
2 changes: 1 addition & 1 deletion dist_tools/get_installable_extras.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
if compare_matlab_versions(matlab_version, manifest_mat_version) >= 0
suffixes = [suffixes ['_only_from.' manifest_mat_rdate]];
else
new_funcs = readlines(fullfile(item.folder, item.name));
new_funcs = readlines(fullfile(manifest_dir, item.name));
funcs_not_installed = [funcs_not_installed new_funcs];
funcs_not_installed_requirements = [funcs_not_installed_requirements ...
repmat({manifest_mat_rdate}, ...
Expand Down
4 changes: 2 additions & 2 deletions dist_tools/get_older_matlab_extras.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
if ~isempty(suffixes)
s_extras = suffixes{1};
for iextra=2:length(suffixes)
s_extras = [s_extras ', ' suffixes{iextra}];
s_extras = ['MANIFEST' s_extras ', ' suffixes{iextra}];
end
fprintf('Adding functions for matlab versions %s to support current version %s.\n', ...
fprintf('Adding functions from %s to support matlab version %s.\n', ...
s_extras, matlab_version);
end

Expand Down
45 changes: 15 additions & 30 deletions dist_tools/install_package.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ function make_file_operations_script(operations, script_fn, header, dry)
for ifn=1:length(source_rfns)
source_fn = fullfile(source_dir, source_rfns{ifn});
if ~exist(source_fn, 'file')
throw(MException('DistPackage:FileNotFound', [source_fn ' does not exist in source dir']));
throw(MException('DistPackage:FileNotFound', [protect_path(source_fn) ' does not exist in source dir']));
end
target_fn = fullfile(target_dir, source_rfns{ifn});
if exist(target_fn, 'file')
backup_rfn = add_fn_prefix(source_rfns{ifn}, backup_prefix);
warning('DistPackage:ExistingTarget', ...
['"' source_rfns{ifn} '" already exists in target directory. ' ...
['"' protect_path(source_rfns{ifn}) '" already exists in target directory. ' ...
'It will be backuped to "' backup_rfn '"']);
install_operations(iop).file1 = target_fn;
install_operations(iop).action = 'move';
Expand Down Expand Up @@ -227,11 +227,13 @@ function make_file_operations_script(operations, script_fn, header, dry)
function fns = read_manifest(manifest_fn)
fns = cellfun(@(fn) strtrim(fn), strsplit_(fileread(manifest_fn), '\n'), 'UniformOutput', false);
fns = fns(~cellfun(@isempty, fns));
files_not_found = cellfun(@(fn) ~exist(fullfile(fileparts(manifest_fn), fn), 'file'), fns);
[root, ignore_bfn, ignore_ext] = fileparts(manifest_fn);
files_not_found = cellfun(@(fn) ~exist(fullfile(root, fn), 'file'), fns);
if any(files_not_found)
fns_not_found = cellfun(@(fn) protect_path(fn), fns(files_not_found), 'UniformOutput', false);
throw(MException('DistPackage:FileNotFound', ...
sprintf('Non-existing files from %s:\n%s', ...
manifest_fn, strjoin_(fns(files_not_found), '\n'))));
protect_path(manifest_fn), strjoin_(fns_not_found, '\n'))));
end
end

Expand All @@ -245,12 +247,12 @@ function execute_file_operations(operations, dry)
operation = operations(iop);
if isdir(operation.file2) && exist(operation.file2, 'dir') || exist(operation.file2, 'file')
throw(MException('DistPackage:TargetExists', ...
['Target ' operation.file2 ' already exists. ' ...
['Target ' protect_path(operation.file2) ' already exists. ' ...
'Installation aborted, consider manually cleaning target directory.']));
end
if ~isfield(operation, 'dont_check_file1') && (isdir(operation.file1) && ~exist(operation.file1, 'dir') || ~exist(operation.file1, 'file'))
throw(MException('DistPackage:FileNotFound', ...
[operation.file1 ' does not exist. ' ...
[protect_path(operation.file1) ' does not exist. ' ...
'Installation aborted, consider manually cleaning target directory.']));
end
if ~dry
Expand Down Expand Up @@ -290,13 +292,13 @@ function execute_file_operations(operations, dry)
else
switch operation.action
case 'copy'
disp(['copy ' operation.file1 ' to ' operation.file2]);
disp(['copy ' protect_path(operation.file1) ' to ' protect_path(operation.file2)]);
case 'link'
disp(['link ' operation.file1 ' to ' operation.file2]);
disp(['link ' protect_path(operation.file1) ' to ' protect_path(operation.file2)]);
case 'remove'
disp(['remove ' operation.file1]);
disp(['remove ' protect_path(operation.file1)]);
case 'move'
disp(['move ' operation.file1 ' to ' operation.file2]);
disp(['move ' protect_path(operation.file1) ' to ' protect_path(operation.file2)]);
end
end
end
Expand Down Expand Up @@ -330,7 +332,9 @@ function check_inputs(package_name, source_dir, target_dir, mode, extras, dry)
end

if ~exist(source_dir, 'dir')
throw(MException('DistPackage:DirNotFound', 'source_dir does not exist'));
throw(MException('DistPackage:DirNotFound', ...
sprintf('source_dir "%s "does not exist', ...
protect_path(source_dir))));
end

if ~exist(target_dir, 'dir')
Expand Down Expand Up @@ -378,25 +382,6 @@ function check_inputs(package_name, source_dir, target_dir, mode, extras, dry)
end
end

function joined = strjoin_(toks, delimiter)
% For compatibility
if ~verLessThan('matlab', '8.2')
joined = strjoin(toks, delimiter);
else
d = delimiter;
n = numel(toks);
if n == 0
joined = '';
elseif n == 1
joined = toks{1};
else
ss = cell(1, 2 * n - 1);
ss(1:2:end) = toks;
[ss{2:2:end}] = deal(d);
joined = [ss{:}];
end
end
end

function ppath = protect_path(path)
ppath = strrep(path, '\', '\\');
Expand Down
19 changes: 19 additions & 0 deletions dist_tools/strjoin_.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function joined = strjoin_(toks, delimiter)
% For compatibility
if ~verLessThan('matlab', '8.2')
joined = strjoin(toks, delimiter);
else
d = delimiter;
n = numel(toks);
if n == 0
joined = '';
elseif n == 1
joined = toks{1};
else
ss = cell(1, 2 * n - 1);
ss(1:2:end) = toks;
[ss{2:2:end}] = deal(d);
joined = [ss{:}];
end
end
end
2 changes: 1 addition & 1 deletion run_tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function run_tests(to_run, stop_on_error, do_coverage, re_match_filter)
%
install_error_msg = sprintf(['To run unit tests, nirstorm debug functions must be installed.\n'...
' Use nst_install(''copy'', ''debug'') or ' ...
'nst_install(''copy'', ''debug'') (linux only).\n'...
'nst_install(''link'', ''debug'') (linux only).\n'...
'WARNING: these functions override brainstorm behavior.']);
try
if ~exist(fullfile(bst_get('BrainstormUserDir'), 'process', 'bst_error'), 'file')
Expand Down
46 changes: 24 additions & 22 deletions test/InstallSourceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,22 @@ function test_package_install(testCase)
testCase.assertTrue(all(~files_exist(target_dir, base_rel_fns)));
testCase.assertTrue(~exist(uninstall_script_fn, 'file')>0);

% Test installation and uninstallation, link mode
install_package(package_name, root_src_dir, target_dir, 'link', {}, 0);
testCase.assertTrue(all(files_exist(testCase.tmp_dir, package_rel_files)));
testCase.assertTrue(all(files_exist(target_dir, base_rel_fns)));
testCase.assertTrue(all(files_are_symlinks(target_dir, base_rel_fns)));
testCase.assertTrue(all(~files_exist(target_dir, extra_rel_fns)));
testCase.assertTrue(all(~files_exist(target_dir,files_not_to_install)));
testCase.assertTrue(exist(uninstall_script_fn, 'file')>0);

uninstall_package(package_name, target_dir);
testCase.assertTrue(all(files_exist(testCase.tmp_dir, package_rel_files)));
testCase.assertTrue(all(~files_exist(target_dir, base_rel_fns)));
testCase.assertTrue(~exist(uninstall_script_fn, 'file')>0);

if isempty(strfind(computer, 'WIN'))
% Test installation and uninstallation, link mode
install_package(package_name, root_src_dir, target_dir, 'link', {}, 0);
testCase.assertTrue(all(files_exist(testCase.tmp_dir, package_rel_files)));
testCase.assertTrue(all(files_exist(target_dir, base_rel_fns)));
testCase.assertTrue(all(files_are_symlinks(target_dir, base_rel_fns)));
testCase.assertTrue(all(~files_exist(target_dir, extra_rel_fns)));
testCase.assertTrue(all(~files_exist(target_dir,files_not_to_install)));
testCase.assertTrue(exist(uninstall_script_fn, 'file')>0);

uninstall_package(package_name, target_dir);
testCase.assertTrue(all(files_exist(testCase.tmp_dir, package_rel_files)));
testCase.assertTrue(all(~files_exist(target_dir, base_rel_fns)));
testCase.assertTrue(~exist(uninstall_script_fn, 'file')>0);
end

% Test installation with already existing items in target_dir
% -> should be backuped
existing_target_fn = fullfile(target_dir, 'func1.m');
Expand All @@ -192,7 +194,7 @@ function test_package_install(testCase)
fout = fopen(existing_target_fn, 'w');
fprintf(fout, 'blah');
fclose(fout);
install_package(package_name, root_src_dir, target_dir, 'link', {}, 0);
install_package(package_name, root_src_dir, target_dir, 'copy', {}, 0);
testCase.assertTrue(exist(existing_target_backup_fn, 'file')>0);
testCase.assertTrue(all(files_exist(testCase.tmp_dir, package_rel_files)));
testCase.assertTrue(all(files_exist(target_dir, base_rel_fns)));
Expand All @@ -213,7 +215,7 @@ function test_package_install(testCase)
fout = fopen(existing_target_fn, 'w');
fprintf(fout, 'blah');
fclose(fout);
install_package(package_name, root_src_dir, target_dir, 'link', {}, 0);
install_package(package_name, root_src_dir, target_dir, 'copy', {}, 0);
testCase.assertTrue(exist(existing_target_backup_fn, 'file')>0);
testCase.assertTrue(all(files_exist(testCase.tmp_dir, package_rel_files)));
testCase.assertTrue(all(files_exist(target_dir, base_rel_fns)));
Expand Down Expand Up @@ -292,7 +294,7 @@ function test_package_install_errors(testCase)
fclose(fout);
exception_caught = 0;
try
install_package(package_name, root_src_dir, target_dir, 'link', {}, 0);
install_package(package_name, root_src_dir, target_dir, 'copy', {}, 0);
catch ME
testCase.assertTrue(strcmp(ME.identifier, 'DistPackage:TargetExists'))
testCase.assertTrue(~isempty(strfind(ME.message, existing_target_backup_fn)));
Expand All @@ -307,7 +309,7 @@ function test_package_install_errors(testCase)
delete(version_fn)
exception_caught = 0;
try
install_package(package_name, root_src_dir, target_dir, 'link', {}, 0);
install_package(package_name, root_src_dir, target_dir, 'copy', {}, 0);
catch ME
testCase.assertTrue(strcmp(ME.identifier, 'DistPackage:FileNotFound'))
testCase.assertTrue(~isempty(strfind(ME.message, 'VERSION file')));
Expand All @@ -324,7 +326,7 @@ function test_package_install_errors(testCase)
write_file(version_fn, {version_tag});
exception_caught = 0;
try
install_package(package_name, root_src_dir, target_dir, 'link', {}, 0);
install_package(package_name, root_src_dir, target_dir, 'copy', {}, 0);
catch ME
testCase.assertTrue(strcmp(ME.identifier, 'DistPackage:BadVersionTag'))
testCase.assertTrue(~isempty(strfind(ME.message, version_tag)));
Expand All @@ -340,7 +342,7 @@ function test_package_install_errors(testCase)
write_file(version_fn, {version_tag});
exception_caught = 0;
try
install_package(package_name, root_src_dir, target_dir, 'link', {}, 0);
install_package(package_name, root_src_dir, target_dir, 'copy', {}, 0);
catch ME
testCase.assertTrue(strcmp(ME.identifier, 'DistPackage:BadVersionTag'))
exception_caught = 1;
Expand Down Expand Up @@ -397,7 +399,7 @@ function test_uninstall_error(testCase)
fprintf(fout, 'blah');
fclose(fout);

install_package(package_name, root_src_dir, target_dir, 'link', {}, 0);
install_package(package_name, root_src_dir, target_dir, 'copy', {}, 0);
delete(existing_target_backup_fn);
delete(existing_target_fn);
exception_caught = 0;
Expand Down Expand Up @@ -451,7 +453,7 @@ function write_file(fn, lines)

function flags = files_are_symlinks(root_dir, rel_fns)
if ~isempty(strfind(computer, 'WIN'))
flags = zeros(1, length(rel_fns))==0;
flags = zeros(1, length(rel_fns))==1;
else
flags = cellfun(@(rfn) unix(['test -L ' fullfile(root_dir, rfn)])==0, rel_fns);
end
Expand Down
3 changes: 3 additions & 0 deletions test/bst_create_test_subject.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

sTemplates = bst_get('AnatomyDefaults');
iTemplate = strcmpi('Colin27_4NIRS', {sTemplates.Name});
if ~any(iTemplate)
error('Template Colin27_4NIRS not found');
end
db_set_template(iSubject, sTemplates(iTemplate), 0);
db_save();
sSubject = bst_get('Subject', subject_name);
Expand Down

0 comments on commit 8ae8298

Please sign in to comment.