import os
import sys
import tempfile
-from subprocess import Popen, PIPE
+from subprocess import Popen, PIPE, check_output
import shutil
import re
import socket
import datetime
-TMPL_AUTOCONF_DRIVER = "DRIVER([${name}], [${short}])\n"
-TMPL_AUTOCONF_AM_CONDITIONAL = """\
-AM_CONDITIONAL(HW_${upper}, test x$HW_${upper} = xyes)
-if test "x$HW_${upper}" = "xyes"; then
- AC_DEFINE(HAVE_HW_${upper}, 1, [${name} support])
-fi
-
-"""
+TMPL_AUTOCONF_DRIVER = "SR_DRIVER([${name}], [${short}])\n"
TMPL_HWMAKE_DRIVERLIB = """if HW_${upper}
libsigrok_la_SOURCES += \\
src/hardware/${short}/api.c
endif
"""
-TMPL_DRIVERS_EXTERN = """\
-#ifdef HAVE_HW_${upper}
-extern SR_PRIV struct sr_dev_driver ${lib}_driver_info;
-#endif
-"""
-TMPL_DRIVERS_POINTER = """\
-#ifdef HAVE_HW_${upper}
- &${lib}_driver_info,
-#endif
-"""
FILE_DRV_API = 'drv-api.c'
FILE_DRV_PROTOCOL = 'drv-protocol.c'
FILE_DRV_PROTOCOL_H = 'drv-protocol.h'
def tmpl_file(filename):
- template = open(TMPLDIR + '/' + filename).read()
+ template = open(os.path.join(TMPLDIR, filename)).read()
return tmpl(template)
tmp = tempfile.mkdtemp()
try:
os.chdir(tmp)
- process = Popen("git clone " + LIBSR, shell=True, stderr=PIPE)
+ process = Popen(['git', 'clone', '--depth=1', LIBSR],
+ stdout=PIPE, stderr=PIPE)
out, err = process.communicate()
if process.returncode:
raise Exception(err.decode())
- gitdir = tmp + '/libsigrok/'
+ gitdir = os.path.join(tmp, 'libsigrok')
do_autoconf(gitdir)
- do_drivers(gitdir)
do_automake(gitdir)
do_driverskel(gitdir)
make_patch(gitdir)
shutil.rmtree(tmp)
-# add DRIVER and AM_CONDITIONAL/AC_DEFINE entries to configure.ac
+# add DRIVER and DRIVER2 entries to configure.ac
def do_autoconf(gitdir):
- cacpath = gitdir + 'configure.ac'
+ cacpath = os.path.join(gitdir, 'configure.ac')
configure_ac = open(cacpath).read()
out = ''
active = False
for line in configure_ac.split('\n')[:-1]:
if state == 'driver':
- m = re.match('DRIVER\(\[([^\]]+)', line)
+ m = re.match(r'SR_DRIVER\(\[([^\]]+)', line)
if m:
active = True
if active:
if (m and m.group(1).upper() > names['name'].upper()) or m is None:
out += tmpl(TMPL_AUTOCONF_DRIVER)
- state = 'automake'
- active = False
- elif state == 'automake':
- m = re.match('AM_CONDITIONAL\(HW_([^,]+)', line)
- if m:
- active = True
- else:
- submatch = re.match('(if|\s*AC_DEFINE|fi|$)', line)
- if active and submatch is None:
- # we're past the conditionals
- out += tmpl(TMPL_AUTOCONF_AM_CONDITIONAL)
- state = 'done'
- if active:
- if (m and m.group(1) > names['upper']):
- out += tmpl(TMPL_AUTOCONF_AM_CONDITIONAL)
state = 'done'
+ active = False
out += line + '\n'
if state != 'done':
- raise Exception('No DRIVER entries found in configure.ac')
+ raise Exception('No SR_DRIVER entries found in configure.ac')
open(cacpath, 'w').write(out)
-# add HAVE_HW_ extern and pointers to drivers.c
-def do_drivers(gitdir):
- path = gitdir + 'src/drivers.c'
- source = open(path).read()
- out = ''
- state = 'extern'
- first_entry = ''
- for line in source.split('\n')[:-1]:
- m = re.match('#ifdef HAVE_HW_(.*)', line)
- if m:
- if not first_entry:
- first_entry = m.group(1)
- elif m.group(1) == first_entry:
- # second time we see this, so we're past the externs
- if state != 'idle':
- # tack driver on to the end of the list
- out += tmpl(TMPL_DRIVERS_EXTERN)
- state = 'pointer'
- if state == 'extern':
- if m.group(1) > names['upper']:
- out += tmpl(TMPL_DRIVERS_EXTERN)
- state = 'idle'
- elif state == 'pointer':
- if m.group(1) > names['upper']:
- out += tmpl(TMPL_DRIVERS_POINTER)
- state = 'done'
- elif state == 'pointer' and not re.match('(\s*&|#endif|$)', line):
- # we passed the last entry
- out += tmpl(TMPL_DRIVERS_POINTER)
- state = 'done'
- out += line + '\n'
- if state != 'done':
- raise Exception('No "HAVE_HW_*" markers found in drivers.c' + state)
- open(path, 'w').write(out)
-
-
# add HW_ entry to Makefile.am
def do_automake(gitdir):
- path = gitdir + 'Makefile.am'
+ path = os.path.join(gitdir, 'Makefile.am')
hwmake = open(path).read()
out = ''
state = 'copy'
for line in hwmake.split('\n')[:-1]:
- if state == 'copy' and re.match('if HW_(.*)$', line):
+ if state == 'copy' and re.match(r'if\s+HW_\w+$', line):
state = 'drivers'
if state == 'drivers':
- m = re.match('if HW_(.*)$', line)
+ m = re.match(r'if\s+HW_(\w+)$', line)
if m:
drv_short = m.group(1)
if drv_short > names['upper']:
out += tmpl(TMPL_HWMAKE_DRIVERLIB)
state = 'done'
- elif not re.match('(libsigrok_la_SOURCES|\s*src/hardware/|endif)', line):
+ elif not re.match(r'\s*libsigrok_la_SOURCES\b|\s*src/hardware/|endif\b', line):
print("[%s]" % line.strip())
# we passed the last entry
out += tmpl(TMPL_HWMAKE_DRIVERLIB)
def do_driverskel(gitdir):
- drvdir = gitdir + 'src/hardware/' + names['short']
+ drvdir = os.path.join(gitdir, 'src', 'hardware', names['short'])
os.mkdir(drvdir)
- open(drvdir + '/api.c', 'w').write(tmpl_file(FILE_DRV_API))
- open(drvdir + '/protocol.c', 'w').write(tmpl_file(FILE_DRV_PROTOCOL))
- open(drvdir + '/protocol.h', 'w').write(tmpl_file(FILE_DRV_PROTOCOL_H))
+ open(os.path.join(drvdir, 'api.c'), 'w').write(tmpl_file(FILE_DRV_API))
+ open(os.path.join(drvdir, 'protocol.c'), 'w').write(tmpl_file(FILE_DRV_PROTOCOL))
+ open(os.path.join(drvdir, 'protocol.h'), 'w').write(tmpl_file(FILE_DRV_PROTOCOL_H))
def make_patch(gitdir):
os.chdir(gitdir)
- command('git add src/hardware/' + names['short'])
- cmd = 'git commit -m "%s: Initial driver skeleton." ' % names['short']
- cmd += 'configure.ac Makefile.am src/drivers.c src/hardware/' + names['short']
+ command(['git', 'add', os.path.join('src', 'hardware', names['short'])])
+ cmd = ['git', 'commit',
+ '-m', '%s: Initial driver skeleton.' % names['short'],
+ 'configure.ac', 'Makefile.am',
+ os.path.join('src', 'hardware', names['short'])]
command(cmd)
- cmd = "git format-patch HEAD~1"
- out, err = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate()
+ cmd = ['git', 'format-patch', 'HEAD~1']
+ out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate()
if err:
raise Exception(err.decode())
patch = out.decode().strip()
- shutil.move(gitdir + '/' + patch, scriptdir + '/' + patch)
+ shutil.move(os.path.join(gitdir, patch),
+ os.path.join(scriptdir, patch))
print(patch)
def command(cmd):
- out, err = Popen(cmd, shell=True, stderr=PIPE).communicate()
+ out, err = Popen(cmd, stderr=PIPE).communicate()
if err:
raise Exception(err.decode())
def parse_gitconfig():
+ author = email = None
try:
- author = email = None
- for line in open(os.environ['HOME'] + '/.gitconfig').readlines():
- m = re.match('\s*(\w+)\s*=\s*(.*)\s*$', line)
- if m:
- key, value = m.groups()
- if key == 'name':
- author = value
- elif key == 'email':
- email = value
- if author and email:
- break
+ author = check_output(["git", "config", "user.name"]).decode().strip();
+ email = check_output(["git", "config", "user.email"]).decode().strip();
except:
- pass
- if not author or not email:
- print("Please put your name and email in ~/.gitconfig")
+ print("Please set your name and email in your git config")
sys.exit()
-
return author, email
#
'short': re.sub('[^a-z0-9]', '-', name.lower()),
'lib': re.sub('[^a-z0-9]', '_', name.lower()),
'upper': re.sub('[^A-Z0-9]', '_', name.upper()),
- 'libupper': re.sub('[^A-Z0-9]', '', name.upper()),
'year': datetime.datetime.now().year,
'author': author,
'email': email,