]> sigrok.org Git - libsigrokdecode.git/blame - tools/install-decoders
Show backtrace when decode() aborts due to an error.
[libsigrokdecode.git] / tools / install-decoders
CommitLineData
03d6d746 1#!/usr/bin/env python3
64b354d6
UH
2##
3## This file is part of the libsigrokdecode project.
4##
5## Copyright (C) 2013 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##
03d6d746
BV
20
21import os
22import sys
23from shutil import copy
24from getopt import getopt
25
26
3ab22459
GS
27_inst_pp_col_max = 80
28_inst_pp_col = 0
29def _install_pretty_print(item):
30 """Pretty print an install item. Enforce maximum line width."""
31 global _inst_pp_col
32 if item is None:
33 _inst_pp_col = 0
34 return
35 _inst_pp_col += len(item)
36 if _inst_pp_col > _inst_pp_col_max:
37 print()
38 _inst_pp_col = len(item)
39 print(item, end = "")
40
135b790c 41def install(srcdir, dstdir, s):
03d6d746
BV
42 worklist = []
43 for pd in os.listdir(srcdir):
44 pd_dir = srcdir + '/' + pd
45 if not os.path.isdir(pd_dir):
46 continue
47 install_list = []
48 for f in os.listdir(pd_dir):
49 pd_file = pd_dir + '/' + f
50 if not os.path.isfile(pd_file):
51 continue
52 if f == 'config':
53 install_list.extend(config_get_extra_install(pd_file))
54 elif f[-3:] == '.py':
55 install_list.append(f)
135b790c
UH
56 if install_list:
57 worklist.append((pd, pd_dir, install_list))
03d6d746 58
3ab22459 59 worklist.sort()
135b790c 60 print("Installing %d %s:" % (len(worklist), s))
03d6d746 61 for pd, pd_dir, install_list in worklist:
3ab22459 62 _install_pretty_print("{} ".format(pd))
169f4272 63 pd_dst = os.path.join(dstdir, pd)
03d6d746
BV
64 try:
65 os.mkdir(pd_dst)
169f4272
BV
66 except OSError as e:
67 if e.errno != os.errno.EEXIST:
68 raise
69 else:
70 pass
03d6d746 71 for f in install_list:
169f4272 72 copy(os.path.join(pd_dir, f), pd_dst)
03d6d746 73 print()
3ab22459 74 _install_pretty_print(None)
03d6d746
BV
75
76
77def config_get_extra_install(config_file):
78 install_list = []
79 for line in open(config_file).read().split('\n'):
80 line = line.strip()
81 if len(line) == 0 or line[0] == '#':
82 continue
83 words = line.split()
84 if words[0] != 'extra-install':
85 continue
86 install_list.extend(words[1:])
87
88 return install_list
89
90
91def usage(msg=None):
92 if msg:
93 print(msg)
94 ret = 1
95 else:
96 ret = 0
97 print("""Usage:
98 install-decoders [-i <decoder source>] -o <install path>""")
99 sys.exit(ret)
100
101
102#
103# main
104#
105
106src = 'decoders'
107dst = None
108try:
109 opts, args = getopt(sys.argv[1:], 'i:o:')
110 for opt, arg in opts:
111 if opt == '-i':
112 src = arg
113 elif opt == '-o':
114 dst = arg
115except Exception as e:
116 usage(str(e))
117
118if len(args) != 0 or dst is None:
119 usage()
120
135b790c
UH
121install(src, dst, 'protocol decoders')
122install(src + '/common', dst + '/common', 'common modules')
03d6d746
BV
123
124