merge_libs 8.36 KB
Newer Older
Anders Blomdell's avatar
Anders Blomdell committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python3

import sys
import re
import os

def assert_equal(a, b, message, path=None):
    if a != b:
        raise Exception("%s '%s' != '%s'%s" %
                        (message, a, b, path and " in '%s'" % path or ''))
        raise Exception()
    
class Lib:

    def __init__(self, path):
        assert not os.path.exists(path), "'%s' already exists" % path
        self.path = path
        self.parsed = set()
        self.defined = {}
        self.component = {}
        self.alias = {}
        self.footprint = {}
        pass

    def add(self, path, name, component, alias, footprint):
        if name in self.defined:
            if (component == self.component[name] and
                alias == self.alias[name] and
                footprint == self.footprint[name]):
                print("'%s' in '%s' is duplicate of '%s'" %
                      (name, path, sorted(self.defined[name])),
                      file=sys.stderr)
                pass
            else:
                raise Exception("'%s' in '%s' already defined in '%s'" %
                                (name, path, self.defined[name]))
            pass
        if not name in self.defined:
            self.defined[name] = set()
            self.component[name] = component
            self.alias[name] = alias
            self.footprint[name] = footprint
            pass
        self.defined[name].add(path)

        
        
    
    def merge(self, path):
        if path in self.parsed:
            print('Already parsed %s' % path, file=sys.stderr)
            return
        self.parsed.add(path)
        component = []
        fplist = None
        footprint = set()
        for l in open(path):
            m = re.match('^EESchema-(\S+)\s+Version\s+([0-9.]+)\s*$', l)
            if m:
                assert_equal(m.group(1), 'LIBRARY', 'Unexpected kind', path)
                assert_equal(m.group(2), '2.3', "Unexpected version", path)
                continue
            m = re.match('^#encoding utf-8', l)
            if m:
                continue
            component.append(l)
            m = re.match('^DEF\s+(\S+)', l)
            if m:
                name = m.group(1)
                alias = set([name])
                pass
            m = re.match('^ALIAS\s+(.*)\s*$', l)
            if m:
                alias.update(m.group(1).split())
                pass
            m = re.match('^\$ENDFPLIST', l)
            if m:
                footprint = fplist
                fplist = None
                pass
            if fplist != None:
                fplist.add(l.strip())
                pass
            m = re.match('^\$FPLIST', l)
            if m:
                fplist = set()
                pass
            m = re.match('^ENDDEF', l)
            if m:
                self.add(path, name, component, alias, footprint)
                component = []
                fplist = None
                footprint = set()
                pass
            pass
        pass

    def commit(self):
        with open(self.path, 'w') as f:
            f.write('EESchema-LIBRARY Version 2.3\n')
            f.write('#encoding utf-8\n')
            for c in sorted(self.component):
                f.write(''.join(self.component[c]))
                pass
            pass
            f.write('#\n')
            f.write('#End Library\n')
        pass
    
class DocLib:

    def __init__(self, path):
        assert not os.path.exists(path), "'%s' already exists" % path
        self.path = path
        self.parsed = set()
        self.defined = {}
        self.component = {}
        pass

    def add (self, path, name, component):
        if name in self.defined:
            if component == self.component[name]:
                print("'%s' in '%s' is duplicate of '%s'" %
                      (name, path, sorted(self.defined[name])),
                      file=sys.stderr)
                pass
            else:
                raise Exception("'%s' in '%s' already defined in '%s'" %
                                (name, path, self.defined[name]))
            pass
        if not name in self.defined:
            self.defined[name] = set()
            self.component[name] = component
            pass
        self.defined[name].add(path)

    def merge(self, path):
        if path in self.parsed:
            print('Already parsed %s' % path, file=sys.stderr)
            return
        self.parsed.add(path)
        component = None
        for l in open(path):
            m = re.match('^EESchema-(\S+)\s+Version\s+([0-9.]+)\s*$', l)
            if m:
                assert_equal(m.group(1), 'DOCLIB', 'Unexpected kind', path)
                assert_equal(m.group(2), '2.0', "Unexpected version", path)
                continue
            m = re.match('^#encoding utf-8', l)
            if m:
                continue
            m = re.match('^\$CMP\s+(\S+)', l)
            if m:
                name = m.group(1)
                component = [ ]
                pass
            if component != None:
                component.append(l)
            m = re.match('^\$ENDCMP', l)
            if m:
                self.add(path, name, component)
                component = None
                pass
            pass
        pass

    def commit(self):
        with open(self.path, 'w') as f:
            f.write('EESchema-DOCLIB Version 2.0\n')
            f.write('#encoding utf-8\n')
            for c in sorted(self.component):
                f.write(''.join(self.component[c]))
                f.write('#\n')
                pass
            pass
            f.write('#End Doc Library\n')
        pass
    
