David Anderson avatar David Anderson committed 9678992

Move assembler to the top of the file.

Comments (0)

Files changed (2)

-#!/usr/bin/env python
-
-import subprocess
-import socket
-import os
-import os.path
-import zlib
-import textwrap
-
-def _readfile(filename):
-  f = open(filename)
-  try:
-    return f.read()
-  finally:
-    f.close()
-
-def _pack(filenames, literal_modules, main_func):
-  out = []
-  for filename in filenames:
-    _, basename = os.path.split(filename)
-    assert basename[-3:] == '.py'
-    source = zlib.compress(_readfile(filename))
-    out.append('%s\n%d\n%s' % (basename[:-3], len(source), source))
-  for name, source in literal_modules.iteritems():
-    source = zlib.compress(source)
-    out.append('%s\n%d\n%s' % (name, len(source), source))
-  out.append('\n%s\n' % main_func)
-  return ''.join(out)
-
-def _get_assembler(verbose=False):
-  filename = __file__
-  if filename.endswith('.pyc'):
-    filename = filename[:-1]
-  source = _readfile(filename)
-  assembler = source.split('# BEGIN ASSEMBLER\n')[1]
-  assembler = assembler.split('# END ASSEMBLER\n')[0]
-  return '%s\n_load(%s)\n' % (assembler, verbose)
-
-# BEGIN ASSEMBLER
 def _load(verbose):
   import imp
   import sys
     name = sys.stdin.readline().strip()
     if not name:
       break
-    log('Reading python module ' + name)
+    log('Reading module ' + name)
     n = int(sys.stdin.readline())
     files[name] = zlib.decompress(sys.stdin.read(n))
     log('Read module %s (%d compressed, %d decompressed)'
   log('All code loaded, handing off to %s.%s()' % (module, func))
   sys.modules[module].__dict__[func]()
 # END ASSEMBLER
+# The above is the stage2 assembler that gets run on the remote
+# system. To ensure that syntax errors and exceptions have useful line
+# numbers, keep it at the top of the file.
 
-# Remove textwrap use
+import os
+import os.path
+import socket
+import subprocess
+import zlib
+
+# Stage 1 assembler, compiles and executes stage2.
+_STAGE1 = '''
+import sys;
+exec compile(sys.stdin.read(%d), "assembler.py", "exec")
+'''
+
+def _readfile(filename):
+  f = open(filename)
+  try:
+    return f.read()
+  finally:
+    f.close()
+
+def _pack(filenames, literal_modules, main_func):
+  out = []
+  for filename in filenames:
+    _, basename = os.path.split(filename)
+    assert basename[-3:] == '.py'
+    source = zlib.compress(_readfile(filename))
+    out.append('%s\n%d\n%s' % (basename[:-3], len(source), source))
+  for name, source in literal_modules.iteritems():
+    source = zlib.compress(source)
+    out.append('%s\n%d\n%s' % (name, len(source), source))
+  out.append('\n%s\n' % main_func)
+  return ''.join(out)
+
+def _get_assembler(verbose=False):
+  filename = __file__
+  if filename.endswith('.pyc'):
+    filename = filename[:-1]
+  source = _readfile(filename)
+  assembler = source.split('# END ASSEMBLER\n')[0]
+  return '%s\n_load(%s)\n' % (assembler, verbose)
+
 # Sync string, exception if sync string not seen
 # Assembler at start of file
 # Use a single compressobj
                literal_modules or {},
                main_func or 'main.main')
   stage2 = _get_assembler(verbose_load)
-  stage1 = textwrap.dedent(r'''
-      import sys;
-      exec compile(sys.stdin.read(%d), "assembler.py", "exec")
-      ''') % len(stage2)
+  stage1 = _STAGE1 % len(stage2)
   pycmd = ("P=python2; $P -V 2>/dev/null || P=python; "
            "exec \"$P\" -c '%s'") % stage1
   cmd = ssh_cmd + ['--', pycmd]
       hostname=sys.argv[1],
       module_filenames=['test.py', 'remoteexec.py'],
       literal_modules=mods,
-      main_func='test1.print_stuff')
+      main_func='test1.print_stuff',
+      verbose_load=True)
   f = s.makefile('r')
   s.close()
   print f.read()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.