1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-26 04:00:59 +01:00

merged executeAndPrintShellCommand to runProgram

This commit is contained in:
Wouter den Breejen 2007-06-28 13:17:03 +00:00
parent 729933062b
commit b9fe3f00c1
5 changed files with 47 additions and 73 deletions

View file

@ -39,7 +39,12 @@ void createStateDirs(const DerivationStateOutputDirs & stateOutputDirs, const De
//Make sure the 'root' path which holds the repositorys exists, so svn doenst complain.
string repos_root_path = getStateReposRootPath("stateOutput:staterepospath", stateDir, drvName, stateIdentifier);
executeAndPrintShellCommand("mkdir -p " + repos_root_path, "mkdir", true);
Strings p_args;
p_args.push_back("-p");
p_args.push_back(repos_root_path);
runProgram_AndPrintOutput("mkdir", true, p_args, "mkdir");
//TODO check if we can create state and staterepos dirs
@ -52,7 +57,10 @@ void createStateDirs(const DerivationStateOutputDirs & stateOutputDirs, const De
//Check if and how this dir needs to be versioned
if(d.type == "none"){
executeAndPrintShellCommand("mkdir -p " + fullstatedir, "mkdir", true);
Strings p_args;
p_args.push_back("-p");
p_args.push_back(fullstatedir);
runProgram_AndPrintOutput("mkdir", true, p_args, "mkdir");
continue;
}
@ -62,8 +70,12 @@ void createStateDirs(const DerivationStateOutputDirs & stateOutputDirs, const De
if(IsDirectory(repos))
printMsg(lvlTalkative, format("Repos %1% already exists, so we use that repository") % repos);
else
executeAndPrintShellCommand(svnadminbin + " create " + repos, "svnadmin", true); //TODO create as nixbld.nixbld chmod 700... can you still commit then ??
else{
Strings p_args;
p_args.push_back("create");
p_args.push_back(repos);
runProgram_AndPrintOutput(svnadminbin, true, p_args, "svnadmin"); //TODO create as nixbld.nixbld chmod 700... can you still commit then ??
}
if(d.type == "interval"){
intervalPaths.insert(statePath);
@ -73,8 +85,11 @@ void createStateDirs(const DerivationStateOutputDirs & stateOutputDirs, const De
string fullstatedir_svn = fullstatedir + "/.svn/";
if( ! IsDirectory(fullstatedir_svn) ){
string checkoutcommand = svnbin + " checkout file://" + repos + " " + fullstatedir;
executeAndPrintShellCommand(checkoutcommand, "svn", true); //TODO checkout as user
Strings p_args;
p_args.push_back("checkout");
p_args.push_back("file://" + repos);
p_args.push_back(fullstatedir);
runProgram_AndPrintOutput(svnbin, true, p_args, "svn"); //TODO checkout as user
}
else
printMsg(lvlTalkative, format("Statedir %1% already exists, so dont check out its repository again") % fullstatedir_svn);

View file

@ -8,9 +8,6 @@ namespace nix {
/* Create a state directory. */
void createStateDirs(const DerivationStateOutputDirs & stateOutputDirs, const DerivationStateOutputs & stateOutputs, const StringPairs & env);
/* Create and prints the output prefixed with '[commandName]:' via print(lvlError,... of a shell command. */
void executeAndPrintShellCommand(const string & command, const string & commandName);
}
#endif /* !__STORESTATE_H */

View file

@ -1069,43 +1069,26 @@ string trim(const string & s) {
*/
string runProgram_AndPrintOutput(Path program, bool searchPath, const Strings & args, const string outputPrefix)
void runProgram_AndPrintOutput(Path program, bool searchPath, const Strings & args, const string outputPrefix)
{
string program_output = runProgram(program, true, args);
string program_output = runProgram(program, searchPath, args);
//Add the prefix on every line
//TODO
//Remove the trailing \n
size_t found = program_output.find_last_of("\n");
printMsg(lvlError, format("%1%") % program_output.substr(0,found));
}
void executeAndPrintShellCommand(const string & command, const string & commandName, const bool & captureOutput)
{
///////////////////////
if(captureOutput){
Strings progam_args = tokenizeStringWithQuotes(command);
string program_command = progam_args.front();
progam_args.pop_front();
string program_output = runProgram(program_command, true, progam_args);
//Remove the trailing \n
size_t found = program_output.find_last_of("\n");
printMsg(lvlError, format("%1%") % program_output.substr(0,found));
return;
Strings lines = tokenizeString(program_output, "\n");
for (Strings::const_iterator i = lines.begin(); i != lines.end(); ++i){
if(trim(*i) != "")
printMsg(lvlError, format("[%2%]- %1%") % *i % outputPrefix);
}
/////////////////////
string tempoutput = "/tmp/svnoutput.txt";
string newcommand = command;
if(captureOutput)
newcommand += " &> " + tempoutput; //the &> sends also stderr to stdout
//Remove the trailing \n
//size_t found = program_output.find_last_of("\n");
//printMsg(lvlError, format("%1%") % program_output.substr(0,found));
}
void executeShellCommand(const string & command)
{
int kidstatus, deadpid;
pid_t kidpid = fork();
switch (kidpid) {
@ -1113,36 +1096,17 @@ void executeAndPrintShellCommand(const string & command, const string & commandN
throw SysError("unable to fork");
case 0:
try { // child
int rv = system(newcommand.c_str());
int rv = system(command.c_str());
//int rv = execlp(svnbin.c_str(), svnbin.c_str(), ">", tempoutput.c_str(), NULL); //TODO make this work ... ?
string line;
std::ifstream myfile (tempoutput.c_str());
if(captureOutput){
if (myfile.is_open()){
while (! myfile.eof() )
{
getline (myfile,line);
if(trim(line) != "")
printMsg(lvlError, format("[%2%]: %1%") % line % commandName);
}
myfile.close();
}
else{
throw SysError("executeAndPrintShellCommand(..) error");
quickExit(1);
}
}
if (rv == -1) {
throw SysError("executeAndPrintShellCommand(..) error");
throw SysError("executeShellCommand(..) error");
quickExit(99);
}
quickExit(0);
} catch (std::exception & e) {
std::cerr << format("executeAndPrintShellCommand(..) child error: %1%\n") % e.what();
std::cerr << format("executeShellCommand(..) child error: %1%\n") % e.what();
quickExit(1);
}
}
@ -1152,9 +1116,6 @@ void executeAndPrintShellCommand(const string & command, const string & commandN
std::cerr << format("state child waitpid error\n");
quickExit(1);
}
if(captureOutput)
remove(tempoutput.c_str()); //Remove the tempoutput file
}
string time_t2string(const time_t & t)

View file

@ -268,7 +268,7 @@ Strings unpackStrings(const string & s);
Strings tokenizeString(const string & s, const string & separators = " \t\n\r");
/* String tokenizer for commandline agruments. */
Strings tokenizeStringWithQuotes(const string & s);
//Strings tokenizeStringWithQuotes(const string & s); //TODO maybe remove the function
/* Convert the exit status of a child as returned by wait() into an
error string. */
@ -293,10 +293,10 @@ string trimr(const string & s);
string trim(const string & s);
//excecute a shell command
void executeAndPrintShellCommand(const string & command, const string & commandName, const bool & captureOutput);
void executeShellCommand(const string & command);
//
string runProgram_AndPrintOutput(Path program, bool searchPath, const Strings & args, const string outputPrefix);
void runProgram_AndPrintOutput(Path program, bool searchPath, const Strings & args, const string outputPrefix);
//Convert time_t to a string
string time_t2string(const time_t & t);

View file

@ -213,7 +213,7 @@ static void opRunComponent(Strings opFlags, Strings opArgs)
//******************* Run ****************************
executeAndPrintShellCommand(componentPath + binary, "", false); //more efficient way needed ???
executeShellCommand(componentPath + binary); //more efficient way needed ???
//******************* With everything in place, we call the commit script on all statePaths **********************
@ -317,12 +317,13 @@ static void opRunComponent(Strings opFlags, Strings opArgs)
}
//make the call
executeAndPrintShellCommand(nixLibexecDir + "/nix/nix-statecommit.sh " + svnbin +
" \"" + subversionedstatepathsarray + "\" " +
" \"" + subversionedpathsCommitBooleansarray + "\" " +
" \"" + nonversionedstatepathsarray + "\" " +
" \"" + commandsarray + "\" ",
"commit-script", true);
Strings p_args;
p_args.push_back(svnbin);
p_args.push_back(subversionedstatepathsarray);
p_args.push_back(subversionedpathsCommitBooleansarray);
p_args.push_back(nonversionedstatepathsarray);
p_args.push_back(commandsarray);
runProgram_AndPrintOutput(nixLibexecDir + "/nix/nix-statecommit.sh", true, p_args, "svn");
//TODO
//Scan again??