def test_file_Or_Directory_Exists(): test_File_Name = ".testFile" testFile = open(test_File_Name, 'w') testFile.write("Test") testFile.close() assert utils.file_Or_Directory_Exists('./', test_File_Name) commandManager.runRmCommand(".testFile") assert not utils.file_Or_Directory_Exists('./', test_File_Name)
def _verifyGAELogin(): commandManager.runGAEAuthListCommand( writeFile=sad._TEMP_GAE_AUTH_FILE_NAME) tempFile = open(sad._TEMP_GAE_AUTH_FILE_NAME, 'r') tempFile.readline() line = tempFile.readline() tokens = line.split(' ') tempFile.close() commandManager.runRmCommand(sad._TEMP_GAE_AUTH_FILE_NAME) return tokens[0] == sad._GAE_LOGIN_ACTIVE_
def test_getConfig(): config = configManager.getConfig() assert isinstance(config, ConfigParser.RawConfigParser) commandManager.runRmCommand(sad._CONFIG_FILE_PATH) with pytest.raises(SystemExit) as pytest_wrapped_e: config = configManager.getConfig() assert pytest_wrapped_e.type == SystemExit commandManager.runRmCommand(sad._CONFIG_DIR_NAME_) with pytest.raises(SystemExit) as pytest_wrapped_e: config = configManager.getConfig() assert pytest_wrapped_e.type == SystemExit
def _verifyHerokuLogIn(): commandManager.runHerokuToken(sad._TEMP_HEROKU_TOKEN_FILE_NAME) tempFile = open(sad._TEMP_HEROKU_TOKEN_FILE_NAME) count = 0 for _ in tempFile: count += 1 tempFile.close() commandManager.runRmCommand(sad._TEMP_HEROKU_TOKEN_FILE_NAME) if count > 1: return True return False
def _verifyGAEProject(projectName): commandManager.runGAEListProjects(sad._TEMP_LS_VERIFY_FILE_NAME) tempFile = open(sad._TEMP_LS_VERIFY_FILE_NAME) flag = False for line in tempFile: tokens = line.split(' ') if tokens[0] == projectName: flag = True break tempFile.close() commandManager.runRmCommand(sad._TEMP_LS_VERIFY_FILE_NAME) return flag
def _verifyProject(projectName): if projectName is None: projectName = "None" commandManager.runHerokuInfoCommand(projectName, sad._TEMP_HEROKU_INFO_FILE_NAME) tempFile = open(sad._TEMP_HEROKU_INFO_FILE_NAME, 'r') line = tempFile.readline() tokens = line.split(' ') tempFile.close() commandManager.runRmCommand(sad._TEMP_HEROKU_INFO_FILE_NAME) if (tokens[0] == '==='): return True return False
def _verifyHerokuInstallation(): commandManager.runLsCommand(sad._HEROKU_SNAP_PATH, writeFile=sad._TEMP_LS_VERIFY_FILE_NAME) tempFile = open(sad._TEMP_LS_VERIFY_FILE_NAME, 'r') line = tempFile.readline() tempFile.close() commandManager.runRmCommand(sad._TEMP_LS_VERIFY_FILE_NAME) tokens = line.split(' ') tokens[0] = tokens[0].rstrip('\n') if (tokens[0] != sad._HEROKU_SNAP_PATH): logManager.printVerbose("Snap is not installed") if (utils.hasSupport()): answer = inputManager.getYesNoAnswer( "Do you want to install snapd? (y/n):") if answer: commandManager.runPackageManagerInstall( sad._HEORKU_SNAP_PACKAGE) logManager.printVerbose("Waiting for snapd...") time.sleep(30) else: herokuErrorHandler.addError("Snap is not installed", sad._CRITICAL_ERROR_) else: herokuErrorHandler.addError("Snap is not installed", sad._CRITICAL_ERROR_) herokuErrorHandler.handle() commandManager.runSnapListCommand(sad._TEMP_SNAP_LIST_FILE_NAME) tempFile = open(sad._TEMP_SNAP_LIST_FILE_NAME, 'r') flag = False for line in tempFile: tokens = line.split(' ') if (tokens[0] == sad._DEPLOY_HEROKU_OPTION): flag = True break tempFile.close() commandManager.runRmCommand(sad._TEMP_SNAP_LIST_FILE_NAME) if not flag: logManager.printVerbose("heroku-cli is not installed") answer = inputManager.getYesNoAnswer( "Do you want to install heroku-cli? (y/n):") if answer: commandManager.runSnapInstallCommand( sad._DEPLOY_HEROKU_OPTION, sad._HEROKU_HEROKU_CLI_VERSION_) else: herokuErrorHandler.addError("heroku-cli is not installerd", sad._CRITICAL_ERROR_) herokuErrorHandler.handle()
def generateBot(testFlag=True): config = configManager.getConfig() hosting = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_) commandManager.runMkdirCommand(sad._OUTPUT_BOT_DIR_) commandManager.runLsCommand(sad._MODULES_DIR_, writeFile=sad._TEMP_LS_MODULES_FILE_NAME) modules = [] tempLsFile = open(sad._TEMP_LS_MODULES_FILE_NAME, 'r') for line in tempLsFile: modules.append(line.rstrip('\n')) tempLsFile.close() modules = [ module.split('.')[0] for module in modules if len(module.split('.')) == 2 and module.split('.')[1] == sad._MODULES_EXTENTION_ ] initFile = open(sad._MODULES_DIR_ + sad._DF_ + sad._INIT_PY, 'w') for module in modules: if module != sad.__INIT__: initFile.write("from " + module + " import *\n") initFile.close() commandManager.runRmCommand(sad._TEMP_LS_MODULES_FILE_NAME) outputBotFile = open(sad.OUTPUT_BOT_PATH, 'w') utils._generateHeaders(outputBotFile, testFlag) outputBotFile.write("import sys\n") outputBotFile.write("import os\n") outputBotFile.write("from telegram.ext import Updater\n") outputBotFile.write( "import ravegen.Decorators.functionManager as functionManager\n") outputBotFile.write("sys.path.insert(0, '.')\n") outputBotFile.write("import modules\n") outputBotFile.write("import logging\n") if testFlag: _generateTestBot(outputBotFile) else: if hosting == sad._DEPLOY_HEROKU_OPTION: herokuBotManager.generateBot(outputBotFile) elif hosting == sad._DEPLOY_GAE_OPTION: gaeBotManager.generateBot(outputBotFile)
def _verifyRemoteHeroku(gitUrl): if gitUrl is None: return -1 commandManager.runGitRemoteCommand(sad._TEMP_GIT_REMOTE_FILE_NAME) tempFile = open(sad._TEMP_GIT_REMOTE_FILE_NAME, 'r') for line in tempFile: tokens = line.split('\t') tokens[1] = tokens[1].split(' ')[0] if (tokens[0] == sad._DEPLOY_HEROKU_OPTION): tempFile.close() commandManager.runRmCommand(sad._TEMP_GIT_REMOTE_FILE_NAME) if (tokens[1] == gitUrl): return 1 return 0 tempFile.close() commandManager.runRmCommand(sad._TEMP_GIT_REMOTE_FILE_NAME) return -1
def _verifyGAEInstallation(): commandManager.runLsCommand(sad._GAE_PATH_, writeFile=sad._TEMP_LS_VERIFY_FILE_NAME) tempFile = open(sad._TEMP_LS_VERIFY_FILE_NAME, 'r') line = tempFile.readline() tempFile.close() commandManager.runRmCommand(sad._TEMP_LS_VERIFY_FILE_NAME) tokens = line.split(' ') tokens[0] = tokens[0].rstrip('\n') if tokens[0] != sad._GAE_PATH_: logManager.printVerbose("Google Cloud SDK is not installed") if utils.hasSupport(): gaeErrorHandler.addError( "You have to install Google Cloud SDK. Instructions: https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu", sad._CRITICAL_ERROR_) else: gaeErrorHandler.addError("Google Cloud SDK is no installed", sad._CRITICAL_ERROR_) gaeErrorHandler.handle()
def test_verifyConfig(): config = configManager.getConfig() configManager.verifyConfig(config) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_, "TEST") with pytest.raises(SystemExit) as pytest_wrapped_e: configManager.verifyConfig(config) assert pytest_wrapped_e.type == SystemExit configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_, sad._DEPLOY_HEROKU_OPTION) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_, "") with pytest.raises(SystemExit) as pytest_wrapped_e: configManager.verifyConfig(config) assert pytest_wrapped_e.type == SystemExit commandManager.runRmCommand(sad._CONFIG_FILE_PATH) commandManager.runTouchCommand(sad._CONFIG_FILE_PATH) with pytest.raises(SystemExit) as pytest_wrapped_e: config = configManager.getConfig() assert pytest_wrapped_e.type == SystemExit
def deployBot(withOptions=False, testFlag=True, generateFlag=True): if not withOptions: if utils.file_Or_Directory_Exists(sad._ACTUAL_PATH, sad._OUTPUT_BOT_DIR_): if utils.file_Or_Directory_Exists(sad._OUTPUT_BOT_DIR_, sad._OUTPUT_BOT_NAME_): generateFlag = False headers = utils._getHeaders() testFlag = headers[sad._HEADER_TOKEN_FLAG] == sad._STR_TRUE_ if testFlag: if generateFlag: generateBot(testFlag) logManager.printVerbose("Running Test Bot") commandManager.runPythonCommand(sad.OUTPUT_BOT_PATH) commandManager.runRmCommand(sad._MODULES_DIR_ + sad._DF_ + sad._LINUX_ALL_TAG_ + sad._PYC_EXTENTION) else: cloudManager.configure() if generateFlag: generateBot(testFlag) cloudManager.deploy()
def _createSkeleton(): appConfigFile = open(sad._GAE_APPENGINE_CONFIG_FILE_NAME_, 'w') appConfigFile.write("import os\n") appConfigFile.write("from google.appengine.ext import vendor\n") appConfigFile.write( "vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))\n" ) appConfigFile.close() commandManager.runCpCommand(sad.OUTPUT_BOT_PATH, sad._ACTUAL_PATH) appYamlFile = open(sad._GAE_APP_YAML, 'w') runtimeFile = open(sad._CONFIG_RUNTIME_FILE_PATH_, 'r') runtime = runtimeFile.readline() runtimeFile.close() appYamlFile.write("runtime: " + runtime + "\n") appYamlFile.write("api_version: 1\n") appYamlFile.write("threadsafe: yes\n\n") appYamlFile.write("handlers:\n") appYamlFile.write("- url: .*\n") appYamlFile.write(" script: bot.app\n\n") appYamlFile.write("libraries:\n") appYamlFile.write("- name: webapp2\n") appYamlFile.write(" version: \"2.5.2\"\n") appYamlFile.write("- name: flask\n") appYamlFile.write(" version: latest\n") appYamlFile.write("- name: ssl\n") appYamlFile.write(" version: latest\n") appYamlFile.close() if _generateReq(): commandManager.runCpCommand(sad._GAE_TEMP_REQ_FILE_PATH, sad._GAE_REQ_FILE_NAME) logManager.printVerbose("We need to install the requirements") logManager.printVerbose("We need sudo privileges") logManager.printVerbose( "Command to run: sudo pip install -t lib -r requirements.txt") commandManager.runPipInstallReq() commandManager.runRmCommand(sad._GAE_TEMP_REQ_FILE_PATH) commandManager.runTouchSudoCommand(sad._GAE_LIB_DIR_NAME_ + sad._DF_ + sad._INIT_PY)
def _verifyCurlInstallation(): commandManager.runLsCommand(sad._CURL_PATH, writeFile=sad._TEMP_LS_VERIFY_FILE_NAME) tempFile = open(sad._TEMP_LS_VERIFY_FILE_NAME, 'r') line = tempFile.readline() tempFile.close() commandManager.runRmCommand(sad._TEMP_LS_VERIFY_FILE_NAME) tokens = line.split(' ') tokens[0] = tokens[0].rstrip('\n') if tokens[0] != sad._CURL_PATH: logManager.printVerbose("Curl is not installed") flagOption = inputManager.getYesNoAnswer( "Do you want to install Curl (Y/n): ") if not flagOption: gaeErrorHandler.addError("Curl is necessary to deploy the bot", sad._CRITICAL_ERROR_) else: if utils.hasSupport(): commandManager.runPackageManagerInstall(sad._CURL_PACKAGE) else: gaeErrorHandler.addError("Curl is necessary to deploy the bot", sad._CRITICAL_ERROR_) gaeErrorHandler.handle()
def _deleteSkeleton(): commandManager.runRmCommand(sad._GAE_APPENGINE_CONFIG_FILE_NAME_) commandManager.runRmCommand(sad._OUTPUT_BOT_NAME_) commandManager.runRmCommand(sad._GAE_APP_YAML) commandManager.runRmCommand(sad._GAE_REQ_FILE_NAME)
def _deleteSkeleton(): commandManager.runRmCommand(sad._HEROKU_PROCFILE_NAME, sad._HEROKU_REQ_FILE_NAME, sad._HEROKU_RUNTIME_FILE_NAME)