From e65bae2fc33533415594831d8ac279171302b304 Mon Sep 17 00:00:00 2001 From: Thomas Pfau Date: Thu, 15 Mar 2018 11:28:26 +0100 Subject: [PATCH] Updated to mainly use the COBRA gitignore information --- .gitignore | 2 + initCobraToolbox.m | 2 +- src/base/install/getFilesInDir.m | 65 +++++++++++++++++---- src/base/install/removeGitIgnoredNewFiles.m | 28 --------- src/base/install/removeTempFiles.m | 43 ++++++++++++++ src/base/solvers/changeCobraSolver.m | 3 +- test/testAll.m | 4 +- 7 files changed, 105 insertions(+), 42 deletions(-) delete mode 100644 src/base/install/removeGitIgnoredNewFiles.m create mode 100644 src/base/install/removeTempFiles.m diff --git a/.gitignore b/.gitignore index 707d369c80..aa1a3a3e3f 100755 --- a/.gitignore +++ b/.gitignore @@ -134,6 +134,8 @@ mapMitocartaTransportColoured.xml mapNodesArea.xml mapPPIUnified.xml +#generated MILP problem files +MILPProblem.mat # generated tutorial documentation docs/source/_static/tutorials/*.html diff --git a/initCobraToolbox.m b/initCobraToolbox.m index 832bd3d0de..c55f4a8f4b 100644 --- a/initCobraToolbox.m +++ b/initCobraToolbox.m @@ -547,7 +547,7 @@ function initCobraToolbox() cd(currentDir); % cleanup at the end of the successful run - removeGitIgnoredNewFiles(CBTDIR, dirContent); + removeTempFiles(CBTDIR, dirContent); % clear all temporary variables % Note: global variables are kept in memory - DO NOT clear all the variables! diff --git a/src/base/install/getFilesInDir.m b/src/base/install/getFilesInDir.m index 477e7eb0aa..ab7135f3a7 100644 --- a/src/base/install/getFilesInDir.m +++ b/src/base/install/getFilesInDir.m @@ -2,7 +2,9 @@ % List all files in the supplied (git tracked) Directory with their absolute path name % based on the git ls-file command. This command will never list git % specific files (i.e. files in .git folders or the git specific attribute -% files e.g. .gitignore) +% files e.g. .gitignore) if the directory is not git controlled, the +% gitTypeFlag is assumed to be 'all' and all files (except for .git files +% will be returned. % % USAGE: % files = getFilesInDir(varargin) @@ -21,6 +23,9 @@ % and not tracked. (new files) % 'all' - all files except for the git % specific files (e.g. .git, .gitignore etc). +% 'COBRAIgnored' - use the COBRA gitIgnore +% file to determine the +% ignored files. % (Default: 'all') % restrictToPattern - give a regexp pattern to filter the % files, this option is ignored if @@ -36,7 +41,13 @@ % Get only the gitIgnored files in the current folder % files = getFilesInDir('gitTypeFlag', 'ignored'); -gitFileTypes = {'tracked','all','untracked','ignored'}; +persistent COBRAIgnored + +if isempty(COBRAIgnored) + COBRAIgnored = regexptranslate('wildcard',getIgnoredFiles()); +end + +gitFileTypes = {'tracked','all','untracked','ignored','COBRAIgnored'}; parser = inputParser(); parser.addParamValue('dirToList',pwd,@(x) exist(x,'file') == 7); parser.addParamValue('gitTypeFlag','all',@(x) ischar(x) && any(strcmpi(x,gitFileTypes))); @@ -50,26 +61,56 @@ absPath = pwd; %get all files in the directory. gitType = lower(parser.Results.gitTypeFlag); + +%test, whether the folder is git controlled +[gitStatus,~] = system('git status'); +if gitStatus ~= 0 && ~strcmpi(gitType,'cobraignored') + gitType = 'all'; +end + switch gitType case 'all' - [~, trackedfiles] = system('git ls-files'); - trackedfiles = strsplit(trackedfiles, '\n'); - [~, untrackedfiles] = system('git ls-files -o'); - untrackedfiles = strsplit(untrackedfiles, '\n'); - files = [trackedfiles,untrackedfiles]; + if gitStatus == 0 + [status, trackedfiles] = system('git ls-files'); + [status, untrackedfiles] = system('git ls-files -o'); + trackedfiles = strsplit(trackedfiles, '\n'); + untrackedfiles = strsplit(untrackedfiles, '\n'); + files = [trackedfiles,untrackedfiles]; + else + files = rdir(['**' filesep '*']); + files = {files.name}'; %Need to transpose to give consistent results. + end + case 'tracked' - [~, files] = system('git ls-files'); + [status, files] = system('git ls-files'); files = strsplit(files, '\n'); case 'untracked' %Files, which are not tracked but are not ignored. - [~, files] = system('git ls-files -o -exclude-standard'); + [status, files] = system('git ls-files -o -exclude-standard'); files = strsplit(files, '\n'); case 'ignored' [~, files] = system('git ls-files -o -i --exclude-standard'); files = strsplit(files, '\n'); + case 'cobraignored' + if gitStatus == 0 + [status, trackedfiles] = system('git ls-files'); + [status, untrackedfiles] = system('git ls-files -o'); + trackedfiles = strsplit(trackedfiles, '\n'); + untrackedfiles = strsplit(untrackedfiles, '\n'); + files = [trackedfiles,untrackedfiles]; + else + files = rdir(['**' filesep '*']); + files = {files.name}'; %Need to transpose to give consistent results. + end + matching = false(size(files)); + for i = 1:numel(COBRAIgnored) + matching = matching | ~cellfun(@(x) isempty(regexp(x,COBRAIgnored{i},'ONCE')),files); + end + files = files(matching); + end -files = strcat(absPath, files); +files = strcat(absPath, filesep, files); %Filter according to the restriction pattern. if ~isempty(parser.Results.restrictToPattern) @@ -77,3 +118,7 @@ end cd(currentDir); +end + + + diff --git a/src/base/install/removeGitIgnoredNewFiles.m b/src/base/install/removeGitIgnoredNewFiles.m deleted file mode 100644 index 653adc2865..0000000000 --- a/src/base/install/removeGitIgnoredNewFiles.m +++ /dev/null @@ -1,28 +0,0 @@ -function removeGitIgnoredNewFiles(directory, oldcontent) -% Removes all files that are in the specified directory but not part of -% the oldcontent if they match any file indicated by the gitignored files -% of the COBRA Toolbox .gitignore file. -% -% USAGE: -% removeGitIgnoredNewFiles(directory, content) -% -% INPUT: -% directory: The directory which should be checked for changing files. -% content: Absolute file names of the original conten in a cell array. -% - -currentDir = cd(directory); - -% get the new Content of the folder. -newContent = getFilesInDir('gitTypeFlag','ignored'); - -% get all .log files that were present only after initCobraToolbox was called. -newIgnoredFiles = setdiff(newContent, oldcontent); - -% by adding the folder, we already have the correct path. -if ~isempty(newIgnoredFiles) - delete(newIgnoredFiles{:}); -end - -cd(currentDir); -end \ No newline at end of file diff --git a/src/base/install/removeTempFiles.m b/src/base/install/removeTempFiles.m new file mode 100644 index 0000000000..1ffb38a42f --- /dev/null +++ b/src/base/install/removeTempFiles.m @@ -0,0 +1,43 @@ +function removeTempFiles(directory, oldcontent, COBRAGitIgnoredOnly) +% Removes all files that are in the specified directory but not part of +% the oldcontent. By default only removes those files which match files +% mentioned in the COBRA .gitignore file. +% USAGE: +% removeTempFiles(directory, oldcontent, COBRAGitIgnoredOnly) +% +% INPUT: +% directory: The directory which should be checked for changing files. +% content: Absolute file names of the original conten in a cell array. +% +% OPTIONAL INPUT: +% COBRAGitIgnoredOnly: Whether to only remove files which are listed by +% the COBRA gitignore file. +% (Default: true) +% + + +if ~exist('COBRAGitIgnoredOnly','var') + COBRAGitIgnoredOnly = true; +end + +currentDir = cd(directory); + +if COBRAGitIgnoredOnly + gitTypeFlag = 'COBRAIgnored'; +else + gitTypeFlag = 'all'; +end + +% get the new Content of the folder. +newContent = getFilesInDir('gitTypeFlag',gitTypeFlag); + +% get all .log files that were present only after initCobraToolbox was called. +newIgnoredFiles = setdiff(newContent, oldcontent); + +% by adding the folder, we already have the correct path. +if ~isempty(newIgnoredFiles) + delete(newIgnoredFiles{:}); +end + +cd(currentDir); +end \ No newline at end of file diff --git a/src/base/solvers/changeCobraSolver.m b/src/base/solvers/changeCobraSolver.m index 7fe75ddf88..de17ac1d4d 100644 --- a/src/base/solvers/changeCobraSolver.m +++ b/src/base/solvers/changeCobraSolver.m @@ -222,7 +222,8 @@ %actually set i.e. initCobraToolbox is called before). This is only %necessary, if the solver is being validated. if validationLevel == 1 - finish = onCleanup(@() removeGitIgnoredNewFiles(pwd, rdir(['**' filesep '*']))); + origFiles = getFilesInDir('gitTypeFlag','all'); + finish = onCleanup(@() removeTempFiles(pwd, origFiles)); end % configure the environment variables configEnvVars(); diff --git a/test/testAll.m b/test/testAll.m index f76d9923f1..7cc7e3f76e 100644 --- a/test/testAll.m +++ b/test/testAll.m @@ -218,7 +218,7 @@ fprintf(['\n > The exit code is ', num2str(exit_code), '.\n\n']); %clean up temporary files. - removeGitIgnoredNewFiles(testDirPath,testDirContent); + removeTempFiles(testDirPath,testDirContent); % ensure that we ALWAYS call exit if ~isempty(strfind(getenv('HOME'), 'jenkins')) || ~isempty(strfind(getenv('USERPROFILE'), 'jenkins')) @@ -226,7 +226,7 @@ end catch ME %Also clean up temporary files in case of an error. - removeGitIgnoredNewFiles(testDirPath,testDirContent); + removeTempFiles(testDirPath,testDirContent); if ~isempty(strfind(getenv('HOME'), 'jenkins')) || ~isempty(strfind(getenv('USERPROFILE'), 'jenkins')) % only exit on jenkins. exit(1);