<commit_before># Tulip import plugin written in Python for importing the dependencies graph of a
# CMake project (target -> target dependencies + target -> external libraries dependencies)

# The plugin use the graphviz output feature of CMake enabling to generate dot files
# representing the different dependencies graphs of the project (global + target specific)
# The global dot file is then imported in Tulip through the graphviz import plugin
# included in the framework

from tulip import *
import tulipplugins
import tempfile
import subprocess
import os
import sys
import shutil

cmakeProjectSourceDirParamName = "dir::CMake project source dir"
cmakeExecutableParamName = "file::CMake executable"
cmakeParametersParamName = "CMake parameters"

cmakeProjectDotFileName = "cmake_project" 

class CMakeDependenciesGraphImport(tlp.ImportModule):
	def __init__(self, context):
		tlp.ImportModule.__init__(self, context)
		# you can add parameters to the plugin here through the following syntax
		# self.add<Type>Parameter("<paramName>", "<paramDoc>", "<paramDefaultValue>")
		# (see documentation of class tlp.WithParameter to see what types of parameters are supported)
		self.addStringParameter(cmakeProjectSourceDirParamName, "The root source directory of the CMake project")
		self.addStringParameter(cmakeExecutableParamName, """Optional parameter in order to provide the path to the CMake executable.
By default CMake executable path is assumed to be in your PATH environment variable""", "cmake")
		self.addStringParameter(cmakeParametersParamName, "Optionnal parameter for providing some parameters to CMake in order to correctly configure the project")
		

	def importGraph(self):
		# This method is called to import a new graph.
		# An empty graph to populate is accessible through the "graph" class attribute
		# (see documentation of class tlp.Graph).

		# The parameters provided by the user are stored in a Tulip DataSet 
		# and can be accessed through the "dataSet" class attribute
		# (see documentation of class tlp.DataSet).
	
		# The method must return a boolean indicating if the
		# graph has been successfully imported.
	
		# Check that we are using at least Tulip 4.8
		tulipVersionNumbers = list(map(lambda s: int(s), tlp.getTulipRelease().split('.')))	
		tulipVersionOk = tulipVersionNumbers[0] >= 4 and tulipVersionNumbers[1] >= 8	
		if not tulipVersionOk:
			if self.pluginProgress:
				self.pluginProgress.setError('Error : Minimum Tulip version to execute that plugin is 4.8.0')
			return False
	
		# get the CMake project source directory path
		cmakeProjectSourceDir = self.dataSet[cmakeProjectSourceDirParamName]
		
		# create a temporary directory that we will use as a CMake binary directory
		tmpdir = tempfile.mkdtemp()

		# build the command line to execute cmake and generate the dot files
		command = self.dataSet[cmakeExecutableParamName] + ' ' + self.dataSet[cmakeParametersParamName] + ' ' \
		          + ' --graphviz=' + cmakeProjectDotFileName + ' ' + cmakeProjectSourceDir
		
		# create a temporary file in which we will redirect stderr
		stderrfile = tmpdir + '/stderr.txt'
		
		# execute the cmake process
		p = subprocess.Popen(command, cwd=tmpdir, shell=True, stdout=subprocess.PIPE, stderr=open(stderrfile, 'wb'))
		# add some execution feedback trough the plugin progress
		for line in p.stdout.readlines():
			lineStr = line.decode('utf-8')[:-1]
			if self.pluginProgress:
				self.pluginProgress.setComment(lineStr)
		
		# wait for the process to complete
		retval = p.wait()	
		
		# something went wrong
		if retval != 0:
			# read stderr file and transmit error string to the plugin progress
			errors = open(stderrfile, 'rb').read().decode('utf-8')
			if self.pluginProgress:
				self.pluginProgress.setError(errors)
			# remove temporary directory
			shutil.rmtree(tmpdir)
			return False	
		
		# check if CMake has generated the expected dot file
		cmakeProjectDotFilePath = tmpdir + '/' + cmakeProjectDotFileName
		if not os.path.exists(cmakeProjectDotFilePath):
			cmakeProjectDotFilePath = tmpdir + '/' + cmakeProjectDotFileName + '.dot'
			if not os.path.exists(cmakeProjectDotFilePath):
				self.pluginProgress.setError("Error : the graphviz file has not been generated by CMake")
			# remove temporary directory	
			shutil.rmtree(tmpdir)	
			return False	
		
		# import the dot file in Tulip trough the graphviz import plugin<commit_msg>read cmake stdout line by line in order for the plugin progress to correctly display execution feedback<commit_after># Tulip import plugin written in Python for importing the dependencies graph of a
