Changeset 47be2684 in flex_extract.git for Source


Ignore:
Timestamp:
Oct 28, 2020, 10:28:24 AM (4 years ago)
Author:
Leopold Haimberger <leopold.haimberger@…>
Branches:
ctbto, dev
Children:
75db9b0
Parents:
697b8d0
Message:

Adaptations to allow for a system installation with separate user and system path. Updated documentation

Location:
Source/Python
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • Source/Python/Classes/ControlFile.py

    r0a75335 r47be2684  
    404404        self.exedir = _config.PATH_FORTRAN_SRC
    405405        self.installdir = None
     406        self.sysinstalldir = None
    406407        self.makefile = None
    407408        self.destination = None
  • Source/Python/Mods/tools.py

    rf61e1df r47be2684  
    249249                        type=none_or_str, default=None,
    250250                        help='The name of the ECMWF server name where the'
    251                         'job script is to be submitted ' 
     251                        'job script is to be submitted '
    252252                        '(e.g. ecgate | cca | ccb)')
    253253
     
    624624    '''Creates a directory.
    625625
    626     If the directory already exists, an information is printed and the creation 
     626    If the directory already exists, an information is printed and the creation
    627627    skipped. The program stops only if there is another problem.
    628628
     
    856856    ----------
    857857    cmd_list : list of str
    858         A list of the components for the command line execution. 
    859         They will be concatenated with blank space for the command 
     858        A list of the components for the command line execution.
     859        They will be concatenated with blank space for the command
    860860        to be submitted, like ['mv', file1, file2] for mv file1 file2.
    861861
     
    910910
    911911    return start_period, end_period
     912
     913
     914def check_for_string_in_file(filepath, search_string):
     915    """
     916    Search for a specific string in a file and return True if
     917    the string was found.
     918
     919    Parameters
     920    ----------
     921    filepath : str
     922        The full file path which is to be examined.
     923
     924    search_string : str
     925        The string which is looked up for in the file.
     926
     927    Return
     928    ------
     929    Boolean :
     930        True : String was found
     931        False : String was not found
     932    """
     933    with open(filepath, 'r') as fio:
     934        for line in fio:
     935            if search_string in line:
     936                return True
     937    return False
     938
     939
     940def overwrite_lines_in_file(filepath, search_string, sub_string):
     941    """
     942    Overwrites lines which contain the given search string with the
     943    substitution string.
     944
     945    Parameters
     946    ----------
     947    search_string : str
     948        The string which is looked up for in the file.
     949
     950    sub_string : str
     951        The string which overwrites the search string.
     952
     953    Return
     954    ------
     955    """
     956    with open(filepath, 'r') as fio:
     957        data = fio.readlines()
     958
     959    with open(filepath, 'w') as fio:
     960        for line in data:
     961            if search_string in line:
     962                fio.write(sub_string)
     963            else:
     964                fio.write(line)
     965
     966    return
     967
  • Source/Python/_config.py

    r0a75335 r47be2684  
    3838# ------------------------------------------------------------------------------
    3939
    40 _VERSION_STR = '7.1.2'
     40_VERSION_STR = '7.1.2_ctbto'
    4141
    4242FLAG_ON_ECMWFSERVER = 'ecgb' in platform.node()
     
    4444QUEUES_LIST = ['ecgate', 'cca', 'ccb']
    4545
    46 INSTALL_TARGETS = ['local', 'ecgate', 'cca', 'ccb']
     46INSTALL_TARGETS = ['local', 'syslocal', 'ecgate', 'cca', 'ccb']
    4747
    4848CDS_DATASET_ML = 'reanalysis-era5-complete'
     
    7070FILE_GRIB_INDEX = 'date_time_stepRange.idx'
    7171FILE_GRIBTABLE = 'ecmwf_grib1_table_128'
     72FILE_SYS_CONFIG = '.setup.rc'
    7273
    7374# ------------------------------------------------------------------------------
     
    7980
    8081# ------------------------------------------------------------------------------
    81 PATHES
     82LOAD ENVIRONMENT VARIABLES FOR SYS VERSION; IF NECESSARRY
    8283# ------------------------------------------------------------------------------
    8384
     
    8990if PATH_LOCAL_PYTHON not in sys.path:
    9091    sys.path.append(PATH_LOCAL_PYTHON)
     92
     93# ------------------------------------------------------------------------------
     94#  PATHES
     95# ------------------------------------------------------------------------------
     96
    9197PATH_FLEXEXTRACT_DIR = os.path.normpath(os.path.dirname(os.path.abspath(
    9298    inspect.getfile(inspect.currentframe()))) + '/../../')
     99if not os.path.isdir(os.path.join(PATH_FLEXEXTRACT_DIR,'Run')):
     100    # if it does not exist, we have a system installation in place
     101    # we need to have a sys and user path
     102    # configure correct system path
     103    PATH_SYSTEM_DIR = os.path.join(PATH_FLEXEXTRACT_DIR, FLEXEXTRACT_DIRNAME)
     104    # configure correct user path
     105    PATH_FLEXEXTRACT_DIR = os.environ.get('FLEXEXTRACT_USER_DIR')
     106else:
     107    PATH_SYSTEM_DIR = PATH_FLEXEXTRACT_DIR
     108
    93109PATH_RUN_DIR = os.path.join(PATH_FLEXEXTRACT_DIR, 'Run')
    94 PATH_SOURCES = os.path.join(PATH_FLEXEXTRACT_DIR, 'Source')
     110PATH_SOURCES = os.path.join(PATH_SYSTEM_DIR, 'Source')
    95111PATH_TEMPLATES = os.path.join(PATH_FLEXEXTRACT_DIR, 'Templates')
    96112PATH_ECMWF_ENV = os.path.join(PATH_RUN_DIR, FILE_USER_ENVVARS)
    97113PATH_GRIBTABLE = os.path.join(PATH_TEMPLATES, FILE_GRIBTABLE)
    98114PATH_JOBSCRIPTS = os.path.join(PATH_RUN_DIR, 'Jobscripts')
    99 PATH_FORTRAN_SRC = os.path.join(PATH_SOURCES, 'Fortran')
     115if os.path.isdir(os.path.join(PATH_SYSTEM_DIR,'Fortran')):
     116    PATH_FORTRAN_SRC = PATH_SYSTEM_DIR
     117else:
     118    PATH_FORTRAN_SRC = os.path.join(PATH_SOURCES, 'Fortran')
    100119PATH_PYTHONTEST_SRC = os.path.join(PATH_SOURCES, 'Pythontest')
    101120PATH_INPUT_DIR = os.path.join(PATH_RUN_DIR, INPUT_DIRNAME_DEFAULT)
  • Source/Python/install.py

    r8028176 r47be2684  
    1919#    June 2020 - Anne Philipp
    2020#        - renamed "convert" functions to "fortran" functions
    21 #        - reconfigured mk_tarball to select *.template files instead 
     21#        - reconfigured mk_tarball to select *.template files instead
    2222#          of *.nl and *.temp
    2323#        - added check for makefile settings
     
    7575import subprocess
    7676import tarfile
     77import shutil
    7778from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    7879
     
    8283from Classes.UioFiles import UioFiles
    8384from Mods.tools import (make_dir, put_file_to_ecserver, submit_job_to_ecserver,
    84                         silent_remove, execute_subprocess, none_or_str)
     85                        silent_remove, execute_subprocess, none_or_str,
     86                        overwrite_lines_in_file, check_for_string_in_file)
    8587
    8688# ------------------------------------------------------------------------------
     
    103105    check_install_conditions(c)
    104106
    105     if c.install_target.lower() != 'local': # ecgate or cca
     107    if c.install_target.lower() not in ['local', 'syslocal']: # ecgate or cca
    106108        install_via_gateway(c)
    107109    else: # local
    108110        install_local(c)
    109111
     112    print("SUCCESS: INSTALLATION FINISHED!")
     113
    110114    return
    111115
     
    128132    parser.add_argument('--target', dest='install_target',
    129133                        type=none_or_str, default=None,
    130                         help="Valid targets: local | ecgate | cca , \
     134                        help="Valid targets: syslocal | local | ecgate | cca , \
    131135                        the latter two are at ECMWF")
    132136    parser.add_argument("--makefile", dest="makefile",
     
    150154    parser.add_argument("--installdir", dest="installdir",
    151155                        type=none_or_str, default=None,
    152                         help='Root directory of the '
     156                        help='Root (user) directory of the '
    153157                        'flex_extract installation')
     158    parser.add_argument("--sysinstalldir", dest="sysinstalldir",
     159                        type=none_or_str, default=None,
     160                        help='System installation path; where '
     161                        'executables are stored.')
    154162
    155163    # arguments for job submission to ECMWF, only needed by submit.py
     
    228236                            _config.FLEXEXTRACT_DIRNAME + '.tar')
    229237
    230     if c.installdir == _config.PATH_FLEXEXTRACT_DIR:
    231         print('WARNING: installdir has not been specified')
    232         print('flex_extract will be installed in here by compiling the ' +
    233               'Fortran source in ' + _config.PATH_FORTRAN_SRC)
    234         os.chdir(_config.PATH_FORTRAN_SRC)
    235     else: # creates the target working directory for flex_extract
    236         c.installdir = os.path.expandvars(os.path.expanduser(
    237             c.installdir))
    238         if os.path.abspath(_config.PATH_FLEXEXTRACT_DIR) != \
    239            os.path.abspath(c.installdir):
     238    c.installdir = os.path.abspath(os.path.expandvars(os.path.expanduser(
     239                c.installdir)))
     240    c.sysinstalldir = os.path.abspath(os.path.expandvars(os.path.expanduser(
     241        c.sysinstalldir)))
     242
     243    # this is standard installation into a single directory
     244    if c.install_target == 'local':
     245
     246        # installation into the current directory
     247        if os.path.abspath(_config.PATH_FLEXEXTRACT_DIR) == c.installdir:
     248            print('WARNING: installdir has not been specified')
     249            print('flex_extract will be installed in current dir by compiling the ' +
     250                  'Fortran source in ' + _config.PATH_FORTRAN_SRC)
     251            os.chdir(_config.PATH_FORTRAN_SRC)
     252        # installation into a different path
     253        elif os.path.abspath(_config.PATH_FLEXEXTRACT_DIR) != c.installdir :
     254
     255            # creates the target working directory for flex_extract
    240256            mk_tarball(tar_file, c.install_target)
    241257            make_dir(os.path.join(c.installdir,
     
    248264                                  _config.PATH_REL_FORTRAN_SRC))
    249265
    250     # Create Fortran executable
    251     print('Install ' +  _config.FLEXEXTRACT_DIRNAME + ' software at ' +
    252           c.install_target + ' in directory ' +
    253           os.path.abspath(c.installdir) + '\n')
    254 
    255     del_fortran_build('.')
    256     mk_fortran_build('.', c.makefile)
     266        # Create Fortran executable
     267        print('Install ' +  _config.FLEXEXTRACT_DIRNAME + ' software at ' +
     268              c.install_target + ' in directory ' + c.installdir + '\n')
     269
     270        del_fortran_build('.')
     271        mk_fortran_build('.', c.makefile)
     272        os.chdir('../../')
     273        # make sure that the correct calling of submit.py script is in run_local.sh
     274        overwrite_lines_in_file('Run/run_local.sh',
     275                                'pyscript=', 'pyscript=../Source/Python/submit.py\n')
     276
     277    # this is system installation were executables and user files are separated
     278    elif c.install_target == 'syslocal':
     279        if os.path.abspath(_config.PATH_FLEXEXTRACT_DIR) == c.sysinstalldir :
     280            sys.exit('ERROR: System installation path is equal to user '
     281                     'installation path.\n Please change either the system '
     282                     'installation path or use installation target "local".')
     283        if os.path.abspath(_config.PATH_FLEXEXTRACT_DIR) == c.installdir :
     284            print('Flex_extract will be installed in current directory!')
     285        else: # install user part in different dir
     286            print('Flex_extract will be installed in ' + c.installdir )
     287
     288            c.installdir = os.path.join(c.installdir,_config.FLEXEXTRACT_DIRNAME)
     289            if os.path.isdir(c.installdir):
     290                shutil.rmtree(c.installdir)
     291
     292            # copy all files except Python and Fortranfiles to this dir
     293            shutil.copytree(_config.PATH_FLEXEXTRACT_DIR,
     294                            c.installdir, symlinks=True)
     295            shutil.rmtree(os.path.join(c.installdir,'Source'))
     296            shutil.rmtree(os.path.join(c.installdir,'.git'))
     297            for x in UioFiles(c.installdir, '*~').files:
     298                silent_remove(x)
     299
     300            os.remove(os.path.join(c.installdir,'setup.sh'))
     301            os.remove(os.path.join(c.installdir,'setup_local.sh'))
     302
     303        # configure run_local script correctly
     304        # check if source of system config file is already in run_local.sh,
     305        # if not, add it
     306        if not check_for_string_in_file(os.path.join(c.installdir,'Run/run_local.sh'),
     307                                 'source .setup.rc'):
     308            overwrite_lines_in_file(os.path.join(c.installdir,'Run/run_local.sh'),
     309                                    '# PATH TO SUBMISSION SCRIPT',
     310                                    '# PATH TO SUBMISSION SCRIPT\nsource '+_config.FILE_SYS_CONFIG+'\n')
     311        # make sure that the correct calling of submit.py script is in run_local.sh
     312        overwrite_lines_in_file(os.path.join(c.installdir,'Run/run_local.sh'),
     313                                'pyscript=', 'pyscript=submit.py\n')
     314
     315        # change permission for file to executable
     316        execute_subprocess(['chmod', '0775',
     317                            os.path.join(os.path.abspath(c.installdir),'Run/run_local.sh')])
     318
     319
     320        # create systemdir
     321        c.sysinstalldir = os.path.join(c.sysinstalldir,_config.FLEXEXTRACT_DIRNAME)
     322        if os.path.isdir(c.sysinstalldir):
     323            shutil.rmtree(c.sysinstalldir)
     324
     325        # create setup file for running flex_extract with system installation
     326        with open(os.path.join(os.path.abspath(c.installdir),'Run/.setup.rc'),'w') as fio:
     327            fio.write('#!/bin/bash \n')
     328            fio.write('export FLEXEXTRACT_USER_DIR='+os.path.abspath(c.installdir)+'\n')
     329            fio.write('export PATH='+os.path.abspath(c.sysinstalldir)+'/Python:${PATH}\n')
     330            fio.write('export PATH='+os.path.abspath(c.sysinstalldir)+':${PATH}\n')
     331
     332        # copy all Python and Fortranfiles to this dir
     333        shutil.copytree(_config.PATH_SOURCES, c.sysinstalldir, symlinks=True)
     334
     335        os.chdir(os.path.join(c.sysinstalldir,'Fortran'))
     336        # Create Fortran executable
     337        print('Install ' +  _config.FLEXEXTRACT_DIRNAME + ' software as ' +
     338              c.install_target + ' in directory ' +
     339              os.path.abspath(c.sysinstalldir) + '\n')
     340
     341        del_fortran_build('.')
     342        mk_fortran_build('.', c.makefile)
     343
     344        outfile = [x for x in UioFiles('.','*.out').files]
     345        test=os.path.join(c.sysinstalldir,'calc_etadot')
     346        if len(outfile) != 1:
     347            print('WARNING: Multiple executables for Fortran code are available!')
     348        # move executable one dir up and delete Fortran dir
     349        os.chdir('..')
     350        shutil.move(outfile[0], os.path.join(c.sysinstalldir,'calc_etadot'))
     351        shutil.rmtree(os.path.join(os.path.abspath(c.sysinstalldir),'Fortran'))
    257352
    258353    os.chdir(_config.PATH_FLEXEXTRACT_DIR)
     
    289384        sys.exit(1)
    290385
    291     if c.install_target and c.install_target != 'local':
     386    if c.install_target and c.install_target not in ['local', 'syslocal']:
    292387        if not c.ecgid or not c.ecuid:
    293388            print('Please enter your ECMWF user id and group id '
     
    305400        if not c.installdir:
    306401            c.installdir = '${HOME}'
    307     else: # local
     402    elif c.install_target == 'local':
    308403        if not c.installdir:
    309404            c.installdir = _config.PATH_FLEXEXTRACT_DIR
     405    elif c.install_target == 'syslocal':
     406        if not c.installdir:
     407            c.installdir = _config.PATH_FLEXEXTRACT_DIR
     408        if not c.sysinstalldir:
     409            print('ERROR: System installation was selected but '
     410                  'no system installation path was defined.')
     411            sys.exit()
    310412
    311413    if not c.makefile:
     
    323425        else:
    324426            pass
    325        
     427
    326428    return
    327429
     
    715817        print(e)
    716818    else:
    717         execute_subprocess(['ls', '-l', 
     819        execute_subprocess(['ls', '-l',
    718820                            os.path.join(src_path, _config.FORTRAN_EXECUTABLE)],
    719821                           error_msg='FORTRAN EXECUTABLE COULD NOT BE FOUND!')
  • Source/Python/submit.py

    ra916e8f r47be2684  
    114114    else:
    115115        submit(job_template, c, queue)
    116         exit_message = 'FLEX_EXTRACT JOB SCRIPT IS SUBMITED!'
     116        exit_message = 'FLEX_EXTRACT JOB SCRIPT IS SUBMITTED!'
    117117
    118118    normal_exit(exit_message)
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG