|
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 # |
|
26 # Files within these directories are maintained. i.e. it is possible to fix |
|
27 # a directory tree with files already left in it. |
|
28 # |
|
29 |
|
30 |
|
31 import os |
|
32 import os.path |
|
33 import re |
|
34 import sys |
|
35 import shutil |
|
36 from optparse import OptionParser |
|
37 |
|
38 def mergetwo(firstdir, seconddir): |
|
39 # Move files from firstdir into seconddir. If firstdir and seconddir both have |
|
40 # a directory "X" then combines the contents of theses |
|
41 for d in os.listdir(firstdir): |
|
42 fileitem = os.path.join(firstdir,d) |
|
43 dest = os.path.join(seconddir,d) |
|
44 print "moving %s, %s to %s " % (d, fileitem, dest) |
|
45 if os.path.isdir(dest) and os.path.isdir(fileitem): |
|
46 mergetwo(fileitem, dest) |
|
47 try: |
|
48 os.rmdir(fileitem) |
|
49 except: |
|
50 print "\tfailed rmdir %s" % fileitem |
|
51 else: |
|
52 shutil.move(fileitem, dest) |
|
53 try: |
|
54 os.rmdir(firstdir) |
|
55 except: |
|
56 print "\tfailed rmdir %s" % firstdir |
|
57 |
|
58 |
|
59 |
|
60 def visit(dirname, link = False): |
|
61 # Find directories with names that differ only in case |
|
62 nameclash = {} |
|
63 # print "dir %s\n" %(dirname) |
|
64 for f in os.listdir(dirname): |
|
65 fullpath = os.path.join(dirname,f) |
|
66 if os.path.isdir(fullpath) and not os.path.islink(fullpath): |
|
67 # print "\tmergeable %s" %(f) |
|
68 fl = f.lower() |
|
69 if nameclash.has_key(fl): |
|
70 mergetwo(fullpath, os.path.join(dirname, nameclash[fl])) |
|
71 if link: |
|
72 print "\tlinking %s <- %s" %(nameclash[fl], fullpath) |
|
73 os.symlink(nameclash[fl], fullpath) |
|
74 else: |
|
75 nameclash[fl] = f |
|
76 else: |
|
77 pass |
|
78 # print "%s is not a dir\n" %(f) |
|
79 |
|
80 for d in nameclash.values(): |
|
81 # print "\tVisiting %s" %(d) |
|
82 visit(os.path.join(dirname, d)) |
|
83 |
|
84 |
|
85 dirname = sys.argv[1] |
|
86 |
|
87 parser = OptionParser(prog = "unsplitdirs", |
|
88 usage = "%prog [-h | options] [<file>]") |
|
89 |
|
90 parser.add_option("-l", "--link", default = False, |
|
91 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") |
|
92 |
|
93 (options, args) = parser.parse_args() |
|
94 |
|
95 logname="stdin" |
|
96 if len(args) > 0: |
|
97 dirname = args[0] |
|
98 else: |
|
99 dirname ='.' |
|
100 |
|
101 visit(dirname, options.link) |