]> sigrok.org Git - libsigrokdecode.git/blame - tools/install-decoders
srd_inst_decode(): Improve comments and log messages.
[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
135b790c 27def install(srcdir, dstdir, s):
03d6d746
BV
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)
135b790c
UH
42 if install_list:
43 worklist.append((pd, pd_dir, install_list))
03d6d746 44
135b790c 45 print("Installing %d %s:" % (len(worklist), s))
03d6d746
BV
46 col = 0
47 for pd, pd_dir, install_list in worklist:
48 msg = pd + ' '
49 if (col + len(msg) > 80):
50 print()
51 col = 0
52 print(msg, end='')
53 col += len(msg)
169f4272 54 pd_dst = os.path.join(dstdir, pd)
03d6d746
BV
55 try:
56 os.mkdir(pd_dst)
169f4272
BV
57 except OSError as e:
58 if e.errno != os.errno.EEXIST:
59 raise
60 else:
61 pass
03d6d746 62 for f in install_list:
169f4272 63 copy(os.path.join(pd_dir, f), pd_dst)
03d6d746
BV
64 print()
65
66
67def config_get_extra_install(config_file):
68 install_list = []
69 for line in open(config_file).read().split('\n'):
70 line = line.strip()
71 if len(line) == 0 or line[0] == '#':
72 continue
73 words = line.split()
74 if words[0] != 'extra-install':
75 continue
76 install_list.extend(words[1:])
77
78 return install_list
79
80
81def usage(msg=None):
82 if msg:
83 print(msg)
84 ret = 1
85 else:
86 ret = 0
87 print("""Usage:
88 install-decoders [-i <decoder source>] -o <install path>""")
89 sys.exit(ret)
90
91
92#
93# main
94#
95
96src = 'decoders'
97dst = None
98try:
99 opts, args = getopt(sys.argv[1:], 'i:o:')
100 for opt, arg in opts:
101 if opt == '-i':
102 src = arg
103 elif opt == '-o':
104 dst = arg
105except Exception as e:
106 usage(str(e))
107
108if len(args) != 0 or dst is None:
109 usage()
110
135b790c
UH
111install(src, dst, 'protocol decoders')
112install(src + '/common', dst + '/common', 'common modules')
03d6d746
BV
113
114