5 def mkdirhier(dirname
):
7 os
.system("mkdir -p %s" % dirname
)
9 def try_make_dir(dirname
):
11 Not yet a mkdirhier and not handling exception and not
12 testing if that file is really a dir...
14 if os
.path
.exists(dirname
):
18 def module_dir_name(module_name
):
19 return module_name
.replace('_', '-')
21 def package_dir_name(module_name
, package_suffix
):
22 base_name
= module_dir_name(module_name
)
23 dir_name
= "%s-%s" % (base_name
, package_suffix
)
24 return os
.path
.join(base_name
, dir_name
)
26 def expand(base
, name
):
27 return base
.replace("${PN}", name
)
29 def prepare_qtopia_tree(modules
):
31 Create directories for the modules under the current working directory.
33 for (module_name
, module
) in modules
:
34 base_name
= module_dir_name(module_name
)
35 try_make_dir(base_name
)
36 for package_name
in module
.keys():
37 dir = package_dir_name(module_name
, package_name
)
39 os
.system("cd %s; git-init" % dir)
40 os
.system("cd %s; rm -rf *" % dir)
43 def copy_qtopia_tree(modules
, source_tree
):
45 For every package copy the files given in the dir glob of the package
46 from the source_tree to the new location.
47 We substitute variables like ${PN} with the current package name, copy some
48 default files (e.g. license and build system boilerplate) into the package.
50 We return a list of filenames relative to the source_tree that we have used
51 to fill the package. We do not move from the source_tree for two reasons:
52 1.) Two packages might need the same file
53 2.) We might want to split the tree more than once
57 for (module_name
, module
) in modules
:
58 base_name
= module_dir_name(module_name
)
59 try_make_dir(base_name
)
60 for package_name
in module
.keys():
61 package_module
= module
[package_name
]
62 assert "dir" in package_module
64 if type(package_module
["dir"]) == type([]):
65 source_dirs
= package_module
["dir"]
67 source_dirs
= [package_module
["dir"]]
69 # Copy all matching dirs to the destination
70 dir_name
= package_dir_name(module_name
, package_name
)
71 for source_dir
in source_dirs
:
72 source_path
= expand(source_dir
, package_name
)
73 source_exp
= re
.compile(source_path
)
76 for root
, dirs
, files
in os
.walk(source_tree
):
77 if (source_path
in root
and (root
.endswith(source_path
) or root
[root
.find(source_path
)+len(source_path
)] == '/')) or \
78 ("regexp" in package_module
and source_exp
.search(root
)):
80 try_make_dir(os
.path
.join(dir_name
, "src"))
82 base_path
= root
[root
.find(source_path
)+len(source_path
)+1:]
85 try_make_dir(os
.path
.join(dir_name
, base_path
, dir))
86 try_make_dir(os
.path
.join(dir_name
, "src", base_path
, dir))
88 source_file
= os
.path
.join(root
, file)
89 (_
, extension
) = os
.path
.splitext(file)
91 if not "nosrc" in package_module
and extension
in [".cpp", ".c", ".h", ".ui", ".rc"]:
92 file_name
= os
.path
.join("src", file)
93 destination_file
= os
.path
.join(dir_name
, "src", base_path
, file)
94 elif extension
in [".pro", ".pri"]:
97 destination_file
= os
.path
.join(dir_name
, base_path
, file)
99 handled_files
[source_file
] = 1
100 shutil
.copyfile(source_file
, destination_file
)
103 print "Not found", dir_name
, source_path
105 # Copy specific files
106 if "files" in package_module
:
107 for root
, dirs
, files
in os
.walk(source_tree
):
108 for package_file
in package_module
["files"]:
109 file_glob
= re
.compile(expand(package_file
, package_name
))
111 path
= os
.path
.join(root
, file)
112 search
= file_glob
.search(path
)
114 print "Copying", path
, dir_name
115 new_path
= os
.path
.join(dir_name
, path
[search
.start()+1:])
116 mkdirhier(os
.path
.dirname(new_path
))
117 shutil
.copyfile(path
, new_path
)
118 handled_files
[path
] = 1
123 def patch_packages(modules
):
125 Enrich the newly build packages with some more data or apply
131 def verify_tree(touched_files
, source_tree
, files_to_ignore
):
133 Compare which files we have touched with the ones available in the source tree
135 for root
, dirs
, files
in os
.walk(source_tree
):
137 path
= os
.path
.join(root
, file)
138 (_
, extension
) = os
.path
.splitext(file)
139 if extension
in [".pro", ".pri"]:
141 elif "qtopiacore/qt" in path
:
143 if not path
in touched_files
:
145 for ignore
in files_to_ignore
:
146 if re
.search(ignore
, path
):
150 print "Not handled the following file: '%s'" % path
152 def tar_packages(modules
):
154 Tar all directories up so they can be put somewhere or imported into git.
156 for (module_name
, module
) in modules
:
157 for package_name
in module
.keys():
158 dir = package_dir_name(module_name
, package_name
)
159 os
.system("cd %s; git-add *" % dir)
160 os
.system("cd %s; git-commit -a -m 'Updated the source tree'" % dir)
165 parser
= optparse
.OptionParser(version
="Qtopia Split Tree 0.1",
166 usage
= """%prog [options]
168 Split up a given Qtopia Source tree into smaller chunks and enrich them
169 with patches and an alternative buildsystem
172 parser
.add_option("-s", "--source", help="The Qtopia Source Tree", action
="store", dest
="source", default
=None)
173 options
, args
= parser
.parse_args(argv
)
175 if not options
.source
:
176 print "You need to specify a Qtopia Source tree"
179 # Now start to do the work
183 # 4.) Remove the copied files
184 # 5.) check what is left and complain
188 modules
= [(x
,getattr(config
,x
)) for x
in dir(config
) if x
.startswith('qtopia_')]
189 prepare_qtopia_tree(modules
)
190 used_files
= copy_qtopia_tree(modules
, options
.source
)
191 patch_packages(modules
)
192 verify_tree(used_files
, options
.source
, config
.IGNORE_LIST
)
193 tar_packages(modules
)
196 if __name__
== "__main__":