diff --git a/+dj/Connection.m b/+dj/Connection.m index 650814ed..517486fc 100644 --- a/+dj/Connection.m +++ b/+dj/Connection.m @@ -28,15 +28,6 @@ % initQuery is the SQL query to be executed at the start % of each new session. setupDJ(true); - try - mymVersion = mym('version'); - assert(mymVersion.major > 2 || mymVersion.major==2 && mymVersion.minor>=6) - catch - error 'Outdated version of mYm. Please upgrade to version 2.6 or later' - end - if verLessThan('matlab', '8.6') - error 'MATLAB version 8.6 (R2015b) or greater is required' - end self.host = host; self.user = username; self.password = password; diff --git a/+tests/TestSetupMYM.m b/+tests/TestSetupMYM.m new file mode 100644 index 00000000..9e79b6cd --- /dev/null +++ b/+tests/TestSetupMYM.m @@ -0,0 +1,117 @@ + +classdef TestSetupMYM < tests.Prep +% TestSetupMyM tests setupMYM + + methods (Test) + function TestSetupMYM_testDefaultInstall(testCase) + + [dir, nam, ext] = fileparts(mfilename('fullpath')); + tmym = strcat(dir, '/../', 'mym'); + + if isdir(tmym) + fprintf('testDefaultInstall: mymdir %s exists. removing\n', ... + tmym); + rmdir(tmym, 's'); + end + + ms = setupMYM(); + testCase.verifyTrue(strcmp(ms, 'master')); + testCase.verifyTrue(isdir(tmym)); + + end + function TestSetupMYM_testVersionInstallFresh(testCase) + [dir, nam, ext] = fileparts(mfilename('fullpath')); + tmym = strcat(dir, '/../', 'mym'); + + if isdir(tmym) + fprintf('testVersionInstallFresh: removing mymdir %s\n', ... + tmym); + rmdir(tmym, 's'); + end + + % TODO: how manage version string? + ms = setupMYM('2.7.2'); + testCase.verifyTrue(strcmp(ms, '2.7.2')); + testCase.verifyTrue(isdir(tmym)); + + end + function TestSetupMYM_testVersionInstallStale(testCase) + [dir, nam, ext] = fileparts(mfilename('fullpath')); + tmym = strcat(dir, '/../', 'mym'); + + if ~isdir(tmym) % XXX: valid? how handle otherwise? + fprintf('testVersionInstallStale: spoofing mymdir %s\n', ... + tmym); + mkdir(tmym); + testCase.verifyTrue(isdir(tmym)); + end + + % TODO: how manage version string? + ms = setupMYM('2.7.2'); % TODO: how properly verify? + % also: .. persistent & test state ... + + end + function TestSetupMYM_testVersionInstallStaleForce(testCase) + [dir, nam, ext] = fileparts(mfilename('fullpath')); + tmym = strcat(dir, '/../', 'mym'); + + if ~isdir(tmym) % XXX: valid? how handle otherwise? + fprintf('testVersionInstallStaleForce: spoofing mymdir %s\n', ... + tmym); + mkdir(tmym); + testCase.verifyTrue(isdir(tmym)); + end + + % TODO: how manage version string? + ms = setupMYM('2.7.2', true); + testCase.verifyTrue(strcmp(ms, '2.7.2')); + testCase.verifyTrue(isdir(tmym)); + + end + function TestSetupMYM_testMasterInstallFresh(testCase) + [dir, nam, ext] = fileparts(mfilename('fullpath')); + tmym = strcat(dir, '/../', 'mym'); + + if isdir(tmym) + fprintf('testMasterInstallFresh: removing mymdir %s\n', ... + tmym); + rmdir(tmym, 's'); + end + + ms = setupMYM('master'); + testCase.verifyTrue(strcmp(ms, 'master')); + testCase.verifyTrue(isdir(tmym)); + + end + function TestSetupMYM_testMasterInstallStale(testCase) + [dir, nam, ext] = fileparts(mfilename('fullpath')); + tmym = strcat(dir, '/../', 'mym'); + + if ~isdir(tmym) % XXX: valid? how handle otherwise? + fprintf('testMasterInstallStale: spoofing mymdir %s\n', ... + tmym); + mkdir(tmym); + testCase.verifyTrue(isdir(tmym)); + end + + ms = setupMYM('master'); % TODO: how verify fail? + % also: .. persistent & test state ... + end + function TestSetupMYM_testMasterInstallStaleForce(testCase) + [dir, nam, ext] = fileparts(mfilename('fullpath')); + tmym = strcat(dir, '/../', 'mym'); + + if ~isdir(tmym) % XXX: valid? how handle otherwise? + fprintf('testMasterInstallStaleForce: spoofing mymdir %s\n', ... + tmym); + mkdir(tmym); + testCase.verifyTrue(isdir(tmym)); + end + + ms = setupMYM('master'); + testCase.verifyTrue(strcmp(ms, 'master')); + testCase.verifyTrue(isdir(tmym)); + + end + end +end diff --git a/setupDJ.m b/setupDJ.m index 25bb9b07..e22acef6 100644 --- a/setupDJ.m +++ b/setupDJ.m @@ -10,38 +10,31 @@ function setupDJ(skipPathAddition, force) return end + if verLessThan('matlab', '9.1') + error('DataJoint:System:UnsupportedMatlabVersion', ... + 'MATLAB version 9.1 (R2016b) or greater is required'); + end + base = fileparts(mfilename('fullpath')); if nargin < 1 skipPathAddition = false; end - + if ~skipPathAddition - fprintf('Adding DataJoint to the path...\n') - addpath(base) + fprintf('Adding DataJoint to the path...\n'); + addpath(base); end - mymdir = fullfile(base, 'mym'); - % if mym directory missing, download and install - if ~isdir(mymdir) - fprintf('mym missing. Downloading...\n') - target = fullfile(base, 'mym.zip'); - mymURL = 'https://github.com/datajoint/mym/archive/master.zip'; - target = websave(target, mymURL); - if isunix && ~ismac - % on Linux Matlab unzip doesn't work properly so use system unzip - system(sprintf('unzip -o %s -d %s', target, base)) - else - unzip(target, base) - end - % rename extracted mym-master directory to mym - movefile(fullfile(base, 'mym-master'), mymdir) - delete(target) + setupMYM('master', force); + + try + mymVersion = mym('version'); + assert(mymVersion.major > 2 || mymVersion.major==2 && mymVersion.minor>=6); + catch + error('DataJoint:System:UnsupportedMyMVersion', ... + 'Outdated version of mYm. Please upgrade to version 2.6 or later'); end - - % run mymSetup.m - fprintf('Setting up mym...\n') - run(fullfile(mymdir, 'mymSetup.m')) - + INVOKED = 1; end diff --git a/setupMYM.m b/setupMYM.m new file mode 100644 index 00000000..7fa2e2a1 --- /dev/null +++ b/setupMYM.m @@ -0,0 +1,76 @@ +function mymVersion = setupMYM(version, force) + + default_version = 'master'; % else git tag string e.g. '2.7.2' + + if nargin < 1 + version = default_version; + end + if nargin < 2 + force = false; + end + + persistent INSTALLED_VERSION; + + if ~isempty(INSTALLED_VERSION) && ~force + % note: for 'master', is currently 'master' not mym('version') + % using mym('version') needs a master:version mapping, + % and how to handle this is a wider dev issue (releses, tests, etc) + mymVersion = INSTALLED_VERSION; + return; + end + + base = fileparts(mfilename('fullpath')); + + mymdir = fullfile(base, 'mym'); + + if isdir(mymdir) + if force + fprintf('force install.. removing %s\n', mymdir); + rmdir(mymdir, 's'); + elseif(~strcmp(version, default_version)) + warning('DataJoint:System:setupMyMwarning', ... + ['Warning: mym directory exists. not re-installing.\n', ... + ' to override, pass force=true\n']); + end + end + + if ~isdir(mymdir) %% mym directory missing, download and install + fprintf('Installing %s...\n', version); + target = fullfile(base, 'mym.zip'); + + mymURL = 'https://github.com/datajoint/mym/archive/'; + + if strcmp(version, 'master') + mymURL = strcat(mymURL, version, '.zip'); + else + mymURL = strcat(mymURL, 'v', version, '.zip'); + end + + fprintf('downloading %s to %s\n', mymURL, target); + target = websave(target, mymURL); + + extdir = fullfile(base, sprintf('mym-%s', version)); + fprintf('extracting %s into %s\n', target, extdir); + + if isunix && ~ismac + % on Linux Matlab unzip doesn't work properly so use system unzip + system(sprintf('unzip -o %s -d %s', target, base)); + else + unzip(target, base); + end + + % rename extracted mym-master directory to mym + fprintf('renaming %s to %s\n', extdir, mymdir); + movefile(extdir, mymdir); + + delete(target); + end + + % run mymSetup.m + fprintf('Setting up mym...\n'); + run(fullfile(mymdir, 'mymSetup.m')); + + mymVersion = version; + INSTALLED_VERSION = mymVersion; + +end