Ticket #11760: trac_11760-avoid_multiple_defs_of_SAGE_ROOT.scripts.patch

File trac_11760-avoid_multiple_defs_of_SAGE_ROOT.scripts.patch, 11.5 KB (added by leif, 5 years ago)

SCRIPTS repo. Based on Sage 4.7.2.alpha3.

  • sage-location

    # HG changeset patch
    # User Leif Leonhardy <not.really@online.de>
    # Date 1318168354 -7200
    # Node ID 17d69430d27730cc35a55c90406136efd3faaa8f
    # Parent  60153e03ae7c5946ac3539d248ce1e3002edf1b0
    #11760: Avoid multiple definitions of SAGE_ROOT in pkg-config (.pc) files. Also cosmetic changes to sage-location.
    
    diff --git a/sage-location b/sage-location
    a b  
    88location_file = os.path.join(SAGE_ROOT, 'local', 'lib', 'sage-current-location.txt')
    99flags_file    = os.path.join(SAGE_ROOT, 'local', 'lib', 'sage-flags.txt')
    1010
    11 # The flags we care about recording in the local/lib/sage-flags.txt file
     11
     12# The flags we care about recording in the local/lib/sage-flags.txt file.
    1213# In SAGE_FAT_BINARY mode we only require that ['sse', 'sse2', '3d',
    13 #  'mmx', 'cmov'] be available, and in particular, don't require pni
    14 # or ssse3.
     14# 'mmx', 'cmov'] be available, and in particular, we don't require pni
     15# (Prescott New Instructions, including SSE3) or ssse3.
    1516
    16 try:
    17     SAGE_FAT_BINARY = os.environ['SAGE_FAT_BINARY']
    18 except:
    19     SAGE_FAT_BINARY = ""
     17SAGE_FAT_BINARY = os.environ.get('SAGE_FAT_BINARY',"")
    2018
    2119if SAGE_FAT_BINARY == "yes":
    2220    FLAGS = ['sse', 'sse2', '3d', 'mmx', 'cmov']
     
    2624
    2725def write_location_file():
    2826    """
    29     Write the location file with the current SAGE_ROOT
     27    Write the location file with the current value of ``SAGE_ROOT``.
    3028    """
    3129    # Write new location file.
    3230    O = open(location_file,'w')
     
    3533
    3634def read_location_file():
    3735    """
    38     If the location file exists, return the real path contained in it.  Otherwise, return None.
     36    If the location file exists, return the real path contained in it.
     37    Otherwise return ``None``.
    3938    """
    4039    if os.path.exists(location_file):
    41         # The install moved and we had written the flags and location files. 
     40        # The install moved and we had written the flags and location files.
    4241        O = open(location_file)
    4342        R = O.read().strip()
    4443        O.close()
     
    4645    else:
    4746        return None
    4847
     48
    4949def install_moved():
    5050    """
    51     Check whether or not this install of Sage moved.  If it hasn't
    52     moved make sure the location and processor flags files are
    53     written.
     51    Check whether or not this installation of Sage has moved.  If it hasn't
     52    moved, make sure the location and processor flags files are written.
    5453
    55     Returns True if location possibly moved, and sets the global
    56     OLD_SAGE_ROOT variable to the old SAGE_ROOT path.
     54    Return ``True`` if location possibly changed, and set the global
     55    ``OLD_SAGE_ROOT`` variable to the old ``SAGE_ROOT`` path.
    5756
    58     Returns False if location has not moved.
     57    Return ``False`` if the Sage installation hasn't moved.
    5958    """
    60     # Write the flags file if it isn't there.
     59    # Write the flags file if it isn't there:
    6160    if not os.path.exists(flags_file):
    6261        f=open(flags_file,'w')
    6362        f.write(get_flags_info())
     
    7372    elif path != SAGE_ROOT:
    7473        # location moved
    7574        global OLD_SAGE_ROOT
    76         OLD_SAGE_ROOT=path
     75        OLD_SAGE_ROOT = path
    7776        write_location_file()
    7877        return True
    7978    else:
    8079        # path didn't change
    8180        return False
    8281
     82
    8383def get_flags_info():
    8484    """
    85     Return a space-separated string that lists the flags supported by
    86     this CPU from /proc/cpuinfo.
     85    Return a space-separated string that lists the flags indicating what's
     86    supported by this CPU by reading ``/proc/cpuinfo``.
    8787    """
    8888    try:
    8989        r = open('/proc/cpuinfo').read()
     
    9999        return ' '.join(set(x for x in r.split() if x in FLAGS))
    100100
    101101    except IOError:
    102         # On a system without /proc/cpuinfo, so don't bother. In
    103         # particular, for non Linux systems I have no clue how to get
     102        # On a system without /proc/cpuinfo, so don't bother.  In
     103        # particular, for non-Linux systems I have no clue how to get
    104104        # the processor flags, and we so far have never ever had any
    105105        # problem with processor flags on such machines.  So we don't
    106106        # bother.
    107107        return ''
    108108
     109
    109110def check_processor_flags():
    110111    """
    111112    Make sure all processor flags from the build machine are on this machine.
    112     If the sage-flags.txt file is missing, don't bother to do this check.
     113    If the ``sage-flags.txt`` file is missing, don't bother to do this check.
    113114    """
    114115    if not os.path.exists(flags_file): return
    115116    # We check that the processor flags of the original build are a
     
    134135        print "*"*70
    135136        sys.exit(1)
    136137
     138
    137139def update_library_files():
    138140    """
    139     Run ranlib on the library directory and manually change the path
    140     in .la library files.
     141    Run ``ranlib`` on all static libraries (``*.a``) in the library directory,
     142    and manually change the paths in all of ``libtool``'s ``.la`` library files.
    141143    """
    142144    LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib')
    143     # The .a files should be re-ranlib'd
    144     os.system('cd "%s"; ranlib *.a 1>/dev/null 2>/dev/null'%LIB)
     145    # The .a files should be re-ranlib'd:
     146    os.system('cd "%s"; ranlib *.a 1>/dev/null 2>/dev/null' % LIB)
    145147
    146     # The .la files hardcode path info, so we manually fix the path
    147     # info
     148    # The .la files hardcode path info, so we manually fix the path info:
    148149    for F in os.listdir(LIB):
    149150       if os.path.splitext(F)[1]==".la":
    150151           G = open(os.path.join(LIB,F)).read()
    151            i = G.find('libdir=')
    152            j = i+8 + G[i+8:].find("'")
    153            z = G[i+8:j].strip().strip("'")
    154            i = z.rfind('local/')
    155            if i != -1:
     152           i = G.find('libdir=')
     153           j = i+8 + G[i+8:].find("'")
     154           z = G[i+8:j].strip().strip("'")
     155           i = z.rfind('local/')
     156           if i != -1:
    156157               z = z[:i]
    157158               H = G.replace(z, os.path.abspath(SAGE_ROOT) + '/')
    158159               open(os.path.join(LIB, F),'w').write(H)
     
    160161
    161162def initialize_pkgconfig_files():
    162163    """
    163     Insert a sage_local variable in each pkg_config file and replace
    164     them to make the paths portable.
     164    Insert a ``SAGE_ROOT`` variable into each ``pkg-config`` file and replace
     165    occurrences of its current value by references to it (``${SAGE_ROOT}``).
     166    This way we only have to change the definition of ``SAGE_ROOT`` whenever
     167    the Sage installation has moved, i.e., paths have changed.
    165168    """
     169    import re
    166170    LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib')
    167     PKG = os.path.join(LIB,'pkgconfig')
     171    PKG = os.path.join(LIB, 'pkgconfig')
    168172    for name in os.listdir(PKG):
    169         filename=os.path.join(PKG,name)
     173        filename = os.path.join(PKG, name)
    170174        if os.path.splitext(filename)[1]==".pc":
    171175            with open(filename) as file:
    172176                config = file.read()
    173                
     177
     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
     182
    174183            new_config = config.replace(os.path.abspath(SAGE_ROOT), "${SAGE_ROOT}")
    175 
    176             new_config = 'SAGE_ROOT=%s\n'%os.path.abspath(SAGE_ROOT)+new_config
    177 
     184            new_config = 'SAGE_ROOT=%s\n' % os.path.abspath(SAGE_ROOT) + new_config
    178185            with open(filename, 'w') as file:
    179186                file.write(new_config)
    180187
    181    
    182188
    183189def update_pkgconfig_files():
    184190    """
    185     Change paths in package configuration files.
     191    Change / update paths in all ``pkg-config`` (``*.pc``) files pointing into
     192    the Sage installation tree, which is necessary whenever Sage has moved.
     193    Requires that the files have previously been processed by
     194    ``initialize_pkgconfig_files()``, in particular, each of them contains a
     195    definition of the variable ``SAGE_ROOT``, since only that is updated here.
    186196    """
    187197    LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib')
    188     PKG = os.path.join(LIB,'pkgconfig')
     198    PKG = os.path.join(LIB, 'pkgconfig')
    189199    for name in os.listdir(PKG):
    190         filename=os.path.join(PKG,name)
     200        filename = os.path.join(PKG,name)
    191201        if os.path.splitext(filename)[1]==".pc":
    192202            with open(filename) as file:
    193203                config = file.read()
    194                
    195             prefix_start=config.find('SAGE_ROOT=')
    196             prefix_end=config.find('\n', prefix_start)
    197             new_prefix='SAGE_ROOT=%s'%os.path.abspath(SAGE_ROOT)
    198             new_config=config[:prefix_start]+new_prefix+config[prefix_end:]
    199204
     205            prefix_start = config.find('SAGE_ROOT=')
     206            if prefix_start==-1:
     207                # This should never happen, unless the user modified the file.
     208                sys.stderr.write(
     209                    "Error: sage_location: update_pkgconfig_files():\n" +
     210                    "  File \"%s\" doesn't contain a definition of SAGE_ROOT.\n" %
     211                    name + "  Skipping it...\n")
     212                sys.stderr.flush()
     213                continue
     214
     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:]
    200218            with open(filename, 'w') as file:
    201219                file.write(new_config)
    202    
     220
    203221
    204222def remove_files(path, remove_extensions):
    205223    """
    206     Walk the tree starting at path and remove all files with
    207     extensions in remove_ext.  The extensions in remove_extensions
    208     should start with a period, i.e., remove_files(path, ('.pyc',
    209     '.pyo')).
     224    Walk the tree starting at ``path``, and remove all files with
     225    extensions in ``remove_extensions``.
     226    The extensions in ``remove_extensions`` should start with a period, i.e.,
     227    e.g. use ``remove_files(path, ('.pyc', '.pyo'))``.
    210228    """
    211229    for root, dirs, files in os.walk(path):
    212230        for file in files:
    213             filename=os.path.join(root,file)
     231            filename = os.path.join(root, file)
    214232            if os.path.splitext(filename)[1] in remove_extensions:
    215233                try:
    216234                    os.unlink(filename)
    217235                except OSError, msg:
    218236                    print msg
    219            
     237
     238
    220239def __mysig(a,b):
    221     raise KeyboardInterrupt, "computation timed out because alarm was set for %s seconds"%__alarm_time
     240    raise KeyboardInterrupt, "Computation timed out because alarm was set for %s seconds." % __alarm_time
    222241
    223 if __name__ ==  '__main__':
    224    
     242
     243if __name__ == '__main__':
     244
    225245    check_processor_flags()
     246    # Note: install_moved() may also run e.g. initialize_pkgconfig_files().
    226247    if install_moved():
    227         print "The Sage install tree may have moved"
    228         print "(from %s to %s)"%(OLD_SAGE_ROOT, SAGE_ROOT)
    229         print "Changing various hardcoded paths"
    230         print "(please wait at most a few minutes)..."
    231         print "Do not interrupt this."
    232         update_library_files()
     248        print "The Sage installation tree may have moved"
     249        print "(from %s to %s)." % (OLD_SAGE_ROOT, SAGE_ROOT)
     250        print "Changing various hardcoded paths..."
     251        print "(Please wait at most a few minutes.)"
     252        print "DO NOT INTERRUPT THIS."
     253        sys.stdout.flush() # One never knows...
     254        update_library_files()
    233255        update_pkgconfig_files()
    234         # Compiled python files need to be regenerated, so we remove them
     256        # Compiled python files need to be regenerated, so we remove them:
    235257        remove_files(os.path.join(SAGE_ROOT, 'local', 'lib', 'python'),
    236258                     remove_extensions=('.pyc', '.pyo'))
    237         print "Done resetting paths"
     259        print "Done resetting paths."