unsplitdirs.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #
  2. # Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
  3. # All rights reserved.
  4. # This component and the accompanying materials are made available
  5. # under the terms of the License "Eclipse Public License v1.0"
  6. # which accompanies this distribution, and is available
  7. # at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. #
  9. # Initial Contributors:
  10. # Nokia Corporation - initial contribution.
  11. #
  12. # Contributors:
  13. #
  14. # Description:
  15. # unsplitdirs utility
  16. # This utility converts a directory tree which may contain "splits" due to case inconsistencies into
  17. # a combined form. This is best illustrated as follows:
  18. # epoc32/RELEASE/ARMV5/urel
  19. # epoc32/Release/armv5/UREL
  20. # epoc32/RELEASE/armv5/Urel
  21. # are "healed by this script into:
  22. # epoc32/RELEASE/ARMV5/urel (i.e. the first occurrence.
  23. #
  24. #
  25. # Files within these directories are maintained. i.e. it is possible to fix
  26. # a directory tree with files already left in it.
  27. #
  28. import os
  29. import os.path
  30. import re
  31. import sys
  32. import shutil
  33. from optparse import OptionParser
  34. def mergetwo(firstdir, seconddir):
  35. # Move files from firstdir into seconddir. If firstdir and seconddir both have
  36. # a directory "X" then combines the contents of theses
  37. for d in os.listdir(firstdir):
  38. fileitem = os.path.join(firstdir,d)
  39. dest = os.path.join(seconddir,d)
  40. print "moving %s, %s to %s " % (d, fileitem, dest)
  41. if os.path.isdir(dest) and os.path.isdir(fileitem):
  42. mergetwo(fileitem, dest)
  43. try:
  44. os.rmdir(fileitem)
  45. except:
  46. print "\tfailed rmdir %s" % fileitem
  47. else:
  48. shutil.move(fileitem, dest)
  49. try:
  50. os.rmdir(firstdir)
  51. except:
  52. print "\tfailed rmdir %s" % firstdir
  53. def visit(dirname, link = False):
  54. # Find directories with names that differ only in case
  55. nameclash = {}
  56. # print "dir %s\n" %(dirname)
  57. for f in os.listdir(dirname):
  58. fullpath = os.path.join(dirname,f)
  59. if os.path.isdir(fullpath) and not os.path.islink(fullpath):
  60. # print "\tmergeable %s" %(f)
  61. fl = f.lower()
  62. if nameclash.has_key(fl):
  63. mergetwo(fullpath, os.path.join(dirname, nameclash[fl]))
  64. if link:
  65. print "\tlinking %s <- %s" %(nameclash[fl], fullpath)
  66. os.symlink(nameclash[fl], fullpath)
  67. else:
  68. nameclash[fl] = f
  69. else:
  70. pass
  71. # print "%s is not a dir\n" %(f)
  72. for d in nameclash.values():
  73. # print "\tVisiting %s" %(d)
  74. visit(os.path.join(dirname, d))
  75. dirname = sys.argv[1]
  76. parser = OptionParser(prog = "unsplitdirs",
  77. usage = "%prog [-h | options] [<file>]")
  78. parser.add_option("-l", "--link", default = False,
  79. action="store_true", dest="link", help="Turn mismatched-case directories into symbolic links e.g. if armv5 is the default then make the link ARMV5->armv5")
  80. (options, args) = parser.parse_args()
  81. logname="stdin"
  82. if len(args) > 0:
  83. dirname = args[0]
  84. else:
  85. dirname ='.'
  86. visit(dirname, options.link)