checkcopies.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import getopt, sys, os, re, time
  2. def usage():
  3. print """
  4. usage:
  5. checkcopies.py -d <directory>
  6. <directory> is a folder containing svg files.
  7. looks for files that have the same content but different names
  8. """
  9. def main():
  10. try:
  11. opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory"])
  12. except getopt.GetoptError, err:
  13. # print help information and exit:
  14. print str(err) # will print something like "option -a not recognized"
  15. usage()
  16. return
  17. outputDir = None
  18. gotnot = 0
  19. for o, a in opts:
  20. #print o
  21. #print a
  22. if o in ("-d", "--directory"):
  23. outputDir = a
  24. elif o in ("-h", "--help"):
  25. usage()
  26. return
  27. else:
  28. assert False, "unhandled option"
  29. if(not(outputDir)):
  30. print "missing -d {directory} parameter"
  31. usage()
  32. return
  33. fs = []
  34. for f in os.listdir(outputDir):
  35. if f.endswith(".svg"):
  36. fs.append(os.path.splitext(f)[0])
  37. #print "appending", f, os.path.splitext(f)[0]
  38. fs.sort(key=str.lower) # remove the .svg so the sort works better
  39. dirs = []
  40. for f in fs:
  41. dirs.append(f + ".svg")
  42. dirlen = len(dirs)
  43. matches = []
  44. for i in range(dirlen):
  45. if i in matches:
  46. continue
  47. f1 = dirs[i]
  48. path = os.path.join(outputDir, f1)
  49. if not os.path.isfile(path):
  50. continue
  51. if not f1.endswith(".svg"):
  52. continue
  53. # print "testing", f1
  54. txt1 = None
  55. try:
  56. infile = open(path, "r")
  57. txt1 = infile.read()
  58. infile.close()
  59. except:
  60. print "failure", f1
  61. if txt1 == None:
  62. continue
  63. for j in range(i + 1, dirlen):
  64. f2 = dirs[j]
  65. path = os.path.join(outputDir,f2)
  66. if not os.path.isfile(path):
  67. continue
  68. if not f2.endswith(".svg"):
  69. continue
  70. if f1 == f2:
  71. continue
  72. txt2 = None
  73. try:
  74. infile = open(path, "r")
  75. txt2 = infile.read()
  76. infile.close()
  77. except:
  78. print "failure", f2
  79. continue
  80. if txt2 == None:
  81. continue
  82. if txt1 == txt2:
  83. matches.append(j)
  84. print "<map package='{0}' to='{1}' />".format(f1, f2)
  85. if __name__ == "__main__":
  86. main()