spack/
subprocess.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* Copyright 2022-2023 Danny McClanahan */
/* SPDX-License-Identifier: (Apache-2.0 OR MIT) */

/* use super_process::{base, exe, fs, stream, sync}; */

pub mod python {
  use super_process::{
    base::{self, CommandBase},
    exe, fs,
    sync::SyncInvocable,
  };

  use async_trait::async_trait;
  use displaydoc::Display;
  use once_cell::sync::Lazy;
  use regex::Regex;
  use thiserror::Error;

  use std::{env, ffi::OsString, io, path::Path, str};

  /// Things that can go wrong when detecting python.
  #[derive(Debug, Display, Error)]
  pub enum PythonError {
    /// unknown error: {0}
    UnknownError(String),
    /// error executing command: {0}
    Command(#[from] exe::CommandErrorWrapper),
    /// error setting up command: {0}
    Setup(#[from] base::SetupErrorWrapper),
    /// io error: {0}
    Io(#[from] io::Error),
  }

  #[derive(Debug, Clone)]
  pub struct PythonInvocation {
    exe: exe::Exe,
    inner: exe::Command,
  }

  #[async_trait]
  impl CommandBase for PythonInvocation {
    async fn setup_command(self) -> Result<exe::Command, base::SetupError> {
      let Self { exe, mut inner } = self;
      inner.unshift_new_exe(exe);
      Ok(inner)
    }
  }

  /// Refers to a particular python executable [`PYTHON_CMD`] first on the
  /// `$PATH`.
  #[derive(Debug, Clone)]
  pub struct FoundPython {
    pub exe: exe::Exe,
    /// Version string parsed from the python executable.
    pub version: String,
  }

  /// Pattern we match against when executing [`Python::detect`].
  pub static PYTHON_VERSION_REGEX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"^Python (3\.[0-9]+\.[0-9]+).*\n$").unwrap());

  impl FoundPython {
    fn determine_python_exename() -> exe::Exe {
      let exe_name: OsString = env::var_os("SPACK_PYTHON").unwrap_or_else(|| "python3".into());
      let exe_path = Path::new(&exe_name).to_path_buf();
      exe::Exe(fs::File(exe_path))
    }

    /// Check for a valid python installation by parsing the output of
    /// `--version`.
    pub async fn detect() -> Result<Self, PythonError> {
      let py = Self::determine_python_exename();
      let command = PythonInvocation {
        exe: py.clone(),
        inner: exe::Command {
          argv: ["--version"].as_ref().into(),
          ..Default::default()
        },
      }
      .setup_command()
      .await
      .map_err(|e| e.with_context(format!("in FoundPython::detect(py = {:?})", &py)))?;
      let output = command.invoke().await?;
      let stdout = str::from_utf8(&output.stdout).map_err(|e| {
        PythonError::UnknownError(format!(
          "could not parse utf8 from '{} --version' stdout ({}); received:\n{:?}",
          &py, &e, &output.stdout
        ))
      })?;
      match PYTHON_VERSION_REGEX.captures(stdout) {
        Some(m) => Ok(Self {
          exe: py,
          version: m.get(1).unwrap().as_str().to_string(),
        }),
        None => Err(PythonError::UnknownError(format!(
          "could not parse '{} --version'; received:\n(stdout):\n{}",
          py, &stdout,
        ))),
      }
    }

    pub(crate) fn with_python_exe(self, inner: exe::Command) -> PythonInvocation {
      let Self { exe, .. } = self;
      PythonInvocation { exe, inner }
    }
  }

  #[cfg(test)]
  mod test {
    use super::*;

    use tokio;

    #[tokio::test]
    async fn test_detect_python() -> Result<(), crate::Error> {
      let _python = FoundPython::detect().await.unwrap();
      Ok(())
    }
  }
}

pub mod spack {
  use super::python;
  use crate::{commands, summoning};
  use super_process::{
    base::{self, CommandBase},
    exe, fs,
    sync::SyncInvocable,
  };

  use async_trait::async_trait;
  use displaydoc::Display;
  use thiserror::Error;

