David Anderson avatar David Anderson committed 20231fa

Fix API, add verbose logging to remote assembler

Comments (0)

Files changed (3)

-^dist$
-
+^dist/
+^MANIFEST$
   out.append('\n%s\n' % main_func)
   return ''.join(out)
 
-def _get_assembler():
+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 assembler + '_load()\n'
+  return '%s\n_load(%s)\n' % (assembler, verbose)
 
 # BEGIN ASSEMBLER
-def _load():
-  def __log(s):
-    print s
-    sys.stdout.flush()
+def _load(verbose):
   import imp
+  import sys
   import zlib
-  import sys
+  def _log(s):
+    sys.stderr.write(s+'\n')
+    sys.stderr.flush()
+  if verbose:
+    log = _log
+  else:
+    log = lambda _: None
+  log('Assembler started')
   files = {}
   while True:
     name = sys.stdin.readline().strip()
     if not name:
       break
+    log('Reading python module ' + name)
     n = int(sys.stdin.readline())
     files[name] = zlib.decompress(sys.stdin.read(n))
+    log('Read module %s (%d compressed, %d decompressed)'
+        % (name, n, len(files[name])))
+  log('Finished reading modules, starting compilation')
   while files:
     l = len(files)
     for name in list(files.keys()):
+      log('Compiling ' + name)
       code = compile(files[name], name, 'exec')
       d = {}
       try:
         eval(code, d, d)
       except ImportError:
+        log("Can't compile %s yet, needs other modules" % name)
         continue
       mod = imp.new_module(name)
       mod.__dict__.update(d)
       sys.modules[name] = mod
       del files[name]
+      log('Compiled and loaded ' + name)
     if len(files) == l:
-      print 'Infinite compile loop'
-      sys.stdout.flush()
+      _log("Infinite compile loop, you're probably missing an import")
       exit(1)
   del sys.modules[__name__].__dict__['_load']
   sys.stdout.flush()
   sys.stderr.flush()
   module, func = sys.stdin.readline().strip().rsplit('.', 2)
+  log('All code loaded, handing off to %s.%s()' % (module, func))
   sys.modules[module].__dict__[func]()
 # END ASSEMBLER
 
+# Remove textwrap use
+# Sync string, exception if sync string not seen
+# Assembler at start of file
+# Use a single compressobj
+# style guide
+
 def remote_exec(hostname=None, user=None, port=22,
                 ssh_cmd=None, module_filenames=None,
-                literal_modules=None, main_func=None):
+                literal_modules=None, main_func=None,
+                verbose_load=False):
   if not ssh_cmd:
     if user:
       user = user + '@'
   main = _pack(module_filenames or [],
                literal_modules or {},
                main_func or 'main.main')
-  stage2 = _get_assembler()
+  stage2 = _get_assembler(verbose_load)
   stage1 = textwrap.dedent(r'''
       import sys;
       exec compile(sys.stdin.read(%d), "assembler.py", "exec")
   s1.close()
   def setup():
     s2.close()
-  # stdout=slb
-  p = subprocess.Popen(cmd, stdin=sla, preexec_fn=setup, close_fds=True)
+  p = subprocess.Popen(cmd, stdin=sla, stdout=slb, preexec_fn=setup, close_fds=True)
   os.close(sla)
   os.close(slb)
   s2.sendall(stage2)
   s2.sendall(main)
-  p.wait()
+  return p, s2
 
 if __name__ == '__main__':
   import remoteexec
-  remoteexec.remote_exec(hostname=sys.argv[1],
-                         module_filenames=['test.py', 'remoteexec.py'],
-                         literal_modules=mods,
-                         main_func='test1.print_stuff')
+  p, s = remoteexec.remote_exec(
+      hostname=sys.argv[1],
+      module_filenames=['test.py', 'remoteexec.py'],
+      literal_modules=mods,
+      main_func='test1.print_stuff')
+  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.