Ticket #11760: trac_11760-additional_improvements_to_pkgconfig_funs.optional.scripts.patch

File trac_11760-additional_improvements_to_pkgconfig_funs.optional.scripts.patch, 5.5 KB (added by leif, 10 years ago)

SCRIPTS repo. Optional improvements to {initialize,update}_pkgconfig_files(). Apply on top of other patch..

  • sage-location

    # HG changeset patch
    # User Leif Leonhardy <not.really@online.de>
    # Date 1318200007 -7200
    # Node ID 1c9dbdf4b6f73f7f17e4af4e703f063235986b15
    # Parent  17d69430d27730cc35a55c90406136efd3faaa8f
    #11760: Further improvements to sage_location's {initialize,update}_pkgconfig_files().
    
    diff --git a/sage-location b/sage-location
    a b  
    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.
    168168    """
     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')
    169172    import re
    170     LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib')
    171     PKG = os.path.join(LIB, 'pkgconfig')
     173    # Pattern matching a *definition* of SAGE_ROOT:
     174    def_pat = re.compile(r"^SAGE_ROOT=.*\n", re.MULTILINE)
     175    # Pattern matching the current *value* of SAGE_ROOT (absolute path):
     176    # (For the alternate implementation below only.  Will have weird effects
     177    # in case SAGE_ROOT_absolute contains other characters having special
     178    # meaning in regular expressions.  IMHO rather unlikely, but one never
     179    # knows...)
     180    # use_pat = re.compile(SAGE_ROOT_absolute.replace(".", r"\."))
     181
    172182    for name in os.listdir(PKG):
    173183        filename = os.path.join(PKG, name)
    174184        if os.path.splitext(filename)[1]==".pc":
    175185            with open(filename) as file:
    176186                config = file.read()
    177187
    178             if re.search("^SAGE_ROOT=", config, re.MULTILINE):
    179                 # There's already a definition of SAGE_ROOT,
    180                 # so skip this file. (Cf. #11760).
    181                 continue
     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()
    182204
    183             new_config = config.replace(os.path.abspath(SAGE_ROOT), "${SAGE_ROOT}")
    184             new_config = 'SAGE_ROOT=%s\n' % os.path.abspath(SAGE_ROOT) + new_config
     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)
     210
     211            definition = "SAGE_ROOT=%s\n" % SAGE_ROOT_absolute
    185212            with open(filename, 'w') as file:
    186                 file.write(new_config)
     213                file.write(definition + config)
    187214
    188215
    189216def update_pkgconfig_files():
     
    194221    ``initialize_pkgconfig_files()``, in particular, each of them contains a
    195222    definition of the variable ``SAGE_ROOT``, since only that is updated here.
    196223    """
    197     LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib')
     224    SAGE_ROOT_absolute = os.path.abspath(SAGE_ROOT)
     225    LIB = os.path.join(SAGE_ROOT_absolute, 'local', 'lib')
    198226    PKG = os.path.join(LIB, 'pkgconfig')
     227    import re
     228    # Pattern matching a *definition* of SAGE_ROOT:
     229    def_pat = re.compile(r"^SAGE_ROOT=.*\n", re.MULTILINE)
     230
    199231    for name in os.listdir(PKG):
    200         filename = os.path.join(PKG,name)
     232        filename = os.path.join(PKG, name)
    201233        if os.path.splitext(filename)[1]==".pc":
    202234            with open(filename) as file:
    203235                config = file.read()
    204236
    205             prefix_start = config.find('SAGE_ROOT=')
    206             if prefix_start==-1:
     237            if not def_pat.search(config):
     238                # There's no definition of SAGE_ROOT in the .pc file at all.
    207239                # This should never happen, unless the user modified the file.
    208240                sys.stderr.write(
    209241                    "Error: sage_location: update_pkgconfig_files():\n" +
     
    211243                    name + "  Skipping it...\n")
    212244                sys.stderr.flush()
    213245                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].
    214249
    215             prefix_end = config.find('\n', prefix_start)
    216             new_prefix = 'SAGE_ROOT=%s' % os.path.abspath(SAGE_ROOT)
    217             new_config = config[:prefix_start] + new_prefix + config[prefix_end:]
     250            # Delete all previous definitions of SAGE_ROOT:
     251            config = def_pat.sub("", config)
     252
     253            definition = "SAGE_ROOT=%s\n" % SAGE_ROOT_absolute
    218254            with open(filename, 'w') as file:
    219                 file.write(new_config)
     255                file.write(definition + config)
    220256
    221257
    222258def remove_files(path, remove_extensions):