  use std::{
    io,
    path::{Path, PathBuf},
    str,
  };

  #[derive(Debug, Display, Error)]
  pub enum InvocationSummoningError {
    /// error validating arguments: {0}
    Validation(#[from] base::SetupErrorWrapper),
    /// error executing command: {0}
    Command(#[from] exe::CommandErrorWrapper),
    /// error summoning: {0}
    Summon(#[from] summoning::SummoningError),
    /// error finding compilers: {0}
    CompilerFind(#[from] commands::compiler_find::CompilerFindError),
    /// error bootstrapping: {0}
    Bootstrap(#[from] commands::install::InstallError),
    /// error location python: {0}
    Python(#[from] python::PythonError),
    /// io error: {0}
    Io(#[from] io::Error),
  }

  /// Builder for spack subprocesss.
  #[derive(Debug, Clone)]
  pub struct SpackInvocation {
    /// Information about the python executable used to execute spack with.
    python: python::FoundPython,
    /// Information about the spack checkout.
    repo: summoning::SpackRepo,
    /// Version parsed from executing with '--version'.
    #[allow(dead_code)]
    pub version: String,
  }

  pub(crate) static SUMMON_CUR_PROCESS_LOCK: once_cell::sync::Lazy<tokio::sync::Mutex<()>> =
    once_cell::sync::Lazy::new(|| tokio::sync::Mutex::new(()));

  impl SpackInvocation {
    pub(crate) fn cache_location(&self) -> &Path { self.repo.cache_location() }

    /// Create an instance.
    ///
    /// You should prefer to call [`Self::clone`] on the first instance you
    /// construct instead of repeatedly calling this method when executing
    /// multiple spack subprocesss in a row.
    pub async fn create(
      python: python::FoundPython,
      repo: summoning::SpackRepo,
    ) -> Result<Self, InvocationSummoningError> {
      let script_path = format!("{}", repo.script_path.display());
      let command = python
        .clone()
        .with_python_exe(exe::Command {
          argv: [&script_path, "--version"].as_ref().into(),
          ..Default::default()
        })
        .setup_command()
        .await
        .map_err(|e| e.with_context(format!("with py {:?} and repo {:?}", &python, &repo)))?;
      let output = command.clone().invoke().await?;
      let version = str::from_utf8(&output.stdout)
        .map_err(|e| format!("utf8 decoding error {}: from {:?}", e, &output.stdout))
        .and_then(|s| {
          s.strip_suffix('\n')
            .ok_or_else(|| format!("failed to strip final newline from output: '{}'", s))
        })
        .map_err(|e: String| {
          python::PythonError::UnknownError(format!(
            "error parsing '{} {} --version' output: {}",
            &python.exe, &script_path, e
          ))
        })?
        .to_string();
      Ok(Self {
        python,
        repo,
        version,
      })
    }

    async fn ensure_compilers_found(&self) -> Result<(), InvocationSummoningError> {
      let find_site_compilers = commands::compiler_find::CompilerFind {
        spack: self.clone(),
        paths: vec![PathBuf::from("/usr/bin")],
        scope: Some("site".to_string()),
      };
      find_site_compilers.compiler_find().await?;
      Ok(())
    }

    async fn bootstrap(
      &self,
      cache_dir: summoning::CacheDir,
    ) -> Result<(), InvocationSummoningError> {
      let bootstrap_proof_name: PathBuf = format!("{}.bootstrap_proof", cache_dir.dirname()).into();
      let bootstrap_proof_path = cache_dir.location().join(bootstrap_proof_name);

      match tokio::fs::File::open(&bootstrap_proof_path).await {
        Ok(_) => return Ok(()),
        /* If not found, continue. */
        Err(e) if e.kind() == io::ErrorKind::NotFound => (),
        Err(e) => return Err(e.into()),
      }

      let bootstrap_lock_name: PathBuf = format!("{}.bootstrap_lock", cache_dir.dirname()).into();
      let bootstrap_lock_path = cache_dir.location().join(bootstrap_lock_name);
      let mut lockfile =
        tokio::task::spawn_blocking(move || fslock::LockFile::open(&bootstrap_lock_path))
          .await
          .unwrap()?;
      /* This unlocks the lockfile upon drop! */
      let _lockfile = tokio::task::spawn_blocking(move || {
        lockfile.lock_with_pid()?;
        Ok::<_, io::Error>(lockfile)
      })
      .await
      .unwrap()?;

      /* See if the target file was created since we locked the lockfile. */
      if tokio::fs::File::open(&bootstrap_proof_path).await.is_ok() {
        /* If so, return early! */
        return Ok(());
      }

      eprintln!(
        "bootstrapping spack {}",
        crate::versions::patches::PATCHES_TOPLEVEL_COMPONENT,
      );

      self.ensure_compilers_found().await?;

      let bootstrap_install = commands::install::Install {
        spack: self.clone(),
        spec: commands::CLISpec::new("m4"),
        verbosity: Default::default(),
        env: None,
        repos: None,
      };
      let installed_spec = bootstrap_install.install_find().await?;

      use tokio::io::AsyncWriteExt;
      let mut proof = tokio::fs::File::create(bootstrap_proof_path).await?;
      proof
        .write_all(format!("{}", installed_spec.hashed_spec()).as_bytes())
        .await?;

      Ok(())
    }

    /// Create an instance via [`Self::create`], with good defaults.
    pub async fn summon() -> Result<Self, InvocationSummoningError> {
      /* Our use of file locking within the summoning process does not
       * differentiate between different threads within the same process, so
       * we additionally lock in-process here. */
      let _lock = SUMMON_CUR_PROCESS_LOCK.lock().await;

      let python = python::FoundPython::detect().await?;
      let cache_dir = summoning::CacheDir::get_or_create().await?;
      let spack_repo = summoning::SpackRepo::summon(cache_dir.clone()).await?;
      let spack = Self::create(python, spack_repo).await?;
      spack.bootstrap(cache_dir).await?;
      Ok(spack)
    }

    /// Get a [`CommandBase`] instance to execute spack with the given `argv`.
    pub(crate) fn with_spack_exe(self, inner: exe::Command) -> ReadiedSpackInvocation {
      let Self { python, repo, .. } = self;
      ReadiedSpackInvocation {
        python,
        repo,
        inner,
      }
    }
  }

  #[cfg(test)]
  mod test {
    use super::*;

    use crate::{subprocess::python::*, summoning::*};

    use tokio;

    #[tokio::test]
    async fn test_summon() -> Result<(), crate::Error> {
      let spack = SpackInvocation::summon().await?;
      // This is the current version number for the spack installation.
      assert_eq!(spack.version, "0.22.0.dev0");
      Ok(())
    }

    #[tokio::test]
    async fn test_create_invocation() -> Result<(), crate::Error> {
      let _lock = SUMMON_CUR_PROCESS_LOCK.lock().await;

      // Access a few of the relevant files and directories.
      let python = FoundPython::detect().await.unwrap();
      let cache_dir = CacheDir::get_or_create().await.unwrap();
      let spack_exe = SpackRepo::summon(cache_dir).await.unwrap();
      let spack = SpackInvocation::create(python, spack_exe).await?;

      // This is the current version number for the spack installation.
      assert_eq!(spack.version, "0.22.0.dev0");
      Ok(())
    }
  }

  pub(crate) struct ReadiedSpackInvocation {
    pub python: python::FoundPython,
    pub repo: summoning::SpackRepo,
    pub inner: exe::Command,
  }

  #[async_trait]
  impl base::CommandBase for ReadiedSpackInvocation {
    async fn setup_command(self) -> Result<exe::Command, base::SetupError> {
      let Self {
        python,
        repo:
          summoning::SpackRepo {
            script_path,
            repo_path,
            ..
          },
        mut inner,
      } = self;

      assert!(inner.wd.is_none(), "assuming working dir was not yet set");
      inner.wd = Some(fs::Directory(repo_path));

      assert!(inner.exe.is_empty());
      inner.unshift_new_exe(exe::Exe(fs::File(script_path)));
      let py = python.with_python_exe(inner);

      Ok(py.setup_command().await?)
    }
  }
}