def __init__(self, remote_machine, server_class = "ThreadedServer"): self.proc = None self.tun = None self._tmpdir_ctx = None rpyc_root = local.path(rpyc.__file__).dirname self._tmpdir_ctx = remote_machine.tempdir() tmp = self._tmpdir_ctx.__enter__() copy(rpyc_root, tmp) script = (tmp / "deployed-rpyc.py") script.write(SERVER_SCRIPT.replace("$SERVER$", server_class)) self.proc = remote_machine.python.popen(script, new_session = True) line = "" try: line = self.proc.stdout.readline() remote_port = int(line.strip()) except Exception: try: self.proc.terminate() except Exception: pass stdout, stderr = self.proc.communicate() raise ProcessExecutionError(self.proc.argv, self.proc.returncode, line + stdout, stderr) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("localhost", 0)) self.local_port = s.getsockname()[1] s.close() self.tun = remote_machine.tunnel(self.local_port, remote_port)
def deployment(remote_machine, code = SERVER_SCRIPT): with remote_machine.tempdir() as tmp: copy(RPYC_ROOT, tmp) delete(tmp // ".pyc", tmp // "*/.pyc") with remote_machine.cwd(tmp): with remote_machine.env(PYTHONPATH = remote_machine.env.get("PYTHONPATH", "") + ":%s" % (tmp,)): proc = (remote_machine.python << code).popen() line = "" try: line = proc.stdout.readline() remote_port = int(line.strip()) except Exception: try: proc.kill() except Exception: pass stdout, stderr = proc.communicate() raise ProcessExecutionError(proc.argv, proc.returncode, line + stdout, stderr) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("localhost", 0)) local_port = s.getsockname()[1] s.close() with remote_machine.tunnel(local_port, remote_port) as tun: try: yield DeployedServer(local_port) finally: try: proc.kill() except Exception: pass
def __init__(self, remote_machine, server_class="ThreadedServer"): self.proc = None self.tun = None self._tmpdir_ctx = None rpyc_root = local.path(rpyc.__file__).dirname self._tmpdir_ctx = remote_machine.tempdir() tmp = self._tmpdir_ctx.__enter__() copy(rpyc_root, tmp) script = (tmp / "deployed-rpyc.py") script.write(SERVER_SCRIPT.replace("$SERVER$", server_class)) self.proc = remote_machine.python.popen(script, new_session=True) line = "" try: line = self.proc.stdout.readline() remote_port = int(line.strip()) except Exception: try: self.proc.terminate() except Exception: pass stdout, stderr = self.proc.communicate() raise ProcessExecutionError(self.proc.argv, self.proc.returncode, line + stdout, stderr) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("localhost", 0)) self.local_port = s.getsockname()[1] s.close() self.tun = remote_machine.tunnel(self.local_port, remote_port)
def main(self, src, dst): if local.path(dst).exists(): if not self.overwrite: logger.debug("Oh no! That's terrible") raise ValueError("Destination already exists") else: delete(dst) logger.debug("I'm going to copy %s to %s", src, dst) copy(src, dst) logger.debug("Great success")
def deployment(remote_machine): """Sets up a temporary, short-lived RPyC deployment on the given remote machine. A deployment: 1. Creates a temporary directory on the remote machine, and copies RPyC's code from the local machine to the remote temporary directory. 2. Starts an RPyC server on the remote machine, binding to an arbitrary TCP port, allowing only in-bound connections (connections from the same machine). The server reports the chosen port over ``stdout``. 3. An SSH tunnel is created from a local port on the local host, to the remote machine's chosen TCP port. This tunnel is authenticated and encrypted. 4. The deployment returns a :class:`DeployedServer <rpyc.utils.zerodeploy.DeployedServer>` object, which can be used to connect to the newly-spawned server. 5. When the deployment context is exited, the SSH tunnel is torn down, the remote server is terminated and the temporary directory deleted. :param remote_machine: a ``plumbum.SshMachine`` instance, representing an SSH connection to the desired remote machine """ RPYC_ROOT = local.path(rpyc.__file__).dirname with remote_machine.tempdir() as tmp: copy(RPYC_ROOT, tmp) delete(tmp // ".pyc", tmp // "*/.pyc") with remote_machine.cwd(tmp): with remote_machine.env(PYTHONPATH = remote_machine.env.get("PYTHONPATH", "") + ":%s" % (tmp,)): proc = (remote_machine.python << SERVER_SCRIPT).popen() line = "" try: line = proc.stdout.readline() remote_port = int(line.strip()) except Exception: try: proc.kill() except Exception: pass stdout, stderr = proc.communicate() raise ProcessExecutionError(proc.argv, proc.returncode, line + stdout, stderr) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("localhost", 0)) local_port = s.getsockname()[1] s.close() with remote_machine.tunnel(local_port, remote_port) as tun: try: yield DeployedServer(local_port) finally: try: proc.kill() except Exception: pass
def test_copy_move_delete(self): from plumbum.cmd import touch with local.tempdir() as dir: (dir / "orog").mkdir() (dir / "orog" / "rec").mkdir() for i in range(20): touch(dir / "orog" / ("f%d.txt" % (i, ))) for i in range(20, 40): touch(dir / "orog" / "rec" / ("f%d.txt" % (i, ))) move(dir / "orog", dir / "orig") s1 = sorted(f.basename for f in (dir / "orig").walk()) copy(dir / "orig", dir / "dup") s2 = sorted(f.basename for f in (dir / "dup").walk()) self.assertEqual(s1, s2) with SshMachine("localhost") as rem: with rem.tempdir() as dir2: copy(dir / "orig", dir2) s3 = sorted(f.basename for f in (dir2 / "orig").walk()) self.assertEqual(s1, s3) copy(dir2 / "orig", dir2 / "dup") s4 = sorted(f.basename for f in (dir2 / "dup").walk()) self.assertEqual(s1, s4) copy(dir2 / "dup", dir / "dup2") s5 = sorted(f.basename for f in (dir / "dup2").walk()) self.assertEqual(s1, s5) with SshMachine("localhost") as rem2: with rem2.tempdir() as dir3: copy(dir2 / "dup", dir3) s6 = sorted(f.basename for f in (dir3 / "dup").walk()) self.assertEqual(s1, s6) move(dir3 / "dup", dir / "superdup") self.assertFalse((dir3 / "dup").exists()) s7 = sorted(f.basename for f in (dir / "superdup").walk()) self.assertEqual(s1, s7) # test rm delete(dir)
def test_copy_move_delete(self): from plumbum.cmd import touch with local.tempdir() as dir: (dir / "orog").mkdir() (dir / "orog" / "rec").mkdir() for i in range(20): touch(dir / "orog" / ("f%d.txt" % (i,))) for i in range(20,40): touch(dir / "orog" / "rec" / ("f%d.txt" % (i,))) move(dir / "orog", dir / "orig") s1 = sorted(f.basename for f in (dir / "orig").walk()) copy(dir / "orig", dir / "dup") s2 = sorted(f.basename for f in (dir / "dup").walk()) self.assertEqual(s1, s2) with SshMachine("localhost") as rem: with rem.tempdir() as dir2: copy(dir / "orig", dir2) s3 = sorted(f.basename for f in (dir2 / "orig").walk()) self.assertEqual(s1, s3) copy(dir2 / "orig", dir2 / "dup") s4 = sorted(f.basename for f in (dir2 / "dup").walk()) self.assertEqual(s1, s4) copy(dir2 / "dup", dir / "dup2") s5 = sorted(f.basename for f in (dir / "dup2").walk()) self.assertEqual(s1, s5) with SshMachine("localhost") as rem2: with rem2.tempdir() as dir3: copy(dir2 / "dup", dir3) s6 = sorted(f.basename for f in (dir3 / "dup").walk()) self.assertEqual(s1, s6) move(dir3 / "dup", dir / "superdup") self.assertFalse((dir3 / "dup").exists()) s7 = sorted(f.basename for f in (dir / "superdup").walk()) self.assertEqual(s1, s7) # test rm delete(dir)
def deployment(remote_machine, code=SERVER_SCRIPT): with remote_machine.tempdir() as tmp: copy(RPYC_ROOT, tmp) delete(tmp // ".pyc", tmp // "*/.pyc") with remote_machine.cwd(tmp): with remote_machine.env( PYTHONPATH=remote_machine.env.get("PYTHONPATH", "") + ":%s" % (tmp, )): proc = (remote_machine.python << code).popen() line = "" try: line = proc.stdout.readline() remote_port = int(line.strip()) except Exception: try: proc.kill() except Exception: pass stdout, stderr = proc.communicate() raise ProcessExecutionError(proc.argv, proc.returncode, line + stdout, stderr) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("localhost", 0)) local_port = s.getsockname()[1] s.close() with remote_machine.tunnel(local_port, remote_port) as tun: try: yield DeployedServer(local_port) finally: try: proc.kill() except Exception: pass
def main(self): local.cwd.chdir(HERE) sys.path.insert(0, str(ROOT)) local.env["PYTHONPATH"] = ROOT local.python("build_openni.py") local.python("build_nite.py") from primesense import openni2, nite2 dist = local.path("../dist") dist.delete() dist.mkdir() tmp = local.path("tmp") tmp.delete() tmp.mkdir() copy("../primesense", tmp / "primesense") copy("MANIFEST.in", tmp) copy("../LICENSE", tmp) copy("../README.rst", tmp) ver = "%s.%s.%s.%s-%s" % ( openni2.c_api.ONI_VERSION_MAJOR, openni2.c_api.ONI_VERSION_MINOR, openni2.c_api.ONI_VERSION_MAINTENANCE, openni2.c_api.ONI_VERSION_BUILD, config.get("pypi", "release")) data = local.path("setup_template.py").read().replace("$VERSION$", ver) (tmp / "setup.py").write(data) with local.cwd(tmp): if self.upload: # copy pypirc to ~ orig = local.path("~/.pypirc") restore = False if orig.exists(): copy(orig, "~/.pypirc-openni-wrapper") restore = True copy(ROOT / "_pypirc", "~/.pypirc") try: local.python["setup.py", "sdist", "--formats=zip,gztar", None if self.dont_register else "register", "upload"] & FG finally: if restore: copy("~/.pypirc-openni-wrapper", orig) else: local.python["setup.py", "sdist", "--formats=zip,gztar"] & FG for fn in tmp / "dist" // "*": fn.move(dist)
def main(self): local.cwd.chdir(HERE) sys.path.insert(0, str(ROOT)) local.env["PYTHONPATH"] = ROOT local.python("build_openni.py") local.python("build_nite.py") from primesense import openni2, nite2 dist = local.path("../dist") dist.delete() dist.mkdir() tmp = local.path("tmp") tmp.delete() tmp.mkdir() copy("../primesense", tmp / "primesense") copy("MANIFEST.in", tmp) copy("../LICENSE", tmp) copy("../README.rst", tmp) ver = "%s.%s.%s.%s-%s" % (openni2.c_api.ONI_VERSION_MAJOR, openni2.c_api.ONI_VERSION_MINOR, openni2.c_api.ONI_VERSION_MAINTENANCE, openni2.c_api.ONI_VERSION_BUILD, config.get("pypi", "release")) data = local.path("setup_template.py").read().replace("$VERSION$", ver) (tmp / "setup.py").write(data) with local.cwd(tmp): if self.upload: # copy pypirc to ~ orig = local.path("~/.pypirc") restore = False if orig.exists(): copy(orig, "~/.pypirc-openni-wrapper") restore = True copy(ROOT / "_pypirc", "~/.pypirc") try: local.python["setup.py", "sdist", "--formats=zip,gztar", None if self.dont_register else "register", "upload"] & FG finally: if restore: copy("~/.pypirc-openni-wrapper", orig) else: local.python["setup.py", "sdist", "--formats=zip,gztar"] & FG for fn in tmp / "dist" // "*": fn.move(dist)