Source code for mojo.libdist.SourceCodeManager

"""
SourceCodeManager
=================
Contains class 'SourceCodeManager'
Organize the source code and copy from local disk
"""


import os
import subprocess
import tarfile
import tempfile

from ..bricabrac.fileIO import removeFile, globCopy

from . import libdistData


[docs]class SCM: """ Get source code from disk. :param path: path :type path: string """ def __init__(self, path): """ Constructor :param path: path :type path: string """ self.path = path self.pathToSourceCodeTarFile = "" self.tempDir = os.curdir self.traceSuitePath = libdistData.TRACE_SUITE def setTempDir(self): self.tempDir = tempfile.mkdtemp() def _copyFile(self, clean=True): self.traceSuitePath = os.path.join(self.tempDir) # make clean if clean: initialCleanUpCommand = libdistData.MAKE_COMMAND.format(1) + " " + "clean" initialCleanUpProcess = subprocess.Popen(initialCleanUpCommand.split(), cwd=self.path, stdout=subprocess.PIPE, stderr=subprocess.PIPE) _, _ = initialCleanUpProcess.communicate() self.traceSuitePath = os.path.join(self.tempDir, libdistData.TRACE_SUITE) if os.path.exists(self.traceSuitePath): removeFile(self.traceSuitePath) globCopy(self.path, self.tempDir, match=True) oldName = os.path.basename(os.path.normpath(self.path)) if oldName != libdistData.TRACE_SUITE: oldPath = os.path.join(self.tempDir, oldName) os.rename(oldPath, self.traceSuitePath) if not clean: initialCleanUpCommand = libdistData.MAKE_COMMAND.format(1) + " " + "clean" initialCleanUpProcess = subprocess.Popen(initialCleanUpCommand.split(), cwd=self.traceSuitePath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) _, _ = initialCleanUpProcess.communicate() def getData(self, clean=True): self._copyFile(clean=clean)
[docs] def cleanUp(self): """Remove all temporary files and directories. """ removeFile(self.tempDir) self.tempDir = ""
[docs]class SourceCodeManager(SCM): """ Get source code form disk and create a tar file with source code of all projects and return path to this file. :param path: path :type path: string """
[docs] def createTarFile(self): """Create source code tar file containing the source code of all projects form global projects list using specifications from command line parameters. """ # copy the source code from a local version of the trace suite self.traceSuitePath = self.path # make clean initialCleanUpCommand = libdistData.MAKE_COMMAND.format(1) + " " + "clean" initialCleanUpProcess = subprocess.Popen(initialCleanUpCommand.split(), cwd=self.traceSuitePath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) _, _ = initialCleanUpProcess.communicate() self.pathToSourceCodeTarFile = os.path.join(self.tempDir, libdistData.SOURCE_CODE_TARFILE) with tarfile.open(self.pathToSourceCodeTarFile, "w:gz") as tar: tar.add(self.traceSuitePath, arcname=libdistData.TRACE_SUITE)
[docs] def cleanUp(self): """Remove all temporary files and directories. """ removeFile(self.tempDir) self.tempDir = ""
def __enter__(self): self.setTempDir() self.createTarFile() return self def __exit__(self, *args): self.cleanUp()