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