From 14088e71bb002a9d93d27c97a305655f7cb07fdf Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sun, 8 Jul 2012 21:46:40 +0200 Subject: [PATCH 1/2] Implement natural sorting so that our version number is correct. --- setup.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 2f8919f4b..51dc2eed9 100755 --- a/setup.py +++ b/setup.py @@ -28,10 +28,53 @@ ############################################################################### from setuptools import setup, find_packages +import re VERSION_FILE = 'openlp/.version' try: + def natural_sort_key(s): + """ + Return a tuple by which s is sorted. + + ``s`` + A string value from the list we want to sort. + """ + def try_int(s): + """ + Convert string s to an integer if possible. Fail silently and return + the string as-is if it isn't an integer. + + ``s`` + The string to try to convert. + """ + try: + return int(s) + except: + return s + return map(try_int, re.findall(r'(\d+|\D+)', s)) + + def natural_compare(a, b): + """ + Compare two strings naturally and return the result. + + ``a`` + A string to compare. + + ``b`` + A string to compare. + """ + return cmp(natural_sort_key(a), natural_sort_key(b)) + + def natural_sort(seq, compare=natural_compare): + """ + Returns a copy of seq, sorted by natural string sort. + """ + import copy + temp = copy.copy(seq) + temp.sort(compare) + return temp + from bzrlib.branch import Branch b = Branch.open_containing('.')[0] b.lock_read() @@ -46,7 +89,8 @@ try: if revision_id in tags: version = u'%s' % tags[revision_id][0] else: - version = '%s-bzr%s' % (sorted(b.tags.get_tag_dict().keys())[-1], revno) + version = '%s-bzr%s' % \ + (natural_sort(b.tags.get_tag_dict().keys())[-1], revno) ver_file = open(VERSION_FILE, u'w') ver_file.write(version) ver_file.close() From 4e03451db73d470e23f556434316edeca99531ef Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 9 Jul 2012 19:56:02 +0200 Subject: [PATCH 2/2] Pulled natural sorting methods out from the try (no real reason for them to be in there) and moved "try_int" to outside the other method (then it is only compiled once). --- setup.py | 87 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/setup.py b/setup.py index 51dc2eed9..b101d2907 100755 --- a/setup.py +++ b/setup.py @@ -31,50 +31,53 @@ from setuptools import setup, find_packages import re VERSION_FILE = 'openlp/.version' +SPLIT_ALPHA_DIGITS = re.compile(r'(\d+|\D+)') + +def try_int(s): + """ + Convert string s to an integer if possible. Fail silently and return + the string as-is if it isn't an integer. + + ``s`` + The string to try to convert. + """ + try: + return int(s) + except Exception: + return s + +def natural_sort_key(s): + """ + Return a tuple by which s is sorted. + + ``s`` + A string value from the list we want to sort. + """ + return map(try_int, SPLIT_ALPHA_DIGITS.findall(s)) + +def natural_compare(a, b): + """ + Compare two strings naturally and return the result. + + ``a`` + A string to compare. + + ``b`` + A string to compare. + """ + return cmp(natural_sort_key(a), natural_sort_key(b)) + +def natural_sort(seq, compare=natural_compare): + """ + Returns a copy of seq, sorted by natural string sort. + """ + import copy + temp = copy.copy(seq) + temp.sort(compare) + return temp try: - def natural_sort_key(s): - """ - Return a tuple by which s is sorted. - - ``s`` - A string value from the list we want to sort. - """ - def try_int(s): - """ - Convert string s to an integer if possible. Fail silently and return - the string as-is if it isn't an integer. - - ``s`` - The string to try to convert. - """ - try: - return int(s) - except: - return s - return map(try_int, re.findall(r'(\d+|\D+)', s)) - - def natural_compare(a, b): - """ - Compare two strings naturally and return the result. - - ``a`` - A string to compare. - - ``b`` - A string to compare. - """ - return cmp(natural_sort_key(a), natural_sort_key(b)) - - def natural_sort(seq, compare=natural_compare): - """ - Returns a copy of seq, sorted by natural string sort. - """ - import copy - temp = copy.copy(seq) - temp.sort(compare) - return temp - + # Try to import Bazaar from bzrlib.branch import Branch b = Branch.open_containing('.')[0] b.lock_read()