[d69b677] | 1 | #!/usr/bin/env python |
---|
[64cf353] | 2 | # -*- coding: utf-8 -*- |
---|
[991df6a] | 3 | #******************************************************************************* |
---|
| 4 | # @Author: Leopold Haimberger (University of Vienna) |
---|
| 5 | # |
---|
| 6 | # @Date: November 2015 |
---|
| 7 | # |
---|
| 8 | # @Change History: |
---|
| 9 | # |
---|
| 10 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 11 | # - applied PEP8 style guide |
---|
| 12 | # - added documentation |
---|
[ff99eae] | 13 | # - moved install_args_and_control in here |
---|
[2fb99de] | 14 | # - splitted code in smaller functions |
---|
| 15 | # - delete convert build files in here instead of compile job script |
---|
| 16 | # - changed static path names to Variables from config file |
---|
[991df6a] | 17 | # |
---|
| 18 | # @License: |
---|
| 19 | # (C) Copyright 2015-2018. |
---|
| 20 | # |
---|
| 21 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 22 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 23 | # |
---|
| 24 | # @Program Functionality: |
---|
| 25 | # Depending on the selected installation environment (locally or on the |
---|
| 26 | # ECMWF server ecgate or cca) the program extracts the commandline |
---|
| 27 | # arguments and the CONTROL file parameter and prepares the corresponding |
---|
| 28 | # environment. The necessary files are collected in a tar-ball and placed |
---|
| 29 | # at the target location. There its untared, the environment variables will |
---|
| 30 | # be set and the Fortran code will be compiled. If the ECMWF environment is |
---|
| 31 | # selected a job script is prepared and submitted for the remaining |
---|
| 32 | # configurations after putting the tar-ball to the target ECMWF server. |
---|
| 33 | # |
---|
| 34 | # @Program Content: |
---|
| 35 | # - main |
---|
[54a8a01] | 36 | # - get_install_cmdline_arguments |
---|
[991df6a] | 37 | # - install_via_gateway |
---|
[54a8a01] | 38 | # - mk_tarball |
---|
[2fb99de] | 39 | # - un_tarball |
---|
[54a8a01] | 40 | # - mk_env_vars |
---|
| 41 | # - mk_compilejob |
---|
| 42 | # - mk_job_template |
---|
| 43 | # - delete_convert_build |
---|
| 44 | # - make_convert_build |
---|
[991df6a] | 45 | # |
---|
| 46 | #******************************************************************************* |
---|
| 47 | |
---|
[64cf353] | 48 | # ------------------------------------------------------------------------------ |
---|
| 49 | # MODULES |
---|
| 50 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 51 | import os |
---|
| 52 | import sys |
---|
[2fb99de] | 53 | import glob |
---|
[d69b677] | 54 | import subprocess |
---|
| 55 | import inspect |
---|
[ff99eae] | 56 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
---|
[991df6a] | 57 | |
---|
| 58 | # software specific classes and modules from flex_extract |
---|
[2fb99de] | 59 | import _config |
---|
[25b14be] | 60 | from classes.ControlFile import ControlFile |
---|
| 61 | from classes.UioFiles import UioFiles |
---|
| 62 | from mods.tools import make_dir, put_file_to_ecserver, submit_job_to_ecserver |
---|
[d69b677] | 63 | |
---|
[64cf353] | 64 | # ------------------------------------------------------------------------------ |
---|
| 65 | # FUNCTIONS |
---|
| 66 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 67 | def main(): |
---|
[274f9ef] | 68 | '''Controls the installation process. Calls the installation function |
---|
| 69 | if target is specified. |
---|
[991df6a] | 70 | |
---|
[274f9ef] | 71 | Parameters |
---|
| 72 | ---------- |
---|
[991df6a] | 73 | |
---|
[274f9ef] | 74 | Return |
---|
| 75 | ------ |
---|
[991df6a] | 76 | ''' |
---|
| 77 | |
---|
[54a8a01] | 78 | args = get_install_cmdline_arguments() |
---|
[4971f63] | 79 | c = ControlFile(args.controlfile) |
---|
[54a8a01] | 80 | c.assign_args_to_control(args) |
---|
| 81 | c.check_install_conditions() |
---|
[991df6a] | 82 | |
---|
[54a8a01] | 83 | install_via_gateway(c) |
---|
[991df6a] | 84 | |
---|
[54a8a01] | 85 | return |
---|
[991df6a] | 86 | |
---|
[54a8a01] | 87 | def get_install_cmdline_arguments(): |
---|
[274f9ef] | 88 | '''Decomposes the command line arguments and assigns them to variables. |
---|
| 89 | Apply default values for non mentioned arguments. |
---|
[efdb01a] | 90 | |
---|
[274f9ef] | 91 | Parameters |
---|
| 92 | ---------- |
---|
[efdb01a] | 93 | |
---|
[274f9ef] | 94 | Return |
---|
| 95 | ------ |
---|
| 96 | args : :obj:`Namespace` |
---|
| 97 | Contains the commandline arguments from script/program call. |
---|
[efdb01a] | 98 | ''' |
---|
[54a8a01] | 99 | parser = ArgumentParser(description='Install flex_extract software locally or \ |
---|
[efdb01a] | 100 | on ECMWF machines', |
---|
| 101 | formatter_class=ArgumentDefaultsHelpFormatter) |
---|
| 102 | |
---|
[54a8a01] | 103 | parser.add_argument('--target', dest='install_target', default=None, |
---|
[efdb01a] | 104 | help="Valid targets: local | ecgate | cca , \ |
---|
| 105 | the latter two are at ECMWF") |
---|
[54a8a01] | 106 | parser.add_argument("--makefile", dest="makefile", default=None, |
---|
[efdb01a] | 107 | help='Name of Makefile to use for compiling CONVERT2') |
---|
[54a8a01] | 108 | parser.add_argument("--ecuid", dest="ecuid", default=None, |
---|
[efdb01a] | 109 | help='user id at ECMWF') |
---|
[54a8a01] | 110 | parser.add_argument("--ecgid", dest="ecgid", default=None, |
---|
[efdb01a] | 111 | help='group id at ECMWF') |
---|
[54a8a01] | 112 | parser.add_argument("--gateway", dest="gateway", default=None, |
---|
[efdb01a] | 113 | help='name of local gateway server') |
---|
[54a8a01] | 114 | parser.add_argument("--destination", dest="destination", default=None, |
---|
[efdb01a] | 115 | help='ecaccess destination, e.g. leo@genericSftp') |
---|
| 116 | |
---|
| 117 | parser.add_argument("--flexpart_root_scripts", dest="flexpart_root_scripts", |
---|
[54a8a01] | 118 | default=None, help="FLEXPART root directory on ECMWF \ |
---|
| 119 | servers (to find grib2flexpart and COMMAND file)\n\ |
---|
| 120 | Normally flex_extract resides in the scripts directory \ |
---|
[2fb99de] | 121 | of the FLEXPART distribution.") |
---|
[efdb01a] | 122 | |
---|
[54a8a01] | 123 | # arguments for job submission to ECMWF, only needed by submit.py |
---|
[efdb01a] | 124 | parser.add_argument("--job_template", dest='job_template', |
---|
| 125 | default="job.temp.o", |
---|
| 126 | help="job template file for submission to ECMWF") |
---|
| 127 | |
---|
| 128 | parser.add_argument("--controlfile", dest="controlfile", |
---|
| 129 | default='CONTROL.temp', |
---|
[991df6a] | 130 | help="file with CONTROL parameters") |
---|
[efdb01a] | 131 | |
---|
| 132 | args = parser.parse_args() |
---|
| 133 | |
---|
[54a8a01] | 134 | return args |
---|
| 135 | |
---|
[efdb01a] | 136 | |
---|
[54a8a01] | 137 | def install_via_gateway(c): |
---|
[274f9ef] | 138 | '''Perform the actual installation on local machine or prepare data |
---|
| 139 | transfer to remote gate and submit a job script which will |
---|
| 140 | install everything on the remote gate. |
---|
| 141 | |
---|
| 142 | Parameters |
---|
| 143 | ---------- |
---|
| 144 | c : :obj:`ControlFile` |
---|
| 145 | Contains all the parameters of CONTROL file and |
---|
| 146 | command line. |
---|
| 147 | |
---|
| 148 | Return |
---|
| 149 | ------ |
---|
| 150 | |
---|
[54a8a01] | 151 | ''' |
---|
[2fb99de] | 152 | import tarfile |
---|
| 153 | |
---|
| 154 | ecd = _config.PATH_FLEXEXTRACT_DIR |
---|
| 155 | tarball_name = _config.FLEXEXTRACT_DIRNAME + '.tar' |
---|
| 156 | tar_file = os.path.join(ecd, tarball_name) |
---|
[54a8a01] | 157 | |
---|
[2fb99de] | 158 | target_dirname = _config.FLEXEXTRACT_DIRNAME |
---|
| 159 | fortran_executable = _config.FORTRAN_EXECUTABLE |
---|
[54a8a01] | 160 | |
---|
[97e09f4] | 161 | if c.install_target.lower() != 'local': # ecgate or cca |
---|
[54a8a01] | 162 | |
---|
[2fb99de] | 163 | mk_compilejob(c.makefile, c.install_target, c.ecuid, c.ecgid, |
---|
[54a8a01] | 164 | c.flexpart_root_scripts) |
---|
| 165 | |
---|
[2fb99de] | 166 | mk_job_template(c.ecuid, c.ecgid, c.gateway, |
---|
[54a8a01] | 167 | c.destination, c.flexpart_root_scripts) |
---|
| 168 | |
---|
[2fb99de] | 169 | mk_env_vars(c.ecuid, c.ecgid, c.gateway, c.destination) |
---|
[54a8a01] | 170 | |
---|
[25b14be] | 171 | mk_tarball(tar_file, c.install_target) |
---|
[54a8a01] | 172 | |
---|
| 173 | put_file_to_ecserver(ecd, tarball_name, c.install_target, |
---|
| 174 | c.ecuid, c.ecgid) |
---|
| 175 | |
---|
[2fb99de] | 176 | submit_job_to_ecserver(c.install_target, |
---|
[25b14be] | 177 | os.path.join(_config.PATH_REL_JOBSCRIPTS, |
---|
[2fb99de] | 178 | _config.FILE_INSTALL_COMPILEJOB)) |
---|
[54a8a01] | 179 | |
---|
[2fb99de] | 180 | print('job compilation script has been submitted to ecgate for ' + |
---|
| 181 | 'installation in ' + c.flexpart_root_scripts + |
---|
| 182 | '/' + target_dirname) |
---|
| 183 | print('You should get an email with subject "flexcompile" within ' + |
---|
| 184 | 'the next few minutes!') |
---|
[54a8a01] | 185 | |
---|
| 186 | else: #local |
---|
[2fb99de] | 187 | if c.flexpart_root_scripts == _config.PATH_FLEXEXTRACT_DIR : |
---|
| 188 | print('WARNING: FLEXPART_ROOT_SCRIPTS has not been specified') |
---|
| 189 | print('flex_extract will be installed in here by compiling the ' + |
---|
| 190 | 'Fortran source in ' + _config.PATH_FORTRAN_SRC) |
---|
| 191 | os.chdir(_config.PATH_FORTRAN_SRC) |
---|
[54a8a01] | 192 | else: # creates the target working directory for flex_extract |
---|
| 193 | c.flexpart_root_scripts = os.path.expandvars(os.path.expanduser( |
---|
[2fb99de] | 194 | c.flexpart_root_scripts)) |
---|
[54a8a01] | 195 | if os.path.abspath(ecd) != os.path.abspath(c.flexpart_root_scripts): |
---|
[25b14be] | 196 | mk_tarball(tar_file, c.install_target) |
---|
[2fb99de] | 197 | make_dir(os.path.join(c.flexpart_root_scripts, |
---|
| 198 | target_dirname)) |
---|
| 199 | os.chdir(os.path.join(c.flexpart_root_scripts, |
---|
| 200 | target_dirname)) |
---|
| 201 | un_tarball(tar_file) |
---|
| 202 | os.chdir(os.path.join(c.flexpart_root_scripts, |
---|
| 203 | target_dirname, |
---|
[25b14be] | 204 | _config.PATH_REL_FORTRAN_SRC)) |
---|
[54a8a01] | 205 | |
---|
| 206 | # Create Fortran executable - CONVERT2 |
---|
[2fb99de] | 207 | print('Install ' + target_dirname + ' software at ' + |
---|
| 208 | c.install_target + ' in directory ' + |
---|
| 209 | os.path.abspath(c.flexpart_root_scripts) + '\n') |
---|
| 210 | |
---|
| 211 | delete_convert_build('.') |
---|
| 212 | make_convert_build('.', c.makefile) |
---|
[54a8a01] | 213 | |
---|
[2fb99de] | 214 | os.chdir(ecd) |
---|
| 215 | if os.path.isfile(tar_file): |
---|
| 216 | os.remove(tar_file) |
---|
[54a8a01] | 217 | |
---|
| 218 | return |
---|
| 219 | |
---|
[25b14be] | 220 | def mk_tarball(tarball_path, target): |
---|
[274f9ef] | 221 | '''Creates a tarball with all necessary files which need to be sent to the |
---|
| 222 | installation directory. |
---|
| 223 | It does not matter if this is local or remote. |
---|
| 224 | Collects all python files, the Fortran source and makefiles, |
---|
| 225 | the ECMWF_ENV file, the CONTROL files as well as the |
---|
| 226 | template files. |
---|
| 227 | |
---|
| 228 | Parameters |
---|
| 229 | ---------- |
---|
| 230 | tarball_path : :obj:`string` |
---|
| 231 | The complete path to the tar file which will contain all |
---|
| 232 | relevant data for flex_extract. |
---|
| 233 | |
---|
| 234 | target : :obj:`string` |
---|
| 235 | The queue where the job is submitted to. |
---|
| 236 | |
---|
| 237 | Return |
---|
| 238 | ------ |
---|
| 239 | |
---|
[54a8a01] | 240 | ''' |
---|
[2fb99de] | 241 | import tarfile |
---|
| 242 | from glob import glob |
---|
| 243 | |
---|
| 244 | print('Create tarball ...') |
---|
| 245 | |
---|
| 246 | # change to FLEXEXTRACT directory so that the tar can contain |
---|
| 247 | # relative pathes to the files and directories |
---|
| 248 | ecd = _config.PATH_FLEXEXTRACT_DIR + '/' |
---|
| 249 | os.chdir(ecd) |
---|
| 250 | |
---|
| 251 | # get lists of the files to be added to the tar file |
---|
[25b14be] | 252 | if target == 'local': |
---|
| 253 | ECMWF_ENV_FILE = [] |
---|
| 254 | else: |
---|
| 255 | ECMWF_ENV_FILE = [_config.PATH_REL_ECMWF_ENV] |
---|
| 256 | |
---|
[2fb99de] | 257 | pyfiles = [os.path.relpath(x, ecd) |
---|
[25b14be] | 258 | for x in UioFiles(_config.PATH_LOCAL_PYTHON, '*py').files] |
---|
[2fb99de] | 259 | controlfiles = [os.path.relpath(x, ecd) |
---|
[25b14be] | 260 | for x in UioFiles(_config.PATH_CONTROLFILES, |
---|
| 261 | 'CONTROL*').files] |
---|
[2fb99de] | 262 | tempfiles = [os.path.relpath(x, ecd) |
---|
[25b14be] | 263 | for x in UioFiles(_config.PATH_TEMPLATES , '*').files] |
---|
[2fb99de] | 264 | ffiles = [os.path.relpath(x, ecd) |
---|
[25b14be] | 265 | for x in UioFiles(_config.PATH_FORTRAN_SRC, '*.f*').files] |
---|
[2fb99de] | 266 | hfiles = [os.path.relpath(x, ecd) |
---|
[25b14be] | 267 | for x in UioFiles(_config.PATH_FORTRAN_SRC, '*.h').files] |
---|
[2fb99de] | 268 | makefiles = [os.path.relpath(x, ecd) |
---|
[25b14be] | 269 | for x in UioFiles(_config.PATH_FORTRAN_SRC, 'Makefile*').files] |
---|
[2fb99de] | 270 | |
---|
| 271 | # concatenate single lists to one for a better looping |
---|
| 272 | filelist = pyfiles + controlfiles + tempfiles + ffiles + hfiles + \ |
---|
| 273 | makefiles + ECMWF_ENV_FILE |
---|
| 274 | |
---|
| 275 | # create installation tar-file |
---|
[54a8a01] | 276 | try: |
---|
[2fb99de] | 277 | with tarfile.open(tarball_path, "w:gz") as tar_handle: |
---|
| 278 | for file in filelist: |
---|
| 279 | tar_handle.add(file) |
---|
| 280 | |
---|
[54a8a01] | 281 | except subprocess.CalledProcessError as e: |
---|
[2fb99de] | 282 | print('... ERROR CODE:\n ... ' + str(e.returncode)) |
---|
| 283 | print('... ERROR MESSAGE:\n ... ' + str(e)) |
---|
| 284 | |
---|
| 285 | sys.exit('... could not make installation tar ball!') |
---|
[54a8a01] | 286 | |
---|
| 287 | return |
---|
| 288 | |
---|
[2fb99de] | 289 | |
---|
| 290 | def un_tarball(tarball_path): |
---|
[274f9ef] | 291 | '''Extracts the given tarball into current directory. |
---|
[2fb99de] | 292 | |
---|
[274f9ef] | 293 | Parameters |
---|
| 294 | ---------- |
---|
| 295 | tarball_path : :obj:`string` |
---|
| 296 | The complete path to the tar file which will contain all |
---|
| 297 | relevant data for flex_extract. |
---|
| 298 | |
---|
| 299 | Return |
---|
| 300 | ------ |
---|
[2fb99de] | 301 | |
---|
| 302 | ''' |
---|
| 303 | import tarfile |
---|
| 304 | |
---|
| 305 | print('Untar ...') |
---|
| 306 | |
---|
| 307 | with tarfile.open(tarball_path) as tar_handle: |
---|
| 308 | tar_handle.extractall() |
---|
| 309 | |
---|
| 310 | return |
---|
| 311 | |
---|
| 312 | def mk_env_vars(ecuid, ecgid, gateway, destination): |
---|
[274f9ef] | 313 | '''Creates a file named ECMWF_ENV which contains the |
---|
| 314 | necessary environmental variables at ECMWF servers. |
---|
| 315 | It is based on the template ECMWF_ENV.template. |
---|
| 316 | |
---|
| 317 | Parameters |
---|
| 318 | ---------- |
---|
| 319 | ecuid : :obj:`string` |
---|
| 320 | The user id on ECMWF server. |
---|
[54a8a01] | 321 | |
---|
[274f9ef] | 322 | ecgid : :obj:`string` |
---|
| 323 | The group id on ECMWF server. |
---|
[54a8a01] | 324 | |
---|
[274f9ef] | 325 | gateway : :obj:`string` |
---|
| 326 | The gateway server the user is using. |
---|
[54a8a01] | 327 | |
---|
[274f9ef] | 328 | destination : :obj:`string` |
---|
| 329 | The remote destination which is used to transfer files |
---|
| 330 | from ECMWF server to local gateway server. |
---|
[54a8a01] | 331 | |
---|
[274f9ef] | 332 | Return |
---|
| 333 | ------ |
---|
[54a8a01] | 334 | |
---|
| 335 | ''' |
---|
[0aaeb04] | 336 | from genshi.template.text import NewTextTemplate |
---|
| 337 | from genshi.template import TemplateLoader |
---|
[54a8a01] | 338 | |
---|
[0aaeb04] | 339 | loader = TemplateLoader(_config.PATH_TEMPLATES, auto_reload=False) |
---|
| 340 | ecmwfvars_template = loader.load(_config.TEMPFILE_USER_ENVVARS, |
---|
| 341 | cls=NewTextTemplate) |
---|
| 342 | |
---|
| 343 | stream = ecmwfvars_template.generate(user_name = ecuid, |
---|
| 344 | user_group = ecgid, |
---|
| 345 | gateway_name = gateway, |
---|
| 346 | destination_name = destination |
---|
| 347 | ) |
---|
| 348 | |
---|
| 349 | with open(_config.PATH_ECMWF_ENV, 'w') as f: |
---|
| 350 | f.write(stream.render('text')) |
---|
[54a8a01] | 351 | |
---|
| 352 | return |
---|
| 353 | |
---|
[2fb99de] | 354 | def mk_compilejob(makefile, target, ecuid, ecgid, fp_root): |
---|
[274f9ef] | 355 | '''Modifies the original job template file so that it is specified |
---|
| 356 | for the user and the environment were it will be applied. Result |
---|
| 357 | is stored in a new file "job.temp" in the python directory. |
---|
| 358 | |
---|
| 359 | Parameters |
---|
| 360 | ---------- |
---|
| 361 | makefile : :obj:`string` |
---|
| 362 | Name of the makefile which should be used to compile FORTRAN |
---|
| 363 | CONVERT2 program. |
---|
[54a8a01] | 364 | |
---|
[274f9ef] | 365 | target : :obj:`string` |
---|
| 366 | The target where the installation should be done, e.g. the queue. |
---|
[54a8a01] | 367 | |
---|
[274f9ef] | 368 | ecuid : :obj:`string` |
---|
| 369 | The user id on ECMWF server. |
---|
[54a8a01] | 370 | |
---|
[274f9ef] | 371 | ecgid : :obj:`string` |
---|
| 372 | The group id on ECMWF server. |
---|
[54a8a01] | 373 | |
---|
[274f9ef] | 374 | fp_root : :obj:`string` |
---|
| 375 | Path to the root directory of FLEXPART environment or flex_extract |
---|
| 376 | environment. |
---|
[54a8a01] | 377 | |
---|
[274f9ef] | 378 | Return |
---|
| 379 | ------ |
---|
[a4b6cef] | 380 | |
---|
[991df6a] | 381 | ''' |
---|
[0aaeb04] | 382 | from genshi.template.text import NewTextTemplate |
---|
| 383 | from genshi.template import TemplateLoader |
---|
| 384 | |
---|
| 385 | loader = TemplateLoader(_config.PATH_TEMPLATES, auto_reload=False) |
---|
| 386 | compile_template = loader.load(_config.TEMPFILE_INSTALL_COMPILEJOB, |
---|
| 387 | cls=NewTextTemplate) |
---|
| 388 | |
---|
| 389 | if fp_root == '../': |
---|
| 390 | fp_root = '$HOME' |
---|
| 391 | |
---|
| 392 | stream = compile_template.generate( |
---|
| 393 | usergroup = ecgid, |
---|
[c5074d2] | 394 | username = ecuid, |
---|
[0aaeb04] | 395 | version_number = _config._VERSION_STR, |
---|
| 396 | fp_root_scripts = fp_root, |
---|
| 397 | makefile = makefile, |
---|
| 398 | fortran_program = _config.FORTRAN_EXECUTABLE |
---|
| 399 | ) |
---|
| 400 | |
---|
| 401 | compilejob = os.path.join(_config.PATH_JOBSCRIPTS, |
---|
[2fb99de] | 402 | _config.FILE_INSTALL_COMPILEJOB) |
---|
[0aaeb04] | 403 | |
---|
| 404 | with open(compilejob, 'w') as f: |
---|
| 405 | f.write(stream.render('text')) |
---|
| 406 | |
---|
[54a8a01] | 407 | return |
---|
| 408 | |
---|
[2fb99de] | 409 | def mk_job_template(ecuid, ecgid, gateway, destination, fp_root): |
---|
[274f9ef] | 410 | '''Modifies the original job template file so that it is specified |
---|
| 411 | for the user and the environment were it will be applied. Result |
---|
| 412 | is stored in a new file. |
---|
[54a8a01] | 413 | |
---|
[274f9ef] | 414 | Parameters |
---|
| 415 | ---------- |
---|
| 416 | ecuid : :obj:`string` |
---|
| 417 | The user id on ECMWF server. |
---|
[54a8a01] | 418 | |
---|
[274f9ef] | 419 | ecgid : :obj:`string` |
---|
| 420 | The group id on ECMWF server. |
---|
[54a8a01] | 421 | |
---|
[274f9ef] | 422 | gateway : :obj:`string` |
---|
| 423 | The gateway server the user is using. |
---|
[54a8a01] | 424 | |
---|
[274f9ef] | 425 | destination : :obj:`string` |
---|
| 426 | The remote destination which is used to transfer files |
---|
| 427 | from ECMWF server to local gateway server. |
---|
[54a8a01] | 428 | |
---|
[274f9ef] | 429 | fp_root : :obj:`string` |
---|
| 430 | Path to the root directory of FLEXPART environment or flex_extract |
---|
| 431 | environment. |
---|
| 432 | |
---|
| 433 | Return |
---|
| 434 | ------ |
---|
[54a8a01] | 435 | |
---|
| 436 | ''' |
---|
[c5074d2] | 437 | from genshi.template.text import NewTextTemplate |
---|
| 438 | from genshi.template import TemplateLoader |
---|
| 439 | |
---|
| 440 | loader = TemplateLoader(_config.PATH_TEMPLATES, auto_reload=False) |
---|
| 441 | compile_template = loader.load(_config.TEMPFILE_INSTALL_JOB, |
---|
| 442 | cls=NewTextTemplate) |
---|
| 443 | |
---|
| 444 | fp_root_path_to_python = os.path.join(fp_root, |
---|
| 445 | _config.FLEXEXTRACT_DIRNAME, |
---|
| 446 | _config.PATH_REL_PYTHON) |
---|
| 447 | |
---|
| 448 | stream = compile_template.generate( |
---|
| 449 | usergroup = ecgid, |
---|
| 450 | username = ecuid, |
---|
| 451 | version_number = _config._VERSION_STR, |
---|
| 452 | fp_root_path = fp_root_path_to_python, |
---|
| 453 | ) |
---|
| 454 | |
---|
| 455 | tempjobfile = os.path.join(_config.PATH_TEMPLATES, |
---|
| 456 | _config.TEMPFILE_JOB) |
---|
| 457 | |
---|
| 458 | with open(tempjobfile, 'w') as f: |
---|
| 459 | f.write(stream.render('text')) |
---|
| 460 | |
---|
[54a8a01] | 461 | return |
---|
| 462 | |
---|
[2fb99de] | 463 | def delete_convert_build(src_path): |
---|
[274f9ef] | 464 | '''Clean up the Fortran source directory and remove all |
---|
| 465 | build files (e.g. \*.o, \*.mod and CONVERT2) |
---|
| 466 | |
---|
| 467 | Parameters |
---|
| 468 | ---------- |
---|
| 469 | src_path : :obj:`string` |
---|
| 470 | Path to the fortran source directory. |
---|
[54a8a01] | 471 | |
---|
[274f9ef] | 472 | Return |
---|
| 473 | ------ |
---|
[54a8a01] | 474 | |
---|
| 475 | ''' |
---|
| 476 | |
---|
[2fb99de] | 477 | modfiles = UioFiles(src_path, '*.mod') |
---|
| 478 | objfiles = UioFiles(src_path, '*.o') |
---|
| 479 | exefile = UioFiles(src_path, _config.FORTRAN_EXECUTABLE) |
---|
[54a8a01] | 480 | |
---|
| 481 | modfiles.delete_files() |
---|
| 482 | objfiles.delete_files() |
---|
| 483 | exefile.delete_files() |
---|
| 484 | |
---|
| 485 | return |
---|
| 486 | |
---|
[2fb99de] | 487 | def make_convert_build(src_path, makefile): |
---|
[274f9ef] | 488 | '''Compiles the Fortran code and generates the executable. |
---|
| 489 | |
---|
| 490 | Parameters |
---|
| 491 | ---------- |
---|
| 492 | src_path : :obj:`string` |
---|
| 493 | Path to the fortran source directory. |
---|
[54a8a01] | 494 | |
---|
[274f9ef] | 495 | makefile : :obj:`string` |
---|
| 496 | The name of the makefile which should be used. |
---|
[54a8a01] | 497 | |
---|
[274f9ef] | 498 | Return |
---|
| 499 | ------ |
---|
[54a8a01] | 500 | |
---|
| 501 | ''' |
---|
| 502 | |
---|
| 503 | try: |
---|
[2fb99de] | 504 | print('Using makefile: ' + makefile) |
---|
| 505 | p = subprocess.Popen(['make', '-f', |
---|
| 506 | os.path.join(src_path, makefile)], |
---|
[54a8a01] | 507 | stdin=subprocess.PIPE, |
---|
| 508 | stdout=subprocess.PIPE, |
---|
| 509 | stderr=subprocess.PIPE, |
---|
| 510 | bufsize=1) |
---|
| 511 | pout, perr = p.communicate() |
---|
[2fb99de] | 512 | print(pout) |
---|
[54a8a01] | 513 | if p.returncode != 0: |
---|
[2fb99de] | 514 | print(perr) |
---|
| 515 | print('Please edit ' + makefile + |
---|
| 516 | ' or try another Makefile in the src directory.') |
---|
| 517 | print('Most likely GRIB_API_INCLUDE_DIR, GRIB_API_LIB ' |
---|
| 518 | 'and EMOSLIB must be adapted.') |
---|
| 519 | print('Available Makefiles:') |
---|
| 520 | print(UioFiles(src_path, 'Makefile*')) |
---|
[54a8a01] | 521 | sys.exit('Compilation failed!') |
---|
| 522 | except ValueError as e: |
---|
[2fb99de] | 523 | print('ERROR: Makefile call failed:') |
---|
| 524 | print(e) |
---|
[d69b677] | 525 | else: |
---|
[2fb99de] | 526 | subprocess.check_call(['ls', '-l', |
---|
| 527 | os.path.join(src_path, |
---|
| 528 | _config.FORTRAN_EXECUTABLE)]) |
---|
[a4b6cef] | 529 | |
---|
[d69b677] | 530 | return |
---|
| 531 | |
---|
[a4b6cef] | 532 | |
---|
[d69b677] | 533 | if __name__ == "__main__": |
---|
| 534 | main() |
---|