#!python3
# Copyright 2007-2017 Gemr. All Rights Reserved.
# Licensed to MIT see LICENSE.txt
import os
import warnings
import gdeps as GDeps
__author__ = 'Suryavarman (http://sourceforge.net/u/suryavarman/profile/)'
[docs]class Versioning(GDeps.Config, GDeps.Reporting):
def __init__(self, inConfigFile, inFolderPath, inLogFileDirectory, inReposUrl, inCloneArgs, inUpdateArgs, inSectionName, inExePath):
""" Versionning software initialization """
GDeps.Config.__init__(self, inConfigFile, inSectionName)
GDeps.Reporting.__init__(self)
self.m_FolderPath = inFolderPath
""" create this sub folder name and checkout inside it """
self.m_LogFileDirectory = inLogFileDirectory
""" store the log file directory """
self.m_ReposUrl = inReposUrl
""" repository url """
self.m_CloneArgs = inCloneArgs
"""( optional argument ) to clone the repository
"""
self.m_UpdateArgs = inUpdateArgs
"""( optional argument ) to update the repository
"""
self.m_ExePath = inExePath
self.Load()
[docs] def clone(self):
""" clone the repository on the current directory
:rtype: GDeps.MacroReporting
"""
return GDeps.MacroReporting()
[docs] def update(self):
""" update the repository on the current directory
:rtype: GDeps.MacroReporting
"""
return GDeps.MacroReporting()
[docs] def clean(self):
""" Clean the repository on the current directory before to clone
:rtype: GDeps.MacroReporting
"""
return GDeps.MacroReporting()
[docs] def exist(self):
return os.path.exists(self.m_FolderPath) and os.path.exists(self.m_FolderPath + '\\' + self.getFolderRepoConfigPath())
[docs] def getFolderRepoConfigPath(self):
""" has to return the sub folder repository configuration name :
**Example**
.git
.svn
.hg
"""
pass
[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 = os.path.normpath(self.m_LogFileDirectory + r'\\' + inLogFileName + '.log')
aCommand = inCommand + ' ' + ('>>' if inAppend else '>') + ' "' + aLogFilePath + '" 2>&1'
returncode, returnstdoutlines = GDeps.call(aCommand)
inReport.append(inReportParser, inLogFileName, aLogFilePath, False)
return returncode, returnstdoutlines
[docs] def getCloneTmpDir(self):
return os.path.normpath(self.m_FolderPath + r"\\.gdeps_tmp")
[docs] def CloneNoEmptyDir(self, inCommand, inLogFileName, inReport, inCallInFolderDir=False):
""" Helper to force the cloning inside a non empty directory
:param inCommand: the clone command to the temporary directory.
This directory has to be obtain by this function :
self.getCloneTmpDir()
:param inLogFileName: See the function :func:`GDeps.Versioning.callWithLogFile`
:param inReport: See the function :func: GDeps.Versioning.callWithLogFile
:param inCallInFolderDir: If True to call the application self.getCloneTmpDir() will be the current dir.
"""
if not os.path.exists(self.m_FolderPath):
os.mkdir(self.m_FolderPath)
if not os.path.exists(self.m_FolderPath):
assert False, "We cannot create the directory : " + self.m_FolderPath
""" Clone into a non empty directory
.. note::
https://stackoverflow.com/questions/2411031/how-do-i-clone-into-a-non-empty-directory
"""
aTemporyFolder = self.getCloneTmpDir()
aLogFilePath = os.path.normpath(self.m_LogFileDirectory + r'\\' + inLogFileName + '.log')
# aRemoveFolderCommand = 'rmdir /S /Q '
if os.path.exists(aTemporyFolder):
GDeps.rmdir(aTemporyFolder, aLogFilePath, )
inReport.append(GDeps.rmdir_Report(), inLogFileName, aLogFilePath, False)
# self.callWithLogFile(aRemoveFolderCommand + aTemporyFolder, inLogFileName, inReport, GDeps.rmdir_Report(), True)
currendir = os.getcwd()
if inCallInFolderDir:
os.chdir(self.getFolderRepoConfigPath())
self.callWithLogFile(inCommand, inLogFileName, inReport, self)
os.chdir(currendir)
if not inReport.getlen(inReport.m_Errors):
""" Better do not create a Folder Repo Config Path ( .git, .hg, .svn )
Move .svn/.hg/.git to the original folder
.. note::
`XCOPY <http://www.computerhope.com/xcopyhlp.htm>`_
"""
aLocalFolder = '\\' + self.getFolderRepoConfigPath()
aTmpFolder = aTemporyFolder + aLocalFolder
aFolder = self.m_FolderPath + aLocalFolder
if os.path.exists(aFolder):
GDeps.rmdir(aFolder, aLogFilePath)
inReport.append(GDeps.rmdir_Report(), inLogFileName, aLogFilePath, False)
# if os.path.exists(aFolder):
# self.callWithLogFile(aRemoveFolderCommand + aFolder, inLogFileName, inReport, GDeps.rmdir_Report(), True )
os.mkdir(aFolder)
# aXcopyCommand = 'xcopy ' + aTmpFolder + ' ' + aFolder + ' /H /K /Y /E /I'
# self.callWithLogFile(aXcopyCommand, inLogFileName, inReport, GDeps.copy_Report(), True)
GDeps.copy(aTmpFolder, aFolder, aLogFilePath)
inReport.append(GDeps.copy_Report(), inLogFileName, aLogFilePath, False)
# else:
# warnings.warn('There some errors we prefer to stop the cloning operation.')
# inReport.printIssues()
# Delete the temporary directory
# GDeps.call(aRemoveFolderCommand + aTemporyFolder)
GDeps.rmdir(aTemporyFolder, aLogFilePath)
inReport.append(GDeps.rmdir_Report(), inLogFileName, aLogFilePath, False)
if not inReport.getlen(inReport.m_Errors):
""" Revert the missing files """
inReport.append2(self.clean())