<gh_stars>1-10
import argparse
import distutils.spawn
import os
import platform
import re
import subprocess



parser = argparse.ArgumentParser(description="Connected Vision Module Generator")
parser.add_argument("-m", "--moduleDir", help="root directory of module which contains the def directory with the module description", type=str, required=True)
parser.add_argument("-s", "--settingsFile", help="generator settings JSON file path", type=str, required=False, default="")
parser.add_argument("-v", "--verbose", help="verbose output", required=False, action="store_true")
args = parser.parse_args()

# check if node (Node.js executable) is found on the system path
if not distutils.spawn.find_executable("node"):
	scriptFile = "activate." + ("bat" if platform.system() == "Windows" else "sh")
	
	raise Exception("\n\nFailed to locate node (Node.js executable).\nPlease execute the Connected Vision environment activation script" + (" by calling \"source ./" + scriptFile + "\"" if platform.system() == "Linux" else "") + ".\nThe " + scriptFile + " file is generated by Conan when calling the following command:\nconan install ConnectedVision/x.y.z@covi/stable -b outdated -g virtualenv")

# check the module directory
if not os.path.isdir(args.moduleDir) or not os.path.exists(args.moduleDir):
	raise Exception("\n\nthe specified module directory path is invalid: \"" + args.moduleDir + "\"")

moduleDir = os.path.normpath(args.moduleDir)

settingsFile = ""

# check the settings file
if args.settingsFile:
	if not os.path.isfile(args.settingsFile) or not os.path.exists(args.settingsFile):
		raise Exception("\n\nthe specified settings file path is invalid: \"" + args.settingsFile + "\"")

	settingsFile = os.path.normpath(args.settingsFile)

# check the ConnectedVision environment variable
if "ConnectedVision" not in os.environ:
	raise Exception("\n\nthe environment variable \"ConnectedVision\" is not defined")

connectedVisionDir = os.path.normpath(os.environ["ConnectedVision"])

verboseSuffix = ""

if args.verbose:
	verboseSuffix = "-v"

createItemListPath = os.path.normpath(os.path.join(os.path.dirname(__file__), os.pardir, "CreateItemList.js"))
print("")
print("CreateItemList.js")
print("-----------------")
subprocess.check_call(["node", createItemListPath, moduleDir, connectedVisionDir, settingsFile, verboseSuffix])
print("")


codeFromTemplatePath = os.path.normpath(os.path.join(os.path.dirname(__file__), os.pardir, "CodeFromTemplate.js"))
globalJson = os.path.join(moduleDir, "global.json")
artefactsJson = os.path.join(moduleDir, "artefacts.json")

print("")
print("CodeFromTemplate.js - DATA items")
print("--------------------------------")
subprocess.check_call(["node", codeFromTemplatePath, globalJson, os.path.join(moduleDir, "items_data.json"), verboseSuffix])
print("")


print("")
print("CodeFromTemplate.js - COMPUTATION items")
print("--------------------------------")
subprocess.check_call(["node", codeFromTemplatePath, globalJson, os.path.join(moduleDir, "items_computation.json"), artefactsJson, verboseSuffix])
print("")


print("")
print("CodeFromTemplate.js - BUILD items")
print("--------------------------------")
subprocess.check_call(["node", codeFromTemplatePath, globalJson, os.path.join(moduleDir, "items_build.json"), artefactsJson, verboseSuffix])
print("")


if not args.verbose:
	print("clean up")
	print("--------")
	os.remove(os.path.join(moduleDir, "global.json"))
	os.remove(os.path.join(moduleDir, "items_data.json"))
	os.remove(os.path.join(moduleDir, "items_computation.json"))
	os.remove(os.path.join(moduleDir, "items_build.json"))
	os.remove(os.path.join(moduleDir, "artefacts.json"))
import argparse
import distutils.spawn
import os
import platform
import re
import subprocess



parser = argparse.ArgumentParser(description="Connected Vision Module Generator")
parser.add_argument("-m", "--moduleDir", help="root directory of module which contains the def directory with the module description", type=str, required=True)
parser.add_argument("-s", "--settingsFile", help="generator settings JSON file path", type=str, required=False, default="")
parser.add_argument("-v", "--verbose", help="verbose output", required=False, action="store_true")
args = parser.parse_args()

# check if node (Node.js executable) is found on the system path
if not distutils.spawn.find_executable("node"):
	scriptFile = "activate." + ("bat" if platform.system() == "Windows" else "sh")
	
	raise Exception("\n\nFailed to locate node (Node.js executable).\nPlease execute the Connected Vision environment activation script" + (" by calling \"source ./" + scriptFile + "\"" if platform.system() == "Linux" else "") + ".\nThe " + scriptFile + " file is generated by Conan when calling the following command:\nconan install ConnectedVision/x.y.z@covi/stable -b outdated -g virtualenv")

# check the module directory
if not os.path.isdir(args.moduleDir) or not os.path.exists(args.moduleDir):
	raise Exception("\n\nthe specified module directory path is invalid: \"" + args.moduleDir + "\"")

moduleDir = os.path.normpath(args.moduleDir)

settingsFile = ""

# check the settings file
if args.settingsFile:
	if not os.path.isfile(args.settingsFile) or not os.path.exists(args.settingsFile):
		raise Exception("\n\nthe specified settings file path is invalid: \"" + args.settingsFile + "\"")

	settingsFile = os.path.normpath(args.settingsFile)

# check the ConnectedVision environment variable
if "ConnectedVision" not in os.environ:
	raise Exception("\n\nthe environment variable \"ConnectedVision\" is not defined")

connectedVisionDir = os.path.normpath(os.environ["ConnectedVision"])

verboseSuffix = ""

if args.verbose:
	verboseSuffix = "-v"

createItemListPath = os.path.normpath(os.path.join(os.path.dirname(__file__), os.pardir, "CreateItemList.js"))
print("")
print("CreateItemList.js")
print("-----------------")
subprocess.check_call(["node", createItemListPath, moduleDir, connectedVisionDir, settingsFile, verboseSuffix])
print("")


codeFromTemplatePath = os.path.normpath(os.path.join(os.path.dirname(__file__), os.pardir, "CodeFromTemplate.js"))
globalJson = os.path.join(moduleDir, "global.json")
artefactsJson = os.path.join(moduleDir, "artefacts.json")

print("")
print("CodeFromTemplate.js - DATA items")
print("--------------------------------")
subprocess.check_call(["node", codeFromTemplatePath, globalJson, os.path.join(moduleDir, "items_data.json"), verboseSuffix])
print("")


print("")
print("CodeFromTemplate.js - COMPUTATION items")
print("--------------------------------")
subprocess.check_call(["node", codeFromTemplatePath, globalJson, os.path.join(moduleDir, "items_computation.json"), artefactsJson, verboseSuffix])
print("")


print("")
print("CodeFromTemplate.js - BUILD items")
print("--------------------------------")
subprocess.check_call(["node", codeFromTemplatePath, globalJson, os.path.join(moduleDir, "items_build.json"), artefactsJson, verboseSuffix])
print("")


if not args.verbose:
	print("clean up")
	print("--------")
	os.remove(os.path.join(moduleDir, "global.json"))
	os.remove(os.path.join(moduleDir, "items_data.json"))
	os.remove(os.path.join(moduleDir, "items_computation.json"))
	os.remove(os.path.join(moduleDir, "items_build.json"))
	os.remove(os.path.join(moduleDir, "artefacts.json"))
# check if node (Node.js executable) is found on the system path # check the module directory # check the settings file # check the ConnectedVision environment variable