PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/compiletest/procsrv.rs

https://gitlab.com/pranith/rust
Rust | 97 lines | 73 code | 12 blank | 12 comment | 4 complexity | 859a2954184bfa3f2618969f00c6d3e0 MD5 | raw file
  1. // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. use std::dynamic_lib::DynamicLibrary;
  11. use std::io::prelude::*;
  12. use std::path::PathBuf;
  13. use std::process::{ExitStatus, Command, Child, Output, Stdio};
  14. fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
  15. // Need to be sure to put both the lib_path and the aux path in the dylib
  16. // search path for the child.
  17. let mut path = DynamicLibrary::search_path();
  18. match aux_path {
  19. Some(p) => path.insert(0, PathBuf::from(p)),
  20. None => {}
  21. }
  22. path.insert(0, PathBuf::from(lib_path));
  23. // Add the new dylib search path var
  24. let var = DynamicLibrary::envvar();
  25. let newpath = DynamicLibrary::create_path(&path);
  26. let newpath = newpath.to_str().unwrap().to_string();
  27. cmd.env(var, &newpath);
  28. }
  29. pub struct Result {pub status: ExitStatus, pub out: String, pub err: String}
  30. pub fn run(lib_path: &str,
  31. prog: &str,
  32. aux_path: Option<&str>,
  33. args: &[String],
  34. env: Vec<(String, String)> ,
  35. input: Option<String>) -> Option<Result> {
  36. let mut cmd = Command::new(prog);
  37. cmd.args(args)
  38. .stdin(Stdio::piped())
  39. .stdout(Stdio::piped())
  40. .stderr(Stdio::piped());
  41. add_target_env(&mut cmd, lib_path, aux_path);
  42. for (key, val) in env {
  43. cmd.env(&key, &val);
  44. }
  45. match cmd.spawn() {
  46. Ok(mut process) => {
  47. if let Some(input) = input {
  48. process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
  49. }
  50. let Output { status, stdout, stderr } =
  51. process.wait_with_output().unwrap();
  52. Some(Result {
  53. status: status,
  54. out: String::from_utf8(stdout).unwrap(),
  55. err: String::from_utf8(stderr).unwrap()
  56. })
  57. },
  58. Err(..) => None
  59. }
  60. }
  61. pub fn run_background(lib_path: &str,
  62. prog: &str,
  63. aux_path: Option<&str>,
  64. args: &[String],
  65. env: Vec<(String, String)> ,
  66. input: Option<String>) -> Option<Child> {
  67. let mut cmd = Command::new(prog);
  68. cmd.args(args)
  69. .stdin(Stdio::piped())
  70. .stdout(Stdio::piped())
  71. .stderr(Stdio::piped());
  72. add_target_env(&mut cmd, lib_path, aux_path);
  73. for (key, val) in env {
  74. cmd.env(&key, &val);
  75. }
  76. match cmd.spawn() {
  77. Ok(mut process) => {
  78. if let Some(input) = input {
  79. process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
  80. }
  81. Some(process)
  82. },
  83. Err(..) => None
  84. }
  85. }