github.com/singularityware/singularity@v3.1.1+incompatible/etc/configure_transform.py (about)

     1  #!/usr/bin/env python
     2  
     3  '''
     4  Copyright (c) 2017-2018, Sylabs, Inc. All rights reserved.
     5  
     6  Copyright (c) 2015-2017, Gregory M. Kurtzer. All rights reserved.
     7  Copyright (c) 2017, Vanessa Sochat All rights reserved.
     8  
     9  Copyright (c) 2016, The Regents of the University of California, through
    10  Lawrence Berkeley National Laboratory (subject to receipt of any required
    11  approvals from the U.S. Dept. of Energy).  All rights reserved.
    12  
    13  This software is licensed under a customized 3-clause BSD license.  Please
    14  consult LICENSE file distributed with the sources of this project regarding
    15  your rights to use or distribute this software.
    16  
    17  NOTICE.
    18  This Software was developed under funding from the U.S. Department of
    19  Energy and the U.S. Government consequently retains certain rights. As such,
    20  the U.S. Government has been granted for itself and others acting on its
    21  behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software
    22  to reproduce, distribute copies to the public, prepare derivative works, and
    23  perform publicly and display publicly, and to permit other to do so.
    24  '''
    25  
    26  import os
    27  import re
    28  import sys
    29  sys.path.append('../libexec/python')  # noqa
    30  
    31  from sutils import (
    32      get_fullpath,
    33      read_file,
    34      write_file
    35  )
    36  
    37  from message import bot
    38  import optparse
    39  
    40  
    41  def get_parser():
    42  
    43      description = "singularity configuration parsing helper in python"
    44      parser = optparse.OptionParser(description=description)
    45  
    46      # Configuration defaults header
    47      help = "configuration defaults header file (../src/lib/config_defaults.h)"
    48      parser.add_option("--defaults",
    49                        dest='defaults',
    50                        help=help,
    51                        type=str)
    52  
    53      # input configuration file
    54      help = "the configuration input file path (singularity.conf.in)"
    55      parser.add_option("--infile",
    56                        dest='infile',
    57                        help=help,
    58                        type=str)
    59  
    60      # Output configuration file
    61      help = "the configuration output file path (singularity.conf)"
    62      parser.add_option("--outfile",
    63                        dest='outfile',
    64                        help=help,
    65                        type=str)
    66  
    67      return parser
    68  
    69  
    70  def main():
    71      '''parse configuration options and produce
    72         configuration output file
    73      '''
    74      bot.info("\n*** STARTING PYTHON CONFIGURATION HELPER ****")
    75      parser = get_parser()
    76  
    77      try:
    78          (args, options) = parser.parse_args()
    79      except Exception:
    80          bot.error("Input args to %s improperly set, exiting."
    81                    % os.path.abspath(__file__))
    82          parser.print_help()
    83          sys.exit(1)
    84  
    85      # Check for required args
    86      [check_required(parser, arg) for arg in [args.defaults,
    87                                               args.infile,
    88                                               args.outfile]]
    89  
    90      # Run the configuration
    91      configure(args)
    92  
    93  
    94  def check_required(parser, arg):
    95      '''check_required arg checks that an argument is defined.
    96      It is a workaround for missing required parameter of argparse
    97      :param parser: the parser
    98      :param arg: the argument
    99      '''
   100      if not arg:   # if filename is not given
   101          parser.error('Missing required argument.')
   102          parser.print_help()
   103          sys.exit(1)
   104  
   105  
   106  def configure(args):
   107  
   108      # Get fullpath to each file, and concurrently check that exists
   109      defaultfile = get_fullpath(args.defaults)  # ../src/lib/config_defaults.h
   110      infile = get_fullpath(args.infile)         # singularity.conf.in
   111  
   112      # Find define statements
   113      define_re = re.compile("#define ([A-Z_]+) (.*)")
   114  
   115      # Read in input and default files
   116      defaultfile = read_file(defaultfile)
   117      data = "".join(read_file(infile))
   118  
   119      # Lookup for values we want replaced
   120      lookup = {'0': 'no',
   121                '1': 'yes'}
   122  
   123      defaults = {}
   124      # Read in defaults to dictionary
   125      for line in defaultfile:
   126          match = define_re.match(line)
   127          if match:
   128              key, value = match.groups()
   129  
   130              # Maintain the original default set by user
   131              defaults[key] = value
   132  
   133              # Use parsed value for final config
   134              new_value = value.replace('"', '')
   135              if new_value in lookup:
   136                  new_value = lookup[new_value]
   137              data = data.replace("@" + key + "@", new_value)
   138  
   139      # Write to output file
   140      outfile = "%s.tmp" % args.outfile
   141      write_file(outfile, data)
   142      os.rename(outfile, args.outfile)
   143  
   144      bot.info("*** FINISHED PYTHON CONFIGURATION HELPER ****\n")
   145  
   146  
   147  if __name__ == '__main__':
   148      main()