X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=source%2Fnew-driver;h=81f40699ada296e03a5855031189744e0101445f;hb=b63f8cef412cce8ee15afd97709e6f8b3e722437;hp=5bfab9c4c678d272b581787e785c115193e25fa3;hpb=fe85502e523fade5fce46dc04be91b2aa60a7ad5;p=sigrok-util.git diff --git a/source/new-driver b/source/new-driver index 5bfab9c..81f4069 100755 --- a/source/new-driver +++ b/source/new-driver @@ -36,16 +36,6 @@ 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} - (DRVS) {&${lib}_driver_info, NULL}, -#endif -""" FILE_DRV_API = 'drv-api.c' FILE_DRV_PROTOCOL = 'drv-protocol.c' FILE_DRV_PROTOCOL_H = 'drv-protocol.h' @@ -57,7 +47,7 @@ def tmpl(template): def tmpl_file(filename): - template = open(TMPLDIR + '/' + filename).read() + template = open(os.path.join(TMPLDIR, filename)).read() return tmpl(template) @@ -66,13 +56,13 @@ def new_driver(): tmp = tempfile.mkdtemp() try: os.chdir(tmp) - process = Popen("git clone --depth=1 " + 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) @@ -83,7 +73,7 @@ def new_driver(): # 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 = '' @@ -105,50 +95,9 @@ def do_autoconf(gitdir): 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 = '' - iflevel = 0 - state = 'extern' - first_entry = '' - for line in source.split('\n')[:-1]: - m = re.match(r'\s*#\s*if(?:def)?\s+HAVE_HW_(\w+)', line) - if m: - if iflevel == 0: - 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' - iflevel += 1 - elif re.match(r'\s*#\s*endif\b', line): - iflevel -= 1 - elif iflevel == 0 and state == 'pointer': - # 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 = '' @@ -175,30 +124,33 @@ def do_automake(gitdir): 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())