From 7a6588a1349f5fa42adb929b55e1a811684b5d64 Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Wed, 7 Mar 2018 01:18:52 +0100 Subject: [PATCH 1/6] fixing configure.ac for python3 Signed-off-by: Rene Hopf --- src/configure.ac | 25 ++++++++++++----------- src/hal/utils/halcompile.g | 42 +++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/configure.ac b/src/configure.ac index 7081f8dd91d..0df1dae5a98 100644 --- a/src/configure.ac +++ b/src/configure.ac @@ -1387,7 +1387,7 @@ install with "sudo apt-get install tclx"]) fi AC_MSG_CHECKING([for python pango and cairo modules]) -if $PYTHON -c 'import pango,cairo'; then +if $PYTHON -c 'from gi.repository import cairo, Pango, PangoCairo'; then AC_MSG_RESULT([found]) else AC_MSG_RESULT(no) @@ -1561,21 +1561,22 @@ if test "$PYTHON" = "none"; then AC_MSG_ERROR([Python missing. Install it or specify --disable-python to skip the parts of LinuxCNC that depend on Python]) fi -AC_MSG_CHECKING([python version]) -if ! $PYTHON -c 'import sys;raise SystemExit,sys.hexversion<0x2070000' -then - AC_MSG_ERROR(Python version too old (2.7 or newer required)) -fi -AC_MSG_RESULT(OK) +#TODO: fix version check +#AC_MSG_CHECKING([python version]) +#if ! $PYTHON -c 'import sys;raise SystemExit,sys.hexversion<0x2070000' +#then +# AC_MSG_ERROR(Python version too old (2.7 or newer required)) +#fi +#AC_MSG_RESULT(OK) AC_MSG_CHECKING([version of python libraries]) -LIBPYTHON=`$PYTHON -c 'import distutils.sysconfig; print "python" + distutils.sysconfig.get_config_vars().get("VERSION")'` +LIBPYTHON=`$PYTHON -c 'import distutils.sysconfig; print("python" + (distutils.sysconfig.get_config_vars().get("LDVERSION") or distutils.sysconfig.get_config_vars().get("VERSION")))'` AC_MSG_RESULT($LIBPYTHON) AC_MSG_CHECKING([match between tk and Tkinter versions]) -PYTHON_TK_VERSION="`$PYTHON -c 'import _tkinter; print _tkinter.TK_VERSION'`" -PYTHON_TCL_VERSION="`$PYTHON -c 'import _tkinter; print _tkinter.TCL_VERSION'`" +PYTHON_TK_VERSION="`$PYTHON -c 'import _tkinter; print (_tkinter.TK_VERSION)'`" +PYTHON_TCL_VERSION="`$PYTHON -c 'import _tkinter; print (_tkinter.TCL_VERSION)'`" if test "$PYTHON_TCL_VERSION" != "$TCL_VERSION"; then AC_MSG_RESULT([TCL mismatch: $TCL_VERSION vs $PYTHON_TCL_VERSION]) @@ -1589,7 +1590,7 @@ fi AC_MSG_RESULT([$PYTHON_TK_VERSION]) AC_MSG_CHECKING(location of Python header files) -INCLUDEPY=`$PYTHON -c 'import distutils.sysconfig; print distutils.sysconfig.get_config_vars().get("INCLUDEPY")'` +INCLUDEPY=`$PYTHON -c 'import distutils.sysconfig; print (distutils.sysconfig.get_config_vars().get("INCLUDEPY"))'` AC_MSG_RESULT($INCLUDEPY) AC_MSG_CHECKING([for Python headers]) @@ -1619,7 +1620,7 @@ AC_CHECK_HEADER($INCLUDEPY/Python.h,[], [AC_MSG_ERROR([Required header Python.h missing. Install it, or specify --disable-python to skip the parts of LinuxCNC that depend on Python])]) AC_MSG_CHECKING(for site-package location) -SITEPY=`$PYTHON -c 'import distutils.sysconfig; print distutils.sysconfig.get_python_lib()'` +SITEPY=`$PYTHON -c 'import distutils.sysconfig; print (distutils.sysconfig.get_python_lib())'` AC_MSG_RESULT($SITEPY) AC_CHECK_HEADERS(GL/gl.h GL/glu.h,[],[AC_MSG_ERROR([Required OpenGL header missing. Install it, or specify --disable-python to skip the parts of LinuxCNC that depend on Python])]) diff --git a/src/hal/utils/halcompile.g b/src/hal/utils/halcompile.g index cf9e56eb979..383264cadea 100644 --- a/src/hal/utils/halcompile.g +++ b/src/hal/utils/halcompile.g @@ -121,16 +121,16 @@ def parse(filename): f = open(filename).read() if '\r' in f: if require_unix_line_endings: - raise SystemExit, "%s:0: Error: File contains DOS-style or Mac-style line endings." % filename + raise (SystemExit, "%s:0: Error: File contains DOS-style or Mac-style line endings." % filename) else: print >>sys.stderr, "%s:0: Warning: File contains DOS-style or Mac-style line endings." % filename f = open(filename, "rU").read() a, b = f.split("\n;;\n", 1) p = _parse('File', a + "\n\n", filename) - if not p: raise SystemExit, 1 + if not p: raise (SystemExit, 1) if require_license: if not finddoc('license'): - raise SystemExit, "%s:0: License not specified" % filename + raise (SystemExit, "%s:0: License not specified" % filename) return a, b dirmap = {'r': 'HAL_RO', 'rw': 'HAL_RW', 'in': 'HAL_IN', 'out': 'HAL_OUT', 'io': 'HAL_IO' } @@ -702,7 +702,7 @@ def find_modinc(): if os.path.exists(e): modinc = e return e - raise SystemExit, "Unable to locate Makefile.modinc" + raise (SystemExit, "Unable to locate Makefile.modinc") def build_usr(tempdir, filename, mode, origfilename): binname = os.path.basename(os.path.splitext(filename)[0]) @@ -717,7 +717,7 @@ def build_usr(tempdir, filename, mode, origfilename): f.close() result = os.system("cd %s && make -S %s" % (tempdir, binname)) if result != 0: - raise SystemExit, os.WEXITSTATUS(result) or 1 + raise (SystemExit, os.WEXITSTATUS(result) or 1) output = os.path.join(tempdir, binname) if mode == INSTALL: shutil.copy(output, os.path.join(BASE, "bin", binname)) @@ -739,7 +739,7 @@ def build_rt(tempdir, filename, mode, origfilename): target = "modules" result = os.system("cd %s && make -S %s" % (tempdir, target)) if result != 0: - raise SystemExit, os.WEXITSTATUS(result) or 1 + raise (SystemExit, os.WEXITSTATUS(result) or 1) if mode == COMPILE: for extension in ".ko", ".so", ".o": kobjname = os.path.splitext(filename)[0] + extension @@ -747,7 +747,7 @@ def build_rt(tempdir, filename, mode, origfilename): shutil.copy(kobjname, os.path.basename(kobjname)) break else: - raise SystemExit, "Unable to copy module from temporary directory" + raise (SystemExit, "Unable to copy module from temporary directory") def finddoc(section=None, name=None): for item in docs: @@ -955,7 +955,7 @@ def process(filename, mode, outfilename): a, b = parse(filename) base_name = os.path.splitext(os.path.basename(outfilename))[0] if comp_name != base_name: - raise SystemExit, "Component name (%s) does not match filename (%s)" % (comp_name, base_name) + raise (SystemExit, "Component name (%s) does not match filename (%s)" % (comp_name, base_name)) f = open(outfilename, "w") @@ -964,15 +964,15 @@ def process(filename, mode, outfilename): if options.get("userspace"): if functions: - raise SystemExit, "Userspace components may not have functions" + raise (SystemExit, "Userspace components may not have functions") if not pins: - raise SystemExit, "Component must have at least one pin" + raise (SystemExit, "Component must have at least one pin") prologue(f) lineno = a.count("\n") + 3 if options.get("userspace"): if functions: - raise SystemExit, "May not specify functions with a userspace component." + raise (SystemExit, "May not specify functions with a userspace component.") f.write("#line %d \"%s\"\n" % (lineno, filename)) f.write(b) else: @@ -985,7 +985,7 @@ def process(filename, mode, outfilename): f.write(b) f.write("}\n") else: - raise SystemExit, "Must use FUNCTION() when more than one function is defined" + raise (SystemExit, "Must use FUNCTION() when more than one function is defined") epilogue(f) f.close() @@ -1050,19 +1050,19 @@ def main(): require_license = True if k in ("-o", "--outfile"): if len(args) != 1: - raise SystemExit, "Cannot specify -o with multiple input files" + raise (SystemExit, "Cannot specify -o with multiple input files") outfile = v if k in ("-?", "-h", "--help"): usage(0) if outfile and mode != PREPROCESS and mode != DOCUMENT: - raise SystemExit, "Can only specify -o when preprocessing or documenting" + raise (SystemExit, "Can only specify -o when preprocessing or documenting") if mode == MODINC: if args: - raise SystemExit, \ - "Can not specify input files when using --print-modinc" - print find_modinc() + raise (SystemExit, \ + "Can not specify input files when using --print-modinc") + print (find_modinc()) return 0 for f in args: @@ -1083,7 +1083,7 @@ def main(): if not os.path.isdir(manpath): manpath = os.path.join(BASE, "docs/man/man9") outfile = os.path.join(manpath, basename + ".9") - print "INSTALLDOC", outfile + print ("INSTALLDOC", outfile) document(f, outfile) elif f.endswith(".comp"): process(f, mode, outfile) @@ -1095,7 +1095,7 @@ def main(): try: os.unlink(outfile) except os.error: pass open(outfile, "w").writelines(lines) - os.chmod(outfile, 0555) + os.chmod(outfile, 0o555) elif f.endswith(".c") and mode != PREPROCESS: initialize() tempdir = tempfile.mkdtemp() @@ -1108,14 +1108,14 @@ def main(): finally: shutil.rmtree(tempdir) else: - raise SystemExit, "Unrecognized file type for mode %s: %r" % (modename[mode], f) + raise (SystemExit, "Unrecognized file type for mode %s: %r" % (modename[mode], f)) except: ex_type, ex_value, exc_tb = sys.exc_info() try: os.unlink(outfile) except: # os.error: pass - raise ex_type, ex_value, exc_tb + raise (ex_type, ex_value, exc_tb) if __name__ == '__main__': main() From c804607349e88d1d76c3836911f37c2fceea1ba7 Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Wed, 7 Mar 2018 01:38:16 +0100 Subject: [PATCH 2/6] remove tabs from halcompile Signed-off-by: Rene Hopf --- src/hal/utils/halcompile.g | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/hal/utils/halcompile.g b/src/hal/utils/halcompile.g index 383264cadea..1511fd6c124 100644 --- a/src/hal/utils/halcompile.g +++ b/src/hal/utils/halcompile.g @@ -390,7 +390,7 @@ static int comp_id; print >>f, " inst->_personality = personality;" if options.get("extra_setup"): print >>f, " r = extra_setup(inst, prefix, extra_arg);" - print >>f, " if(r != 0) return r;" + print >>f, " if(r != 0) return r;" # the extra_setup() function may have changed the personality if has_personality: print >>f, " personality = inst->_personality;" @@ -510,8 +510,8 @@ static int comp_id; print >>f, " r = export(buf, i, personality[i%16]);" else: print >>f, " r = export(buf, i);" - print >>f, " }" - else: + print >>f, " }" + else: print >>f, " if(count && names[0]) {" print >>f, " rtapi_print_msg(RTAPI_MSG_ERR," \ "\"count= and names= are mutually exclusive\\n\");" @@ -549,7 +549,7 @@ static int comp_id; if options.get("constructable") and not options.get("singleton"): print >>f, " hal_set_constructor(comp_id, export_1);" print >>f, " if(r) {" - if options.get("extra_cleanup"): + if options.get("extra_cleanup"): print >>f, " extra_cleanup();" print >>f, " hal_exit(comp_id);" print >>f, " } else {" @@ -560,7 +560,7 @@ static int comp_id; print >>f print >>f, "void rtapi_app_exit(void) {" - if options.get("extra_cleanup"): + if options.get("extra_cleanup"): print >>f, " extra_cleanup();" print >>f, " hal_exit(comp_id);" print >>f, "}" From 5e14beb3d152b8b7277e7d5ce01dbcf6c254527a Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Wed, 7 Mar 2018 02:15:18 +0100 Subject: [PATCH 3/6] fix raise syntax and indentation Signed-off-by: Rene Hopf --- src/hal/utils/halcompile.g | 39 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/hal/utils/halcompile.g b/src/hal/utils/halcompile.g index 1511fd6c124..3d27d7c61b0 100644 --- a/src/hal/utils/halcompile.g +++ b/src/hal/utils/halcompile.g @@ -121,16 +121,16 @@ def parse(filename): f = open(filename).read() if '\r' in f: if require_unix_line_endings: - raise (SystemExit, "%s:0: Error: File contains DOS-style or Mac-style line endings." % filename) + raise SystemExit("%s:0: Error: File contains DOS-style or Mac-style line endings." % filename) else: print >>sys.stderr, "%s:0: Warning: File contains DOS-style or Mac-style line endings." % filename f = open(filename, "rU").read() a, b = f.split("\n;;\n", 1) p = _parse('File', a + "\n\n", filename) - if not p: raise (SystemExit, 1) + if not p: raise SystemExit(1) if require_license: if not finddoc('license'): - raise (SystemExit, "%s:0: License not specified" % filename) + raise SystemExit("%s:0: License not specified" % filename) return a, b dirmap = {'r': 'HAL_RO', 'rw': 'HAL_RW', 'in': 'HAL_IN', 'out': 'HAL_OUT', 'io': 'HAL_IO' } @@ -510,7 +510,7 @@ static int comp_id; print >>f, " r = export(buf, i, personality[i%16]);" else: print >>f, " r = export(buf, i);" - print >>f, " }" + print >>f, " }" else: print >>f, " if(count && names[0]) {" print >>f, " rtapi_print_msg(RTAPI_MSG_ERR," \ @@ -702,7 +702,7 @@ def find_modinc(): if os.path.exists(e): modinc = e return e - raise (SystemExit, "Unable to locate Makefile.modinc") + raise SystemExit("Unable to locate Makefile.modinc") def build_usr(tempdir, filename, mode, origfilename): binname = os.path.basename(os.path.splitext(filename)[0]) @@ -717,7 +717,7 @@ def build_usr(tempdir, filename, mode, origfilename): f.close() result = os.system("cd %s && make -S %s" % (tempdir, binname)) if result != 0: - raise (SystemExit, os.WEXITSTATUS(result) or 1) + raise SystemExit(os.WEXITSTATUS(result) or 1) output = os.path.join(tempdir, binname) if mode == INSTALL: shutil.copy(output, os.path.join(BASE, "bin", binname)) @@ -739,7 +739,7 @@ def build_rt(tempdir, filename, mode, origfilename): target = "modules" result = os.system("cd %s && make -S %s" % (tempdir, target)) if result != 0: - raise (SystemExit, os.WEXITSTATUS(result) or 1) + raise SystemExit(os.WEXITSTATUS(result) or 1) if mode == COMPILE: for extension in ".ko", ".so", ".o": kobjname = os.path.splitext(filename)[0] + extension @@ -747,7 +747,7 @@ def build_rt(tempdir, filename, mode, origfilename): shutil.copy(kobjname, os.path.basename(kobjname)) break else: - raise (SystemExit, "Unable to copy module from temporary directory") + raise SystemExit("Unable to copy module from temporary directory") def finddoc(section=None, name=None): for item in docs: @@ -955,7 +955,7 @@ def process(filename, mode, outfilename): a, b = parse(filename) base_name = os.path.splitext(os.path.basename(outfilename))[0] if comp_name != base_name: - raise (SystemExit, "Component name (%s) does not match filename (%s)" % (comp_name, base_name)) + raise SystemExit("Component name (%s) does not match filename (%s)" % (comp_name, base_name)) f = open(outfilename, "w") @@ -964,15 +964,15 @@ def process(filename, mode, outfilename): if options.get("userspace"): if functions: - raise (SystemExit, "Userspace components may not have functions") + raise SystemExit("Userspace components may not have functions") if not pins: - raise (SystemExit, "Component must have at least one pin") + raise SystemExit("Component must have at least one pin") prologue(f) lineno = a.count("\n") + 3 if options.get("userspace"): if functions: - raise (SystemExit, "May not specify functions with a userspace component.") + raise SystemExit("May not specify functions with a userspace component.") f.write("#line %d \"%s\"\n" % (lineno, filename)) f.write(b) else: @@ -985,7 +985,7 @@ def process(filename, mode, outfilename): f.write(b) f.write("}\n") else: - raise (SystemExit, "Must use FUNCTION() when more than one function is defined") + raise SystemExit("Must use FUNCTION() when more than one function is defined") epilogue(f) f.close() @@ -1009,7 +1009,7 @@ Usage: [sudo] %(name)s --install --userspace pyfile... %(name)s --print-modinc """ % {'name': os.path.basename(sys.argv[0])} - raise SystemExit, exitval + raise SystemExit(exitval) def main(): global require_license @@ -1050,18 +1050,17 @@ def main(): require_license = True if k in ("-o", "--outfile"): if len(args) != 1: - raise (SystemExit, "Cannot specify -o with multiple input files") + raise SystemExit("Cannot specify -o with multiple input files") outfile = v if k in ("-?", "-h", "--help"): usage(0) if outfile and mode != PREPROCESS and mode != DOCUMENT: - raise (SystemExit, "Can only specify -o when preprocessing or documenting") + raise SystemExit("Can only specify -o when preprocessing or documenting") if mode == MODINC: if args: - raise (SystemExit, \ - "Can not specify input files when using --print-modinc") + raise SystemExit,("Can not specify input files when using --print-modinc") print (find_modinc()) return 0 @@ -1108,14 +1107,14 @@ def main(): finally: shutil.rmtree(tempdir) else: - raise (SystemExit, "Unrecognized file type for mode %s: %r" % (modename[mode], f)) + raise SystemExit("Unrecognized file type for mode %s: %r" % (modename[mode], f)) except: ex_type, ex_value, exc_tb = sys.exc_info() try: os.unlink(outfile) except: # os.error: pass - raise (ex_type, ex_value, exc_tb) + raise ex_type(ex_value, exc_tb) if __name__ == '__main__': main() From 2c892f0639eb33cc99ac28746131cad2561a5ee4 Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Wed, 7 Mar 2018 02:33:05 +0100 Subject: [PATCH 4/6] fixed python version check in configure.ac Signed-off-by: Rene Hopf --- src/configure.ac | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/configure.ac b/src/configure.ac index 0df1dae5a98..98e6aa2817a 100644 --- a/src/configure.ac +++ b/src/configure.ac @@ -1561,13 +1561,12 @@ if test "$PYTHON" = "none"; then AC_MSG_ERROR([Python missing. Install it or specify --disable-python to skip the parts of LinuxCNC that depend on Python]) fi -#TODO: fix version check -#AC_MSG_CHECKING([python version]) -#if ! $PYTHON -c 'import sys;raise SystemExit,sys.hexversion<0x2070000' -#then -# AC_MSG_ERROR(Python version too old (2.7 or newer required)) -#fi -#AC_MSG_RESULT(OK) +AC_MSG_CHECKING([python version]) +if ! $PYTHON -c 'import sys;raise SystemExit(sys.hexversion<0x2070000)' +then + AC_MSG_ERROR(Python version too old (2.7 or newer required)) +fi +AC_MSG_RESULT(OK) AC_MSG_CHECKING([version of python libraries]) LIBPYTHON=`$PYTHON -c 'import distutils.sysconfig; print("python" + (distutils.sysconfig.get_config_vars().get("LDVERSION") or distutils.sysconfig.get_config_vars().get("VERSION")))'` From 9a7c4e00df89223a2176b65a18564b6849d2f911 Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Wed, 7 Mar 2018 06:02:48 +0100 Subject: [PATCH 5/6] elbpcom python3 fix --- src/hal/utils/elbpcom.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/hal/utils/elbpcom.py b/src/hal/utils/elbpcom.py index 139143db51f..fdd3b2cf3a1 100644 --- a/src/hal/utils/elbpcom.py +++ b/src/hal/utils/elbpcom.py @@ -64,11 +64,11 @@ def transact(sdata, quiet=False, response=True): if not response: return try: data, daddr = s.recvfrom(1280) - if not quiet: print "<", data.encode("hex") - if not quiet: print " ", re.sub('[^ -~]', '.', data) + if not quiet: print ("<", data.encode("hex")) + if not quiet: print (" ", re.sub('[^ -~]', '.', data)) return data except socket.timeout: - if not quiet: print "! no response" + if not quiet: print ("! no response") return None def interact(): @@ -96,35 +96,35 @@ def optimal_size(space, info, address, nbytes): if info: return 2 info = transact(make_read_request(space, True, 2, True, 2, 4).encode("hex"), quiet=True) if info is None: - raise RuntimeError, "Failed to get information about memory space %d" % space + raise RuntimeError("Failed to get information about memory space %d" % space) memsizes, memranges = struct.unpack("= maxaddr: raise ValueError, "Address out of range (address=%d nbytes=%d maxaddr=%d)" % (address, nbytes, maxaddr) + #if address + nbytes >= maxaddr: raise ValueError("Address out of range (address=%d nbytes=%d maxaddr=%d)" % (address, nbytes, maxaddr)) for i in (3,2,1,0): b = (1<", command + print (">", command) transact(command) elif options.write: - if options.space is None: raise SystemExit, "--write must specifiy --space" - if options.address is None: raise SystemExit, "--write must specifiy --address" + if options.space is None: raise SystemExit("--write must specifiy --space") + if options.address is None: raise SystemExit("--write must specifiy --address") write = options.write.decode("hex") size = optimal_size(options.space, options.info, options.address, len(write) if options.increment else 0) command = make_write_request(options.space, options.info, size, options.increment, options.address, write) command = command.encode("hex") - print ">", command + print (">", command) transact(command, response=False) elif args: From 52ab8bd862cb105ee4d9769d014226b0784c0225 Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Wed, 7 Mar 2018 06:11:28 +0100 Subject: [PATCH 6/6] halcompile python3 fixes --- src/hal/utils/halcompile.g | 613 ++++++++++++++++++------------------- 1 file changed, 306 insertions(+), 307 deletions(-) diff --git a/src/hal/utils/halcompile.g b/src/hal/utils/halcompile.g index 3d27d7c61b0..217f52f9508 100644 --- a/src/hal/utils/halcompile.g +++ b/src/hal/utils/halcompile.g @@ -16,7 +16,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import os, sys, tempfile, shutil, getopt, time +import os, sys, tempfile, shutil, getopt, time, traceback BASE = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "..")) sys.path.insert(0, os.path.join(BASE, "lib", "python")) @@ -123,7 +123,7 @@ def parse(filename): if require_unix_line_endings: raise SystemExit("%s:0: Error: File contains DOS-style or Mac-style line endings." % filename) else: - print >>sys.stderr, "%s:0: Warning: File contains DOS-style or Mac-style line endings." % filename + sys.stderr.write("%s:0: Warning: File contains DOS-style or Mac-style line endings." % filename) f = open(filename, "rU").read() a, b = f.split("\n;;\n", 1) p = _parse('File', a + "\n\n", filename) @@ -151,7 +151,7 @@ def initialize(): def Warn(msg, *args): if args: msg = msg % args - print >>sys.stderr, "%s:%d: Warning: %s" % (S.filename, S.line, msg) + sys.stderr.write("%s:%d: Warning: %s" % (S.filename, S.line, msg)) def Error(msg, *args): if args: @@ -251,9 +251,8 @@ def to_c(name): return re.sub("_+", "_", name) def prologue(f): - print >> f, "/* Autogenerated by %s on %s -- do not edit */" % ( - sys.argv[0], time.asctime()) - print >> f, """\ + f.write("/* Autogenerated by %s on %s -- do not edit */\n" % (sys.argv[0],time.asctime())) + f.write("""\ #include "rtapi.h" #ifdef RTAPI #include "rtapi_app.h" @@ -264,9 +263,9 @@ def prologue(f): #include "rtapi_math64.h" static int comp_id; -""" +""") for name in includes: - print >>f, "#include %s" % name + f.write("#include %s\n" % name) names = {} @@ -279,16 +278,16 @@ static int comp_id; s = s.replace("\v", "\\v") return '"%s"' % s - print >>f, "#ifdef MODULE_INFO" + f.write("#ifdef MODULE_INFO\n") for v in docs: if not v: continue v = ":".join(map(str, v)) - print >>f, "MODULE_INFO(linuxcnc, %s);" % q(v) + f.write("MODULE_INFO(linuxcnc, %s);\n" % q(v)) license = finddoc('license') if license and license[1]: - print >>f, "MODULE_LICENSE(\"%s\");" % license[1].split("\n")[0] - print >>f, "#endif // MODULE_INFO" - print >>f + f.write("MODULE_LICENSE(\"%s\");\n" % license[1].split("\n")[0]) + f.write("#endif // MODULE_INFO\n") + f.write("\n") has_data = options.get("data") @@ -306,271 +305,271 @@ static int comp_id; for type, name, default, doc in modparams: decl = mp_decl_map[type] if decl: - print >>f, "%s %s" % (type, name), - if default: print >>f, "= %s;" % default - else: print >>f, ";" - print >>f, "%s(%s, %s);" % (decl, name, q(doc)) + f.write("%s %s\n" % (type, name)) + if default: f.write("= %s;\n" % default) + else: f.write(";\n") + f.write("%s(%s, %s);\n" % (decl, name, q(doc))) - print >>f - print >>f, "struct __comp_state {" - print >>f, " struct __comp_state *_next;" + f.write("\n") + f.write("struct __comp_state {\n") + f.write(" struct __comp_state *_next;\n") if has_personality: - print >>f, " int _personality;" + f.write(" int _personality;\n") for name, type, array, dir, value, personality in pins: if array: if isinstance(array, tuple): array = array[0] - print >>f, " hal_%s_t *%s[%s];" % (type, to_c(name), array) + f.write(" hal_%s_t *%s[%s];\n" % (type, to_c(name), array)) else: - print >>f, " hal_%s_t *%s;" % (type, to_c(name)) + f.write(" hal_%s_t *%s;\n" % (type, to_c(name))) names[name] = 1 for name, type, array, dir, value, personality in params: if array: if isinstance(array, tuple): array = array[0] - print >>f, " hal_%s_t %s[%s];" % (type, to_c(name), array) + f.write(" hal_%s_t %s[%s];\n" % (type, to_c(name), array)) else: - print >>f, " hal_%s_t %s;" % (type, to_c(name)) + f.write(" hal_%s_t %s;\n" % (type, to_c(name))) names[name] = 1 for type, name, array, value in variables: if array: - print >>f, " %s %s[%d];\n" % (type, name, array) + f.write(" %s %s[%d];\n" % (type, name, array)) else: - print >>f, " %s %s;\n" % (type, name) + f.write(" %s %s;\n" % (type, name)) if has_data: - print >>f, " void *_data;" + f.write(" void *_data;\n") - print >>f, "};" + f.write("};\n") if options.get("userspace"): - print >>f, "#include " + f.write("#include \n") - print >>f, "struct __comp_state *__comp_first_inst=0, *__comp_last_inst=0;" + f.write("struct __comp_state *__comp_first_inst=0, *__comp_last_inst=0;\n") - print >>f + f.write("\n") for name, fp in functions: if names.has_key(name): Error("Duplicate item name: %s" % name) - print >>f, "static void %s(struct __comp_state *__comp_inst, long period);" % to_c(name) + f.write("static void %s(struct __comp_state *__comp_inst, long period);\n" % to_c(name)) names[name] = 1 - print >>f, "static int __comp_get_data_size(void);" + f.write("static int __comp_get_data_size(void);\n") if options.get("extra_setup"): - print >>f, "static int extra_setup(struct __comp_state *__comp_inst, char *prefix, long extra_arg);" + f.write("static int extra_setup(struct __comp_state *__comp_inst, char *prefix, long extra_arg);\n") if options.get("extra_cleanup"): - print >>f, "static void extra_cleanup(void);" + f.write("static void extra_cleanup(void);\n") if not options.get("no_convenience_defines"): - print >>f, "#undef TRUE" - print >>f, "#define TRUE (1)" - print >>f, "#undef FALSE" - print >>f, "#define FALSE (0)" - print >>f, "#undef true" - print >>f, "#define true (1)" - print >>f, "#undef false" - print >>f, "#define false (0)" - - print >>f + f.write("#undef TRUE\n") + f.write("#define TRUE (1)\n") + f.write("#undef FALSE\n") + f.write("#define FALSE (0)\n") + f.write("#undef true\n") + f.write("#define true (1)\n") + f.write("#undef false\n") + f.write("#define false (0)\n") + + f.write("\n") if has_personality: - print >>f, "static int export(char *prefix, long extra_arg, long personality) {" + f.write("static int export(char *prefix, long extra_arg, long personality) {\n") else: - print >>f, "static int export(char *prefix, long extra_arg) {" + f.write("static int export(char *prefix, long extra_arg) {\n") if len(functions) > 0: - print >>f, " char buf[HAL_NAME_LEN + 1];" - print >>f, " int r = 0;" + f.write(" char buf[HAL_NAME_LEN + 1];\n") + f.write(" int r = 0;\n") if has_array: - print >>f, " int j = 0;" - print >>f, " int sz = sizeof(struct __comp_state) + __comp_get_data_size();" - print >>f, " struct __comp_state *inst = hal_malloc(sz);" - print >>f, " memset(inst, 0, sz);" + f.write(" int j = 0;\n") + f.write(" int sz = sizeof(struct __comp_state) + __comp_get_data_size();\n") + f.write(" struct __comp_state *inst = hal_malloc(sz);\n") + f.write(" memset(inst, 0, sz);\n") if has_data: - print >>f, " inst->_data = (char*)inst + sizeof(struct __comp_state);" + f.write(" inst->_data = (char*)inst + sizeof(struct __comp_state);\n") if has_personality: - print >>f, " inst->_personality = personality;" + f.write(" inst->_personality = personality;\n") if options.get("extra_setup"): - print >>f, " r = extra_setup(inst, prefix, extra_arg);" - print >>f, " if(r != 0) return r;" + f.write(" r = extra_setup(inst, prefix, extra_arg);\n") + f.write(" if(r != 0) return r;\n") # the extra_setup() function may have changed the personality if has_personality: - print >>f, " personality = inst->_personality;" + f.write(" personality = inst->_personality;\n") for name, type, array, dir, value, personality in pins: if personality: - print >>f, "if(%s) {" % personality + f.write("if(%s) {\n" % personality) if array: if isinstance(array, tuple): array = array[1] - print >>f, " for(j=0; j < (%s); j++) {" % array - print >>f, " r = hal_pin_%s_newf(%s, &(inst->%s[j]), comp_id," % ( - type, dirmap[dir], to_c(name)) - print >>f, " \"%%s%s\", prefix, j);" % to_hal("." + name) - print >>f, " if(r != 0) return r;" + f.write(" for(j=0; j < (%s); j++) {\n" % array) + f.write(" r = hal_pin_%s_newf(%s, &(inst->%s[j]), comp_id," % ( + type, dirmap[dir], to_c(name))) + f.write(" \"%%s%s\", prefix, j);\n" % to_hal("." + name)) + f.write(" if(r != 0) return r;\n") if value is not None: - print >>f, " *(inst->%s[j]) = %s;" % (to_c(name), value) - print >>f, " }" + f.write(" *(inst->%s[j]) = %s;\n" % (to_c(name), value)) + f.write(" }\n") else: - print >>f, " r = hal_pin_%s_newf(%s, &(inst->%s), comp_id," % ( - type, dirmap[dir], to_c(name)) - print >>f, " \"%%s%s\", prefix);" % to_hal("." + name) - print >>f, " if(r != 0) return r;" + f.write(" r = hal_pin_%s_newf(%s, &(inst->%s), comp_id," % ( + type, dirmap[dir], to_c(name))) + f.write(" \"%%s%s\", prefix);\n" % to_hal("." + name)) + f.write(" if(r != 0) return r;\n") if value is not None: - print >>f, " *(inst->%s) = %s;" % (to_c(name), value) + f.write(" *(inst->%s) = %s;\n" % (to_c(name), value)) if personality: - print >>f, "}" + f.write("}\n") for name, type, array, dir, value, personality in params: if personality: - print >>f, "if(%s) {" % personality + f.write("if(%s) {\n" % personality) if array: if isinstance(array, tuple): array = array[1] - print >>f, " for(j=0; j < %s; j++) {" % array - print >>f, " r = hal_param_%s_newf(%s, &(inst->%s[j]), comp_id," % ( - type, dirmap[dir], to_c(name)) - print >>f, " \"%%s%s\", prefix, j);" % to_hal("." + name) - print >>f, " if(r != 0) return r;" + f.write(" for(j=0; j < %s; j++) {\n" % array) + f.write(" r = hal_param_%s_newf(%s, &(inst->%s[j]), comp_id," % ( + type, dirmap[dir], to_c(name))) + f.write(" \"%%s%s\", prefix, j);\n" % to_hal("." + name)) + f.write(" if(r != 0) return r;\n") if value is not None: - print >>f, " inst->%s[j] = %s;" % (to_c(name), value) - print >>f, " }" + f.write(" inst->%s[j] = %s;\n" % (to_c(name), value)) + f.write(" }\n") else: - print >>f, " r = hal_param_%s_newf(%s, &(inst->%s), comp_id," % ( - type, dirmap[dir], to_c(name)) - print >>f, " \"%%s%s\", prefix);" % to_hal("." + name) + f.write(" r = hal_param_%s_newf(%s, &(inst->%s), comp_id," % ( + type, dirmap[dir], to_c(name))) + f.write(" \"%%s%s\", prefix);\n" % to_hal("." + name)) if value is not None: - print >>f, " inst->%s = %s;" % (to_c(name), value) - print >>f, " if(r != 0) return r;" + f.write(" inst->%s = %s;\n" % (to_c(name), value)) + f.write(" if(r != 0) return r;\n") if personality: - print >>f, "}" + f.write("}\n") for type, name, array, value in variables: if value is None: continue if array: - print >>f, " for(j=0; j < %s; j++) {" % array - print >>f, " inst->%s[j] = %s;" % (name, value) - print >>f, " }" + f.write(" for(j=0; j < %s; j++) {\n" % array) + f.write(" inst->%s[j] = %s;\n" % (name, value)) + f.write(" }\n") else: - print >>f, " inst->%s = %s;" % (name, value) + f.write(" inst->%s = %s;\n" % (name, value)) for name, fp in functions: - print >>f, " rtapi_snprintf(buf, sizeof(buf), \"%%s%s\", prefix);"\ - % to_hal("." + name) - print >>f, " r = hal_export_funct(buf, (void(*)(void *inst, long))%s, inst, %s, 0, comp_id);" % ( - to_c(name), int(fp)) - print >>f, " if(r != 0) return r;" - print >>f, " if(__comp_last_inst) __comp_last_inst->_next = inst;" - print >>f, " __comp_last_inst = inst;" - print >>f, " if(!__comp_first_inst) __comp_first_inst = inst;" - print >>f, " return 0;" - print >>f, "}" + f.write(" rtapi_snprintf(buf, sizeof(buf), \"%%s%s\", prefix);\n"\ + % to_hal("." + name)) + f.write(" r = hal_export_funct(buf, (void(*)(void *inst, long))%s, inst, %s, 0, comp_id);\n" % ( + to_c(name), int(fp))) + f.write(" if(r != 0) return r;\n") + f.write(" if(__comp_last_inst) __comp_last_inst->_next = inst;\n") + f.write(" __comp_last_inst = inst;\n") + f.write(" if(!__comp_first_inst) __comp_first_inst = inst;\n") + f.write(" return 0;\n") + f.write("}\n") if options.get("count_function"): - print >>f, "static int get_count(void);" + f.write("static int get_count(void);\n") if options.get("rtapi_app", 1): if options.get("constructable") and not options.get("singleton"): - print >>f, "static int export_1(char *prefix, char *argstr) {" - print >>f, " int arg = simple_strtol(argstr, NULL, 0);" - print >>f, " return export(prefix, arg);" - print >>f, "}" + f.write("static int export_1(char *prefix, char *argstr) {\n") + f.write(" int arg = simple_strtol(argstr, NULL, 0);\n") + f.write(" return export(prefix, arg);\n") + f.write("}\n") if not options.get("singleton") and not options.get("count_function") : - print >>f, "static int default_count=%s, count=0;" \ - % options.get("default_count", 1) - print >>f, "char *names[16] = {0,};" + f.write("static int default_count=%s, count=0;\n" \ + % options.get("default_count", 1)) + f.write("char *names[16] = {0,};\n") if not options.get("userspace"): - print >>f, "RTAPI_MP_INT(count, \"number of %s\");" % comp_name - print >>f, "RTAPI_MP_ARRAY_STRING(names, 16, \"names of %s\");" % comp_name + f.write("RTAPI_MP_INT(count, \"number of %s\");\n" % comp_name) + f.write("RTAPI_MP_ARRAY_STRING(names, 16, \"names of %s\");\n" % comp_name) if has_personality: init1 = str(int(options.get('default_personality', 0))) init = ",".join([init1] * 16) - print >>f, "static int personality[16] = {%s};" % init - print >>f, "RTAPI_MP_ARRAY_INT(personality, 16, \"personality of each %s\");" % comp_name - print >>f, "int rtapi_app_main(void) {" - print >>f, " int r = 0;" + f.write("static int personality[16] = {%s};\n" % init) + f.write("RTAPI_MP_ARRAY_INT(personality, 16, \"personality of each %s\");\n" % comp_name) + f.write("int rtapi_app_main(void) {\n") + f.write(" int r = 0;\n") if not options.get("singleton"): - print >>f, " int i;" + f.write(" int i;\n") if options.get("count_function"): - print >>f, " int count = get_count();" + f.write(" int count = get_count();\n") - print >>f, " comp_id = hal_init(\"%s\");" % comp_name - print >>f, " if(comp_id < 0) return comp_id;" + f.write(" comp_id = hal_init(\"%s\");\n" % comp_name) + f.write(" if(comp_id < 0) return comp_id;\n") if options.get("singleton"): if has_personality: - print >>f, " r = export(\"%s\", 0, personality[0]);" % \ - to_hal(removeprefix(comp_name, "hal_")) + f.write(" r = export(\"%s\", 0, personality[0]);\n" % \ + to_hal(removeprefix(comp_name, "hal_"))) else: - print >>f, " r = export(\"%s\", 0);" % \ - to_hal(removeprefix(comp_name, "hal_")) + f.write(" r = export(\"%s\", 0);\n" % \ + to_hal(removeprefix(comp_name, "hal_"))) elif options.get("count_function"): - print >>f, " for(i=0; i>f, " char buf[HAL_NAME_LEN + 1];" - print >>f, " rtapi_snprintf(buf, sizeof(buf), " \ - "\"%s.%%d\", i);" % \ - to_hal(removeprefix(comp_name, "hal_")) + f.write(" for(i=0; i>f, " r = export(buf, i, personality[i%16]);" + f.write(" r = export(buf, i, personality[i%16]);\n") else: - print >>f, " r = export(buf, i);" - print >>f, " }" + f.write(" r = export(buf, i);\n") + f.write(" }\n") else: - print >>f, " if(count && names[0]) {" - print >>f, " rtapi_print_msg(RTAPI_MSG_ERR," \ - "\"count= and names= are mutually exclusive\\n\");" - print >>f, " return -EINVAL;" - print >>f, " }" - print >>f, " if(!count && !names[0]) count = default_count;" - print >>f, " if(count) {" - print >>f, " for(i=0; i>f, " char buf[HAL_NAME_LEN + 1];" - print >>f, " rtapi_snprintf(buf, sizeof(buf), " \ - "\"%s.%%d\", i);" % \ - to_hal(removeprefix(comp_name, "hal_")) + f.write(" if(count && names[0]) {\n") + f.write(" rtapi_print_msg(RTAPI_MSG_ERR," \ + "\"count= and names= are mutually exclusive\\n\");\n") + f.write(" return -EINVAL;\n") + f.write(" }\n") + f.write(" if(!count && !names[0]) count = default_count;\n") + f.write(" if(count) {\n") + f.write(" for(i=0; i>f, " r = export(buf, i, personality[i%16]);" + f.write(" r = export(buf, i, personality[i%16]);\n") else: - print >>f, " r = export(buf, i);" - print >>f, " if(r != 0) break;" - print >>f, " }" - print >>f, " } else {" - print >>f, " int max_names = sizeof(names)/sizeof(names[0]);" - print >>f, " for(i=0; (i < max_names) && names[i]; i++) {" - print >>f, " if (strlen(names[i]) < 1) {"; - print >>f, " rtapi_print_msg(RTAPI_MSG_ERR, \"names[%d] is invalid (empty string)\\n\", i);" - print >>f, " r = -EINVAL;" - print >>f, " break;" - print >>f, " }"; + f.write(" r = export(buf, i);\n") + f.write(" if(r != 0) break;\n") + f.write(" }\n") + f.write(" } else {\n") + f.write(" int max_names = sizeof(names)/sizeof(names[0]);\n") + f.write(" for(i=0; (i < max_names) && names[i]; i++) {\n") + f.write(" if (strlen(names[i]) < 1) {\n") + f.write(" rtapi_print_msg(RTAPI_MSG_ERR, \"names[%d] is invalid (empty string)\\n\", i);\n") + f.write(" r = -EINVAL;\n") + f.write(" break;\n") + f.write(" }\n") if has_personality: - print >>f, " r = export(names[i], i, personality[i%16]);" + f.write(" r = export(names[i], i, personality[i%16]);\n") else: - print >>f, " r = export(names[i], i);" - print >>f, " if(r != 0) break;" - print >>f, " }" - print >>f, " }" + f.write(" r = export(names[i], i);\n") + f.write(" if(r != 0) break;\n") + f.write(" }\n") + f.write(" }\n") if options.get("constructable") and not options.get("singleton"): - print >>f, " hal_set_constructor(comp_id, export_1);" - print >>f, " if(r) {" + f.write(" hal_set_constructor(comp_id, export_1);\n") + f.write(" if(r) {\n") if options.get("extra_cleanup"): - print >>f, " extra_cleanup();" - print >>f, " hal_exit(comp_id);" - print >>f, " } else {" - print >>f, " hal_ready(comp_id);" - print >>f, " }" - print >>f, " return r;" - print >>f, "}" - - print >>f - print >>f, "void rtapi_app_exit(void) {" + f.write(" extra_cleanup();\n") + f.write(" hal_exit(comp_id);\n") + f.write(" } else {\n") + f.write(" hal_ready(comp_id);\n") + f.write(" }\n") + f.write(" return r;\n") + f.write("}\n") + + f.write("\n") + f.write("void rtapi_app_exit(void) {\n") if options.get("extra_cleanup"): - print >>f, " extra_cleanup();" - print >>f, " hal_exit(comp_id);" - print >>f, "}" + f.write(" extra_cleanup();\n") + f.write(" hal_exit(comp_id);\n") + f.write("}\n") if options.get("userspace"): - print >>f, "static void user_mainloop(void);" + f.write("static void user_mainloop(void);\n") if options.get("userinit"): - print >>f, "static void userinit(int argc, char **argv);" + f.write("static void userinit(int argc, char **argv);\n") - print >>f, """ + f.write(""" int __comp_parse_count(int *argc, char **argv) { int i; for (i = 0; i < *argc; i ++) { @@ -589,8 +588,8 @@ int __comp_parse_count(int *argc, char **argv) { } return 0; } -""" - print >>f, """ +""") + f.write(""" int __comp_parse_names(int *argc, char **argv) { int i; for (i = 0; i < *argc; i ++) { @@ -614,80 +613,80 @@ int __comp_parse_names(int *argc, char **argv) { } return 0; } -""" - print >>f, "int argc=0; char **argv=0;" - print >>f, "int main(int argc_, char **argv_) {" - print >>f, " argc = argc_; argv = argv_;" - print >>f, " int found_count, found_names;" - print >>f, " found_count = __comp_parse_count(&argc, argv);" - print >>f, " found_names = __comp_parse_names(&argc, argv);" - print >>f, " if (found_count && found_names) {" - print >>f, " rtapi_print_msg(RTAPI_MSG_ERR, \"count= and names= are mutually exclusive\\n\");" - print >>f, " return 1;" - print >>f, " }" +""") + f.write("int argc=0; char **argv=0;\n") + f.write("int main(int argc_, char **argv_) {\n" ) + f.write(" argc = argc_; argv = argv_;\n") + f.write(" int found_count, found_names;\n") + f.write(" found_count = __comp_parse_count(&argc, argv);\n") + f.write(" found_names = __comp_parse_names(&argc, argv);\n") + f.write(" if (found_count && found_names) {\n") + f.write(" rtapi_print_msg(RTAPI_MSG_ERR, \"count= and names= are mutually exclusive\\n\");\n") + f.write(" return 1;\n") + f.write(" }\n") if options.get("userinit", 0): - print >>f, " userinit(argc, argv);" - print >>f - print >>f, " if(rtapi_app_main() < 0) return 1;" - print >>f, " user_mainloop();" - print >>f, " rtapi_app_exit();" - print >>f, " return 0;" - print >>f, "}" - - print >>f - if not options.get("no_convenience_defines"): - print >>f, "#undef FUNCTION" - print >>f, "#define FUNCTION(name) static void name(struct __comp_state *__comp_inst, long period)" - print >>f, "#undef EXTRA_SETUP" - print >>f, "#define EXTRA_SETUP() static int extra_setup(struct __comp_state *__comp_inst, char *prefix, long extra_arg)" - print >>f, "#undef EXTRA_CLEANUP" - print >>f, "#define EXTRA_CLEANUP() static void extra_cleanup(void)" - print >>f, "#undef fperiod" - print >>f, "#define fperiod (period * 1e-9)" + f.write(" userinit(argc, argv);\n") + f.write("\n") + f.write(" if(rtapi_app_main() < 0) return 1;\n") + f.write(" user_mainloop();\n") + f.write(" rtapi_app_exit();\n") + f.write(" return 0;\n") + f.write("}\n") + + f.write("\n") + if not options.get("no_convenience_defines\n"): + f.write("#undef FUNCTION\n") + f.write("#define FUNCTION(name) static void name(struct __comp_state *__comp_inst, long period)\n") + f.write("#undef EXTRA_SETUP\n") + f.write("#define EXTRA_SETUP() static int extra_setup(struct __comp_state *__comp_inst, char *prefix, long extra_arg)\n") + f.write("#undef EXTRA_CLEANUP\n") + f.write("#define EXTRA_CLEANUP() static void extra_cleanup(void)\n") + f.write("#undef fperiod\n") + f.write("#define fperiod (period * 1e-9)\n") for name, type, array, dir, value, personality in pins: - print >>f, "#undef %s" % to_c(name) + f.write("#undef %s\n" % to_c(name)) if array: if dir == 'in': - print >>f, "#define %s(i) (0+*(__comp_inst->%s[i]))" % (to_c(name), to_c(name)) + f.write("#define %s(i) (0+*(__comp_inst->%s[i]))\n" % (to_c(name), to_c(name))) else: - print >>f, "#define %s(i) (*(__comp_inst->%s[i]))" % (to_c(name), to_c(name)) + f.write("#define %s(i) (*(__comp_inst->%s[i]))\n" % (to_c(name), to_c(name))) else: if dir == 'in': - print >>f, "#define %s (0+*__comp_inst->%s)" % (to_c(name), to_c(name)) + f.write("#define %s (0+*__comp_inst->%s)\n" % (to_c(name), to_c(name))) else: - print >>f, "#define %s (*__comp_inst->%s)" % (to_c(name), to_c(name)) + f.write("#define %s (*__comp_inst->%s)\n" % (to_c(name), to_c(name))) for name, type, array, dir, value, personality in params: - print >>f, "#undef %s" % to_c(name) + f.write("#undef %s\n" % to_c(name)) if array: - print >>f, "#define %s(i) (__comp_inst->%s[i])" % (to_c(name), to_c(name)) + f.write("#define %s(i) (__comp_inst->%s[i])\n" % (to_c(name), to_c(name))) else: - print >>f, "#define %s (__comp_inst->%s)" % (to_c(name), to_c(name)) + f.write("#define %s (__comp_inst->%s)\n" % (to_c(name), to_c(name))) for type, name, array, value in variables: name = name.replace("*", "") - print >>f, "#undef %s" % name - print >>f, "#define %s (__comp_inst->%s)" % (name, name) + f.write("#undef %s\n" % name) + f.write("#define %s (__comp_inst->%s)\n" % (name, name)) if has_data: - print >>f, "#undef data" - print >>f, "#define data (*(%s*)(__comp_inst->_data))" % options['data'] + f.write("#undef data\n") + f.write("#define data (*(%s*)(__comp_inst->_data))\n" % options['data']) if has_personality: - print >>f, "#undef personality" - print >>f, "#define personality (__comp_inst->_personality)" + f.write("#undef personality\n") + f.write("#define personality (__comp_inst->_personality)\n") if options.get("userspace"): - print >>f, "#undef FOR_ALL_INSTS" - print >>f, "#define FOR_ALL_INSTS() struct __comp_state *__comp_inst; for(__comp_inst = __comp_first_inst; __comp_inst; __comp_inst = __comp_inst->_next)" - print >>f - print >>f + f.write("#undef FOR_ALL_INSTS\n") + f.write("#define FOR_ALL_INSTS() struct __comp_state *__comp_inst; for(__comp_inst = __comp_first_inst; __comp_inst; __comp_inst = __comp_inst->_next)\n") + f.write("\n") + f.write("\n") def epilogue(f): data = options.get('data') - print >>f + f.write("\n") if data: - print >>f, "static int __comp_get_data_size(void) { return sizeof(%s); }" % data + f.write("static int __comp_get_data_size(void) { return sizeof(%s); }\n" % data) else: - print >>f, "static int __comp_get_data_size(void) { return 0; }" + f.write("static int __comp_get_data_size(void) { return 0; }\n") INSTALL, COMPILE, PREPROCESS, DOCUMENT, INSTALLDOC, VIEWDOC, MODINC = range(7) modename = ("install", "compile", "preprocess", "document", "installdoc", "viewdoc", "print-modinc") @@ -709,11 +708,11 @@ def build_usr(tempdir, filename, mode, origfilename): makefile = os.path.join(tempdir, "Makefile") f = open(makefile, "w") - print >>f, "%s: %s" % (binname, filename) - print >>f, "\t$(CC) $(EXTRA_CFLAGS) -URTAPI -U__MODULE__ -DULAPI -Os %s -o $@ $< -Wl,-rpath,$(LIBDIR) -L$(LIBDIR) -llinuxcnchal %s" % ( + f.write("%s: %s" % (binname, filename)) + f.write("\t$(CC) $(EXTRA_CFLAGS) -URTAPI -U__MODULE__ -DULAPI -Os %s -o $@ $< -Wl,-rpath,$(LIBDIR) -L$(LIBDIR) -llinuxcnchal %s" % ( options.get("extra_compile_args", ""), - options.get("extra_link_args", "")) - print >>f, "include %s" % find_modinc() + options.get("extra_link_args", ""))) + f.write("include %s" % find_modinc()) f.close() result = os.system("cd %s && make -S %s" % (tempdir, binname)) if result != 0: @@ -728,10 +727,10 @@ def build_rt(tempdir, filename, mode, origfilename): objname = os.path.basename(os.path.splitext(filename)[0] + ".o") makefile = os.path.join(tempdir, "Makefile") f = open(makefile, "w") - print >>f, "obj-m += %s" % objname - print >>f, "include %s" % find_modinc() - print >>f, "EXTRA_CFLAGS += -I%s" % os.path.abspath(os.path.dirname(origfilename)) - print >>f, "EXTRA_CFLAGS += -I%s" % os.path.abspath('.') + f.write("obj-m += %s" % objname) + f.write("include %s" % find_modinc()) + f.write("EXTRA_CFLAGS += -I%s" % os.path.abspath(os.path.dirname(origfilename))) + f.write("EXTRA_CFLAGS += -I%s" % os.path.abspath('.')) f.close() if mode == INSTALL: target = "modules install" @@ -798,11 +797,11 @@ def document(filename, outfilename): if personality: has_personality = True if isinstance(array, tuple): has_personality = True - print >>f, ".TH %s \"9\" \"%s\" \"LinuxCNC Documentation\" \"HAL Component\"" % ( - comp_name.upper(), time.strftime("%F")) - print >>f, ".de TQ\n.br\n.ns\n.TP \\\\$1\n..\n" + f.write(".TH %s \"9\" \"%s\" \"LinuxCNC Documentation\" \"HAL Component\"\n" % ( + comp_name.upper(), time.strftime("%F"))) + f.write(".de TQ\n.br\n.ns\n.TP \\\\$1\n..\n\n") - print >>f, ".SH NAME\n" + f.write(".SH NAME\n") doc = finddoc('component') if doc and doc[2]: if '\n' in doc[2]: @@ -810,137 +809,137 @@ def document(filename, outfilename): else: firstline = doc[2] rest = '' - print >>f, "%s \\- %s" % (doc[1], firstline) + f.write("%s \\- %s\n" % (doc[1], firstline)) else: rest = '' - print >>f, "%s" % doc[1] + f.write("%s\n" % doc[1]) - print >>f, ".SH SYNOPSIS" + f.write(".SH SYNOPSIS\n") if options.get("userspace"): - print >>f, ".B %s" % comp_name + f.write(".B %s\n" % comp_name) else: if rest: - print >>f, rest + f.write(rest) else: - print >>f, ".HP" + f.write(".HP\n") if options.get("singleton") or options.get("count_function"): if has_personality: - print >>f, ".B loadrt %s personality=\\fIP\\fB" % comp_name, + f.write(".B loadrt %s personality=\\fIP\\fB\n" % comp_name) else: - print >>f, ".B loadrt %s" % comp_name, + f.write(".B loadrt %s\n" % comp_name) else: if has_personality: - print >>f, ".B loadrt %s [count=\\fIN\\fB|names=\\fIname1\\fB[,\\fIname2...\\fB]] [personality=\\fIP,P,...\\fB]" % comp_name, + f.write(".B loadrt %s [count=\\fIN\\fB|names=\\fIname1\\fB[,\\fIname2...\\fB]] [personality=\\fIP,P,...\\fB]\n" % comp_name) else: - print >>f, ".B loadrt %s [count=\\fIN\\fB|names=\\fIname1\\fB[,\\fIname2...\\fB]]" % comp_name, + f.write(".B loadrt %s [count=\\fIN\\fB|names=\\fIname1\\fB[,\\fIname2...\\fB]]\n" % comp_name) for type, name, default, doc in modparams: - print >>f, "[%s=\\fIN\\fB]" % name, - print >>f + f.write("[%s=\\fIN\\fB]\n" % name) + f.write("\n") hasparamdoc = False for type, name, default, doc in modparams: if doc: hasparamdoc = True if hasparamdoc: - print >>f, ".RS 4" + f.write(".RS 4\n") for type, name, default, doc in modparams: - print >>f, ".TP" - print >>f, "\\fB%s\\fR" % name, + f.write(".TP\n") + f.write("\\fB%s\\fR\n" % name) if default: - print >>f, "[default: %s]" % default + f.write("[default: %s]\n" % default) else: - print >>f - print >>f, doc - print >>f, ".RE" + f.write("\n") + f.write("%s\n" % doc) + f.write(".RE\n") if options.get("constructable") and not options.get("singleton"): - print >>f, ".PP\n.B newinst %s \\fIname\\fB" % comp_name + f.write(".PP\n.B newinst %s \\fIname\\fB\n" % comp_name) - doc = finddoc('descr') + doc = finddoc('descr') if doc and doc[1]: - print >>f, ".SH DESCRIPTION\n" - print >>f, "%s" % doc[1] + f.write(".SH DESCRIPTION\n") + f.write("%s" % doc[1]) if functions: - print >>f, ".SH FUNCTIONS" + f.write(".SH FUNCTIONS\n") for _, name, fp, doc in finddocs('funct'): - print >>f, ".TP" - print >>f, "\\fB%s\\fR" % to_hal_man(name), + f.write(".TP\n") + f.write("\\fB%s\\fR" % to_hal_man(name)) if fp: - print >>f, "(requires a floating-point thread)" + f.write("(requires a floating-point thread)\n") else: - print >>f - print >>f, doc + f.write("\n") + f.write("%s\n" % doc) lead = ".TP" - print >>f, ".SH PINS" + f.write(".SH PINS\n") for _, name, type, array, dir, doc, value, personality in finddocs('pin'): - print >>f, lead - print >>f, ".B %s\\fR" % to_hal_man(name), - print >>f, type, dir, + f.write(lead + "\n") + f.write(".B %s\\fR" % to_hal_man(name)) + f.write("%s %s\n" % (type, dir)) if array: sz = name.count("#") if isinstance(array, tuple): - print >>f, " (%s=%0*d..%s)" % ("M" * sz, sz, 0, array[1]), + f.write(" (%s=%0*d..%s)\n" % ("M" * sz, sz, 0, array[1])) else: - print >>f, " (%s=%0*d..%0*d)" % ("M" * sz, sz, 0, sz, array-1), + f.write(" (%s=%0*d..%0*d)\n" % ("M" * sz, sz, 0, sz, array-1)) if personality: - print >>f, " [if %s]" % personality, + f.write(" [if %s]\n" % personality) if value: - print >>f, "\\fR(default: \\fI%s\\fR)" % value + f.write("\\fR(default: \\fI%s\\fR)\n" % value) else: - print >>f, "\\fR" + f.write("\\fR\n") if doc: - print >>f, doc - lead = ".TP" + f.write("%s\n" % doc) + lead = ".TP\n" else: - lead = ".TQ" + lead = ".TQ\n" lead = ".TP" if params: - print >>f, ".SH PARAMETERS" + f.write(".SH PARAMETERS\n") for _, name, type, array, dir, doc, value, personality in finddocs('param'): - print >>f, lead - print >>f, ".B %s\\fR" % to_hal_man(name), - print >>f, type, dir, + f.write(lead + "\n") + f.write(".B %s\\fR" % to_hal_man(name)) + f.write("%s %s\n" % (type, dir)) if array: sz = name.count("#") if isinstance(array, tuple): - print >>f, " (%s=%0*d..%s)" % ("M" * sz, sz, 0, array[1]), + f.write(" (%s=%0*d..%s)\n" % ("M" * sz, sz, 0, array[1])) else: - print >>f, " (%s=%0*d..%0*d)" % ("M" * sz, sz, 0, sz, array-1), + f.write(" (%s=%0*d..%0*d)\n" % ("M" * sz, sz, 0, sz, array-1)) if personality: - print >>f, " [if %s]" % personality, + f.write(" [if %s]\n" % personality) if value: - print >>f, "\\fR(default: \\fI%s\\fR)" % value + f.write("\\fR(default: \\fI%s\\fR)\n" % value) else: - print >>f, "\\fR" + f.write("\\fR\n") if doc: - print >>f, doc + f.write("%s\n" % doc) lead = ".TP" else: lead = ".TQ" doc = finddoc('see_also') if doc and doc[1]: - print >>f, ".SH SEE ALSO\n" - print >>f, "%s" % doc[1] + f.write(".SH SEE ALSO\n\n") + f.write("%s\n" % doc[1]) doc = finddoc('notes') if doc and doc[1]: - print >>f, ".SH NOTES\n" - print >>f, "%s" % doc[1] + f.write(".SH NOTES\n\n") + f.write("%s\n" % doc[1]) doc = finddoc('author') if doc and doc[1]: - print >>f, ".SH AUTHOR\n" - print >>f, "%s" % doc[1] + f.write(".SH AUTHOR\n\n") + f.write("%s\n" % doc[1]) doc = finddoc('license') if doc and doc[1]: - print >>f, ".SH LICENSE\n" - print >>f, "%s" % doc[1] + f.write(".SH LICENSE\n\n") + f.write("%s\n" % doc[1]) def process(filename, mode, outfilename): tempdir = tempfile.mkdtemp() @@ -960,7 +959,7 @@ def process(filename, mode, outfilename): f = open(outfilename, "w") if options.get("userinit") and not options.get("userspace"): - print >> sys.stderr, "Warning: comp '%s' sets 'userinit' without 'userspace', ignoring" % filename + sys.stderr.write("Warning: comp '%s' sets 'userinit' without 'userspace', ignoring" % filename) if options.get("userspace"): if functions: @@ -999,7 +998,7 @@ def process(filename, mode, outfilename): shutil.rmtree(tempdir) def usage(exitval=0): - print """%(name)s: Build, compile, and install LinuxCNC HAL components + print ("""%(name)s: Build, compile, and install LinuxCNC HAL components Usage: %(name)s [--compile|--preprocess|--document|--view-doc] compfile... @@ -1008,7 +1007,7 @@ Usage: [sudo] %(name)s --install --userspace cfile... [sudo] %(name)s --install --userspace pyfile... %(name)s --print-modinc -""" % {'name': os.path.basename(sys.argv[0])} +""" % {'name': os.path.basename(sys.argv[0])}) raise SystemExit(exitval) def main(): @@ -1060,7 +1059,7 @@ def main(): if mode == MODINC: if args: - raise SystemExit,("Can not specify input files when using --print-modinc") + raise SystemExit("Can not specify input files when using --print-modinc") print (find_modinc()) return 0 @@ -1109,12 +1108,12 @@ def main(): else: raise SystemExit("Unrecognized file type for mode %s: %r" % (modename[mode], f)) except: - ex_type, ex_value, exc_tb = sys.exc_info() + ex = traceback.format_exc() try: os.unlink(outfile) except: # os.error: pass - raise ex_type(ex_value, exc_tb) + raise SystemExit(ex) if __name__ == '__main__': main()