Merge branch 'org.openembedded.dev' of git.openembedded.net:openembedded into org...
[openembedded.git] / contrib / mono / collect-files.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 ## This utility takes the debian directory from an unpacked debian mono source
5 ## tree (e.g. apt-get source mono), parses the *.install files and generates a
6 ## bitbake include file with the file and package lists. It tries to handle -dbg
7 ## packages by creating additional glob patterns for *.mdb and */.debug/*. Most 
8 ## of these will not match, but that's fine (I think). 
9 ##   -- Henryk Plötz <henryk@openmoko.org>
10 ##
11 ##The output looks like:
12 ##FILES_mono-jit-dbg = "/usr/bin/mono*.mdb \
13 ##        /usr/bin/mono*/*.mdb"
14 ##FILES_mono-jit = "/usr/bin/mono"
15 ##FILES_mono-gac-dbg = "/usr/bin/gacutil*.mdb \
16 ##        /usr/bin/gacutil*/*.mdb \
17 ##        /usr/lib/mono/1.0/gacutil.exe*.mdb \
18 ##        /usr/lib/mono/1.0/gacutil.exe*/*.mdb"
19 ##FILES_mono-gac = "/usr/bin/gacutil \
20 ##        /usr/lib/mono/1.0/gacutil.exe"
21 ## ...
22 ##PACKAGES = "mono-jit-dbg \
23 ##        mono-jit \
24 ##        mono-gac-dbg \
25 ##        mono-gac \
26 ## ...
27
28 import os, sys, re
29
30 def collect_paths(dir):
31     paths = {}
32     
33     os.chdir(dir)
34     for filename in os.listdir("."):
35         if filename.endswith(".install"):
36             fp = file(filename, "r")
37             lines = fp.readlines()
38             fp.close()
39             
40             contents = []
41             for line in lines:
42                 line = line.strip()
43                 if line.startswith("#"): continue
44                 if line == "": continue
45                 
46                 lineparts = line.split()
47                 if lineparts[0].startswith("debian/tmp"):
48                     pattern = lineparts[0][ len("debian/tmp"): ]
49                     if len(lineparts) == 2:
50                         if not pattern.startswith(lineparts[1]):
51                             print >>sys.stderr, "Warning: Apparently I don't fully understand the format in file %s" % filename
52                     elif len(lineparts) > 2:
53                         print >>sys.stderr, "Warning: Apparently I don't fully understand the format in file %s" % filename
54                     
55                     contents.append( pattern )
56                 else:
57                     print >>sys.stderr, "Note: Ignoring %s in %s" % (lineparts, filename)
58                 
59             paths[ filename[ :-len(".install") ] ] = contents
60     
61     return paths
62
63 def collect_packages(paths):
64     # These packages should be populated first (e.g. because their files will otherwise end up
65     # in other packages)
66     PACKAGES_FIRST = ("mono-jit", "mono-gac", "mono-mjs", "mono-gmcs", "mono-utils", "mono-doc")
67     # These should be populated last (because their spec is very broad)
68     PACKAGES_LAST = ("mono-mcs", "libmono-system1.0-cil", "libmono-system2.0-cil", "libmono1.0-cil", "libmono2.0-cil")
69     first = []
70     last = []
71     packages = paths.keys()
72     for packagename in PACKAGES_FIRST + PACKAGES_LAST:
73         if packagename in packages:
74             packages.remove(packagename)
75             if packagename in PACKAGES_FIRST:
76                 first.append(packagename)
77             else:
78                 last.append(packagename)
79     packagenames = first + packages + last
80     
81     return packagenames, paths
82
83 def debugify(packagenames, paths):
84     pnames = []
85     for pkg in packagenames:
86         if not pkg.endswith("-dbg"):
87             result = []
88             for path in paths[pkg]:
89                 if not path.endswith("*"):
90                     result.append(path + "*.mdb")
91                     result.append(path + "*/*.mdb")
92                 else:
93                     result.append(path + ".mdb")
94                     result.append(path + "/*.mdb")
95                 if path.endswith("/"):
96                     result.append(path + ".debug/")
97                     result.append(path + "../.debug/")
98             paths[pkg + "-dbg"] = result
99             pnames.append(pkg + "-dbg")
100         pnames.append(pkg)
101     return pnames, paths
102
103 if __name__ == "__main__":
104     packagenames, paths = collect_packages( collect_paths(".") )
105     packagenames, paths = debugify(packagenames, paths)
106     
107     print "# This is a generated file, please do not edit directly"
108     print "# Use collect-files.py instead. -- Henryk <henryk@openmoko.org>"
109     
110     packages = []
111     for pkg in packagenames:
112         if not paths[pkg]: continue
113         
114         print 'FILES_%s = "%s"' % (pkg, " \\\n\t".join(paths[pkg]))
115         packages.append(pkg)
116     
117     print
118     print 'PACKAGES = "%s"' % (" \\\n\t".join(packages))