Guest User

Untitled

a guest
Jan 15th, 2010
47
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def get_last_revision(revs, sample_rev):
  2.     '''Returns last revision in branch defined by sample_rev
  3.    '''
  4.     def rev_digit_array():
  5.         '''make array of rev rev_digit_array: "1.18.2.6" => ["1","18","2","6"]
  6.        '''
  7.         return sample_rev.split(".")
  8.    
  9.     def rev_branch_base():
  10.         '''make rev_branch_base for branch: ["1","18","0","6"] => "1.18.6."
  11.        '''
  12.         return ".".join(rev_digit_array()[:-2] + [rev_digit_array()[-1], ""])
  13.    
  14.     def branch_base_length():
  15.         '''Base length'''
  16.         return len(rev_branch_base())
  17.    
  18.     def revision_tail(rev):
  19.         '''Revisions tails (everything without rev_branch_base of branch)'''
  20.         return rev[branch_base_length():]
  21.    
  22.     def is_rev_in_branch(rev):
  23.         '''Check if revision is in the branch'''
  24.         return rev.startswith(rev_branch_base()) and revision_tail(rev).isdigit()
  25.    
  26.     def tails_in_branch():
  27.         '''List of tails of revisions in current branch'''
  28.         return [int(revision_tail(rev)) for rev in revs if is_rev_in_branch(rev)]
  29.    
  30.     return "%s%d" % (rev_branch_base(), max(tails_in_branch()))
  31.    
  32.  
  33. if __name__ == "__main__":
  34.     revs = ["1.18", "1.19","1.20","1.21","2.50",
  35.      "1.18.2.1", "1.18.2.2","1.18.2.3","1.18.2.6", "1.19.2.6",
  36.      "1.18.2.1.3.1", "1.18.2.1.3.2","1.18.2.1.3.3","1.18.2.1.3.6","1.18.3.1.3.6"]
  37.     assert get_last_revision(revs, "1.18.0.2") == "1.18.2.6"
  38.     assert get_last_revision(revs, "1.18.2.1.0.3") == "1.18.2.1.3.6"
  39.     print "OK"
RAW Paste Data