]> sigrok.org Git - libsigrokdecode.git/blob - tools/install-decoders
Clean up autoconf leftover configuration.
[libsigrokdecode.git] / tools / install-decoders
1 #!/usr/bin/env python3
2 #
3 # This file is part of the libsigrokdecode project.
4 #
5 # Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #
20
21 import os
22 import sys
23 from shutil import copy
24 from getopt import getopt
25
26
27 def install(srcdir, dstdir):
28     worklist = []
29     for pd in os.listdir(srcdir):
30         pd_dir = srcdir + '/' + pd
31         if not os.path.isdir(pd_dir):
32             continue
33         install_list = []
34         for f in os.listdir(pd_dir):
35             pd_file = pd_dir + '/' + f
36             if not os.path.isfile(pd_file):
37                 continue
38             if f == 'config':
39                 install_list.extend(config_get_extra_install(pd_file))
40             elif f[-3:] == '.py':
41                 install_list.append(f)
42         worklist.append((pd, pd_dir, install_list))
43
44     print("Installing %d protocol decoders:" % len(worklist))
45     col = 0
46     for pd, pd_dir, install_list in worklist:
47         msg = pd + ' '
48         if (col + len(msg) > 80):
49             print()
50             col = 0
51         print(msg, end='')
52         col += len(msg)
53         pd_dst = dstdir + '/' + pd
54         try:
55             os.mkdir(pd_dst)
56         except FileExistsError:
57             pass
58         for f in install_list:
59             copy(pd_dir + '/' + f, pd_dst)
60     print()
61
62
63 def config_get_extra_install(config_file):
64     install_list = []
65     for line in open(config_file).read().split('\n'):
66         line = line.strip()
67         if len(line) == 0 or line[0] == '#':
68             continue
69         words = line.split()
70         if words[0] != 'extra-install':
71             continue
72         install_list.extend(words[1:])
73
74     return install_list
75
76
77 def usage(msg=None):
78     if msg:
79         print(msg)
80         ret = 1
81     else:
82         ret = 0
83     print("""Usage:
84     install-decoders [-i <decoder source>] -o <install path>""")
85     sys.exit(ret)
86
87
88 #
89 # main
90 #
91
92 src = 'decoders'
93 dst = None
94 try:
95     opts, args = getopt(sys.argv[1:], 'i:o:')
96     for opt, arg in opts:
97         if opt == '-i':
98             src = arg
99         elif opt == '-o':
100             dst = arg
101 except Exception as e:
102     usage(str(e))
103
104 if len(args) != 0 or dst is None:
105     usage()
106
107 install(src, dst)
108
109