示例#1
0
 /// <summary>Execute a command and return a single line of output as a String</summary>
 /// <param name="dir">Working directory for the command</param>
 /// <param name="command">as component array</param>
 /// <param name="encoding"></param>
 /// <returns>the one-line output of the command</returns>
 protected internal static string ReadPipe(FilePath dir, string[] command, string
                                           encoding)
 {
     try
     {
         SystemProcess  p        = Runtime.GetRuntime().Exec(command, null, dir);
         BufferedReader lineRead = new BufferedReader(new InputStreamReader(p.GetInputStream
                                                                                (), encoding));
         string r = null;
         try
         {
             r = lineRead.ReadLine();
         }
         finally
         {
             p.GetOutputStream().Close();
             p.GetErrorStream().Close();
             lineRead.Close();
         }
         for (; ;)
         {
             try
             {
                 if (p.WaitFor() == 0 && r != null && r.Length > 0)
                 {
                     return(r);
                 }
                 break;
             }
             catch (Exception)
             {
             }
         }
     }
     catch (IOException e)
     {
         // Stop bothering me, I have a zombie to reap.
         if (SystemReader.GetInstance().GetProperty("jgit.fs.debug") != null)
         {
             System.Console.Error.WriteLine(e);
         }
     }
     // Ignore error (but report)
     return(null);
 }
        /// <summary>Run a hook script in the repository, returning the exit status.</summary>
        /// <remarks>Run a hook script in the repository, returning the exit status.</remarks>
        /// <param name="db">repository the script should see in GIT_DIR environment</param>
        /// <param name="hook">
        /// path of the hook script to execute, must be executable file
        /// type on this platform
        /// </param>
        /// <param name="args">arguments to pass to the hook script</param>
        /// <returns>exit status code of the invoked hook</returns>
        /// <exception cref="System.IO.IOException">the hook could not be executed</exception>
        /// <exception cref="System.Exception">the caller was interrupted before the hook completed
        ///     </exception>
        protected internal virtual int RunHook(Repository db, FilePath hook, params string
                                               [] args)
        {
            string[] argv = new string[1 + args.Length];
            argv[0] = hook.GetAbsolutePath();
            System.Array.Copy(args, 0, argv, 1, args.Length);
            IDictionary <string, string> env = CloneEnv();

            env.Put("GIT_DIR", db.Directory.GetAbsolutePath());
            PutPersonIdent(env, "AUTHOR", author);
            PutPersonIdent(env, "COMMITTER", committer);
            FilePath      cwd = db.WorkTree;
            SystemProcess p   = Runtime.GetRuntime().Exec(argv, ToEnvArray(env), cwd);

            p.GetOutputStream().Close();
            p.GetErrorStream().Close();
            p.GetInputStream().Close();
            return(p.WaitFor());
        }
示例#3
0
文件: FS.cs 项目: thild/monodevelop
 /// <summary>Execute a command and return a single line of output as a String</summary>
 /// <param name="dir">Working directory for the command</param>
 /// <param name="command">as component array</param>
 /// <param name="encoding"></param>
 /// <returns>the one-line output of the command</returns>
 protected internal static string ReadPipe(FilePath dir, string[] command, string
                                           encoding)
 {
     try
     {
         SystemProcess  p        = Runtime.GetRuntime().Exec(command, null, dir);
         BufferedReader lineRead = new BufferedReader(new InputStreamReader(p.GetInputStream
                                                                                (), encoding));
         string r = null;
         try
         {
             r = lineRead.ReadLine();
         }
         finally
         {
             p.GetOutputStream().Close();
             p.GetErrorStream().Close();
             lineRead.Close();
         }
         for (; ;)
         {
             try
             {
                 if (p.WaitFor() == 0 && r != null && r.Length > 0)
                 {
                     return(r);
                 }
                 break;
             }
             catch (Exception)
             {
             }
         }
     }
     catch (IOException)
     {
     }
     // Stop bothering me, I have a zombie to reap.
     // ignore
     return(null);
 }
