[8463d78] | 1 | #!/usr/bin/env python3 |
---|
[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 |
---|
[6f951ca] | 16 | # - changed static path names to variables from config file |
---|
| 17 | # - splitted install function into several smaller pieces |
---|
| 18 | # - use of tarfile package in python |
---|
[991df6a] | 19 | # |
---|
| 20 | # @License: |
---|
[6f951ca] | 21 | # (C) Copyright 2014-2019. |
---|
| 22 | # Anne Philipp, Leopold Haimberger |
---|
[991df6a] | 23 | # |
---|
[44174de] | 24 | # SPDX-License-Identifier: CC-BY-4.0 |
---|
| 25 | # |
---|
[6f951ca] | 26 | # This work is licensed under the Creative Commons Attribution 4.0 |
---|
| 27 | # International License. To view a copy of this license, visit |
---|
| 28 | # http://creativecommons.org/licenses/by/4.0/ or send a letter to |
---|
| 29 | # Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
---|
[991df6a] | 30 | # |
---|
[6f951ca] | 31 | # @Methods: |
---|
| 32 | # main |
---|
| 33 | # get_install_cmdline_args |
---|
| 34 | # install_via_gateway |
---|
| 35 | # check_install_conditions |
---|
| 36 | # mk_tarball |
---|
| 37 | # un_tarball |
---|
| 38 | # mk_env_vars |
---|
| 39 | # mk_compilejob |
---|
| 40 | # mk_job_template |
---|
| 41 | # del_convert_build |
---|
| 42 | # mk_convert_build |
---|
[991df6a] | 43 | # |
---|
| 44 | #******************************************************************************* |
---|
[6f951ca] | 45 | '''This script installs the flex_extract program. |
---|
| 46 | |
---|
| 47 | Depending on the selected installation environment (locally or on the |
---|
| 48 | ECMWF server ecgate or cca) the program extracts the commandline |
---|
| 49 | arguments and the CONTROL file parameter and prepares the corresponding |
---|
| 50 | environment. |
---|
| 51 | The necessary files are collected in a tar-ball and placed |
---|
| 52 | at the target location. There its untared, the environment variables will |
---|
| 53 | be set and the Fortran code will be compiled. |
---|
| 54 | If the ECMWF environment is selected a job script is prepared and submitted |
---|
| 55 | for the remaining configurations after putting the tar-ball to the |
---|
| 56 | target ECMWF server. |
---|
| 57 | |
---|
| 58 | Type: install.py --help |
---|
| 59 | to get information about command line parameters. |
---|
| 60 | Read the documentation for usage instructions. |
---|
| 61 | ''' |
---|
[991df6a] | 62 | |
---|
[64cf353] | 63 | # ------------------------------------------------------------------------------ |
---|
| 64 | # MODULES |
---|
| 65 | # ------------------------------------------------------------------------------ |
---|
[8ce3be6] | 66 | from __future__ import print_function |
---|
| 67 | |
---|
[991df6a] | 68 | import os |
---|
| 69 | import sys |
---|
[2fb99de] | 70 | import glob |
---|
[d69b677] | 71 | import subprocess |
---|
| 72 | import inspect |
---|
[3946de5] | 73 | import tarfile |
---|
[ff99eae] | 74 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
---|
[991df6a] | 75 | |
---|
| 76 | # software specific classes and modules from flex_extract |
---|
[2fb99de] | 77 | import _config |
---|
[ba99230] | 78 | from Classes.ControlFile import ControlFile |
---|
| 79 | from Classes.UioFiles import UioFiles |
---|
| 80 | from Mods.tools import (make_dir, put_file_to_ecserver, submit_job_to_ecserver, |
---|
[8ce3be6] | 81 | silent_remove, execute_subprocess, none_or_str) |
---|
[d69b677] | 82 | |
---|
[64cf353] | 83 | # ------------------------------------------------------------------------------ |
---|
| 84 | # FUNCTIONS |
---|
| 85 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 86 | def main(): |
---|
[274f9ef] | 87 | '''Controls the installation process. Calls the installation function |
---|
| 88 | if target is specified. |
---|
[991df6a] | 89 | |
---|
[274f9ef] | 90 | Parameters |
---|
| 91 | ---------- |
---|
[991df6a] | 92 | |
---|
[274f9ef] | 93 | Return |
---|
| 94 | ------ |
---|
[991df6a] | 95 | ''' |
---|
| 96 | |
---|
[8652e7e] | 97 | args = get_install_cmdline_args() |
---|
[4971f63] | 98 | c = ControlFile(args.controlfile) |
---|
[54a8a01] | 99 | c.assign_args_to_control(args) |
---|
[6cda7c1] | 100 | check_install_conditions(c) |
---|
[991df6a] | 101 | |
---|
[8ce3be6] | 102 | if c.install_target.lower() != 'local': # ecgate or cca |
---|
| 103 | install_via_gateway(c) |
---|
| 104 | else: # local |
---|
| 105 | install_local(c) |
---|
[991df6a] | 106 | |
---|
[54a8a01] | 107 | return |
---|
[991df6a] | 108 | |
---|
[8652e7e] | 109 | def get_install_cmdline_args(): |
---|
[274f9ef] | 110 | '''Decomposes the command line arguments and assigns them to variables. |
---|
| 111 | Apply default values for non mentioned arguments. |
---|
[efdb01a] | 112 | |
---|
[274f9ef] | 113 | Parameters |
---|
| 114 | ---------- |
---|
[efdb01a] | 115 | |
---|
[274f9ef] | 116 | Return |
---|
| 117 | ------ |
---|
[6f951ca] | 118 | args : Namespace |
---|
[274f9ef] | 119 | Contains the commandline arguments from script/program call. |
---|
[efdb01a] | 120 | ''' |
---|
[8ce3be6] | 121 | parser = ArgumentParser(description='Install flex_extract software ' |
---|
| 122 | 'locally or on ECMWF machines', |
---|
[efdb01a] | 123 | formatter_class=ArgumentDefaultsHelpFormatter) |
---|
| 124 | |
---|
[8ce3be6] | 125 | parser.add_argument('--target', dest='install_target', |
---|
| 126 | type=none_or_str, default=None, |
---|
[efdb01a] | 127 | help="Valid targets: local | ecgate | cca , \ |
---|
| 128 | the latter two are at ECMWF") |
---|
[8ce3be6] | 129 | parser.add_argument("--makefile", dest="makefile", |
---|
| 130 | type=none_or_str, default=None, |
---|
| 131 | help='Name of Makefile to use for compiling the ' |
---|
| 132 | 'Fortran program') |
---|
| 133 | parser.add_argument("--ecuid", dest="ecuid", |
---|
| 134 | type=none_or_str, default=None, |
---|
| 135 | help='The user id at ECMWF.') |
---|
| 136 | parser.add_argument("--ecgid", dest="ecgid", |
---|
| 137 | type=none_or_str, default=None, |
---|
| 138 | help='The group id at ECMWF.') |
---|
| 139 | parser.add_argument("--gateway", dest="gateway", |
---|
| 140 | type=none_or_str, default=None, |
---|
| 141 | help='The name of the local gateway server.') |
---|
| 142 | parser.add_argument("--destination", dest="destination", |
---|
| 143 | type=none_or_str, default=None, |
---|
| 144 | help='The ecaccess association, e.g. ' |
---|
| 145 | 'myUser@genericSftp') |
---|
| 146 | |
---|
| 147 | parser.add_argument("--installdir", dest="installdir", |
---|
| 148 | type=none_or_str, default=None, |
---|
| 149 | help='Root directory where ' |
---|
| 150 | 'flex_extract will be installed to.') |
---|
[efdb01a] | 151 | |
---|
[54a8a01] | 152 | # arguments for job submission to ECMWF, only needed by submit.py |
---|
[efdb01a] | 153 | parser.add_argument("--job_template", dest='job_template', |
---|
[8ce3be6] | 154 | type=none_or_str, default="job.template", |
---|
| 155 | help='The rudimentary template file to create a batch ' |
---|
| 156 | 'job template for submission to ECMWF servers.') |
---|
[efdb01a] | 157 | |
---|
| 158 | parser.add_argument("--controlfile", dest="controlfile", |
---|
[8ce3be6] | 159 | type=none_or_str, default='CONTROL_EA5', |
---|
| 160 | help="The file with all CONTROL parameters.") |
---|
[efdb01a] | 161 | |
---|
| 162 | args = parser.parse_args() |
---|
| 163 | |
---|
[54a8a01] | 164 | return args |
---|
| 165 | |
---|
[efdb01a] | 166 | |
---|
[54a8a01] | 167 | def install_via_gateway(c): |
---|
[8ce3be6] | 168 | '''Prepare data transfer to remote gate and submit a job script which will |
---|
[274f9ef] | 169 | install everything on the remote gate. |
---|
| 170 | |
---|
| 171 | Parameters |
---|
| 172 | ---------- |
---|
[6f951ca] | 173 | c : ControlFile |
---|
[274f9ef] | 174 | Contains all the parameters of CONTROL file and |
---|
| 175 | command line. |
---|
| 176 | |
---|
| 177 | Return |
---|
| 178 | ------ |
---|
| 179 | |
---|
[54a8a01] | 180 | ''' |
---|
[2fb99de] | 181 | import tarfile |
---|
| 182 | |
---|
| 183 | tarball_name = _config.FLEXEXTRACT_DIRNAME + '.tar' |
---|
[8ce3be6] | 184 | tar_file = os.path.join(_config.PATH_FLEXEXTRACT_DIR, tarball_name) |
---|
[54a8a01] | 185 | |
---|
[8ce3be6] | 186 | mk_compilejob(c.makefile, c.install_target, c.ecuid, c.ecgid, |
---|
| 187 | c.installdir) |
---|
[54a8a01] | 188 | |
---|
[8ce3be6] | 189 | mk_job_template(c.ecuid, c.ecgid, c.gateway, |
---|
| 190 | c.destination, c.installdir) |
---|
| 191 | |
---|
| 192 | mk_env_vars(c.ecuid, c.ecgid, c.gateway, c.destination) |
---|
| 193 | |
---|
| 194 | mk_tarball(tar_file, c.install_target) |
---|
| 195 | |
---|
| 196 | put_file_to_ecserver(_config.PATH_FLEXEXTRACT_DIR, tarball_name, |
---|
| 197 | c.install_target, c.ecuid, c.ecgid) |
---|
| 198 | |
---|
| 199 | submit_job_to_ecserver(c.install_target, |
---|
| 200 | os.path.join(_config.PATH_REL_JOBSCRIPTS, |
---|
| 201 | _config.FILE_INSTALL_COMPILEJOB)) |
---|
| 202 | |
---|
| 203 | silent_remove(tar_file) |
---|
| 204 | |
---|
| 205 | print('Job compilation script has been submitted to ecgate for ' + |
---|
| 206 | 'installation in ' + c.installdir + |
---|
| 207 | '/' + _config.FLEXEXTRACT_DIRNAME) |
---|
| 208 | print('You should get an email with subject "flexcompile" within ' + |
---|
| 209 | 'the next few minutes!') |
---|
| 210 | |
---|
| 211 | return |
---|
| 212 | |
---|
| 213 | def install_local(c): |
---|
| 214 | '''Perform the actual installation on a local machine. |
---|
[54a8a01] | 215 | |
---|
[8ce3be6] | 216 | Parameters |
---|
| 217 | ---------- |
---|
| 218 | c : ControlFile |
---|
| 219 | Contains all the parameters of CONTROL file and |
---|
| 220 | command line. |
---|
| 221 | |
---|
| 222 | Return |
---|
| 223 | ------ |
---|
| 224 | |
---|
| 225 | ''' |
---|
| 226 | import tarfile |
---|
| 227 | |
---|
| 228 | tar_file = os.path.join(_config.PATH_FLEXEXTRACT_DIR, |
---|
| 229 | _config.FLEXEXTRACT_DIRNAME + '.tar') |
---|
| 230 | |
---|
| 231 | if c.installdir == _config.PATH_FLEXEXTRACT_DIR : |
---|
| 232 | print('WARNING: installdir has not been specified') |
---|
| 233 | print('flex_extract will be installed in here by compiling the ' + |
---|
| 234 | 'Fortran source in ' + _config.PATH_FORTRAN_SRC) |
---|
| 235 | os.chdir(_config.PATH_FORTRAN_SRC) |
---|
| 236 | else: # creates the target working directory for flex_extract |
---|
| 237 | c.installdir = os.path.expandvars(os.path.expanduser( |
---|
| 238 | c.installdir)) |
---|
| 239 | if os.path.abspath(_config.PATH_FLEXEXTRACT_DIR) != \ |
---|
| 240 | os.path.abspath(c.installdir): |
---|
| 241 | mk_tarball(tar_file, c.install_target) |
---|
| 242 | make_dir(os.path.join(c.installdir, |
---|
| 243 | _config.FLEXEXTRACT_DIRNAME)) |
---|
| 244 | os.chdir(os.path.join(c.installdir, |
---|
| 245 | _config.FLEXEXTRACT_DIRNAME)) |
---|
| 246 | un_tarball(tar_file) |
---|
| 247 | os.chdir(os.path.join(c.installdir, |
---|
| 248 | _config.FLEXEXTRACT_DIRNAME, |
---|
| 249 | _config.PATH_REL_FORTRAN_SRC)) |
---|
| 250 | |
---|
| 251 | # Create Fortran executable - CONVERT2 |
---|
| 252 | print('Install ' + _config.FLEXEXTRACT_DIRNAME + ' software at ' + |
---|
| 253 | c.install_target + ' in directory ' + |
---|
| 254 | os.path.abspath(c.installdir) + '\n') |
---|
| 255 | |
---|
| 256 | del_convert_build('.') |
---|
| 257 | mk_convert_build('.', c.makefile) |
---|
| 258 | |
---|
| 259 | os.chdir(_config.PATH_FLEXEXTRACT_DIR) |
---|
| 260 | if os.path.isfile(tar_file): |
---|
| 261 | os.remove(tar_file) |
---|
[54a8a01] | 262 | |
---|
| 263 | return |
---|
| 264 | |
---|
[8ce3be6] | 265 | |
---|
[6cda7c1] | 266 | def check_install_conditions(c): |
---|
| 267 | '''Checks a couple of necessary attributes and conditions |
---|
| 268 | for the installation such as if they exist and contain values. |
---|
| 269 | Otherwise set default values. |
---|
| 270 | |
---|
| 271 | Parameters |
---|
| 272 | ---------- |
---|
[6f951ca] | 273 | c : ControlFile |
---|
[6cda7c1] | 274 | Contains all the parameters of CONTROL file and |
---|
| 275 | command line. |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | Return |
---|
| 279 | ------ |
---|
| 280 | |
---|
| 281 | ''' |
---|
| 282 | |
---|
| 283 | if c.install_target and \ |
---|
| 284 | c.install_target not in _config.INSTALL_TARGETS: |
---|
| 285 | print('ERROR: unknown or missing installation target ') |
---|
| 286 | print('target: ', c.install_target) |
---|
| 287 | print('please specify correct installation target ' + |
---|
| 288 | str(INSTALL_TARGETS)) |
---|
| 289 | print('use -h or --help for help') |
---|
| 290 | sys.exit(1) |
---|
| 291 | |
---|
| 292 | if c.install_target and c.install_target != 'local': |
---|
| 293 | if not c.ecgid or not c.ecuid or \ |
---|
| 294 | not c.gateway or not c.destination: |
---|
| 295 | print('Please enter your ECMWF user id and group id as well ' + |
---|
| 296 | 'as the \nname of the local gateway and the ectrans ' + |
---|
| 297 | 'destination ') |
---|
| 298 | print('with command line options --ecuid --ecgid \ |
---|
| 299 | --gateway --destination') |
---|
| 300 | print('Try "' + sys.argv[0].split('/')[-1] + \ |
---|
| 301 | ' -h" to print usage information') |
---|
| 302 | print('Please consult ecaccess documentation or ECMWF user \ |
---|
| 303 | support for further details') |
---|
| 304 | sys.exit(1) |
---|
| 305 | |
---|
[8ce3be6] | 306 | if not c.installdir: |
---|
| 307 | c.installdir = '${HOME}' |
---|
[6cda7c1] | 308 | else: # local |
---|
[8ce3be6] | 309 | if not c.installdir: |
---|
| 310 | c.installdir = _config.PATH_FLEXEXTRACT_DIR |
---|
[6cda7c1] | 311 | |
---|
| 312 | return |
---|
| 313 | |
---|
| 314 | |
---|
[25b14be] | 315 | def mk_tarball(tarball_path, target): |
---|
[274f9ef] | 316 | '''Creates a tarball with all necessary files which need to be sent to the |
---|
| 317 | installation directory. |
---|
| 318 | It does not matter if this is local or remote. |
---|
| 319 | Collects all python files, the Fortran source and makefiles, |
---|
| 320 | the ECMWF_ENV file, the CONTROL files as well as the |
---|
| 321 | template files. |
---|
| 322 | |
---|
| 323 | Parameters |
---|
| 324 | ---------- |
---|
[6f951ca] | 325 | tarball_path : str |
---|
[274f9ef] | 326 | The complete path to the tar file which will contain all |
---|
| 327 | relevant data for flex_extract. |
---|
| 328 | |
---|
[6f951ca] | 329 | target : str |
---|
[274f9ef] | 330 | The queue where the job is submitted to. |
---|
| 331 | |
---|
| 332 | Return |
---|
| 333 | ------ |
---|
| 334 | |
---|
[54a8a01] | 335 | ''' |
---|
[2fb99de] | 336 | from glob import glob |
---|
| 337 | |
---|
| 338 | print('Create tarball ...') |
---|
| 339 | |
---|
| 340 | # change to FLEXEXTRACT directory so that the tar can contain |
---|
| 341 | # relative pathes to the files and directories |
---|
| 342 | ecd = _config.PATH_FLEXEXTRACT_DIR + '/' |
---|
| 343 | os.chdir(ecd) |
---|
| 344 | |
---|
| 345 | # get lists of the files to be added to the tar file |
---|
[25b14be] | 346 | if target == 'local': |
---|
| 347 | ECMWF_ENV_FILE = [] |
---|
[3946de5] | 348 | runfile = [os.path.relpath(x, ecd) |
---|
| 349 | for x in UioFiles(_config.PATH_REL_RUN_DIR, |
---|
| 350 | 'run_local.sh').files] |
---|
[25b14be] | 351 | else: |
---|
| 352 | ECMWF_ENV_FILE = [_config.PATH_REL_ECMWF_ENV] |
---|
[3946de5] | 353 | runfile = [os.path.relpath(x, ecd) |
---|
| 354 | for x in UioFiles(_config.PATH_REL_RUN_DIR, |
---|
| 355 | 'run.sh').files] |
---|
[25b14be] | 356 | |
---|
[2fb99de] | 357 | pyfiles = [os.path.relpath(x, ecd) |
---|
[3946de5] | 358 | for x in UioFiles(_config.PATH_REL_PYTHON_SRC, '*py').files] |
---|
| 359 | pytestfiles = [os.path.relpath(x, ecd) |
---|
| 360 | for x in UioFiles(_config.PATH_REL_PYTHONTEST_SRC, '*py').files] |
---|
[2fb99de] | 361 | controlfiles = [os.path.relpath(x, ecd) |
---|
[3946de5] | 362 | for x in UioFiles(_config.PATH_REL_CONTROLFILES, |
---|
[25b14be] | 363 | 'CONTROL*').files] |
---|
[1eca806] | 364 | testfiles = [os.path.relpath(x, ecd) |
---|
| 365 | for x in UioFiles(_config.PATH_REL_TEST , '*').files] |
---|
[2fb99de] | 366 | tempfiles = [os.path.relpath(x, ecd) |
---|
[3946de5] | 367 | for x in UioFiles(_config.PATH_REL_TEMPLATES , '*.temp').files] |
---|
| 368 | nlfiles = [os.path.relpath(x, ecd) |
---|
| 369 | for x in UioFiles(_config.PATH_REL_TEMPLATES , '*.nl').files] |
---|
| 370 | gribtable = [os.path.relpath(x, ecd) |
---|
| 371 | for x in UioFiles(_config.PATH_REL_TEMPLATES , '*grib*').files] |
---|
[2fb99de] | 372 | ffiles = [os.path.relpath(x, ecd) |
---|
[3946de5] | 373 | for x in UioFiles(_config.PATH_REL_FORTRAN_SRC, '*.f*').files] |
---|
[2fb99de] | 374 | hfiles = [os.path.relpath(x, ecd) |
---|
[3946de5] | 375 | for x in UioFiles(_config.PATH_REL_FORTRAN_SRC, '*.h').files] |
---|
[2fb99de] | 376 | makefiles = [os.path.relpath(x, ecd) |
---|
[3946de5] | 377 | for x in UioFiles(_config.PATH_REL_FORTRAN_SRC, 'Makefile*').files] |
---|
| 378 | jobdir = [_config.PATH_REL_JOBSCRIPTS] |
---|
[2fb99de] | 379 | |
---|
| 380 | # concatenate single lists to one for a better looping |
---|
[3946de5] | 381 | filelist = pyfiles + pytestfiles + controlfiles + tempfiles + nlfiles + \ |
---|
| 382 | ffiles + gribtable + hfiles + makefiles + ECMWF_ENV_FILE + \ |
---|
[1eca806] | 383 | runfile + jobdir + testfiles +\ |
---|
[3946de5] | 384 | ['CODE_OF_CONDUCT.md', 'LICENSE.md', 'README.md'] |
---|
[2fb99de] | 385 | |
---|
| 386 | # create installation tar-file |
---|
[3946de5] | 387 | exclude_files = [".ksh"] |
---|
[54a8a01] | 388 | try: |
---|
[2fb99de] | 389 | with tarfile.open(tarball_path, "w:gz") as tar_handle: |
---|
| 390 | for file in filelist: |
---|
[3946de5] | 391 | tar_handle.add(file, recursive=False, |
---|
| 392 | filter=lambda tarinfo: None |
---|
| 393 | if os.path.splitext(tarinfo.name)[1] |
---|
| 394 | in exclude_files |
---|
| 395 | else tarinfo) |
---|
[79729d5] | 396 | except tarfile.TarError as e: |
---|
| 397 | sys.exit('\n... error occured while trying to create the tar-file ' + |
---|
| 398 | str(tarball_path)) |
---|
[54a8a01] | 399 | |
---|
| 400 | return |
---|
| 401 | |
---|
[2fb99de] | 402 | |
---|
| 403 | def un_tarball(tarball_path): |
---|
[274f9ef] | 404 | '''Extracts the given tarball into current directory. |
---|
[2fb99de] | 405 | |
---|
[274f9ef] | 406 | Parameters |
---|
| 407 | ---------- |
---|
[6f951ca] | 408 | tarball_path : str |
---|
[274f9ef] | 409 | The complete path to the tar file which will contain all |
---|
| 410 | relevant data for flex_extract. |
---|
| 411 | |
---|
| 412 | Return |
---|
| 413 | ------ |
---|
[2fb99de] | 414 | |
---|
| 415 | ''' |
---|
| 416 | |
---|
| 417 | print('Untar ...') |
---|
| 418 | |
---|
[3946de5] | 419 | try: |
---|
| 420 | with tarfile.open(tarball_path) as tar_handle: |
---|
| 421 | tar_handle.extractall() |
---|
| 422 | except tarfile.TarError as e: |
---|
| 423 | sys.exit('\n... error occured while trying to read tar-file ' + |
---|
| 424 | str(tarball_path)) |
---|
| 425 | except OSError as e: |
---|
| 426 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 427 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 428 | |
---|
| 429 | sys.exit('\n... error occured while trying to read tar-file ' + |
---|
| 430 | str(tarball_path)) |
---|
[2fb99de] | 431 | |
---|
| 432 | return |
---|
| 433 | |
---|
| 434 | def mk_env_vars(ecuid, ecgid, gateway, destination): |
---|
[274f9ef] | 435 | '''Creates a file named ECMWF_ENV which contains the |
---|
| 436 | necessary environmental variables at ECMWF servers. |
---|
| 437 | It is based on the template ECMWF_ENV.template. |
---|
| 438 | |
---|
| 439 | Parameters |
---|
| 440 | ---------- |
---|
[6f951ca] | 441 | ecuid : str |
---|
[274f9ef] | 442 | The user id on ECMWF server. |
---|
[54a8a01] | 443 | |
---|
[6f951ca] | 444 | ecgid : str |
---|
[274f9ef] | 445 | The group id on ECMWF server. |
---|
[54a8a01] | 446 | |
---|
[6f951ca] | 447 | gateway : str |
---|
[274f9ef] | 448 | The gateway server the user is using. |
---|
[54a8a01] | 449 | |
---|
[6f951ca] | 450 | destination : str |
---|
[274f9ef] | 451 | The remote destination which is used to transfer files |
---|
| 452 | from ECMWF server to local gateway server. |
---|
[54a8a01] | 453 | |
---|
[274f9ef] | 454 | Return |
---|
| 455 | ------ |
---|
[54a8a01] | 456 | |
---|
| 457 | ''' |
---|
[0aaeb04] | 458 | from genshi.template.text import NewTextTemplate |
---|
| 459 | from genshi.template import TemplateLoader |
---|
[3946de5] | 460 | from genshi.template.eval import UndefinedError |
---|
[54a8a01] | 461 | |
---|
[3946de5] | 462 | try: |
---|
| 463 | loader = TemplateLoader(_config.PATH_TEMPLATES, auto_reload=False) |
---|
| 464 | ecmwfvars_template = loader.load(_config.TEMPFILE_USER_ENVVARS, |
---|
| 465 | cls=NewTextTemplate) |
---|
| 466 | |
---|
| 467 | stream = ecmwfvars_template.generate(user_name = ecuid, |
---|
| 468 | user_group = ecgid, |
---|
| 469 | gateway_name = gateway, |
---|
| 470 | destination_name = destination |
---|
| 471 | ) |
---|
| 472 | except UndefinedError as e: |
---|
| 473 | print('... ERROR ' + str(e)) |
---|
| 474 | |
---|
| 475 | sys.exit('\n... error occured while trying to generate template ' + |
---|
| 476 | _config.PATH_ECMWF_ENV) |
---|
| 477 | except OSError as e: |
---|
| 478 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 479 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 480 | |
---|
| 481 | sys.exit('\n... error occured while trying to generate template ' + |
---|
| 482 | _config.PATH_ECMWF_ENV) |
---|
[0aaeb04] | 483 | |
---|
[3946de5] | 484 | try: |
---|
| 485 | with open(_config.PATH_ECMWF_ENV, 'w') as f: |
---|
| 486 | f.write(stream.render('text')) |
---|
| 487 | except OSError as e: |
---|
| 488 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 489 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
[0aaeb04] | 490 | |
---|
[3946de5] | 491 | sys.exit('\n... error occured while trying to write ' + |
---|
| 492 | _config.PATH_ECMWF_ENV) |
---|
[54a8a01] | 493 | |
---|
| 494 | return |
---|
| 495 | |
---|
[2fb99de] | 496 | def mk_compilejob(makefile, target, ecuid, ecgid, fp_root): |
---|
[274f9ef] | 497 | '''Modifies the original job template file so that it is specified |
---|
| 498 | for the user and the environment were it will be applied. Result |
---|
| 499 | is stored in a new file "job.temp" in the python directory. |
---|
| 500 | |
---|
| 501 | Parameters |
---|
| 502 | ---------- |
---|
[6f951ca] | 503 | makefile : str |
---|
[274f9ef] | 504 | Name of the makefile which should be used to compile FORTRAN |
---|
| 505 | CONVERT2 program. |
---|
[54a8a01] | 506 | |
---|
[6f951ca] | 507 | target : str |
---|
[274f9ef] | 508 | The target where the installation should be done, e.g. the queue. |
---|
[54a8a01] | 509 | |
---|
[6f951ca] | 510 | ecuid : str |
---|
[274f9ef] | 511 | The user id on ECMWF server. |
---|
[54a8a01] | 512 | |
---|
[6f951ca] | 513 | ecgid : str |
---|
[274f9ef] | 514 | The group id on ECMWF server. |
---|
[54a8a01] | 515 | |
---|
[6f951ca] | 516 | fp_root : str |
---|
[274f9ef] | 517 | Path to the root directory of FLEXPART environment or flex_extract |
---|
| 518 | environment. |
---|
[54a8a01] | 519 | |
---|
[274f9ef] | 520 | Return |
---|
| 521 | ------ |
---|
[a4b6cef] | 522 | |
---|
[991df6a] | 523 | ''' |
---|
[0aaeb04] | 524 | from genshi.template.text import NewTextTemplate |
---|
| 525 | from genshi.template import TemplateLoader |
---|
[3946de5] | 526 | from genshi.template.eval import UndefinedError |
---|
[0aaeb04] | 527 | |
---|
| 528 | if fp_root == '../': |
---|
| 529 | fp_root = '$HOME' |
---|
| 530 | |
---|
[3946de5] | 531 | try: |
---|
| 532 | loader = TemplateLoader(_config.PATH_TEMPLATES, auto_reload=False) |
---|
| 533 | compile_template = loader.load(_config.TEMPFILE_INSTALL_COMPILEJOB, |
---|
| 534 | cls=NewTextTemplate) |
---|
| 535 | |
---|
| 536 | stream = compile_template.generate( |
---|
| 537 | usergroup = ecgid, |
---|
| 538 | username = ecuid, |
---|
| 539 | version_number = _config._VERSION_STR, |
---|
| 540 | fp_root_scripts = fp_root, |
---|
| 541 | makefile = makefile, |
---|
| 542 | fortran_program = _config.FORTRAN_EXECUTABLE |
---|
| 543 | ) |
---|
| 544 | except UndefinedError as e: |
---|
| 545 | print('... ERROR ' + str(e)) |
---|
| 546 | |
---|
| 547 | sys.exit('\n... error occured while trying to generate template ' + |
---|
| 548 | _config.TEMPFILE_INSTALL_COMPILEJOB) |
---|
| 549 | except OSError as e: |
---|
| 550 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 551 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 552 | |
---|
| 553 | sys.exit('\n... error occured while trying to generate template ' + |
---|
| 554 | _config.TEMPFILE_INSTALL_COMPILEJOB) |
---|
[0aaeb04] | 555 | |
---|
[3946de5] | 556 | try: |
---|
| 557 | compilejob = os.path.join(_config.PATH_JOBSCRIPTS, |
---|
| 558 | _config.FILE_INSTALL_COMPILEJOB) |
---|
[0aaeb04] | 559 | |
---|
[3946de5] | 560 | with open(compilejob, 'w') as f: |
---|
| 561 | f.write(stream.render('text')) |
---|
| 562 | except OSError as e: |
---|
| 563 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 564 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 565 | |
---|
| 566 | sys.exit('\n... error occured while trying to write ' + |
---|
| 567 | compilejob) |
---|
[0aaeb04] | 568 | |
---|
[54a8a01] | 569 | return |
---|
| 570 | |
---|
[2fb99de] | 571 | def mk_job_template(ecuid, ecgid, gateway, destination, fp_root): |
---|
[274f9ef] | 572 | '''Modifies the original job template file so that it is specified |
---|
| 573 | for the user and the environment were it will be applied. Result |
---|
| 574 | is stored in a new file. |
---|
[54a8a01] | 575 | |
---|
[274f9ef] | 576 | Parameters |
---|
| 577 | ---------- |
---|
[6f951ca] | 578 | ecuid : str |
---|
[274f9ef] | 579 | The user id on ECMWF server. |
---|
[54a8a01] | 580 | |
---|
[6f951ca] | 581 | ecgid : str |
---|
[274f9ef] | 582 | The group id on ECMWF server. |
---|
[54a8a01] | 583 | |
---|
[6f951ca] | 584 | gateway : str |
---|
[274f9ef] | 585 | The gateway server the user is using. |
---|
[54a8a01] | 586 | |
---|
[6f951ca] | 587 | destination : str |
---|
[274f9ef] | 588 | The remote destination which is used to transfer files |
---|
| 589 | from ECMWF server to local gateway server. |
---|
[54a8a01] | 590 | |
---|
[6f951ca] | 591 | fp_root : str |
---|
[274f9ef] | 592 | Path to the root directory of FLEXPART environment or flex_extract |
---|
| 593 | environment. |
---|
| 594 | |
---|
| 595 | Return |
---|
| 596 | ------ |
---|
[54a8a01] | 597 | |
---|
| 598 | ''' |
---|
[c5074d2] | 599 | from genshi.template.text import NewTextTemplate |
---|
| 600 | from genshi.template import TemplateLoader |
---|
[3946de5] | 601 | from genshi.template.eval import UndefinedError |
---|
[c5074d2] | 602 | |
---|
| 603 | fp_root_path_to_python = os.path.join(fp_root, |
---|
| 604 | _config.FLEXEXTRACT_DIRNAME, |
---|
[3946de5] | 605 | _config.PATH_REL_PYTHON_SRC) |
---|
[ff2a11c] | 606 | if '$' in fp_root_path_to_python: |
---|
| 607 | ind = fp_root_path_to_python.index('$') |
---|
| 608 | fp_root_path_to_python = fp_root_path_to_python[0:ind] + '$' + \ |
---|
| 609 | fp_root_path_to_python[ind:] |
---|
[3946de5] | 610 | |
---|
| 611 | try: |
---|
| 612 | loader = TemplateLoader(_config.PATH_TEMPLATES, auto_reload=False) |
---|
| 613 | compile_template = loader.load(_config.TEMPFILE_INSTALL_JOB, |
---|
| 614 | cls=NewTextTemplate) |
---|
| 615 | |
---|
| 616 | stream = compile_template.generate( |
---|
| 617 | usergroup = ecgid, |
---|
| 618 | username = ecuid, |
---|
| 619 | version_number = _config._VERSION_STR, |
---|
| 620 | fp_root_path = fp_root_path_to_python, |
---|
| 621 | ) |
---|
| 622 | except UndefinedError as e: |
---|
| 623 | print('... ERROR ' + str(e)) |
---|
[c5074d2] | 624 | |
---|
[3946de5] | 625 | sys.exit('\n... error occured while trying to generate template ' + |
---|
| 626 | _config.TEMPFILE_INSTALL_JOB) |
---|
| 627 | except OSError as e: |
---|
| 628 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 629 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 630 | |
---|
| 631 | sys.exit('\n... error occured while trying to generate template ' + |
---|
| 632 | _config.TEMPFILE_INSTALL_JOB) |
---|
| 633 | |
---|
| 634 | |
---|
| 635 | try: |
---|
| 636 | tempjobfile = os.path.join(_config.PATH_TEMPLATES, |
---|
| 637 | _config.TEMPFILE_JOB) |
---|
[c5074d2] | 638 | |
---|
[3946de5] | 639 | with open(tempjobfile, 'w') as f: |
---|
| 640 | f.write(stream.render('text')) |
---|
| 641 | except OSError as e: |
---|
| 642 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 643 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
[c5074d2] | 644 | |
---|
[3946de5] | 645 | sys.exit('\n... error occured while trying to write ' + |
---|
| 646 | tempjobfile) |
---|
[c5074d2] | 647 | |
---|
[54a8a01] | 648 | return |
---|
| 649 | |
---|
[8652e7e] | 650 | def del_convert_build(src_path): |
---|
[274f9ef] | 651 | '''Clean up the Fortran source directory and remove all |
---|
| 652 | build files (e.g. \*.o, \*.mod and CONVERT2) |
---|
| 653 | |
---|
| 654 | Parameters |
---|
| 655 | ---------- |
---|
[6f951ca] | 656 | src_path : str |
---|
[274f9ef] | 657 | Path to the fortran source directory. |
---|
[54a8a01] | 658 | |
---|
[274f9ef] | 659 | Return |
---|
| 660 | ------ |
---|
[54a8a01] | 661 | |
---|
| 662 | ''' |
---|
| 663 | |
---|
[2fb99de] | 664 | modfiles = UioFiles(src_path, '*.mod') |
---|
| 665 | objfiles = UioFiles(src_path, '*.o') |
---|
| 666 | exefile = UioFiles(src_path, _config.FORTRAN_EXECUTABLE) |
---|
[54a8a01] | 667 | |
---|
| 668 | modfiles.delete_files() |
---|
| 669 | objfiles.delete_files() |
---|
| 670 | exefile.delete_files() |
---|
| 671 | |
---|
| 672 | return |
---|
| 673 | |
---|
[8652e7e] | 674 | def mk_convert_build(src_path, makefile): |
---|
[274f9ef] | 675 | '''Compiles the Fortran code and generates the executable. |
---|
| 676 | |
---|
| 677 | Parameters |
---|
| 678 | ---------- |
---|
[6f951ca] | 679 | src_path : str |
---|
[274f9ef] | 680 | Path to the fortran source directory. |
---|
[54a8a01] | 681 | |
---|
[6f951ca] | 682 | makefile : str |
---|
[274f9ef] | 683 | The name of the makefile which should be used. |
---|
[54a8a01] | 684 | |
---|
[274f9ef] | 685 | Return |
---|
| 686 | ------ |
---|
[54a8a01] | 687 | |
---|
| 688 | ''' |
---|
| 689 | |
---|
| 690 | try: |
---|
[2fb99de] | 691 | print('Using makefile: ' + makefile) |
---|
| 692 | p = subprocess.Popen(['make', '-f', |
---|
| 693 | os.path.join(src_path, makefile)], |
---|
[54a8a01] | 694 | stdin=subprocess.PIPE, |
---|
| 695 | stdout=subprocess.PIPE, |
---|
| 696 | stderr=subprocess.PIPE, |
---|
| 697 | bufsize=1) |
---|
| 698 | pout, perr = p.communicate() |
---|
[79729d5] | 699 | print(pout.decode()) |
---|
[54a8a01] | 700 | if p.returncode != 0: |
---|
[79729d5] | 701 | print(perr.decode()) |
---|
[2fb99de] | 702 | print('Please edit ' + makefile + |
---|
| 703 | ' or try another Makefile in the src directory.') |
---|
| 704 | print('Most likely GRIB_API_INCLUDE_DIR, GRIB_API_LIB ' |
---|
| 705 | 'and EMOSLIB must be adapted.') |
---|
| 706 | print('Available Makefiles:') |
---|
| 707 | print(UioFiles(src_path, 'Makefile*')) |
---|
[54a8a01] | 708 | sys.exit('Compilation failed!') |
---|
| 709 | except ValueError as e: |
---|
[2fb99de] | 710 | print('ERROR: Makefile call failed:') |
---|
| 711 | print(e) |
---|
[d69b677] | 712 | else: |
---|
[bf48c8a] | 713 | execute_subprocess(['ls', '-l', os.path.join(src_path, |
---|
| 714 | _config.FORTRAN_EXECUTABLE)], error_msg= |
---|
| 715 | 'FORTRAN EXECUTABLE COULD NOT BE FOUND!') |
---|
[a4b6cef] | 716 | |
---|
[d69b677] | 717 | return |
---|
| 718 | |
---|
[a4b6cef] | 719 | |
---|
[d69b677] | 720 | if __name__ == "__main__": |
---|
| 721 | main() |
---|