Ticket #11760: trac_11760-cumulative_diff_of_both_patches.diff

File trac_11760-cumulative_diff_of_both_patches.diff, 13.4 KB (added by leif, 10 years ago)

Cumulative diff of both patches against Sage 4.7.2.alpha3. For review only.

  • 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    """
    166     LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib')
    167     PKG = os.path.join(LIB,'pkgconfig')
     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    import re
     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
    168182    for name in os.listdir(PKG):
    169         filename=os.path.join(PKG,name)
     183        filename = os.path.join(PKG, name)
    170184        if os.path.splitext(filename)[1]==".pc":
    171185            with open(filename) as file:
    172186                config = file.read()
    173                
    174             new_config = config.replace(os.path.abspath(SAGE_ROOT), "${SAGE_ROOT}")
    175187
    176             new_config = 'SAGE_ROOT=%s\n'%os.path.abspath(SAGE_ROOT)+new_config
     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()
    177204
     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
    178212            with open(filename, 'w') as file:
    179                 file.write(new_config)
     213                file.write(definition + config)
    180214
    181    
    182215
    183216def update_pkgconfig_files():
    184217    """
    185     Change paths in package configuration files.
     218    Change / update paths in all ``pkg-config`` (``*.pc``) files pointing into
     219    the Sage installation tree, which is necessary whenever Sage has moved.
     220    Requires that the files have previously been processed by
     221    ``initialize_pkgconfig_files()``, in particular, each of them contains a
     222    definition of the variable ``SAGE_ROOT``, since only that is updated here.
    186223    """
    187     LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib')
    188     PKG = os.path.join(LIB,'pkgconfig')
     224    SAGE_ROOT_absolute = os.path.abspath(SAGE_ROOT)
     225    LIB = os.path.join(SAGE_ROOT_absolute, 'local', 'lib')
     226    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
    189231    for name in os.listdir(PKG):
    190         filename=os.path.join(PKG,name)
     232        filename = os.path.join(PKG, name)
    191233        if os.path.splitext(filename)[1]==".pc":
    192234            with open(filename) as file:
    193235                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:]
    199236
     237            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].
     249
     250            # Delete all previous definitions of SAGE_ROOT:
     251            config = def_pat.sub("", config)
     252
     253            definition = "SAGE_ROOT=%s\n" % SAGE_ROOT_absolute
    200254            with open(filename, 'w') as file:
    201                 file.write(new_config)
    202    
     255                file.write(definition + config)
     256
    203257
    204258def remove_files(path, remove_extensions):
    205259    """
    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')).
     260    Walk the tree starting at ``path``, and remove all files with
     261    extensions in ``remove_extensions``.
     262    The extensions in ``remove_extensions`` should start with a period, i.e.,
     263    e.g. use ``remove_files(path, ('.pyc', '.pyo'))``.
    210264    """
    211265    for root, dirs, files in os.walk(path):
    212266        for file in files:
    213             filename=os.path.join(root,file)
     267            filename = os.path.join(root, file)
    214268            if os.path.splitext(filename)[1] in remove_extensions:
    215269                try:
    216270                    os.unlink(filename)
    217271                except OSError, msg:
    218272                    print msg
    219            
     273
     274
    220275def __mysig(a,b):
    221     raise KeyboardInterrupt, "computation timed out because alarm was set for %s seconds"%__alarm_time
     276    raise KeyboardInterrupt, "Computation timed out because alarm was set for %s seconds." % __alarm_time
    222277
    223 if __name__ ==  '__main__':
    224    
     278
     279if __name__ == '__main__':
     280
    225281    check_processor_flags()
     282    # Note: install_moved() may also run e.g. initialize_pkgconfig_files().
    226283    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()
     284        print "The Sage installation tree may have moved"
     285        print "(from %s to %s)." % (OLD_SAGE_ROOT, SAGE_ROOT)
     286        print "Changing various hardcoded paths..."
     287        print "(Please wait at most a few minutes.)"
     288        print "DO NOT INTERRUPT THIS."
     289        sys.stdout.flush() # One never knows...
     290        update_library_files()
    233291        update_pkgconfig_files()
    234         # Compiled python files need to be regenerated, so we remove them
     292        # Compiled python files need to be regenerated, so we remove them:
    235293        remove_files(os.path.join(SAGE_ROOT, 'local', 'lib', 'python'),
    236294                     remove_extensions=('.pyc', '.pyo'))
    237         print "Done resetting paths"
     295        print "Done resetting paths."