示例#4
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        public virtual void LaunchAM(ApplicationAttemptId attemptId)
        {
            Credentials credentials = new Credentials();

            Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> token = rmClient.GetAMRMToken
                                                                                     (attemptId.GetApplicationId());
            // Service will be empty but that's okay, we are just passing down only
            // AMRMToken down to the real AM which eventually sets the correct
            // service-address.
            credentials.AddToken(token.GetService(), token);
            FilePath tokenFile = FilePath.CreateTempFile("unmanagedAMRMToken", string.Empty,
                                                         new FilePath(Runtime.GetProperty("user.dir")));

            try
            {
                FileUtil.Chmod(tokenFile.GetAbsolutePath(), "600");
            }
            catch (Exception ex)
            {
                throw new RuntimeException(ex);
            }
            tokenFile.DeleteOnExit();
            DataOutputStream os = new DataOutputStream(new FileOutputStream(tokenFile, true));

            credentials.WriteTokenStorageToStream(os);
            os.Close();
            IDictionary <string, string> env = Sharpen.Runtime.GetEnv();
            AList <string> envAMList         = new AList <string>();
            bool           setClasspath      = false;

            foreach (KeyValuePair <string, string> entry in env)
            {
                string key   = entry.Key;
                string value = entry.Value;
                if (key.Equals("CLASSPATH"))
                {
                    setClasspath = true;
                    if (classpath != null)
                    {
                        value = value + FilePath.pathSeparator + classpath;
                    }
                }
                envAMList.AddItem(key + "=" + value);
            }
            if (!setClasspath && classpath != null)
            {
                envAMList.AddItem("CLASSPATH=" + classpath);
            }
            ContainerId containerId = ContainerId.NewContainerId(attemptId, 0);
            string      hostname    = Sharpen.Runtime.GetLocalHost().GetHostName();

            envAMList.AddItem(ApplicationConstants.Environment.ContainerId.ToString() + "=" +
                              containerId);
            envAMList.AddItem(ApplicationConstants.Environment.NmHost.ToString() + "=" + hostname
                              );
            envAMList.AddItem(ApplicationConstants.Environment.NmHttpPort.ToString() + "=0");
            envAMList.AddItem(ApplicationConstants.Environment.NmPort.ToString() + "=0");
            envAMList.AddItem(ApplicationConstants.Environment.LocalDirs.ToString() + "= /tmp"
                              );
            envAMList.AddItem(ApplicationConstants.AppSubmitTimeEnv + "=" + Runtime.CurrentTimeMillis
                                  ());
            envAMList.AddItem(ApplicationConstants.ContainerTokenFileEnvName + "=" + tokenFile
                              .GetAbsolutePath());
            string[]      envAM  = new string[envAMList.Count];
            SystemProcess amProc = Runtime.GetRuntime().Exec(amCmd, Sharpen.Collections.ToArray
                                                                 (envAMList, envAM));
            BufferedReader errReader = new BufferedReader(new InputStreamReader(amProc.GetErrorStream
                                                                                    (), Sharpen.Extensions.GetEncoding("UTF-8")));
            BufferedReader inReader = new BufferedReader(new InputStreamReader(amProc.GetInputStream
                                                                                   (), Sharpen.Extensions.GetEncoding("UTF-8")));

            // read error and input streams as this would free up the buffers
            // free the error stream buffer
            Sharpen.Thread errThread = new _Thread_244(errReader);
            Sharpen.Thread outThread = new _Thread_258(inReader);
            try
            {
                errThread.Start();
                outThread.Start();
            }
            catch (InvalidOperationException)
            {
            }
            // wait for the process to finish and check the exit code
            try
            {
                int exitCode = amProc.WaitFor();
                Log.Info("AM process exited with value: " + exitCode);
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
            }
            finally
            {
                amCompleted = true;
            }
            try
            {
                // make sure that the error thread exits
                // on Windows these threads sometimes get stuck and hang the execution
                // timeout and join later after destroying the process.
                errThread.Join();
                outThread.Join();
                errReader.Close();
                inReader.Close();
            }
            catch (Exception ie)
            {
                Log.Info("ShellExecutor: Interrupted while reading the error/out stream", ie);
            }
            catch (IOException ioe)
            {
                Log.Warn("Error while closing the error/out stream", ioe);
            }
            amProc.Destroy();
        }
