3 ## This file is part of the sigrok-util project.
5 ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 from subprocess import Popen, PIPE, check_output
30 TMPL_AUTOCONF_DRIVER = "SR_DRIVER([${name}], [${short}])\n"
32 TMPL_HWMAKE_DRIVERLIB = """if HW_${upper}
33 libsigrok_la_SOURCES += \\
34 src/hardware/${short}/protocol.h \\
35 src/hardware/${short}/protocol.c \\
36 src/hardware/${short}/api.c
39 FILE_DRV_API = 'drv-api.c'
40 FILE_DRV_PROTOCOL = 'drv-protocol.c'
41 FILE_DRV_PROTOCOL_H = 'drv-protocol.h'
44 out = re.sub(r'\${([^}]+)}', lambda x: str(names[x.group(1)]), template)
49 def tmpl_file(filename):
50 template = open(TMPLDIR + '/' + filename).read()
56 tmp = tempfile.mkdtemp()
59 process = Popen("git clone --depth=1 " + LIBSR, shell=True, stderr=PIPE)
60 out, err = process.communicate()
61 if process.returncode:
62 raise Exception(err.decode())
63 gitdir = tmp + '/libsigrok/'
68 except Exception as e:
73 # add DRIVER and DRIVER2 entries to configure.ac
74 def do_autoconf(gitdir):
75 cacpath = gitdir + 'configure.ac'
76 configure_ac = open(cacpath).read()
81 for line in configure_ac.split('\n')[:-1]:
83 m = re.match(r'SR_DRIVER\(\[([^\]]+)', line)
87 if (m and m.group(1).upper() > names['name'].upper()) or m is None:
88 out += tmpl(TMPL_AUTOCONF_DRIVER)
93 raise Exception('No SR_DRIVER entries found in configure.ac')
94 open(cacpath, 'w').write(out)
97 # add HW_ entry to Makefile.am
98 def do_automake(gitdir):
99 path = gitdir + 'Makefile.am'
100 hwmake = open(path).read()
104 for line in hwmake.split('\n')[:-1]:
105 if state == 'copy' and re.match(r'if\s+HW_\w+$', line):
107 if state == 'drivers':
108 m = re.match(r'if\s+HW_(\w+)$', line)
110 drv_short = m.group(1)
111 if drv_short > names['upper']:
112 out += tmpl(TMPL_HWMAKE_DRIVERLIB)
114 elif not re.match(r'\s*libsigrok_la_SOURCES\b|\s*src/hardware/|endif\b', line):
115 print("[%s]" % line.strip())
116 # we passed the last entry
117 out += tmpl(TMPL_HWMAKE_DRIVERLIB)
121 raise Exception('No "if HW_" markers found in Makefile.am')
122 open(path, 'w').write(out)
125 def do_driverskel(gitdir):
126 drvdir = gitdir + 'src/hardware/' + names['short']
128 open(drvdir + '/api.c', 'w').write(tmpl_file(FILE_DRV_API))
129 open(drvdir + '/protocol.c', 'w').write(tmpl_file(FILE_DRV_PROTOCOL))
130 open(drvdir + '/protocol.h', 'w').write(tmpl_file(FILE_DRV_PROTOCOL_H))
133 def make_patch(gitdir):
135 command('git add src/hardware/' + names['short'])
136 cmd = 'git commit -m "%s: Initial driver skeleton." ' % names['short']
137 cmd += 'configure.ac Makefile.am src/hardware/' + names['short']
139 cmd = "git format-patch HEAD~1"
140 out, err = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate()
142 raise Exception(err.decode())
143 patch = out.decode().strip()
144 shutil.move(gitdir + '/' + patch, scriptdir + '/' + patch)
149 out, err = Popen(cmd, shell=True, stderr=PIPE).communicate()
151 raise Exception(err.decode())
154 def parse_gitconfig():
155 author = email = None
157 author = check_output(["git", "config", "user.name"]).decode().strip();
158 email = check_output(["git", "config", "user.email"]).decode().strip();
160 print("Please set your name and email in your git config")
168 scriptdir = os.getcwd()
169 if scriptdir.split('/')[-2:] != ['sigrok-util', 'source']:
170 print("Please call this script from the 'source' directory.")
173 LIBSR = 'git://sigrok.org/libsigrok'
176 if len(sys.argv) < 2:
177 print("Usage: new-driver <name>")
180 author, email = parse_gitconfig()
181 name = ' '.join(sys.argv[1:])
184 'short': re.sub('[^a-z0-9]', '-', name.lower()),
185 'lib': re.sub('[^a-z0-9]', '_', name.lower()),
186 'upper': re.sub('[^A-Z0-9]', '_', name.upper()),
187 'year': datetime.datetime.now().year,