def upload(self): f = request.files['file'] if get_file_suffix(f.filename) != 'zip': return { 'status': 'ok', 'error': 'file type mismatch' }, 400 # save zip file on temp folder file_path = '%s/%s' % (PROJECT_TMP_FOLDER, f.filename) with open(file_path, 'wb') as fw: fw.write(f.stream.read()) # unzip zip file dir_path = file_path.replace('.zip', '') if os.path.exists(dir_path): shutil.rmtree(dir_path) unzip_file(file_path, dir_path) # copy to source folder output_path = os.path.join(PROJECT_SOURCE_FILE_FOLDER, f.filename.replace('.zip', '')) print(output_path) if os.path.exists(output_path): shutil.rmtree(output_path) shutil.copytree(dir_path, output_path) return { 'status': 'ok', 'message': 'success' }
def deploy_file(self, id: str = None) -> (dict, tuple): """ Receive HTTP request of deploys and unzip zip files and copy to the destination directories. :param id: spider_id """ args = parser.parse_args() node_id = request.args.get('node_id') f = args.file if get_file_suffix(f.filename) != 'zip': return { 'status': 'ok', 'error': 'file type mismatch' }, 400 # save zip file on temp folder file_path = '%s/%s' % (PROJECT_TMP_FOLDER, f.filename) with open(file_path, 'wb') as fw: fw.write(f.stream.read()) # unzip zip file dir_path = file_path.replace('.zip', '') if os.path.exists(dir_path): shutil.rmtree(dir_path) unzip_file(file_path, dir_path) # get spider and version spider = db_manager.get(col_name=self.col_name, id=id) if spider is None: return None, 400 # make source / destination src = os.path.join(dir_path, os.listdir(dir_path)[0]) # src = dir_path dst = os.path.join(PROJECT_DEPLOY_FILE_FOLDER, str(spider.get('_id'))) # logging info current_app.logger.info('src: %s' % src) current_app.logger.info('dst: %s' % dst) # remove if the target folder exists if os.path.exists(dst): shutil.rmtree(dst) # copy from source to destination shutil.copytree(src=src, dst=dst) # save to db # TODO: task management for deployment db_manager.save('deploys', { 'spider_id': ObjectId(id), 'node_id': node_id, 'finish_ts': datetime.utcnow() }) return { 'code': 200, 'status': 'ok', 'message': 'deploy success' }