spm_update.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. function varargout = spm_update(update)
  2. % Check (and install) SPM updates from the FIL server
  3. % FORMAT spm_update
  4. % This function will contact the FIL server, compare the version number of
  5. % the updates with the one of the SPM installation currently in the MATLAB
  6. % path and display the outcome.
  7. %
  8. % FORMAT spm_update(update)
  9. % Invoking this function with any input parameter will do the same as
  10. % above but will also attempt to download and install the updates.
  11. % Note that it will close any open window and clear the workspace.
  12. %
  13. % FORMAT [sts, msg] = spm_update(update)
  14. % sts - status code:
  15. % NaN - SPM server not accessible
  16. % Inf - no updates available
  17. % 0 - SPM installation up to date
  18. % n - new revision <n> is available for download
  19. % msg - string describing outcome, that would otherwise be displayed.
  20. %__________________________________________________________________________
  21. % Copyright (C) 2010-2018 Wellcome Trust Centre for Neuroimaging
  22. % Guillaume Flandin
  23. % $Id: spm_update.m 7486 2018-11-14 11:10:29Z guillaume $
  24. vspm = spm('Ver');
  25. url = ['http://www.fil.ion.ucl.ac.uk/spm/download/' lower(vspm) '_updates/'];
  26. if ~nargin
  27. update = false;
  28. else
  29. update = true;
  30. end
  31. %-Get list of updates from SPM server
  32. %--------------------------------------------------------------------------
  33. [s,status] = urlread(url);
  34. if ~status
  35. sts = NaN;
  36. msg = 'Cannot access SPM server.';
  37. if ~nargout, error(msg); else varargout = {sts, msg}; end
  38. return
  39. end
  40. %-Get revision number of latest update
  41. %--------------------------------------------------------------------------
  42. n = regexp(s,[lower(vspm) '_updates_r(\d.*?)\.zip'],'tokens','once');
  43. if isempty(n)
  44. sts = Inf;
  45. msg = 'There are no updates available yet.';
  46. if ~nargout, fprintf([blanks(9) msg '\n']);
  47. else varargout = {sts, msg}; end
  48. return
  49. end
  50. n = str2double(n{1});
  51. %-Get revision number of SPM installation
  52. %--------------------------------------------------------------------------
  53. try
  54. [v,r] = spm('Ver','',1); r = str2double(r);
  55. catch
  56. error('SPM cannot be found in MATLAB path.');
  57. end
  58. if ~strcmp(v,vspm), error('Your SPM version is %s and not %s',v,vspm); end
  59. rs = [6225 6470 6685 6906 7219 7487];
  60. if isnan(r), r = rs(1); end
  61. if floor(r) == str2double(vspm(4:end))
  62. try
  63. r = rs(round((r-floor(r))*10)+1);
  64. catch
  65. r = rs(end);
  66. end
  67. end
  68. %-Compare versions
  69. %--------------------------------------------------------------------------
  70. if n > r
  71. sts = n;
  72. msg = sprintf(' A new version of %s is available on:\n',vspm);
  73. msg = [msg sprintf(' %s\n',url)];
  74. msg = [msg sprintf(' (Your version: %d - New version: %d)\n',r,n)];
  75. if ~nargout, fprintf(msg); else varargout = {sts, msg}; end
  76. else
  77. sts = 0;
  78. msg1 = sprintf('Your version of %s is up to date.',vspm);
  79. msg2 = sprintf('(Your version: %d - Online version: %d)',r,n);
  80. if ~nargout, fprintf([blanks(9) msg1 '\n' blanks(5) msg2 '\n']);
  81. else varargout = {sts, sprintf('%s\n%s',msg1,msg2)}; end
  82. return
  83. end
  84. %-and install...
  85. %--------------------------------------------------------------------------
  86. if update
  87. d = spm('Dir');
  88. delete(get(0,'Children')); spm('clean'); evalc('spm_rmpath'); drawnow
  89. try
  90. lastwarn('');
  91. m = ' Download and install in progress...\n';
  92. if ~nargout, fprintf(m); else varargout = {sts, [msg m]}; end
  93. s = unzip([url sprintf('%s_updates_r%d.zip',lower(vspm),n)], d);
  94. m = sprintf(' Success: %d files have been updated.\n',numel(s));
  95. if ~nargout, fprintf(m); else varargout = {sts, [msg m]}; end
  96. catch
  97. le = lasterror;
  98. switch le.identifier
  99. case 'MATLAB:checkfilename:urlwriteError'
  100. fprintf(' Update failed: cannot download update file.\n');
  101. otherwise
  102. fprintf('\n%s\n',le.message);
  103. end
  104. end
  105. [warnmsg, msgid] = lastwarn;
  106. switch msgid
  107. case ''
  108. case 'MATLAB:extractArchive:unableToCreate'
  109. fprintf(' Update failed: check folder permission.\n');
  110. case 'MATLAB:extractArchive:unableToOverwrite'
  111. fprintf(' Update failed: check file permissions.\n');
  112. otherwise
  113. fprintf(' Update failed: %s.\n',warnmsg);
  114. end
  115. addpath(d);
  116. rehash toolboxcache;
  117. end