* Once done, git-add everything and git-commit everything. This even takes
[qtopia-split.git] / split-tree.py
blob4e1862678e427d3b5e0c413ded35f422caeafa04
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 and not path in files_to_ignore:
121 print "Not handled the following file: '%s'" % path
123 def tar_packages(modules):
125 Tar all directories up so they can be put somewhere or imported into git.
127 for (module_name, module) in modules:
128 for package_name in module.keys():
129 dir = package_dir_name(module_name, package_name)
130 os.system("cd %s; git-add *" % dir)
131 os.system("cd %s; git-commit -a -m 'Updated the source tree'" % dir)
132 ### use git-archive
134 def main(argv):
135 import optparse
136 parser = optparse.OptionParser(version="Qtopia Split Tree 0.1",
137 usage = """%prog [options]
139 Split up a given Qtopia Source tree into smaller chunks and enrich them
140 with patches and an alternative buildsystem
141 """)
143 parser.add_option("-s", "--source", help="The Qtopia Source Tree", action="store", dest="source", default=None)
144 options, args = parser.parse_args(argv)
146 if not options.source:
147 print "You need to specify a Qtopia Source tree"
148 sys.exit(-1)
150 # Now start to do the work
151 # 1.) prepare
152 # 2.) copy around
153 # 3.) patch
154 # 4.) Remove the copied files
155 # 5.) check what is left and complain
156 # 6.) tar them up?
157 import config
159 modules = [(x,getattr(config,x)) for x in dir(config) if x.startswith('qtopia_')]
160 prepare_qtopia_tree(modules)
161 used_files = copy_qtopia_tree(modules, options.source)
162 patch_packages(modules)
163 verify_tree(used_files, options.source, config.IGNORE_LIST)
164 tar_packages(modules)
167 if __name__ == "__main__":
168 import sys
169 main(sys.argv)