# CMake project (target -> target dependencies + target -> external libraries dependencies)

# The plugin use the graphviz output feature of CMake enabling to generate dot files
# representing the different dependencies graphs of the project (global + target specific)
# The global dot file is then imported in Tulip through the graphviz import plugin
# included in the framework

from tulip import *
import tulipplugins
import tempfile
import subprocess
import os
import sys
import shutil

cmakeProjectSourceDirParamName = "dir::CMake project source dir"
cmakeExecutableParamName = "file::CMake executable"
cmakeParametersParamName = "CMake parameters"

cmakeProjectDotFileName = "cmake_project" 

class CMakeDependenciesGraphImport(tlp.ImportModule):
	def __init__(self, context):
		tlp.ImportModule.__init__(self, context)
		# you can add parameters to the plugin here through the following syntax
		# self.add<Type>Parameter("<paramName>", "<paramDoc>", "<paramDefaultValue>")
		# (see documentation of class tlp.WithParameter to see what types of parameters are supported)
		self.addStringParameter(cmakeProjectSourceDirParamName, "The root source directory of the CMake project")
		self.addStringParameter(cmakeExecutableParamName, """Optional parameter in order to provide the path to the CMake executable.
By default CMake executable path is assumed to be in your PATH environment variable""", "cmake")
		self.addStringParameter(cmakeParametersParamName, "Optionnal parameter for providing some parameters to CMake in order to correctly configure the project")
		

	def importGraph(self):
		# This method is called to import a new graph.
		# An empty graph to populate is accessible through the "graph" class attribute
		# (see documentation of class tlp.Graph).

		# The parameters provided by the user are stored in a Tulip DataSet 
		# and can be accessed through the "dataSet" class attribute
		# (see documentation of class tlp.DataSet).
	
		# The method must return a boolean indicating if the
		# graph has been successfully imported.
	
		# Check that we are using at least Tulip 4.8
		tulipVersionNumbers = list(map(lambda s: int(s), tlp.getTulipRelease().split('.')))	
		tulipVersionOk = tulipVersionNumbers[0] >= 4 and tulipVersionNumbers[1] >= 8	
		if not tulipVersionOk:
			if self.pluginProgress:
				self.pluginProgress.setError('Error : Minimum Tulip version to execute that plugin is 4.8.0')
			return False
	
		# get the CMake project source directory path
		cmakeProjectSourceDir = self.dataSet[cmakeProjectSourceDirParamName]
		
		# create a temporary directory that we will use as a CMake binary directory
		tmpdir = tempfile.mkdtemp()

		# build the command line to execute cmake and generate the dot files
		command = self.dataSet[cmakeExecutableParamName] + ' ' + self.dataSet[cmakeParametersParamName] + ' ' \
		          + ' --graphviz=' + cmakeProjectDotFileName + ' ' + cmakeProjectSourceDir
		
		# create a temporary file in which we will redirect stderr
		stderrfile = tmpdir + '/stderr.txt'
		
		# execute the cmake process
		p = subprocess.Popen(command, cwd=tmpdir, shell=True, stdout=subprocess.PIPE, stderr=open(stderrfile, 'wb'))
		# add some execution feedback trough the plugin progress
		while True:
			line = p.stdout.readline().decode('utf-8').replace('\r\n', '').replace('\n', '')
			if line == '' and p.poll() != None:
				break
			if self.pluginProgress:
				self.pluginProgress.setComment(line)
		
		# wait for the process to complete
		retval = p.wait()	
		
		# something went wrong
		if retval != 0:
			# read stderr file and transmit error string to the plugin progress
			errors = open(stderrfile, 'rb').read().decode('utf-8')
			if self.pluginProgress:
				self.pluginProgress.setError(errors)
			# remove temporary directory
			shutil.rmtree(tmpdir)
			return False	
		
		# check if CMake has generated the expected dot file
		cmakeProjectDotFilePath = tmpdir + '/' + cmakeProjectDotFileName
		if not os.path.exists(cmakeProjectDotFilePath):
			cmakeProjectDotFilePath = tmpdir + '/' + cmakeProjectDotFileName + '.dot'
			if not os.path.exists(cmakeProjectDotFilePath):
				self.pluginProgress.setError("Error : the graphviz file has not been generated by CMake")
			# remove temporary directory	
			shutil.rmtree(tmpdir)	
			return False	
		
		# import the dot file in Tulip trough the graphviz import plugin
{'fraction_non_alphanumeric': 0.07544856792600023, 'fraction_numerical': 0.0027861361863367883, 'mean_word_length': 4.498774509803922, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2862236', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2426, 'n_words': 932}