]>
Commit | Line | Data |
---|---|---|
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 | 20 | |
5f7742af | 21 | import errno |
03d6d746 BV |
22 | import os |
23 | import sys | |
24 | from shutil import copy | |
25 | from getopt import getopt | |
26 | ||
27 | ||
3ab22459 GS |
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 | ||
135b790c | 42 | def install(srcdir, dstdir, s): |
03d6d746 BV |
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) | |
135b790c UH |
57 | if install_list: |
58 | worklist.append((pd, pd_dir, install_list)) | |
03d6d746 | 59 | |
3ab22459 | 60 | worklist.sort() |
135b790c | 61 | print("Installing %d %s:" % (len(worklist), s)) |
03d6d746 | 62 | for pd, pd_dir, install_list in worklist: |
3ab22459 | 63 | _install_pretty_print("{} ".format(pd)) |
169f4272 | 64 | pd_dst = os.path.join(dstdir, pd) |
03d6d746 BV |
65 | try: |
66 | os.mkdir(pd_dst) | |
169f4272 | 67 | except OSError as e: |
5f7742af | 68 | if e.errno != errno.EEXIST: |
169f4272 BV |
69 | raise |
70 | else: | |
71 | pass | |
03d6d746 | 72 | for f in install_list: |
169f4272 | 73 | copy(os.path.join(pd_dir, f), pd_dst) |
03d6d746 | 74 | print() |
3ab22459 | 75 | _install_pretty_print(None) |
03d6d746 BV |
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 | ||
135b790c UH |
122 | install(src, dst, 'protocol decoders') |
123 | install(src + '/common', dst + '/common', 'common modules') | |
03d6d746 BV |
124 | |
125 |