Ticket #12530: trac_12530-sage_combinat_script-guards-nt.patch

File trac_12530-sage_combinat_script-guards-nt.patch, 8.7 KB (added by nthiery, 9 years ago)
  • sage-combinat

    # HG changeset patch
    # User Nicolas M. Thiery <nthiery@users.sf.net>
    # Date 1329503343 -3600
    # Node ID 7a8a21d105ac87e6c32bc597d10f1dc518866a34
    # Parent  44a4edfb0f0d997d9615aee92a5f204fb8a6bb4e
    #12530: Improve the sage-combinat script to support guards for developpers versions
    
    diff --git a/sage-combinat b/sage-combinat
    a b  
    22
    33import os
    44import re
    5 from re import split
    6 from sys import argv, stderr
    7 from subprocess import *
    85from optparse import OptionParser
    9 import ConfigParser
     6from sys import stderr
     7from subprocess import Popen, PIPE
     8from warnings import warn
    109
    1110if "SAGE_ROOT" in os.environ:
    1211    sage = os.environ["SAGE_ROOT"]+"/sage"
    def get_sage_root(): 
    8887        sage_root = Popen(["echo 'echo ROOT${SAGE_ROOT}ROOT' | "+sage+" -sh"], stdout=PIPE, shell=True).communicate()[0]
    8988    except OSError, e:
    9089        error("Could not start sage"+str(e))
    91     return split("ROOT", sage_root)[1]
     90    return re.split("ROOT", sage_root)[1]
     91
     92"""
     93A regexp matching Sage version strings
     94
     95EXAMPLES::
     96
     97    sage: re.search(version_regexp, "Sage Version 5.0").group()
     98    '5.0'
     99    sage: re.search(version_regexp, "Sage Version 5.4.2").group()
     100    '5.4.2'
     101    sage: re.search(version_regexp, "Sage Version 5.3.1.alpha3").group()
     102    '5.3.1.alpha3'
     103    sage: re.search(version_regexp, "Sage Version 5.3.1.alpha3 blah blah").group()
     104    '5.3.1.alpha3'
     105    sage: re.search(version_regexp, "Sage Version 5.3.beta2 blah blah").group()
     106    '5.3.beta2'
     107    sage: re.search(version_regexp, "Sage Version 5.rc0 blah blah").group()
     108    '5.rc0'
     109
     110    sage: re.match(version_regexp, "Sage Version 5.rc0 blah blah")
     111    sage: re.match(version_regexp, "5.rc0 blah").group()
     112    '5.rc0'
     113    sage: re.match(version_regexp+"$", "4_3_3:")
     114    sage: re.match(version_regexp, "4_3_3:")
     115    '4_3_3'
     116
     117"""
     118version_regexp = '(\d+(\.|_))*(\d+)((\.|_)(alpha|beta|rc)(\d+))?'
    92119
    93120def get_sage_version():
    94121    global sage
    def get_sage_version(): 
    98125    except OSError, e:
    99126        error("Could not start sage"+str(e))
    100127
    101     match = re.search('(\d+\.)*(\d+)',sage_version)
     128    match = re.search(version_regexp, sage_version)
    102129    if match is None:
    103130        error("Cannot determine Sage version number from"+sage_version)
    104131    else:
    105132        version = match.group()
    106133    return version
    107134
     135def encode_sage_version_for_comparison(version):
     136    """
     137    Encodes a version of Sage as a list for comparison purposes
     138
     139    INPUT:
     140
     141    - ``version`` -- a string
     142
     143        sage: encode_sage_version_for_comparison("4.2")
     144        [4, 2, 0]
     145        sage: encode_sage_version_for_comparison("5.0.beta3")
     146        [5, 0, -3, 3, 0]
     147        sage: encode_sage_version_for_comparison("4.2.1.rc0")
     148        [4, 2, 1, -1, 0, 0]
     149        sage: encode_sage_version_for_comparison("4.0.alpha3")
     150        [4, 0, -3, 3, 0]
     151
     152    The only purpose of this encoding is that Python's standard
     153    (lexicographic) comparison of lists implements the comparison of
     154    Sage versions.
     155
     156    EXAMPLES::
     157
     158        sage: e = encode_sage_version_for_comparison
     159        sage: e("4.2") < e("4.2.1")
     160        True
     161        sage: e("4.2.1") < e("4.2.2")
     162        True
     163        sage: e("4.2.rc0") < e("4.2")
     164        True
     165        sage: e("4.7.2.rc0") < e("4.7.2.rc2")
     166        True
     167        sage: e("4.7.2.beta2") < e("4.7.2")
     168        True
     169        sage: e("4.7.2.beta2") < e("4.7.2.beta3")
     170        True
     171        sage: e("4.7.2.beta10") < e("4.7.2.rc0")
     172        True
     173    """
     174    version = version.replace("alpha", "-3.")
     175    version = version.replace("beta", "-2.")
     176    version = version.replace("rc", "-1.")
     177    return [int(s) for s in re.split("\.|_", version)]+[0]
     178
     179def cmp_sage_versions(version1, version2):
     180    """
     181    Compares two Sage versions given as strings
     182
     183    EXAMPLES::
     184
     185        sage: cmp_sage_versions("4.2", "4.2.1")
     186        -1
     187        sage: cmp_sage_versions("4.2.1", "4.2.2")
     188        -1
     189        sage: cmp_sage_versions("4.2.rc0", "4.2")
     190        -1
     191        sage: cmp_sage_versions("4.7.2.rc0", "4.7.2.rc2")
     192        -1
     193        sage: cmp_sage_versions("4.7.2.beta2", "4.7.2")
     194        -1
     195        sage: cmp_sage_versions("4.7.2.beta2", "4.7.2.beta3")
     196        -1
     197        sage: cmp_sage_versions("4.7.2.beta10", "4.7.2.rc0")
     198        -1
     199        sage: cmp_sage_versions("4.7.2.beta10", "4.7.2.beta10")
     200        0
     201
     202    """
     203    return cmp(encode_sage_version_for_comparison(version1), encode_sage_version_for_comparison(version2))
    108204
    109205def cd_to_combinat():
    110206    global sage_combinat_root
    def hg_query(command, dir): 
    117213    r"""
    118214    Runs hg in the directory dir, and returns its output as a string
    119215    """
     216    cd_to_combinat() # make sure we are in the sage combinat directory
    120217    return Popen(["cd %s && %s %s"%(dir,hg,command)], stdout=PIPE, shell=True).communicate()[0].rstrip()
    121218
    122219def hg_status():
    def hg_series(): 
    136233    return hg_query("qseries", sage_combinat_root)
    137234
    138235def hg_all_patches():
    139     return split("\r\n|\r|\n",hg_series())
     236    return re.split("\r\n|\r|\n",hg_series())
    140237
    141238def hg_qnext():
    142239    global sage_combinat_root
    143240    return hg_query("qnext", sage_combinat_root)
    144241
    145242def hg_all_guards():
    146     return split("\r\n|\r|\n",
    147                  hg_query("qselect -s", sage_combinat_root))
     243    return re.split("\r\n|\r|\n",
     244                    hg_query("qselect -s", sage_combinat_root))
    148245
    149246def hg_active_guards():
    150247    # hg qselect -v returns all the active guards, one per line
    151248    # the first line is always "active guards" or "no active guards"
    152     return split("\r\n|\r|\n",
    153                  hg_query("qselect -v", sage_combinat_root))[1:]
     249    return re.split("\r\n|\r|\n",
     250                    hg_query("qselect -v", sage_combinat_root))[1:]
    154251
    155252def hg_are_all_patch_applied():
    156253    return hg_qnext() == "All patches applied"
    def qselect_backward_compatibility_patch 
    181278
    182279    # FIXME: how to change the guards on a one by one basis
    183280
    184     non_version_guards = filter(lambda guard: re.match("^(sage_)?(\d+_)*(\d+)$", guard) is None, hg_active_guards())
     281    non_version_guards = filter(lambda guard: re.match(version_regexp, guard) is None, hg_active_guards())
    185282    info("Current non version guards: %s"%" ".join(non_version_guards))
    186    
     283
    187284    if options.n:
    188285        non_version_guards = []
    189286
    def qselect_backward_compatibility_patch 
    191288
    192289    info("Updated non version guards: %s"%" ".join(non_version_guards))
    193290
    194     sage_version_as_list = [int(s) for s in re.split("\.", sage_version)]
    195291    def is_newversion_guard(guard):
    196292        """
    197         Returns true if this is a guard of the form a_b_c with a.b.c > sage_version
     293        Returns true if this is a guard for version X (like 4_3_3 or 5_0_beta4) with X > sage_version
    198294        """
    199         if not re.match("^\+(sage_)?(\d+_)*(\d+)$",guard):
     295        if not re.match(version_regexp+"$", guard):
     296            if  re.match(version_regexp, guard):
     297                # Catch erroneous guards like 4_3_3:
     298                message = "Invalid version guard in the mercurial queue: %s"%guard
     299                if options.force:
     300                    warn(message)
     301                else:
     302                    error(message)
    200303            return False
    201         guard = re.sub("^\+(sage_)?","", guard)
    202         guard_as_list = [int(s) for s in re.split("_", guard)]
    203         if guard_as_list > sage_version_as_list:
     304        if cmp_sage_versions(guard, sage_version) > 0:
    204305            info("Keep backward compatibility patches for sage "+re.sub("_",".",guard))
    205306            return True
    206307        else:
    207308            info("Skip backward compatibility patches for sage "+re.sub("_",".",guard))
    208309            return False
    209310
    210     version_guards = map(lambda guard: guard[1:], # get rid of leading "+"
    211                             filter(is_newversion_guard, hg_all_guards()))
     311    version_guards = filter(is_newversion_guard,
     312                            map(lambda guard: guard[1:], # get rid of leading "+"
     313                                hg_all_guards()))
    212314
    213315    info("Updating guards")
    214316    system(hg+" qselect -q -n")
    def update(update_from_sage_main = False 
    223325     - push back the patches
    224326     - rebuild
    225327    """
    226     cd_to_combinat()
    227328    check_for_no_diff()
    228329
    229330    info("Storing top applied patch")
    elif args[0] == "install": 
    305406        info("Done")
    306407    assert(os.path.exists(sage_combinat_hg))
    307408    assert(not os.path.exists(sage_combinat_patch_queue))
    308     cd_to_combinat()
    309409    check_for_no_diff(check_patch_queue=False)
    310410    info("Uploading sage-combinat patches into .hg/patches:")
    311411    system("cd .hg/; "+hg+" clone %s patches"%options.server)
    elif args[0] == "update": 
    320420    update()
    321421
    322422elif args[0] == "upgrade":
    323     cd_to_combinat()
    324423    check_for_no_diff()
    325424    info("Upgrading sage")
    326425    system(sage+" -upgrade")