class ArrayUtil: """ THIS IS NOT DESCRIPTION FOR "CommandUtil.py" CLASS. This part represent to Static Initiliazer Code on Java, it's check to version for the avaiable version. At least current Python version must be 3.6.4+, anything else you should update Python version """ #################################################################################################################### if not packageControl.checkVersionForApplicable(3, 6, 4): raise EnvironmentError( "At least current must be 3.6.4+, anything else you should update Python version !!!" ) if packageControl.getPlatformInfo( ) != 'Linux' and packageControl.getPlatformInfo() != 'OS X': raise SystemError( "This class only execute Linux or Posix OS platform !!!") #################################################################################################################### def __init__(self): """ INFO: Default Constructure cannot be Creatable WARNING: If attand to create this object, it will raise NotImplementedError """ assert NotImplementedError( "This object useing only library, cannot be creatable!!!") def __new__(cls): """ INFO: This method was overrided only avoided to __new__ operator :return: NOISE_OBJECT """ return object.__new__(cls) @staticmethod def splitSubList(mainList, partCount=1): """ INFO: This function will sperate to sub list via balancing to equality :param mainList: This is the willing to sperate to sub lists :param partCount: Sub list count :return: LIST[LIST] """ length = len(mainList) return [ mainList[i * length // partCount:(i + 1) * length // partCount] for i in range(partCount) ]
class FormatUtil: """ THIS IS NOT DESCRIPTION FOR "CommandUtil.py" CLASS. This part represent to Static Initiliazer Code on Java, it's check to version for the avaiable version. At least current Python version must be 3.6.4+, anything else you should update Python version """ #################################################################################################################### if not packageControl.checkVersionForApplicable(3, 6, 4): raise EnvironmentError("At least current must be 3.6.4+, anything else you should update Python version !!!") if packageControl.getPlatformInfo() != 'Linux' and packageControl.getPlatformInfo() != 'OS X': raise SystemError("This class only execute Linux or Posix OS platform !!!") #################################################################################################################### def __init__(self): """ INFO: Default Constructure cannot be Creatable WARNING: If attand to create this object, it will raise NotImplementedError """ assert NotImplementedError("This object useing only library, cannot be creatable!!!") def __new__(cls): """ INFO: This method was overrided only avoided to __new__ operator :return: NOISE_OBJECT """ return object.__new__(cls) @staticmethod def humanFormatForNumbers(value): """ INFO: This function will return numbers readble String. Ex. 1000 -> 1K :param value: This value for will calculate for readable :return: STRING """ magnitude = 0 while abs(value) >= 1000: magnitude += 1 value /= 1000.0 return '%.2f%s' % (value, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])
class MailUtil: """ THIS IS NOT DESCRIPTION FOR "CommandUtil.py" CLASS. This part represent to Static Initiliazer Code on Java, it's check to version for the avaiable version. At least current Python version must be 3.6.4+, anything else you should update Python version """ #################################################################################################################### if not packageControl.checkVersionForApplicable(3, 6, 4): raise EnvironmentError( "At least current must be 3.6.4+, anything else you should update Python version !!!" ) if packageControl.getPlatformInfo( ) != 'Linux' and packageControl.getPlatformInfo() != 'OS X': raise SystemError( "This class only execute Linux or Posix OS platform !!!") #################################################################################################################### def __init__(self): """ INFO: Default Constructure cannot be Creatable WARNING: If attand to create this object, it will raise NotImplementedError """ assert NotImplementedError( "This object useing only library, cannot be creatable!!!") def __new__(cls): """ INFO: This method was overrided only avoided to __new__ operator :return: NOISE_OBJECT """ return object.__new__(cls) @staticmethod def sendMail(mailFrom, mailTo, mailCc, mailSbj, mailBodyArray, smtpIp, attachs=[]): """ This Function send e-mail with directly :raise If any Exception will re-raise this Exception :param mailFrom: Mail From part :param mailTo: Mail To part :param mailCc: Mail Cc part :param mailSbj: Mail Subject :param mailBodyArray: Mail Body Array, this part should be contain yours html body as list :param smtpIp: Mail STMP server IP :param attachs: If you have to attach objects, you can naming with array(JPG,TXT,PDF...) :return: NONE """ try: mail_Text = "" eMailFile = "emailFile_" + str(random.randrange(1000, 9999)) + ".html" mailBodyFile = open(eMailFile, 'w') mailBodyFile.write("<html><head></head><body><p><pre>") mail_Text += "<html><head></head><body><p><pre>" for body_Line in mailBodyArray: mailBodyFile.write(body_Line) mail_Text += body_Line mailBodyFile.write("</pre></p></body></html>") mail_Text += "</pre></p></body></html>" mailBodyFile.close() mail_TO = mailTo.split(',') mail_CC = mailCc.split(',') msg = MIMEMultipart('alternative') msg['Subject'] = mailSbj msg['From'] = mailFrom msg['To'] = ', '.join(mail_TO) msg['Cc'] = ', '.join(mail_CC) path = os.path.dirname(os.path.realpath(__file__)) dirs = os.listdir(path) for name in attachs: for file in dirs: if name in file: part = MIMEBase('application', "octet-stream") part.set_payload(open(str(file), "rb").read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename=' + str(file)) msg.attach(part) part1 = MIMEText(mail_Text, 'plain') part2 = MIMEText(mail_Text, 'html') msg.attach(part1) msg.attach(part2) aggregatesMail = mail_TO + mail_CC s = smtplib.SMTP(smtpIp) s.sendmail(mailFrom, aggregatesMail, msg.as_string()) s.quit() except Exception as e: raise RuntimeError("Unexpected Error : " + str(e))
class FileUtil: """ THIS IS NOT DESCRIPTION FOR "CommandUtil.py" CLASS. This part represent to Static Initiliazer Code on Java, it's check to version for the avaiable version. At least current Python version must be 3.6.4+, anything else you should update Python version """ #################################################################################################################### if not packageControl.checkVersionForApplicable(3, 6, 4): raise EnvironmentError( "At least current must be 3.6.4+, anything else you should update Python version !!!" ) if packageControl.getPlatformInfo( ) != 'Linux' and packageControl.getPlatformInfo() != 'OS X': raise SystemError( "This class only execute Linux or Posix OS platform !!!") #################################################################################################################### def __init__(self): """ INFO: Default Constructure cannot be Creatable WARNING: If attand to create this object, it will raise NotImplementedError """ assert NotImplementedError( "This object useing only library, cannot be creatable!!!") def __new__(cls): """ INFO: This method was overrided only avoided to __new__ operator :return: NOISE_OBJECT """ return object.__new__(cls) @staticmethod def checkClassName(obj, className): """ INFO: This function will compare object's class name WARNING: If not equal will raise exception :param obj: Which is the checking object :param className: Which is the important class name :return: NONE """ if obj.__class__.__name__ != className: raise EnvironmentError("Object not equal to class name !!!") @staticmethod def checkFile(path): """ INFO: This function checks the path state WARNING: If Not exist or not avaiable format raise exception :param path: File path on which is the target source :return: """ if (not FileUtil.isDirectory(path)): raise IsADirectoryError("Parameter not Directory!!!") if (not FileUtil.isDirectory(path)): raise NotADirectoryError("Directory does not exist !!!") @staticmethod def isDirectory(path): """ INFO: This function checking directory applicable :param path: File path on which is the target source :return: BOOLEAN """ return os.path.isdir(path) @staticmethod def isDirectoryExist(path): """ INFO: This function checking directory exist state :param path: File path on which is the target source :return: BOOLEAN """ return os.path.exists(path) @staticmethod def getFileLinesAsList(fileName): """ INFO: This function will return file's line as list :param fileName: File name for which is the result for list :return: LIST[STRING] """ try: fOpen = open(fileName) result = fOpen.readlines() return result except Exception as exception: raise exception @staticmethod def compareConfigurationFileResult(firstConfList, secondConfList): """ INFO: This function will return to result of list compare WARNING: If parameter different class object from list, it will raise error :param firstConfList: First conf file list :param secondConfList: Second conf file list :return: LIST[STRING] """ FileUtil.checkClassName(firstConfList, "list") FileUtil.checkClassName(secondConfList, "list") resultArray = [] secondArrayCounter = 0 for i in range(0, firstConfList.__len__()): firstListLine = firstConfList[i].strip() addFlag = True for j in range(secondArrayCounter, secondConfList): secondListLine = secondConfList[j].strip() if firstListLine is secondListLine: addFlag = False break secondArrayCounter += 1 if addFlag: resultArray.append(firstListLine) return resultArray @staticmethod def compareConfigurationFileResultWithFile(firstConfFile, secondConfFile): """ INFO: This function will return to result of Files list compare WARNING: If parameter different class object from list, it will raise error or if file error occured on that block it will re-raise excepton :param firstConfFile: First conf file name :param secondConfFile: Second conf file name :return: LIST[STRING] """ try: return FileUtil.compareConfigurationFileResult( FileUtil.getFileLinesAsList(firstConfFile), FileUtil.getFileLinesAsList(secondConfFile)) except Exception as exception: raise exception @staticmethod def removeFileInCurrentPath(fileName): """ INFO: This function will remove current path file :param fileName: which will removes the file name :return: NONE """ path = os.path.dirname(os.path.realpath(__file__)) dirs = os.listdir(path) for file in dirs: if fileName in file: os.remove(file) @staticmethod def removeFileInExternalPath(fileName, pathName): """ INFO: This function will remove specific path file :param fileName: which will removes the file name :param pathName: which will removes the files pathName :return: NONE """ path = os.path.dirname(pathName) dirs = os.listdir(path) for file in dirs: if fileName in file: os.remove(file) @staticmethod def dataFileSplit(fileName, maximumChaperSize=500 * 1024 * 1024, memoryBufferSize=50 * 1024 * 1024 * 1024): """ INFO: This function will sperate files to datasize. This file applicable for data file :param fileName: fileName for which is the source file name :param maximumChaperSize: Maximum chapter size default value 500MB :param memoryBufferSize: Memory buffer size default vaule 50GB :return: NONE """ chapterCount = 0 bufferText = '' with open(fileName, 'rb') as src: while True: target = open(fileName + '.%03d' % chapterCount, 'wb') written = 0 while written < maximumChaperSize: if len(bufferText) > 0: target.write(bufferText) target.write( src.read( min(memoryBufferSize, maximumChaperSize - written))) written += min(memoryBufferSize, maximumChaperSize - written) bufferText = src.read(1) if len(bufferText) == 0: break target.close() if len(bufferText) == 0: break chapterCount += 1 @staticmethod def textFileSplit(fileName, lineCount=20, outputFileName="output.txt"): """ INFO: This function will sperate files to line count, this is applicable for text file :param fileName: fileName for which is the source file name :param lineCount: Split for each file default value 20 :param outputFileName: Output file names for each one output.1.txt, output.2.txt, etc. :return: NONE """ fOpen = open(fileName, 'r') count = 0 at = 0 dest = None for line in fOpen: if count % lineCount == 0: if dest: dest.close() dest = open(outputFileName + str(at) + '.txt', 'w') at += 1 dest.write(line) count += 1 @staticmethod def zipFiles(zipFileName, fileName, filesPath=os.path.dirname(os.path.realpath(__file__))): """ INFO: This function will return to Zipped file name which is the insert to function argument. TRICKS: if fileName will insert to keywork it will zip as file name :param zipFileName: Zipped last file name :param fileName: Source file name :param filesPath: Source file name source path :return: STRING """ try: fileName = fileName.strip() ziph = zipfile.ZipFile(zipFileName.strip() + '.zip', 'w', zipfile.ZIP_DEFLATED) for root, dirs, files in os.walk(filesPath): for file in files: if fileName in file: ziph.write(os.path.join(file)) ziph.close() except Exception as e: raise IOError("File IO process has been occured IOError : " + str(e)) return filesPath + '/' + fileName
class FtpUtil: """ THIS IS NOT DESCRIPTION FOR "CommandUtil.py" CLASS. This part represent to Static Initiliazer Code on Java, it's check to version for the avaiable version. At least current Python version must be 3.6.4+, anything else you should update Python version """ #################################################################################################################### if not packageControl.checkVersionForApplicable(3, 6, 4): raise EnvironmentError( "At least current must be 3.6.4+, anything else you should update Python version !!!" ) if packageControl.getPlatformInfo( ) != 'Linux' and packageControl.getPlatformInfo() != 'OS X': raise SystemError( "This class only execute Linux or Posix OS platform !!!") #################################################################################################################### def __init__(self): """ INFO: Default Constructure cannot be Creatable WARNING: If attand to create this object, it will raise NotImplementedError """ assert NotImplementedError( "This object useing only library, cannot be creatable!!!") def __new__(cls): """ INFO: This method was overrided only avoided to __new__ operator :return: NOISE_OBJECT """ return object.__new__(cls) class AllowAnythingPolicy(paramiko.MissingHostKeyPolicy): """ This Noise class only check paramiko policy and Marker class for ftp connection """ def missing_host_key(self, client, hostname, key): """ INFO: This Function only created for dummy function. It's not necessary for create to body of function. :param client: DUMMY PARAMETER :param hostname: DUMMY PARAMETER :param key: DUMMY PARAMETER :return: NONE """ return @staticmethod def occureConnectionError(e): """ INFO: This function will raise error :param e: Error Message :return: NONE """ raise ConnectionError( "This upload function has been occured error : " + str(e)) @staticmethod def getFtpConnection(user, password, ip, port): """ INFO: This function will return to Ftp Object :param user: Remote FTP user :param password: Remote FTP password :param ip: Remote FTP ip :param port: Remote FTP port :return: FTP OBJECT : Opened FTP Class Object """ ftp = FTP() ftp.connect(ip, port) ftp.login(user, password), return ftp @staticmethod def uploadFile(ftpConnection, fileName, binaryFlag=True): """ INFO: This function will send your declerated file with connection and path WARNING: Dont use this function with directly, it was designed for using other functions TRICKS: The file name could be contain with path, it can obtain certain result :param ftpConnection: This argument must be contain ftplibs FTP class :param fileName: File name for upload :param binaryFlag: Upload file transfer can be binary : it is defult True :return: NONE """ try: uploadFileName = open(fileName, 'r') pathSplit = fileName.strip('/').split('/') finalFileName = pathSplit[len(pathSplit) - 1] print('Uploading ' + finalFileName + '...') if binaryFlag: ftpConnection.storbinary('STOR ' + finalFileName, uploadFileName) else: ftpConnection.storlines('STOR ' + finalFileName, uploadFileName) print('Upload Finished...') except Exception as e: FtpUtil.occureConnectionError(e) @staticmethod def transferedByteDisplay(fileName, bytesToFar, totalByte): """ INFO: This function will display number of the how many bytes transfered. This function usefull for ftp, sftp Internal modules inner callable functions. :param fileName: Filename :param bytesToFar: The proceeded byte count :param totalByte: Total byte count of the filename :return: NONE """ print('Transfer of %r is at %d/%d bytes (%.1f%%)' % (fileName, bytesToFar, totalByte, 100. * bytesToFar / totalByte)) @staticmethod def connectHostGetFile(fileName, filePath, userName, password, ip, port=22): """ INFO: This function will connect the remote host and get from file with ftp. TRICK: fileName argument search as keyword, so you can insert only contain that string. :param fileName: File name in the declerated path :param filePath: File in the remote path :param userName: Remote username :param password: Remote password :param ip: Remote ip address :param port: Remote port, default value is 22 in argument :return: NONE """ try: sftp = paramiko.SSHClient() sftp.set_missing_host_key_policy(FtpUtil.AllowAnythingPolicy()) sftp.connect(ip, port, userName, password, allow_agent=False, look_for_keys=False) except Exception as ex: raise ConnectionError("Connection Error : {0}".format(str(ex))) sftp_last = sftp.open_sftp() sftp_last.chdir(filePath) for filename in sorted(sftp_last.listdir()): filename = filename.strip() if fileName in filename: callback_for_filename = functools.partial( FtpUtil.transferedByteDisplay, filename) sftp_last.get(filename, filename, callback=callback_for_filename) sftp_last.close() sftp.close() @staticmethod def connectHostSendFileWithFtp(user, password, ip, fileName, filePath=os.path.dirname( os.path.realpath(__file__)), port=21): """ INFO: This funtion will send your file with FTP TRICK: portname optional insert, it can change your on demand :param user: Remote FTP user :param password: Remote FTP password :param ip: Remote FTP ip :param port: Remote FTP port :param fileName: File name :param filePath: File path : Default value is current path :return: NONE """ dirs = os.listdir(filePath) try: ftpCon = FtpUtil.getFtpConnection(user, password, ip, port) for file in dirs: if fileName in file: FtpUtil.uploadFile(ftpCon, file) except Exception as e: FtpUtil.occureConnectionError(e) @staticmethod def connectHostSendFileWithSftp(user, password, ip, destinationFilePath, sourceFileWithPath): """ INFO: This function will send your file with SFTP WARNING: sourceFileWithPath argument must be path info :param user: Remote FTP user :param password: Remote FTP password :param ip: Remote FTP ip :param destinationFilePath: Destination file path in remote path :param sourceFileWithPath: Source file name with path :return: NONE """ try: srv = pysftp.Connection(host=ip, username=user, password=password) with srv.cd(destinationFilePath): srv.put(sourceFileWithPath) srv.close() except Exception as e: FtpUtil.occureConnectionError(e)
class MysqlUtil: """ THIS IS NOT DESCRIPTION FOR "CommandUtil.py" CLASS. This part represent to Static Initiliazer Code on Java, it's check to version for the avaiable version. At least current Python version must be 3.6.4+, anything else you should update Python version """ #################################################################################################################### if not packageControl.checkVersionForApplicable(3, 6, 4): raise EnvironmentError( "At least current must be 3.6.4+, anything else you should update Python version !!!" ) if packageControl.getPlatformInfo( ) != 'Linux' and packageControl.getPlatformInfo() != 'OS X': raise SystemError( "This class only execute Linux or Posix OS platform !!!") if not packageControl.isExistCommand("mysqldump"): raise ModuleNotFoundError( "mysqldump command is not found, it should be implemented !!!") #################################################################################################################### def __init__(self): """ INFO: Default Constructure cannot be Creatable WARNING: If attand to create this object, it will raise NotImplementedError """ assert NotImplementedError( "This object useing only library, cannot be creatable!!!") def __new__(cls): """ INFO: This method was overrided only avoided to __new__ operator :return: NOISE_OBJECT """ return object.__new__(cls) @staticmethod def getDatabaseBackup(user, password, databaseName, backupPath, host=""): """ INFO: This function will use the msqldump command and it will create file with defined path. If the path is not it will create that path. Also which is the contain SQL scripts in the sql extension file. WARNING: That function only running existing msqldump and posix systems. Anything else it will raise Error :param host: Hostname of database :param user: User authentication name :param password: Authentication password :param databaseName: Database name in operataring system :param backupPath: it will declare to path of the that path, if doesnt exist it will create that path name :return: NONE """ flag = False dateTimeInfo = time.strftime('%d_%m_%Y__%H_%M_%S') if backupPath.endswith('/'): backupPath += dateTimeInfo else: backupPath += ('/' + dateTimeInfo) if not os.path.exists(backupPath): os.makedirs(backupPath) if os.path.exists(databaseName): fileOpen = open(databaseName) fileOpen.close() flag = True if flag: fileOpen = open(databaseName, "r") fileLength = len(fileOpen.readlines()) fileOpen.close() filePointer = 1 dbfile = open(databaseName, "r") while filePointer <= fileLength: database = dbfile.readline() database = database[:-1] if host: dumpcmd = "mysqldump -h " + host + " -u " + user + " -p" + password + " " + database + " > " + backupPath + "/" + database + ".sql" else: dumpcmd = "mysqldump -u " + user + " -p" + password + " " + database + " > " + backupPath + "/" + database + ".sql" os.system(dumpcmd) filePointer += 1 dbfile.close() else: if host: dumpcmd = "mysqldump -h " + host + " -u " + user + " -p" + password + " " + databaseName + " > " + backupPath + "/" + databaseName + ".sql" else: dumpcmd = "mysqldump -u " + user + " -p" + password + " " + databaseName + " > " + backupPath + "/" + databaseName + ".sql" os.system(dumpcmd)
class CommandUtil: """ THIS IS NOT DESCRIPTION FOR "CommandUtil.py" CLASS. This part represent to Static Initiliazer Code on Java, it's check to version for the avaiable version. At least current Python version must be 3.6.4+, anything else you should update Python version """ #################################################################################################################### if not packageControl.checkVersionForApplicable(3, 6, 4): raise EnvironmentError( "At least current must be 3.6.4+, anything else you should update Python version !!!" ) if packageControl.getPlatformInfo( ) != 'Linux' and packageControl.getPlatformInfo() != 'OS X': raise SystemError( "This class only execute Linux or Posix OS platform !!!") #################################################################################################################### def __init__(self): """ INFO: Default Constructure cannot be Creatable WARNING: If attand to create this object, it will raise NotImplementedError """ assert NotImplementedError( "This object useing only library, cannot be creatable!!!") def __new__(cls): """ INFO: This method was overrided only avoided to __new__ operator :return: NOISE_OBJECT """ return object.__new__(cls) @staticmethod def isDirectory(path): """ INFO: This function checking directory applicable :param path: File path on which is the target source :return: BOOLEAN """ return os.path.isdir(path) @staticmethod def isDirectoryExist(path): """ INFO: This function checking directory exist state :param path: File path on which is the target source :return: BOOLEAN """ return os.path.exists(path) @staticmethod def checkFile(path): """ INFO: This function checks the path state WARNING: If Not exist or not avaiable format raise exception :param path: File path on which is the target source :return: """ if (not CommandUtil.isDirectory(path)): raise IsADirectoryError("Parameter not Directory!!!") if (not CommandUtil.isDirectory(path)): raise NotADirectoryError("Directory does not exist !!!") @staticmethod def sendCommandGetResult(command): """ INFO: This function running on Subprocess library and OS dependent. It can run on MAC, WIN, LNX :param command: This parameter will insert on OS :return: STRING """ return osCommand.getoutput(command) @staticmethod def sendCommandGetResultAsList(command): """ INFO: This function running on Subprocess library and OS dependent. It can run on MAC, WIN, LNX :param command: This parameter will insert on OS :return: LIST[STRING] """ return re.split('[\n]+', osCommand.getoutput(command)) @staticmethod def getLastUpdatedFileName(nullable=None): """ COMMAND: "ls -Artf | tail -n 1" INFO: This function is execute running only current directory WARNING: This Function should be use LinuxOS :return: STRING """ return osCommand.getoutput("ls -Artf | tail -n 1") @staticmethod def getLastUpdatedFileNameWithDirectory(path): """ INFO: This function return added directory infos last file name WARNING: If file does not exist or Format not avaiable it will raise Error :param path: File path on which is the target source :return: STRING """ CommandUtil.checkFile(path) return osCommand.getoutput("ls -Artf " + path + " | tail -n 1") @staticmethod def getLastUpdatedFileNameWithDirectoryAndPath(path): """ INFO: This function return added directory infos last file name with directory WARNING: If file does not exist or Format not avaiable it will raise Error :param path: File path on which is the target source :return: STRING """ return path.rstrip( '/') + '/' + CommandUtil.getLastUpdatedFileNameWithDirectory(path) @staticmethod def getFilesInfoIfKeyContainInDirectory(path, key): """ COMMAND: "grep -rnw '<PATH>' -e '<KEY>'" INFO: This function search on path existing files and return which are the exsiting keyword array WARNING: This Function should be use LinuxOS :param path: Path of which is the important file tree :param key: This parameter will search all files :return: LIST[STRING] """ CommandUtil.checkFile(path) return re.split( '[\n]+', osCommand.getoutput("grep -rnw \'" + path + "\' -e \'" + key + "\'")) @staticmethod def getFilesInfoIfKeyContainInDirectoryAsString(path, key): """ COMMAND: "grep -rnw '<PATH>' -e '<KEY>'" INFO: This function search on path existing files and return which are the exsiting keyword string WARNING: This Function should be use LinuxOS :return: STRING """ CommandUtil.checkFile(path) return osCommand.getoutput("grep -rnw \'" + path + "\' -e \'" + key + "\'") @staticmethod def getCurrentFilesNameAsList(parameter="-l"): """ COMMAND: "ls" INFO: This function return the file names as list on absolute path :param parameter: This parameter will insort with ls command default value is "-l" :return: LIST[STRING] """ return re.split('[\n]+', osCommand.getoutput("ls " + parameter.strip()))