def _deploy_archive(self, current_work_dir, payload, payload_name): """ Creates a temporary file containing the archive and decompresses it into a temporary directory. The directory is scanned for Distutils2 modules, each is then copied over to the work directory and hot-(re)loaded. """ with NamedTemporaryFile(prefix='zato-hd-', suffix=payload_name) as tf: tf.write(payload) tf.flush() tmp_dir = mkdtemp(prefix='zato-hd-') decompress(tf.name, None) for tmp_py_path in visit_py_source_from_distribution(tmp_dir): # Get the path the stuff needs to be copied over to and create # all the directories needed along dest_py_path = os.path.join(current_work_dir, os.path.relpath(tmp_py_path, tmp_dir)) dest_py_dir = os.path.dirname(dest_py_path) try: os.mkdir(dest_py_dir) except OSError, e: if e.errno != EEXIST: raise self._deploy_file(current_work_dir, open(tmp_py_path).read(), dest_py_path) # Clean up shutil.rmtree(tmp_dir)
def _deploy_archive(self, current_work_dir, payload, payload_name): """ Creates a temporary file containing the archive and decompresses it into a temporary directory. The directory is scanned for Distutils2 modules, each is then copied over to the work directory and hot-(re)loaded. """ with NamedTemporaryFile(prefix='zato-hd-', suffix=payload_name) as tf: tf.write(payload) tf.flush() tmp_dir = mkdtemp(prefix='zato-hd-') decompress(tf.name, None) for tmp_py_path in visit_py_source_from_distribution(tmp_dir): # Get the path the stuff needs to be copied over to and create # all the directories needed along dest_py_path = os.path.join( current_work_dir, os.path.relpath(tmp_py_path, tmp_dir)) dest_py_dir = os.path.dirname(dest_py_path) try: os.mkdir(dest_py_dir) except OSError, e: if e.errno != EEXIST: raise self._deploy_file(current_work_dir, open(tmp_py_path).read(), dest_py_path) # Clean up shutil.rmtree(tmp_dir)
def decompress(self, archive, work_dir): """ Decompresses an archive into a randomly named directory. """ # 6 characters will do, we won't deploy millions of services # in the very same (micro-)second after all rand = uuid4().hex[:6] dir_name = os.path.join(work_dir, '{}-{}'.format(fs_safe_now(), rand), os.path.split(archive)[1]) os.makedirs(dir_name) # .. unpack the archive into it .. decompress(archive, dir_name) # .. and return the name of the newly created directory so that the # rest of the machinery can pick the services up return dir_name