PageRenderTime 21ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/scons/scons_ext/spack.py

https://gitlab.com/polyphemus/core
Python | 112 lines | 68 code | 15 blank | 29 comment | 12 complexity | 6a3fd71d23f001c1bda543c22de027e5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-3.0
  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2016, ENPC - EDF R&D
  3. # Author(s): Sylvain Doré
  4. #
  5. # This file is part of the air quality modeling system Polyphemus.
  6. #
  7. # Polyphemus is developed in the INRIA - ENPC joint project-team CLIME and in
  8. # the ENPC - EDF R&D joint laboratory CEREA.
  9. #
  10. # Polyphemus is free software; you can redistribute it and/or modify it under
  11. # the terms of the GNU General Public License as published by the Free
  12. # Software Foundation; either version 2 of the License, or (at your option)
  13. # any later version.
  14. #
  15. # Polyphemus is distributed in the hope that it will be useful, but WITHOUT
  16. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. # more details.
  19. #
  20. # For more information, visit the Polyphemus web site:
  21. # http://cerea.enpc.fr/polyphemus/
  22. import glob
  23. import os
  24. from SCons.Script import *
  25. from . import run
  26. class Spack:
  27. def __init__(self, utils):
  28. self.utils = utils
  29. def dependency(self, path, exclude_dependency, build_dir=None):
  30. path = os.path.abspath(path)
  31. species_file = glob.glob(os.path.join(path, "*.species")) + \
  32. glob.glob(os.path.join(path, "species"))
  33. reactions_file = glob.glob(os.path.join(path, "*.reactions")) + \
  34. glob.glob(os.path.join(path, "reactions"))
  35. spack_config = glob.glob(os.path.join(path, "spack_config"))
  36. if spack_config:
  37. spack_config = spack_config[0]
  38. if not species_file or not reactions_file:
  39. if spack_config:
  40. raise Exception, "In \"" + path + "\":\n"\
  41. "Spack needs a '.species' file "\
  42. "and a '.reactions' file to operate"
  43. return []
  44. if len(species_file) > 1:
  45. if spack_config:
  46. raise Exception, "In \"" + path + "\":\n"\
  47. "Several species file given to Spack, "\
  48. "only one is expected"
  49. return []
  50. if len(reactions_file) > 1:
  51. if spack_config:
  52. raise Exception, "In \"" + path + "\":\n"\
  53. "Several reactions file given to Spack, "\
  54. "only one is expected"
  55. return []
  56. if not spack_config:
  57. raise Exception, "In \"" + path + "\":\n"\
  58. "There is a species file and reactions file, "\
  59. "but Spack needs a 'spack_config' file to operate"
  60. # Creates a vanilla environment since Spack does not depends on
  61. # environment specifics and can be run in the source tree instead
  62. # of the build directory.
  63. spack_env = self.utils.create_env()
  64. spack_fortran_output = []
  65. for filename in ["dimensions.f", "dratedc.f", "fexchem.f",
  66. "jacdchemdc.f", "kinetic.f", "rates.f",
  67. "LU_decompose.f", "LU_solve.f", "LU_solve_tr.f"]:
  68. fortran_file = os.path.join(path, filename)
  69. spack_fortran_output.append(fortran_file)
  70. spack_config_output = os.path.join(path, "species.spack.dat")
  71. spack_exe = os.path.join(self.utils.polyphemus_path,
  72. "include/spack/src/generate_chem_files.sh")
  73. spack_arg = [os.path.basename(p)
  74. for p in species_file + reactions_file]
  75. spack_input = [spack_config] + species_file + reactions_file
  76. # Generates the files with Spack.
  77. spack_target = spack_env.Command(spack_fortran_output +
  78. [spack_config_output],
  79. spack_input,
  80. [ "cd " + path + " && " +
  81. " ".join([spack_exe] + spack_arg)])
  82. # Automatically builds Spack if needed.
  83. spack_dir = os.path.dirname(spack_exe)
  84. run.sconstruct(spack_dir + "/SConstruct")
  85. Depends(spack_target, Dir(spack_dir))
  86. # Copies the generated files into the current build directory.
  87. copy_fortran = []
  88. for f in spack_fortran_output:
  89. copy_fortran.append(Copy(self.utils.rebase_dir(build_dir, f), f))
  90. fortran_target = spack_env.Command(self.utils.rebase_dir(build_dir,
  91. spack_fortran_output),
  92. spack_target,
  93. copy_fortran)
  94. # Avoids building Spack files from the source tree, we have copies
  95. # in the build directories.
  96. exclude_dependency += spack_fortran_output
  97. return [fortran_target]