Skip to content

Commit

Permalink
Updated to mainly use the COBRA gitignore information
Browse files Browse the repository at this point in the history
  • Loading branch information
tpfau committed Mar 19, 2018
1 parent 3fd5162 commit e65bae2
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ mapMitocartaTransportColoured.xml
mapNodesArea.xml
mapPPIUnified.xml

#generated MILP problem files
MILPProblem.mat

# generated tutorial documentation
docs/source/_static/tutorials/*.html
Expand Down
2 changes: 1 addition & 1 deletion initCobraToolbox.m
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
65 changes: 55 additions & 10 deletions src/base/install/getFilesInDir.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)));
Expand All @@ -50,30 +61,64 @@
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)
files = files(cellfun(@(x) ~isempty(regexp(x,parser.Results.restrictToPattern)), files));
end

cd(currentDir);
end



28 changes: 0 additions & 28 deletions src/base/install/removeGitIgnoredNewFiles.m

This file was deleted.

43 changes: 43 additions & 0 deletions src/base/install/removeTempFiles.m
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion src/base/solvers/changeCobraSolver.m
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/testAll.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,15 @@
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'))
exit(exit_code);
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);
Expand Down

0 comments on commit e65bae2

Please sign in to comment.