Ticket #12076: trac_12076.patch

File trac_12076.patch, 5.9 KB (added by mhansen, 10 years ago)
  • sage-location

    # HG changeset patch
    # User Mike Hansen <mhansen@gmail.com>
    # Date 1337973555 25200
    # Node ID 302afd0a122ad80f6baea3873e0313a24d80b2ee
    # Parent  1718c99356a2bc3a3aaf21374c21712bcfa34001
    #12076: sage-location doesn't deal with packages after initialize_pkgconfig_files has run
    
    diff --git a/sage-location b/sage-location
    a b  
    165165    occurrences of its current value by references to it (``${SAGE_ROOT}``).
    166166    This way we only have to change the definition of ``SAGE_ROOT`` whenever
    167167    the Sage installation has moved, i.e., paths have changed.
     168    """   
     169    SAGE_ROOT_absolute = os.path.abspath(SAGE_ROOT)
     170    LIB = os.path.join(SAGE_ROOT_absolute, 'local', 'lib')
     171    PKG = os.path.join(LIB, 'pkgconfig')   
     172    for name in os.listdir(PKG):
     173        filename = os.path.join(PKG, name)
     174        initialize_pkgconfig_file(filename)
     175       
     176def initialize_pkgconfig_file(filename):
     177    """
     178    Insert a ``SAGE_ROOT`` variable into ``filename`` and replace
     179    occurrences of its current value by references to it (``${SAGE_ROOT}``).
    168180    """
    169181    SAGE_ROOT_absolute = os.path.abspath(SAGE_ROOT)
    170     LIB = os.path.join(SAGE_ROOT_absolute, 'local', 'lib')
    171     PKG = os.path.join(LIB, 'pkgconfig')
    172182    import re
    173183    # Pattern matching a *definition* of SAGE_ROOT:
    174184    def_pat = re.compile(r"^SAGE_ROOT=.*\n", re.MULTILINE)
     
    179189    # knows...)
    180190    # use_pat = re.compile(SAGE_ROOT_absolute.replace(".", r"\."))
    181191
    182     for name in os.listdir(PKG):
    183         filename = os.path.join(PKG, name)
    184         if os.path.splitext(filename)[1]==".pc":
    185             with open(filename) as file:
    186                 config = file.read()
     192    if os.path.splitext(filename)[1]==".pc":
     193        with open(filename) as file:
     194            config = file.read()
    187195
    188             if def_pat.search(config):
    189                 # There are already one or more definitions of SAGE_ROOT,
    190                 # which also happens if we previously processed the same
    191                 # file (here) because of [symbolic or hard] links, so this
    192                 # isn't necessarily an error. (Cf. #11760).
    193                 # For simplicity, we just remove any definition, and only
    194                 # issue a warning if there was more than one, and don't
    195                 # check whether a previous definition was already current:
    196                 config, n = def_pat.subn("", config)
    197                 if n>1:
    198                     sys.stderr.write(
    199                         "Warning: sage_location: initialize_pkgconfig_files():\n" +
    200                         "  File \"%s\" already contained %d definitions of SAGE_ROOT.\n" %
    201                             (name, n) +
    202                         "  These will get replaced by a single, current definition.\n")
    203                     sys.stderr.flush()
     196        if def_pat.search(config):
     197            # There are already one or more definitions of SAGE_ROOT,
     198            # which also happens if we previously processed the same
     199            # file (here) because of [symbolic or hard] links, so this
     200            # isn't necessarily an error. (Cf. #11760).
     201            # For simplicity, we just remove any definition, and only
     202            # issue a warning if there was more than one, and don't
     203            # check whether a previous definition was already current:
     204            config, n = def_pat.subn("", config)
     205            if n>1:
     206                sys.stderr.write(
     207                    "Warning: sage_location: initialize_pkgconfig_files():\n" +
     208                    "  File \"%s\" already contained %d definitions of SAGE_ROOT.\n" %
     209                        (name, n) +
     210                    "  These will get replaced by a single, current definition.\n")
     211                sys.stderr.flush()
    204212
    205             # Replace all occurrences of the (current) value of SAGE_ROOT
    206             # by references to the pkg-config variable SAGE_ROOT:
    207             config = config.replace(SAGE_ROOT_absolute, "${SAGE_ROOT}")
    208             # Alternate implementation, *may* be faster:
    209             # config = use_pat.sub("${SAGE_ROOT}", config)
     213        # Replace all occurrences of the (current) value of SAGE_ROOT
     214        # by references to the pkg-config variable SAGE_ROOT:
     215        config = config.replace(SAGE_ROOT_absolute, "${SAGE_ROOT}")
     216        # Alternate implementation, *may* be faster:
     217        # config = use_pat.sub("${SAGE_ROOT}", config)
    210218
    211             definition = "SAGE_ROOT=%s\n" % SAGE_ROOT_absolute
    212             with open(filename, 'w') as file:
    213                 file.write(definition + config)
     219        definition = "SAGE_ROOT=%s\n" % SAGE_ROOT_absolute
     220        with open(filename, 'w') as file:
     221            file.write(definition + config)
    214222
    215223
    216224def update_pkgconfig_files():
     
    235243                config = file.read()
    236244
    237245            if not def_pat.search(config):
    238                 # There's no definition of SAGE_ROOT in the .pc file at all.
    239                 # This should never happen, unless the user modified the file.
    240                 sys.stderr.write(
    241                     "Error: sage_location: update_pkgconfig_files():\n" +
    242                     "  File \"%s\" doesn't contain a definition of SAGE_ROOT.\n" %
    243                     name + "  Skipping it...\n")
    244                 sys.stderr.flush()
    245                 continue
    246                 # We could of course call initialize_pkgconfig_file(filename)
    247                 # here instead to fix this issue, but unfortunately there's no
    248                 # such function (for a single file) [yet].
     246                # There's no definition of SAGE_ROOT in the .pc file
     247                # at all.  This should only happend if the user
     248                # modified the file or installed the package after
     249                # initialize_pkgconfig_files had been initially run.
     250                initialize_pkgconfig_file(filename)
    249251
    250252            # Delete all previous definitions of SAGE_ROOT:
    251253            config = def_pat.sub("", config)