Guest User

Untitled

a guest
Sep 26th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #!/bin/env python3
  2. """mergeslots
  3. Copyright (c) 2018, Nicholas Solin
  4. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of the Universal Gaming Alliance nor the
  14. names of its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16.  
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL NICHOLAS SOLIN BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  
  28. This is a custom merge driver for git. You can either define
  29. it in .git/config like this:
  30.  
  31. [merge "mergeslots"]
  32. name = Merge reserved slots in SCP:SL.
  33. driver = python3 ./mergeslots.py %O %A %B %L
  34.  
  35. Or define it in .gitconfig and run the following command:
  36.  
  37. git config --local include.path ../.gitconfig
  38.  
  39. Then make sure to define it in your .gitattributes file like so:
  40.  
  41. Reserved\ Slots.txt merge=mergeslots
  42.  
  43. This tells git to use the 'mergeslots' merge driver.
  44.  
  45. """
  46. import sys, csv
  47.  
  48. # The arguments from git are the names of temporary files that
  49. # hold the contents of the different versions of the log.txt
  50. # file.
  51.  
  52. ancestor_slots = {}
  53. current_slots = {}
  54. branch_slots = {}
  55.  
  56. # The version of the file from the common ancestor of the two branches.
  57. # This constitutes the 'base' version of the file.
  58. with open(sys.argv[1], 'r', encoding='utf-8') as csvfile:
  59. slots = csv.reader(csvfile, delimiter=';', quotechar='"')
  60. for row in slots:
  61. if len(row) == 1:
  62. ancestor_slots[ row[0] ] = row
  63. else:
  64. ancestor_slots[ row[1] ] = row
  65.  
  66. # The version of the file at the HEAD of the current branch.
  67. # The result of the merge should be left in this file by overwriting it.
  68. with open(sys.argv[2], 'r', encoding='utf-8') as csvfile:
  69. slots = csv.reader(csvfile, delimiter=';', quotechar='"')
  70. for row in slots:
  71. if len(row) == 1:
  72. current_slots[ row[0] ] = row
  73. else:
  74. current_slots[ row[1] ] = row
  75.  
  76. # The version of the file at the HEAD of the other branch.
  77. with open(sys.argv[3], 'r', encoding='utf-8') as csvfile:
  78. slots = csv.reader(csvfile, delimiter=';', quotechar='"')
  79. for row in slots:
  80. if len(row) == 1:
  81. branch_slots[ row[0] ] = row
  82. else:
  83. branch_slots[ row[1] ] = row
  84.  
  85. combined_slots = current_slots.copy()
  86. for key in branch_slots:
  87. if key in current_slots:
  88. if len(branch_slots[key]) < len(current_slots[key]):
  89. combined_slots[key] = current_slots[key]
  90. else:
  91. combined_slots[key] = branch_slots[key]
  92. else:
  93. combined_slots[key] = branch_slots[key]
  94.  
  95. print("")
  96. print("This is the content of the new Reserved Slots.txt file:")
  97. for key in combined_slots:
  98. print(';'.join(combined_slots[key]))
  99. with open(sys.argv[2], 'w') as csvfile:
  100. csvwriter = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL, lineterminator="\r\n")
  101. for key in combined_slots:
  102. csvwriter.writerow(combined_slots[key])
  103.  
  104. # Exit with zero status if the merge went cleanly, non-zero otherwise.
  105. sys.exit(0)
Add Comment
Please, Sign In to add comment