Changeset 3ddbd79 in rtems-source-builder


Ignore:
Timestamp:
01/18/18 02:16:47 (6 years ago)
Author:
Chris Johns <chrisj@…>
Branches:
5, master
Children:
a293ddc
Parents:
f3b1700
Message:

sb: Add an orphan check to sb-check.

The orphans check lets you see which configuration and build set
files in the RSB are not referernced. You can audit the list and
remove any configuration files not being used. Top level
build set files are included so you need to becareful not to
remove something that is valid and useful. To run:

$ ./source-builder/sb-check --check-orphans

Location:
source-builder/sb
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • source-builder/sb/check.py

    rf3b1700 r3ddbd79  
    2828import error
    2929import execute
     30import fnmatch
    3031import log
    3132import options
    3233import path
     34import re
    3335import version
    3436
     
    163165
    164166
     167def check_orphans(opts):
     168
     169    def _find_files(path, globs, excludes = []):
     170        ff = []
     171        for root, dirs, files in os.walk(path, followlinks = True):
     172            for f in files:
     173                for g in globs:
     174                    if fnmatch.fnmatch(f, g) and f not in excludes:
     175                        ff += [os.path.join(root, f)]
     176        return sorted(ff)
     177
     178    def _clean(line):
     179        line = line[0:-1]
     180        b = line.find('#')
     181        if b >= 0:
     182            line = line[1:b]
     183        return line.strip()
     184
     185    def _find(name, opts):
     186        ename = opts.defaults.expand(name)
     187        if ':' in ename:
     188            paths = path.dirname(ename).split(':')
     189            name = path.basename(name)
     190        else:
     191            paths = opts.defaults.get_value('_configdir').split(':')
     192        for p in paths:
     193            n = path.join(opts.defaults.expand(p), name)
     194            if path.exists(n):
     195                return n
     196        return None
     197
     198    paths = opts.defaults.get_value('_configdir').split(':')
     199
     200    cfgs = {}
     201    for p in paths:
     202        ep = opts.defaults.expand(p)
     203        print('Scanning: %s (%s)' % (p, ep))
     204        for f in _find_files(ep, ['*.cfg', '*.bset']):
     205            root, ext = path.splitext(f)
     206            cfgs[f] = { 'src': None, 'ext': ext, 'refs': 0, 'errors':[] }
     207
     208    wss = re.compile(r'\s+')
     209
     210    for c in cfgs:
     211        with open(c, 'r') as f:
     212            cfgs[c]['src'] = f.readlines()
     213        lc = 0
     214        for l in cfgs[c]['src']:
     215            lc += 1
     216            l = _clean(l)
     217            if len(l) == 0:
     218                continue
     219            if l[0] == '%':
     220                ls = wss.split(l, 2)
     221                if ls[0] == '%include':
     222                    name = _find(ls[1], opts)
     223                    if name is None:
     224                        cfgs[c]['errors'] += [lc]
     225                    elif name not in cfgs:
     226                        raise error.general('include: %s: not present' % (ls[1]))
     227                    else:
     228                        cfgs[name]['refs'] += 1
     229            elif cfgs[c]['ext'] == '.bset' and ':' not in l:
     230                for ext in ['', '.cfg', '.bset']:
     231                    name = _find(l + ext, opts)
     232                    if name is not None:
     233                        if name not in cfgs:
     234                            raise error.general('include: %s: not present' % (ls[1]))
     235                        else:
     236                            cfgs[name]['refs'] += 1
     237                        break
     238
     239    topdir = opts.defaults.expand('%{_topdir}')
     240
     241    orphans = []
     242    show = True
     243
     244    for c in cfgs:
     245        if cfgs[c]['refs'] == 0:
     246            orphans += [c]
     247        if len(cfgs[c]['errors']) != 0:
     248            if show:
     249                print('Warnings:')
     250                show = False
     251            print(' %s:' % (path.relpath(c)))
     252            for l in cfgs[c]['errors']:
     253                print('  %3d: %s' % (l, cfgs[c]['src'][l - 1][:-1]))
     254
     255    show = True
     256
     257    for o in sorted(orphans):
     258        if show:
     259            print('Orphans:')
     260            show = False
     261        print(' %s' % (path.relpath(o)))
     262
    165263def run():
    166264    import sys
     
    168266        _opts = options.load(args = sys.argv)
    169267        log.notice('RTEMS Source Builder - Check, %s' % (version.str()))
    170         if host_setup(_opts):
    171             print('Environment is ok')
     268
     269        orphans = _opts.parse_args('--check-orphans', error = False, extra = False)
     270        if orphans:
     271            print('Checking for orphans...')
     272            check_orphans(_opts)
    172273        else:
    173             print('Environment is not correctly set up')
     274            if host_setup(_opts):
     275                print('Environment is ok')
     276            else:
     277                print('Environment is not correctly set up')
    174278    except error.general as gerr:
    175279        print(gerr)
  • source-builder/sb/path.py

    rf3b1700 r3ddbd79  
    9292    path = shell(path)
    9393    return shell(os.path.abspath(host(path)))
     94
     95def relpath(path, start = None):
     96    path = shell(path)
     97    if start is None:
     98        path = os.path.relpath(host(path))
     99    else:
     100        path = os.path.relpath(host(path), start)
     101    return shell(path)
    94102
    95103def splitext(path):
Note: See TracChangeset for help on using the changeset viewer.