Commits

Pierre-Yves David  committed a746462

handle reorder and insert (but not duplicated line

  • Participants
  • Parent commits b2b8459

Comments (0)

Files changed (2)

File mergeseries.py

 #!/usr/bin/python
 import sys
 import tempfile
+from collections import deque
 
 
 result = []
 local = sys.argv[2]
 other = sys.argv[3]
 
-base_content  = set(open(base))
-other_content = set(open(other))
+base_dq = deque(open(base))
+base_content  = set(base_dq)
+local_dq = deque(open(local))
+local_content  = set(local_dq)
+other_dq = deque(open(other))
+other_content  = set(other_dq)
 
+
+processed = set([None])
 other_rm = base_content - other_content
+local_rm = base_content - local_content
 
-for line in open(local):
-    if line not in other_rm:
-        result.append(line)
 
-for line in open(other):
-    if line not in base_content:
-        result.append(line)
+def get_next(dq):
+    item = None
+    blacklist = processed
+    while item in processed | other_rm | local_rm:
+        if not dq:
+            return None
+        item = dq.popleft()
+    return item
+
+cur_base  = get_next(base_dq)
+cur_local = get_next(local_dq)
+cur_other = get_next(other_dq)
+while cur_local is not None or cur_other is not None:
+    if cur_base == cur_local and cur_base == cur_other:
+        processed.add(cur_base)
+        result.append(cur_base)
+        cur_base  = get_next(base_dq)
+        cur_local = get_next(local_dq)
+        cur_other = get_next(other_dq)
+    elif cur_local is not None  and cur_base != cur_local:
+        processed.add(cur_local)
+        result.append(cur_local)
+        cur_local = get_next(local_dq)
+    elif cur_other is not None  and cur_base != cur_other:
+        processed.add(cur_other)
+        result.append(cur_other)
+        cur_other = get_next(other_dq)
+    elif cur_local is not None:
+        processed.add(cur_local)
+        result.append(cur_local)
+        cur_base  = get_next(base_dq)
+        cur_local = get_next(local_dq)
+    elif cur_other is not None:
+        processed.add(cur_other)
+        result.append(cur_other)
+        cur_base  = get_next(base_dq)
+        cur_other = get_next(other_dq)
+
+
+
+
 
 final_target = open(local, 'w')
 final_target.writelines(result)

File test-mq-merge-series.t

   > EOF
   $ $mergeseries base local other
   $ cat local
+  A
+  Babar
+  Denfert
+  F
 
 Simple append test with base
 
   Cat
   Dude
 
-  A
+Local append, other insert
+
+  $ cat > base << EOF
+  > Auguste
+  > Blanqui
+  > EOF
+  $ cat > local << EOF
+  > Auguste
+  > Blanqui
+  > Cat
+  > Dude
+  > EOF
+  $ cat > other << EOF
+  > Auguste
+  > Babar
+  > Blanqui
+  > EOF
+  $ $mergeseries base local other
+  $ cat local
+  Auguste
   Babar
-  Denfert
-  F
+  Blanqui
+  Cat
+  Dude
+
+Local insert, other append
+
+  $ cat > base << EOF
+  > Auguste
+  > Blanqui
+  > EOF
+  $ cat > local << EOF
+  > Auguste
+  > Babar
+  > Blanqui
+  > EOF
+  $ cat > other << EOF
+  > Auguste
+  > Blanqui
+  > Cat
+  > Dude
+  > EOF
+  $ $mergeseries base local other
+  $ cat local
+  Auguste
+  Babar
+  Blanqui
+  Cat
+  Dude
+
+Local reorder, other insert
+
+  $ cat > base << EOF
+  > Paris
+  > Auguste
+  > Blanqui
+  > Babar
+  > Bubur
+  > EOF
+  $ cat > local << EOF
+  > Paris
+  > Blanqui
+  > Auguste
+  > Babar
+  > Bubur
+  > EOF
+  $ cat > other << EOF
+  > Paris
+  > Auguste
+  > Blanqui
+  > Babar
+  > Bibir
+  > Bubur
+  > EOF
+  $ $mergeseries base local other
+  $ cat local
+  Paris
+  Blanqui
+  Auguste
+  Babar
+  Bibir
+  Bubur
+
+Local insert, other reorder
+
+  $ cat > base << EOF
+  > Paris
+  > Auguste
+  > Blanqui
+  > Babar
+  > Bubur
+  > EOF
+  $ cat > local << EOF
+  > Paris
+  > Auguste
+  > Blanqui
+  > Babar
+  > Bibir
+  > Bubur
+  > EOF
+  $ cat > other << EOF
+  > Paris
+  > Blanqui
+  > Auguste
+  > Babar
+  > Bubur
+  > EOF
+  $ $mergeseries base local other
+  $ cat local
+  Paris
+  Blanqui
+  Auguste
+  Babar
+  Bibir
+  Bubur
+Local insert, other reorder
+
+  $ cat > base << EOF
+  > Paris
+  > Auguste
+  > Blanqui
+  > Babar
+  > Bubur
+  > EOF
+  $ cat > local << EOF
+  > Babar
+  > Bubur
+  > EOF
+  $ cat > other << EOF
+  > Paris
+  > Auguste
+  > Blanqui
+  > Voiture
+  > Babar
+  > Bubur
+  > EOF
+  $ $mergeseries base local other
+  $ cat local
+  Voiture
+  Babar
+  Bubur
+
+Comment Management
+
+  $ cat > base << EOF
+  > Paris
+  > Auguste
+  > ######################
+  > # Babar the elephant #
+  > #####################
+  > Blanqui
+  > Babar
+  > Bubur
+  > EOF
+  $ cat > local << EOF
+  > Paris
+  > Auguste
+  > ######################
+  > # Babar the elephant #
+  > ######################
+  > Bibir
+  > Bubur
+  > EOF
+  $ cat > other << EOF
+  > #series head
+  > Paris
+  > Blanqui
+  > Auguste
+  > ######################
+  > # Babar the elephant #
+  > ######################
+  > Babar
+  > Bubur
+  > EOF
+  $ $mergeseries base local other
+  $ cat local
+  #series head
+  Paris
+  Auguste
+  ######################
+  # Babar the elephant #
+  ######################
+  Bibir
+  Bubur