def process_ajax(sub, req): try: if sub == 'start': ret = LogicGclone.start() return jsonify(ret) elif sub == 'stop': LogicGclone.current_data['user_stop'] = True ret = LogicGclone.kill() return jsonify(ret) elif sub == 'version': command = [ModelSetting.get('gclone_path'), 'version'] process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, bufsize=1) ret = [] with process.stdout: for line in iter(process.stdout.readline, b''): ret.append(line) process.wait() # wait for the subprocess to exit return jsonify(ret) elif sub == 'view_config': from framework.common.util import read_file data = read_file(ModelSetting.get('gclone_config_path')) return jsonify({'ret':True, 'data':data}) elif sub == 'gen_config': default = ''' [gc] type = drive scope = drive service_account_file = {first_json} service_account_file_path = {accounts_dir}/ ''' import glob accounts_dir = ModelSetting.get('path_accounts') sa_files = glob.glob(os.path.join(accounts_dir, '*.json')) if len(sa_files) == 0: ret = {'ret':False, 'log:': u'json 파일이 없습니다.'} else: first_json = os.path.join(accounts_dir, sa_files[0]) default = default.format(first_json=first_json, accounts_dir=accounts_dir) logger.debug(default) from framework.common.util import read_file, write_file config_path = ModelSetting.get('gclone_config_path') write_file(default, config_path) ret = {'ret':True, 'data':read_file(config_path)} return jsonify(ret) elif sub == 'log_reset': LogicGclone.current_data['log'] = [] return jsonify('') except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def process_ajax(sub, req): try: if sub == 'rclone_lsjson': remote_path = req.form['remote_path'] ret = RcloneTool.lsjson(ModelSetting.get('rclone_path'), ModelSetting.get('rclone_config_path'), remote_path) return jsonify(ret) elif sub == 'rclone_size': remote_path = req.form['remote_path'] ret = RcloneTool.size(ModelSetting.get('rclone_path'), ModelSetting.get('rclone_config_path'), remote_path) return jsonify(ret) elif sub == 'conf_get': rclone_config_path = req.form['rclone_config_path'] from framework.common.util import read_file ret = {'ret':False, 'data':''} if os.path.exists(rclone_config_path): ret['ret'] = True ret['data'] = read_file(rclone_config_path) return jsonify(ret) elif sub == 'conf_save': rclone_config_path = req.form['rclone_config_path'] data = req.form['conf_text'] data = data.replace("\r\n", "\n" ).replace( "\r", "\n" ) with open(rclone_config_path, 'w') as f: f.write(data) return jsonify(True) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def load_export(): try: from framework.common.util import read_file f = os.path.join(path_app_root, 'export.sh') if os.path.exists(f): return read_file(f) except Exception as exception: logger.error('Exception:%s', exception) logger.error(traceback.format_exc())
def load_data(): try: import framework.common.util as CommonUtil data = CommonUtil.read_file(ModelSetting.get('data_filename')) ret = {} if data is not None: data = data.split('\n') deviceid = data[0].strip() tmp = deviceid.find('192') deviceid = deviceid[tmp:] ModelSetting.set('deviceid', deviceid) logger.debug('deviceid:%s', deviceid) logger.debug('deviceid:%s', len(deviceid)) ModelHDHomerunChannel.query.delete() channel_list = [] for item in data[1:]: if item.strip() == '': continue m = ModelHDHomerunChannel() m.init_data(item) db.session.add(m) m.set_url(deviceid) channel_list.append(m) no = 1 for m in channel_list: if m.use: m.ch_number = no no += 1 for m in channel_list: if not m.use: m.ch_number = no no += 1 db.session.commit() lists = db.session.query(ModelHDHomerunChannel).all() tmp = LogicHDHomerun.channel_list() ret['data'] = [item.as_dict() for item in tmp] else: ret['ret'] = 'fail' ret['log'] = 'load data fail!!' except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) ret['ret'] = 'exception' ret['log'] = str(e) return ret
def process_menu(self, sub, req): arg = {'package_name': P.package_name, 'sub': self.name} if sub == 'setting': arg['status_isntall'] = SystemLogicCommand.execute_command_return( ['which', 'nginx']) if arg['status_isntall'] == '': arg['is_installed'] = False arg['status_isntall'] = 'nginx가 설치되어 있지 않습니다.' arg['status_running'] = '먼저 설치하세요' else: arg['is_installed'] = True if os.path.exists(conf_filepath): arg['conf'] = read_file(conf_filepath) return render_template('{package_name}_{sub}.html'.format( package_name=P.package_name, module_name=self.name, sub=sub), arg=arg) return render_template('sample.html', title='%s - %s' % (P.package_name, sub))
def ajax(sub): logger.debug('%s AJAX sub:%s', package_name, sub) try: if sub == 'setting_save': try: ret = ModelSetting.setting_save(request) return jsonify(ret) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) return jsonify('fail') elif sub == 'read_data': try: data_filename = request.form['data_filename'] import framework.common.util as CommonUtil ret = CommonUtil.read_file(data_filename) return jsonify(ret.split('\n')) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) elif sub == 'load_data': try: ret = LogicHDHomerun.load_data() return jsonify(ret) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) elif sub == 'load_db': try: ret = {} ret['setting'] = ModelSetting.to_dict() tmp = LogicHDHomerun.channel_list() ret['data'] = [x.as_dict() for x in tmp] return jsonify(ret) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) elif sub == 'group_sort': try: ret = {} ret['setting'] = ModelSetting.to_dict() ret['data'] = LogicHDHomerun.group_sort() return jsonify(ret) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) elif sub == 'save': try: LogicHDHomerun.save(request) ret = {} ret['setting'] = ModelSetting.to_dict() tmp = LogicHDHomerun.channel_list() ret['data'] = [x.as_dict() for x in tmp] return jsonify(ret) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) elif sub == 'match_for_epg_name': try: for_epg_name = request.form['for_epg_name'] channel_id = request.form['id'] ret = LogicHDHomerun.match_for_epg_name( channel_id, for_epg_name) return jsonify(ret) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) elif sub == 'delete': try: channel_id = request.form['id'] ret = {} ret['ret'] = LogicHDHomerun.delete(channel_id) ret['setting'] = ModelSetting.to_dict() tmp = LogicHDHomerun.channel_list() ret['data'] = [x.as_dict() for x in tmp] return jsonify(ret) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) elif sub == 'ip_fix': try: deviceid = request.form['deviceid'] tmp = deviceid.find('192') if tmp != -1: deviceid = deviceid[tmp:] else: deviceid = deviceid.lower().replace('https://', '').replace( 'http://', '') ModelSetting.set('deviceid', deviceid) ret = LogicHDHomerun.ip_fix(deviceid) return jsonify(ret) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def __make_rclone_conf(config_path, rclone_upload_remote, folder_id): try: #logger.debug(rclone_upload_remote) from framework.common.util import read_file if os.path.exists(config_path): rclone_info = read_file(config_path) from framework import path_data import time filename = '%s.conf' % (str(time.time()).split('.')[0]) conf_filepath = os.path.join(path_data, 'tmp', filename) #logger.debug('conf_filepath:%s', conf_filepath) #logger.debug(rclone_info) start = -1 dest_remote = None match = None first_rclone_info = None while True: start = rclone_info.find('[', start + 1) if start == -1: break next_start = rclone_info.find('[', start + 1) if next_start == -1: dest_remote = rclone_info[start:] else: dest_remote = rclone_info[start:next_start] #dest_remote = dest_remote.replace('team_drive', 'team_drive_dummy') #logger.debug(dest_remote) if first_rclone_info is None and dest_remote.find( 'access_token') != -1: first_rclone_info = dest_remote import re match = re.compile(r'\[(?P<remote_name>.*?)\]').search( dest_remote.strip()) if match.group('remote_name') == rclone_upload_remote: break else: dest_remote = None match = None #공유드라이브는 삭제 #dest_remote : 찾은 리모트 #rclone_info : 서버리모트가 될 리모트 if rclone_upload_remote is not None: if dest_remote is None: raise Exception('cannot find remote_name') else: # 로컬등 구글게 아니면? #if dest_remote.find('access_token') == -1: if dest_remote.find('type = drive') == -1: if first_rclone_info is not None: src_remote_ready = first_rclone_info else: raise Exception('cannot find google remote_name') else: # 정상인 경우 pass src_remote_ready = dest_remote else: src_remote_ready = first_rclone_info src_remote_ready = src_remote_ready.replace( 'team_drive', 'team_drive_dummy') # gclone이나 로컬이라면 rclone match = re.compile(r'\[(?P<remote_name>.*?)\]').search( src_remote_ready.strip()) server_rclone = src_remote_ready.replace( '[%s]' % match.group('remote_name'), '[%s]' % REMOTE_NAME_SJVA_SHARE_TEMP) server_rclone += '\nroot_folder_id = %s' % folder_id filedata = '%s\n\n%s\n' % (dest_remote, server_rclone) #logger.debug(filedata) import framework.common.util as CommonUtil CommonUtil.write_file(filedata, conf_filepath) return conf_filepath except Exception as exception: logger.error('Exception:%s', exception) logger.error(traceback.format_exc())