class FootPrint:

    def __init__(self, path):
        assert not os.path.exists(path), "'%s' already exists" % path
        self.path = path
        self.entries = {}

    def merge(self, path):
        for p in os.listdir(path):
Anders Blomdell's avatar
Anders Blomdell committed
188
            m = re.match('^(.*).kicad_mod$', p)
Anders Blomdell's avatar
Anders Blomdell committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
            if not m:
                continue
            name = m.group(1)
            pp = os.path.join(path, p)
            for l in open(pp):
                m = re.match('.*\(\s*module\s+(\S+)', l)
                if m:
                    assert_equal(name, m.group(1), 'Wrong module name', pp)
                    pass
                pass
            self.entries[name] = pp
            pass

    def commit(self):
        os.mkdir(self.path)
        for k,v in self.entries.items():
            with open(os.path.join(self.path, '%s.kicad_mod' % k), 'w') as f1:
                with open(v) as f2:
                    f1.write(f2.read())
                    pass
                pass
            pass
        pass
    
if __name__ == '__main__':
    dest = sys.argv[1]
    destdir = os.path.dirname(dest)
    source = sys.argv[2:]
    assert os.path.isdir(destdir), "Destination '%s' does not exist" % destdir
    lib = Lib('%s.lib' % (dest))
    dcm = DocLib('%s.dcm' % (dest))
    fp = FootPrint('%s.pretty' % (dest))
    for s in source:
        for p in os.listdir(s):
            if p.endswith('.lib'):
                lib.merge(os.path.join(s, p))
                pass
            elif p.endswith('.dcm'):
                dcm.merge(os.path.join(s, p))
                pass
            elif p.endswith('.pretty'):
                fp.merge(os.path.join(s, p))
                pass
            pass
        pass
    
    # Sanity checks
    alias = set()
    for k in lib.alias:
        for a in lib.alias[k]:
            alias.add(a)
            if not a in dcm.component:
                print("'%s' doesn't have a description" % a, file=sys.stderr)
                pass
            pass
        pass
    for k in dcm.component:
        if not k in alias:
            print("description '%s' isn't used" % k, file=sys.stderr)
            pass
        pass
    
    import fnmatch
    used = set()
    for k in lib.footprint:
        for p in lib.footprint[k]:
            ok = False
            for f in fp.entries:
                if fnmatch.fnmatch(f, p):
                    ok = True
                    used.add(f)
                    pass
                pass
            if not ok:
Anders Blomdell's avatar
Anders Blomdell committed
263
                print("No footprint for '%s(%s)' in '%s'" %
Anders Blomdell's avatar
Anders Blomdell committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
                      (k, p, lib.defined[k]), file=sys.stderr)
                pass
            pass
        pass
    for u in set(fp.entries.keys()) - used:
        print("Unmatched footprint '%s' in '%s'" %
              (u, fp.entries[u]), file=sys.stderr)
        pass

    # Generate library
    lib.commit()
    dcm.commit()
    fp.commit()