LUKE_startup_jd

PURPOSE ^

SYNOPSIS ^

function [MatToolspath,MatRemotepath,MatHelppath] = LUKE_startup_jd

DESCRIPTION ^

 This function creates or mofifies a startup file to include LUKE paths

 By Joan Decker (CEA/DSM/IRFM, joan.decker@cea.fr) and Yves Peysson (CEA/DSM/IRFM, yves.peysson@cea.fr)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [MatToolspath,MatRemotepath,MatHelppath] = LUKE_startup_jd
0002     %
0003     % This function creates or mofifies a startup file to include LUKE paths
0004     %
0005     % By Joan Decker (CEA/DSM/IRFM, joan.decker@cea.fr) and Yves Peysson (CEA/DSM/IRFM, yves.peysson@cea.fr)
0006     %
0007     MatRemotepath = '';
0008     MatHelppath = '';
0009     %
0010     % -------------------------------------------------------------
0011     % verify that the MatRemote, MatTools and MatHelp toolboxes
0012     % are available on the local machine
0013     % select active directory if there is more than one
0014     % -------------------------------------------------------------
0015     %
0016     searchpath = input('Provide a location to search for Toolboxes and LUKE installations (leave empty to select HOME folder)? ','s');
0017     if ~exist(searchpath,'dir'),
0018         searchpath = '';
0019     end
0020     %
0021     MatToolspath = findandselect_jd('MatTools','d',false,false,'',searchpath);
0022     disp(' ');
0023     if isempty(MatToolspath),
0024         disp('Aborting startup setup.')
0025         return
0026     else
0027         disp(['--> following toolbox selected : ',MatToolspath])
0028     end
0029     %
0030     MatRemotepath = findandselect_jd('MatRemote','d',false,false,'',searchpath);
0031     disp(' ');
0032     if isempty(MatRemotepath),
0033         disp('Aborting startup setup.')
0034         return
0035     else
0036         disp(['--> following toolbox selected : ',MatRemotepath])
0037     end
0038     %
0039     MatHelppath = findandselect_jd('MatHelp','d',false,false,'',searchpath);
0040     disp(' ');
0041     if isempty(MatHelppath),
0042         disp('Aborting startup setup.')
0043         return
0044     else
0045         disp(['--> following toolbox selected : ',MatHelppath])
0046     end
0047     %
0048     % -------------------------------------------------------------
0049     %                   locate & select LUKE installations
0050     % -------------------------------------------------------------
0051     %
0052     LUKEpaths = findandselect_jd('install_LUKE.m','f',true,true,'Which LUKE distributions do you want to include in the startup choice list?',searchpath);
0053     %
0054     nLUKEpaths = length(LUKEpaths);
0055     disp(' ');
0056     if isempty(LUKEpaths),
0057         disp('Aborting startup setup.')
0058         return
0059     else
0060         disp('--> following LUKE versions selected : ')
0061         for iLUKEpath = 1:nLUKEpaths,
0062             disp([' - ',LUKEpaths{iLUKEpath}]);
0063         end
0064     end
0065     %
0066     % -------------------------------------------------------------
0067     %                   locate startup.m file
0068     % -------------------------------------------------------------
0069     %
0070     pathforstartup = fileparts(which('startup.m'));% find existing startup.m file
0071     disp(' ');
0072     if isempty(pathforstartup),
0073         pathforstartup = getenv('HOME');% choose home folder to find or create startup.m file
0074     end
0075     %
0076     startupfile = [pathforstartup,filesep,'startup.m'];
0077     %
0078     if ~exist(startupfile,'file'),
0079         disp(['File startup.m created in folder ',pathforstartup,filesep])
0080     else
0081         disp(['File ',startupfile,' selected.'])
0082     end
0083     %
0084     % -------------------------------------------------------------
0085     %               find or create startup_LUKE.m
0086     % -------------------------------------------------------------
0087     %
0088     startup_LUKE_files = which('startup_LUKE.m','-all');
0089     nfiles = length(startup_LUKE_files);
0090     %
0091     if nfiles > 0,% existing startup_LUKE.m file found on the matlab path
0092         %
0093         startup_LUKEfile = startup_LUKE_files{1};% first file on the matlab path
0094         startup_LUKEpath = fileparts(startup_LUKEfile);
0095         %
0096         disp(' ');
0097         if strcmp(pathforstartup,startup_LUKEpath),% startup_LUKE.m found in the same location as startup.m
0098             disp('A startup_LUKE.m file have been found in the same location as startup.m.');
0099         else% startup_LUKE.m found in a different location
0100             disp(['A startup_LUKE.m file have been found at the following location : ',startup_LUKEpath]);
0101         end
0102         opt = input('It will be replaced by a new one. Are you sure you want to proceed? (y/n) [y] ? ','s');
0103         if isempty(opt),
0104             opt = 'y';
0105         end
0106         %
0107         if ~strcmp(opt,'y'),
0108             disp(' ');
0109             disp('Operation aborted. Please setup startup file manually.');
0110             return
0111         end
0112         %
0113         if ~strcmp(pathforstartup,startup_LUKEpath),
0114             disp(['Please ensure manually that the file ',startup_LUKEfile,' will be in the matlab path upon startup.'])
0115         end
0116         %
0117         if nfiles > 1,% other startup_LUKE.m found on the matlab path
0118             disp(' ');
0119             disp('Additional startup_LUKE.m file have been found in the following locations on the matlab path :');
0120             disp(' ');
0121             %
0122             for ifile = 2:nfiles,
0123                 disp(startup_LUKE_files{ifile});
0124             end
0125             %
0126             disp(' ');
0127             disp(['Please ensure that these additional startup_LUKE.m files will be below the file ',startup_LUKEfile,' on the matlab path']);
0128             disp(' ');
0129             %
0130         end
0131     else
0132         startup_LUKEfile = [pathforstartup,filesep,'startup_LUKE.m'];% startup_LUKE.m file created at the same location as startup.m
0133     end
0134     %
0135     % test that startup_LUKE.m can be created or overwritten
0136     %
0137     fid = fopen(startup_LUKEfile,'w');  
0138     %
0139     if fid == -1,
0140         disp('Warning : Cannot create startup_LUKE.m, permission denied. Please setup startup file manually.');
0141         return
0142     else
0143         fclose(fid);
0144     end
0145     %
0146     if nfiles > 0,% delete existing startup_LUKE.m file
0147         delete(startup_LUKEfile)
0148     end
0149     %
0150     disp('---------------------------------------------------------')
0151     disp(' ');
0152     %
0153     diary(startup_LUKEfile),% create startup_LUKE.m
0154     %
0155     disp('function startup_LUKE');
0156     disp('%');
0157     disp('% Setup paths to MatRemote, MatTools and MatHelp toolboxes');
0158     disp('%');
0159     disp(['matremote_path = ''',MatRemotepath,''';']);% change if needed');
0160     disp(['mattools_path = ''',MatToolspath,''';']);% change if needed');
0161     disp(['mathelp_path = ''',MatHelppath,''';']);% change if needed');
0162     disp('%');
0163     disp('addpath(matremote_path);% MatRemote toolbox');
0164     disp('addpath([matremote_path,filesep,''Profiles'']);% remote profiles');
0165     disp('addpath([matremote_path,filesep,''Bridge'']);% remote profiles');
0166     disp('addpath(mattools_path);% MatTools toolbox');
0167     disp('addpath(mathelp_path);% MatHelp toolbox');
0168     disp('addpath([mathelp_path,filesep,''m2html'']);% m2html library');   
0169     disp('%');
0170     disp('lukeversions = {...');
0171     for ifile = 1:nLUKEpaths,
0172         disp(['   ''',LUKEpaths{ifile},filesep,''',...']);
0173     end
0174     disp('   };');
0175     disp('%');
0176     disp('setappdata(0,''lukeversions'',lukeversions)');
0177     disp('%');
0178     disp('luke_root = getenv(''luke_root'');%prescribed by remote computing');
0179     disp('%');
0180     disp('LUKE = getenv(''LUKE'');%prescribed by environement');
0181     disp('%');
0182     disp('link_mode = getenv(''luke_link_mode'');%prescribed by remote computing');
0183     disp('%');
0184     disp('remotebatch_matlabfun = getenv(''remotebatch_matlabfun'');% characterizes remote computing');
0185     disp('%');
0186     disp('if ~isempty(link_mode),');
0187     disp('    if strcmp(link_mode,''[]''),');
0188     disp('        link_mode = [];');
0189     disp('    else');
0190     disp('        link_mode = str2double(link_mode);');
0191     disp('    end');
0192     disp('elseif ~isempty(remotebatch_matlabfun),');
0193     disp('    link_mode = -1;% local profiles only; no second level remote computing unless prescribed by luke_link_mode');
0194     disp('else');
0195     disp('    link_mode = NaN;% default value, select link_mode prescribed by remote profiles');
0196     disp('end');
0197     disp('%');
0198     disp('if ~isempty(luke_root),');
0199     disp('    %');
0200     disp('    select_LUKE(luke_root,link_mode)');
0201     disp('    %');
0202     disp('elseif ~isempty(LUKE),');
0203     disp('    %');
0204     disp('    select_LUKE(LUKE,link_mode)');
0205     disp('    %');
0206     disp('else');
0207     disp('    %');
0208     disp('    disp('' '');');
0209     disp('    disp(''-------------------------------------------------------------------------------------------'');');
0210     disp('    disp(''>>>>>>>>>>>>>>> To set LUKE paths and start using LUKE, type "select_LUKE;" <<<<<<<<<<<<<<<'');');
0211     disp('    disp(''-------------------------------------------------------------------------------------------'');');
0212     disp('    disp('' '');');
0213     disp('    %');
0214     disp('end');
0215     disp('%');
0216     %
0217     diary off
0218     disp(' ');
0219     disp('---------------------------------------------------------')
0220     %
0221     % -------------------------------------------------------------
0222     %       check or add call to startup_LUKE in startup.m
0223     % -------------------------------------------------------------
0224     %
0225     flag = 0;
0226     %
0227     if exist(startupfile,'file'),%startup.m file already exist
0228         %
0229         % check if call to startup_LUKE is implemented in startup.m
0230         %
0231         filestr = fileread(startupfile);
0232         %
0233         if strfind(filestr,'startup_LUKE'),
0234             disp(' ');
0235             disp('Call to startup_LUKE already implemented in startup.m');
0236             %
0237         else
0238             %
0239             % test that startup.m can be written into
0240             %
0241             fid = fopen(startupfile,'r+');  
0242             if fid == -1,
0243                 disp('Warning : Cannot write to startup.m, permission denied. Please setup startup file manually.');
0244                 flag = 2;
0245             else
0246                 fclose(fid);
0247                 flag = 1;
0248             end
0249             %
0250         end
0251     else    
0252         %
0253         % test that startup.m can be created
0254         %
0255         fid = fopen(startupfile,'w');  
0256         %
0257         if fid == -1,
0258             disp('Warning : Cannot create startup.m, permission denied. Please setup startup file manually.');
0259             flag = 2;
0260         else
0261             fclose(fid);
0262             flag = 1;
0263         end
0264         %
0265     end
0266     %
0267     if flag == 1,
0268         %
0269         % add call to startup_LUKE a the end of startup.m
0270         %
0271         diary(startupfile)
0272         %
0273         disp('%')
0274         disp('startup_LUKE;% set up path to LUKE as well as the toolboxes MatRemote and MatTools')
0275         disp('%')
0276         %
0277         diary off
0278         %
0279     end
0280     %
0281 end
0282 %
0283 %
0284 function foundpaths = findandselect_jd(pathname,pathtype,multi,optlocal,message,searchpath)
0285     %
0286     % This function searches for the file(s) (pathtype == 'f') or folder(s) (pathtype == 'd') named 'pathname' in the home directory.
0287     % if multi = true, can select more than one instances
0288     % if optlocal == true, put local folder first if in the list
0289     % use 'message' for selection, or default message if left empty
0290     %
0291     if multi,
0292         foundpaths = {};
0293     else
0294         foundpaths = '';
0295     end
0296     %
0297     if nargin < 6 || isempty(searchpath),
0298         searchpath = getenv('HOME');
0299     end
0300     %
0301     [res,foundpathstr] = unix(['find ',searchpath,' -type ',pathtype,' -name "',pathname,'"']);
0302     %
0303     if foundpathstr(1) == 10,
0304         foundpathstr = foundpathstr(2:end);%remove possible initial EoF
0305     end
0306     %
0307     ieof = [0,find(foundpathstr == 10)];
0308     %
0309     nfoundpaths = length(ieof) - 1;
0310     %
0311     foundpaths = cell(1,nfoundpaths);
0312     flag = false(1,nfoundpaths);
0313     %
0314     for ifoundpath = 1:nfoundpaths,
0315         foundpaths{ifoundpath} = foundpathstr(ieof(ifoundpath) + 1:ieof(ifoundpath + 1) - 1);% char -> cell
0316         %
0317         if pathtype == 'f',% file
0318             if exist(foundpaths{ifoundpath},'file'),% to test it is a valid path (reject "Permission denied", etc)
0319                 flag(ifoundpath) = true;
0320                 foundpaths{ifoundpath} = fileparts(foundpaths{ifoundpath});
0321             end
0322         elseif pathtype == 'd',% directory
0323             if exist(foundpaths{ifoundpath},'dir'),% to test it is a valid path (reject "Permission denied", etc)
0324                 [~,rootfoundpath] = fileparts(fileparts(foundpaths{ifoundpath}));%remove Help folder if it exists
0325                 if ~strcmp(rootfoundpath,'Help'),
0326                     flag(ifoundpath) = true;
0327                 end
0328             end
0329             %
0330         end
0331     end  
0332     %
0333     foundpaths = foundpaths(flag);
0334     nfoundpaths = length(foundpaths);
0335     %
0336     if nfoundpaths == 0,
0337         disp([pathname,' not found on the local machine. Please download before starting LUKE install again.'])
0338         return
0339     end
0340     %
0341     if nargin > 3 && optlocal,
0342         %
0343         localpath = fileparts(which(mfilename));% local version
0344         %
0345         foundpaths = [{localpath},foundpaths(~strcmp(foundpaths,localpath))];% move local version to first position in list
0346         %
0347     end        
0348     %
0349     if nfoundpaths > 1,
0350         %
0351         ipath = [];
0352         %
0353         while isempty(ipath),
0354             if nargin < 4 || isempty(message),
0355                 if multi,
0356                     message = ['Please select one or more version(s) of ',pathname,' :'];
0357                 else
0358                     message = ['Please select a version of ',pathname,' :'];
0359                 end
0360             end
0361             %
0362             disp(' ');
0363             disp(message)
0364             if multi,
0365                 disp('---> Enter array of choices (ex : [1,2,4]), or simply return to include entire list');
0366             end
0367             disp(' ');
0368             %
0369             for ifoundpath = 1:nfoundpaths,
0370                 disp(['[',num2str(ifoundpath),'] : ',foundpaths{ifoundpath}])
0371             end
0372             %
0373             disp(' ');
0374             if multi,
0375                 selection = input(' ? : ');
0376             else
0377                 selection = input(' ? default : [1] : ');
0378             end
0379             %
0380             if multi,
0381                 if isempty(selection),
0382                     ipath = 1:nfoundpaths;
0383                 elseif isnumeric(selection) && all(sum(repmat(selection(:),[1,nfoundpaths]) == repmat(1:nfoundpaths,[length(selection(:)),1]),2) == ones(length(selection(:)),1)),
0384                     ipath = selection(:).';
0385                 else
0386                     disp('Invalid choice.')
0387                 end
0388             else
0389                 if isempty(selection),
0390                     ipath = 1;
0391                 elseif isnumeric(selection) && isscalar(selection) && any(selection == 1:nfoundpaths),
0392                     ipath = selection;
0393                 else
0394                     disp('Invalid choice.')
0395                 end
0396             end                    
0397         end
0398         %
0399     else
0400         ipath = 1;
0401     end
0402     %
0403     if multi,
0404         foundpaths = foundpaths(ipath);
0405     else
0406         foundpaths = foundpaths{ipath};
0407     end
0408     %
0409 end
0410 %
0411

Community support and wiki are available on Redmine. Last update: 18-Apr-2019.