Source code for gdeps.archive

#!python3

# Copyright 2007-2017 Gemr. All Rights Reserved.
# Licensed to MIT see LICENSE.txt

import os
import shutil
import urllib.request
import gdeps as GDeps

__author__ = 'Suryavarman (http://sourceforge.net/u/suryavarman/profile/)'


[docs]class Archive( GDeps.Config, GDeps.Reporting ) : def __init__( self, inConfigFile, inFolderPath, inLogFileDirectory, inArchiveUrl, inSectionName, inExePath ): """ Archive software/lib initialization """ GDeps.Config.__init__( self, inConfigFile, inSectionName ) GDeps.Reporting.__init__( self ) self.m_FolderPath = inFolderPath """ create this sub folder name and extract inside it. """ self.m_LogFileDirectory = inLogFileDirectory """ store the log file directory """ self.m_ArchiveUrl = inArchiveUrl """ Archive url """ self.m_ExePath = inExePath self.Load()
[docs] def download( self ): """ Download the archive on the current directory :return: The reporting. :rtype: GDeps.MacroReporting .. note:: https://docs.python.org/3.5/library/urllib.request.html#module-urllib.request https://docs.python.org/3.0/library/urllib.request.html .. todo:: Write a report """ self.m_ArchiveFileName = urllib.request.url2pathname(self.m_ArchiveUrl).split('\\')[-1] """ Archive local filename.""" aReport = GDeps.MacroReporting() def reportHook( inParam0, inParam1, inParam2 ): """ :param inParam0: :type inParam0: int :param inParam1: :type inParam1: int :param inParam2: :type inParam2: int """ #print( 'inParam0 ' + str(inParam0) ) #print( 'inParam1 ' + str(inParam1) ) #print( 'inParam2 ' + str(inParam2) ) pass aFilePath, aHeaders = urllib.request.urlretrieve( url = self.m_ArchiveUrl, reporthook = reportHook ) assert os.path.isfile( aFilePath ), "The archive cannot be download. : " + aFilePath shutil.copyfile( aFilePath, self.m_ArchiveFileName ) urllib.request.urlcleanup() assert os.path.isfile( self.m_ArchiveFileName ), "The archive cannot be download. : " + os.getcwd() + '\\' + self.m_ArchiveFileName return aReport
[docs] def extract( self ): """ Override this method to extract the archive in inFolderPath :return: The reporting. :rtype: GDeps.MacroReporting .. note:: Create the folder to extract the archive. """ os.mkdir( self.m_FolderPath ) if( not os.path.exists( self.m_FolderPath ) ): assert False, ( "We cannot create the directory : " + self.m_FolderPath ) return GDeps.MacroReporting()
[docs] def getExtractDir(self): return self.m_FolderPath + "\\" + self.m_ArchiveFileName[0:self.m_ArchiveFileName.rfind('.')]
[docs] def read( self, inSection ): self.m_ExePath = inSection[ GDeps.Keys.ms_ExePath ] assert os.path.isfile( self.m_ExePath ), "This application doesn't exist : " + self.m_ExePath
# return aSection
[docs] def write( self ): aSection = GDeps.Config.write( self ) aSection[ GDeps.Keys.ms_ExePath ] = self.m_ExePath return aSection
[docs] def getCMDExePath( self ): return '"' + self.m_ExePath + '" '
[docs] def callWithLogFile( self, inCommand, inLogFileName, inReport, inReportParser, inAppend = False ): """ Useful to call a command line, add and fill the reporting. :param inLogFileName: the log filename without wildcard. :type inLogFileName: str :param inAppend: if True append standard output at the end of file, if False overwrite the file :type inAppend: bool .. note:: http://www.robvanderwoude.com/redirection.php """ aLogFilePath = self.m_LogFileDirectory + '\\' + inLogFileName + '.log' aCommand = inCommand + ' ' + ('>>' if inAppend else '>') + ' "' + aLogFilePath + '" 2>&1' returncode, returnstdoutlines = GDeps.call( aCommand ) inReport.append( inReportParser, inLogFileName, aLogFilePath, False ) return returncode, returnstdoutlines