Rev | Line | |
---|
[993d33b] | 1 | # Copyright 2013 Chris Johns (chrisj@rtems.org) |
---|
| 2 | # |
---|
| 3 | # This file's license is 2-clause BSD as in this distribution's LICENSE.2 file. |
---|
| 4 | # |
---|
| 5 | |
---|
| 6 | # |
---|
| 7 | # Pkg-config in python. Pkg-config as a tool is a good idea how-ever the |
---|
| 8 | # implementation is really Linux (or Unix) based and requires a couple of |
---|
| 9 | # packages that it should not. If it was implemented with all parts included it |
---|
| 10 | # would be portable and I suspect useful to others on platforms other than |
---|
| 11 | # Linux or Unix equivs that contain the required packages. |
---|
| 12 | # |
---|
| 13 | import re |
---|
| 14 | |
---|
| 15 | class error(Exception): |
---|
| 16 | def __init__(self, msg): |
---|
| 17 | self.msg = msg |
---|
| 18 | |
---|
| 19 | def __str__(self): |
---|
| 20 | return self.msg |
---|
| 21 | |
---|
| 22 | class package: |
---|
| 23 | def __init__(self, file = None): |
---|
| 24 | self.defines = {} |
---|
| 25 | self.fields = {} |
---|
| 26 | if file: |
---|
| 27 | self.load(file) |
---|
| 28 | |
---|
| 29 | def load(self, file): |
---|
| 30 | f = open(file) |
---|
| 31 | tm = False |
---|
| 32 | for l in f.readlines(): |
---|
| 33 | l = l[:-1] |
---|
| 34 | hash = l.find('#') |
---|
| 35 | if hash >= 0: |
---|
| 36 | l = l[:hash] |
---|
| 37 | if len(l): |
---|
| 38 | d = 0 |
---|
| 39 | define = False |
---|
| 40 | eq = l.find('=') |
---|
| 41 | dd = l.find(':') |
---|
| 42 | if eq > 0 and dd > 0: |
---|
| 43 | if eq < dd: |
---|
| 44 | define = True |
---|
| 45 | d = eq |
---|
| 46 | else: |
---|
| 47 | define = False |
---|
| 48 | d = dd |
---|
| 49 | elif eq >= 0: |
---|
| 50 | define = True |
---|
| 51 | d = eq |
---|
| 52 | elif dd >= 0: |
---|
| 53 | define = False |
---|
| 54 | d = dd |
---|
| 55 | if d > 0: |
---|
| 56 | lhs = l[:d].lower() |
---|
| 57 | rhs = l[d + 1:] |
---|
| 58 | |
---|
| 59 | if tm: |
---|
| 60 | print('define: ' + str(define) + ', lhs: ' + lhs + ', ' + rhs) |
---|
| 61 | |
---|
| 62 | if define: |
---|
| 63 | self.defines[lhs] = rhs |
---|
| 64 | else: |
---|
| 65 | self.fields[lhs] = rhs |
---|
| 66 | |
---|
| 67 | def get(self, label): |
---|
| 68 | if label.lower() not in self.fields: |
---|
| 69 | raise error('Label not found: ' + label) |
---|
| 70 | mre = re.compile('\$\{[^\}]+\}') |
---|
| 71 | s = self.fields[label.lower()] |
---|
| 72 | expanded = True |
---|
| 73 | tm = False |
---|
| 74 | while expanded: |
---|
| 75 | expanded = False |
---|
| 76 | if tm: |
---|
| 77 | print 'pc:get: "' + s + '"' |
---|
| 78 | ms = mre.findall(s) |
---|
| 79 | for m in ms: |
---|
| 80 | mn = m[2:-1] |
---|
| 81 | if mn.lower() in self.defines: |
---|
| 82 | s = s.replace(m, self.defines[mn.lower()]) |
---|
| 83 | expanded = True |
---|
| 84 | return s |
---|
Note: See
TracBrowser
for help on using the repository browser.