示例#5
0
            /// <exception cref="System.IO.IOException"/>
            internal override long DoIO(Reporter reporter, string name, long totalSize)
            {
                totalSize *= Mega;
                // create instance of local filesystem
                FileSystem localFS = FileSystem.GetLocal(fsConfig);

                try
                {
                    // native runtime
                    Runtime runTime = Runtime.GetRuntime();
                    // copy the dso and executable from dfs
                    lock (this)
                    {
                        localFS.Delete(HdfsTestDir, true);
                        if (!(localFS.Mkdirs(HdfsTestDir)))
                        {
                            throw new IOException("Failed to create " + HdfsTestDir + " on local filesystem");
                        }
                    }
                    lock (this)
                    {
                        if (!localFS.Exists(HdfsShlib))
                        {
                            if (!FileUtil.Copy(fs, HdfsShlib, localFS, HdfsShlib, false, fsConfig))
                            {
                                throw new IOException("Failed to copy " + HdfsShlib + " to local filesystem");
                            }
                            string        chmodCmd   = new string(Chmod + " a+x " + HdfsShlib);
                            SystemProcess process    = runTime.Exec(chmodCmd);
                            int           exitStatus = process.WaitFor();
                            if (exitStatus != 0)
                            {
                                throw new IOException(chmodCmd + ": Failed with exitStatus: " + exitStatus);
                            }
                        }
                    }
                    lock (this)
                    {
                        if (!localFS.Exists(HdfsRead))
                        {
                            if (!FileUtil.Copy(fs, HdfsRead, localFS, HdfsRead, false, fsConfig))
                            {
                                throw new IOException("Failed to copy " + HdfsRead + " to local filesystem");
                            }
                            string        chmodCmd   = new string(Chmod + " a+x " + HdfsRead);
                            SystemProcess process    = runTime.Exec(chmodCmd);
                            int           exitStatus = process.WaitFor();
                            if (exitStatus != 0)
                            {
                                throw new IOException(chmodCmd + ": Failed with exitStatus: " + exitStatus);
                            }
                        }
                    }
                    // exec the C program
                    Path   inFile  = new Path(DataDir, name);
                    string readCmd = new string(HdfsRead + " " + inFile + " " + totalSize + " " + bufferSize
                                                );
                    SystemProcess process_1 = runTime.Exec(readCmd, null, new FilePath(HdfsTestDir.ToString
                                                                                           ()));
                    int exitStatus_1 = process_1.WaitFor();
                    if (exitStatus_1 != 0)
                    {
                        throw new IOException(HdfsRead + ": Failed with exitStatus: " + exitStatus_1);
                    }
                }
                catch (Exception interruptedException)
                {
                    reporter.SetStatus(interruptedException.ToString());
                }
                finally
                {
                    localFS.Close();
                }
                return(totalSize);
            }
示例#6
0
文件: FS.cs 项目: Gearset/ngit
        /// <summary>Execute a command and return a single line of output as a String</summary>
        /// <param name="dir">Working directory for the command</param>
        /// <param name="command">as component array</param>
        /// <param name="encoding"></param>
        /// <returns>the one-line output of the command</returns>
        protected internal static string ReadPipe(FilePath dir, string[] command, string
                                                  encoding)
        {
            bool debug = System.Boolean.Parse(SystemReader.GetInstance().GetProperty("jgit.fs.debug"
                                                                                     ));

            try
            {
                if (debug)
                {
                    System.Console.Error.WriteLine("readpipe " + Arrays.AsList(command) + "," + dir);
                }
                SystemProcess  p        = Runtime.GetRuntime().Exec(command, null, dir);
                BufferedReader lineRead = new BufferedReader(new InputStreamReader(p.GetInputStream
                                                                                       (), encoding));
                p.GetOutputStream().Close();
                AtomicBoolean  gooblerFail = new AtomicBoolean(false);
                Sharpen.Thread gobbler     = new _Thread_300(p, debug, gooblerFail);
                // ignore
                // Just print on stderr for debugging
                // Just print on stderr for debugging
                gobbler.Start();
                string r = null;
                try
                {
                    r = lineRead.ReadLine();
                    if (debug)
                    {
                        System.Console.Error.WriteLine("readpipe may return '" + r + "'");
                        System.Console.Error.WriteLine("(ignoring remaing output:");
                    }
                    string l;
                    while ((l = lineRead.ReadLine()) != null)
                    {
                        if (debug)
                        {
                            System.Console.Error.WriteLine(l);
                        }
                    }
                }
                finally
                {
                    p.GetErrorStream().Close();
                    lineRead.Close();
                }
                for (; ;)
                {
                    try
                    {
                        int rc = p.WaitFor();
                        gobbler.Join();
                        if (rc == 0 && r != null && r.Length > 0 && !gooblerFail.Get())
                        {
                            return(r);
                        }
                        if (debug)
                        {
                            System.Console.Error.WriteLine("readpipe rc=" + rc);
                        }
                        break;
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (IOException e)
            {
                // Stop bothering me, I have a zombie to reap.
                if (debug)
                {
                    System.Console.Error.WriteLine(e);
                }
            }
            // Ignore error (but report)
            if (debug)
            {
                System.Console.Error.WriteLine("readpipe returns null");
            }
            return(null);
        }