* Ignore certain files for now
[qtopia-split.git] / split-tree.py
blob229913578b8459af71282e9436db8be8e90a25a1
1 #!/usr/bin/env python
3 import os, re, shutil
5 def try_make_dir(dirname):
6 """
7 Not yet a mkdirhier and not handling exception and not
8 testing if that file is really a dir...
9 """
10 if os.path.exists(dirname):
11 return
12 os.mkdir(dirname)
14 def module_dir_name(module_name):
15 return module_name.replace('_', '-')
17 def package_dir_name(module_name, package_suffix):
18 base_name = module_dir_name(module_name)
19 dir_name = "%s-%s" % (base_name, package_suffix)
20 return os.path.join(base_name, dir_name)
22 def expand(base, name):
23 return base.replace("${PN}", name)
25 def prepare_qtopia_tree(modules):
26 """
27 Create directories for the modules under the current working directory.
28 """
29 for (module_name, module) in modules:
30 base_name = module_dir_name(module_name)
31 try_make_dir(base_name)
32 for package_name in module.keys():
33 dir = package_dir_name(module_name, package_name)
34 try_make_dir(dir)
35 os.system("cd %s; git-init" % dir)
36 os.system("cd %s; rm -rf *" % dir)
39 def copy_qtopia_tree(modules, source_tree):
40 """
41 For every package copy the files given in the dir glob of the package
42 from the source_tree to the new location.
43 We substitute variables like ${PN} with the current package name, copy some
44 default files (e.g. license and build system boilerplate) into the package.
46 We return a list of filenames relative to the source_tree that we have used
47 to fill the package. We do not move from the source_tree for two reasons:
48 1.) Two packages might need the same file
49 2.) We might want to split the tree more than once
50 """
51 handled_files = {}
53 for (module_name, module) in modules:
54 base_name = module_dir_name(module_name)
55 try_make_dir(base_name)
56 for package_name in module.keys():
57 package_module = module[package_name]
58 assert "dir" in package_module
59 source_path = expand(package_module["dir"], package_name)
61 dir_name = package_dir_name(module_name, package_name)
62 # Inefficient but who cares?
63 found = False
64 for root, dirs, files in os.walk(source_tree):
65 if source_path in root and (root.endswith(source_path) or root[root.find(source_path)+len(source_path)] == '/'):
66 found = True
67 try_make_dir(os.path.join(dir_name, "src"))
69 print source_path, root
70 base_path = root[root.find(source_path)+len(source_path)+1:]
71 #try_make_dir(os.path.join(dir_name, base_path))
72 #try_make_dir(os.path.join(dir_name, "src", base_path))
74 for dir in dirs:
75 try_make_dir(os.path.join(dir_name, base_path, dir))
76 try_make_dir(os.path.join(dir_name, "src", base_path, dir))
77 for file in files:
78 source_file = os.path.join(root, file)
79 (_, extension) = os.path.splitext(file)
80 file_name = file
81 if extension in [".cpp", ".c", ".h", ".ui", ".rc"]:
82 file_name = os.path.join("src", file)
83 destination_file = os.path.join(dir_name, "src", base_path, file)
84 elif extension in [".pro", ".pri"]:
85 continue
86 else:
87 destination_file = os.path.join(dir_name, base_path, file)
89 handled_files[source_file] = 1
90 shutil.copyfile(source_file, destination_file)
92 if not found:
93 print "Not found", dir_name, source_path
97 return handled_files
99 def patch_packages(modules):
101 Enrich the newly build packages with some more data or apply
102 patches to them.
104 pass
107 def verify_tree(touched_files, source_tree, files_to_ignore):
109 Compare which files we have touched with the ones available in the source tree
111 pass
112 for root, dirs, files in os.walk(source_tree):
113 for file in files:
114 path = os.path.join(root, file)
115 (_, extension) = os.path.splitext(file)
116 if extension in [".pro", ".pri"]:
117 continue
118 elif "qtopiacore/qt" in path:
119 continue
120 if not path in touched_files:
121 found = False
122 for ignore in files_to_ignore:
123 if re.search(ignore, path):
124 found = True
126 if not found:
127 print "Not handled the following file: '%s'" % path
129 def tar_packages(modules):
131 Tar all directories up so they can be put somewhere or imported into git.
133 for (module_name, module) in modules:
134 for package_name in module.keys():
135 dir = package_dir_name(module_name, package_name)
136 os.system("cd %s; git-add *" % dir)
137 os.system("cd %s; git-commit -a -m 'Updated the source tree'" % dir)
138 ### use git-archive
140 def main(argv):
141 import optparse
142 parser = optparse.OptionParser(version="Qtopia Split Tree 0.1",
143 usage = """%prog [options]
145 Split up a given Qtopia Source tree into smaller chunks and enrich them
146 with patches and an alternative buildsystem
147 """)
149 parser.add_option("-s", "--source", help="The Qtopia Source Tree", action="store", dest="source", default=None)
150 options, args = parser.parse_args(argv)
152 if not options.source:
153 print "You need to specify a Qtopia Source tree"
154 sys.exit(-1)
156 # Now start to do the work
157 # 1.) prepare
158 # 2.) copy around
159 # 3.) patch
160 # 4.) Remove the copied files
161 # 5.) check what is left and complain
162 # 6.) tar them up?
163 import config
165 modules = [(x,getattr(config,x)) for x in dir(config) if x.startswith('qtopia_')]
166 prepare_qtopia_tree(modules)
167 used_files = copy_qtopia_tree(modules, options.source)
168 patch_packages(modules)
169 verify_tree(used_files, options.source, config.IGNORE_LIST)
170 tar_packages(modules)
173 if __name__ == "__main__":
174 import sys
175 main(sys.argv)