Skip to content

Instantly share code, notes, and snippets.

@luzpaz
Last active December 6, 2019 06:44
Show Gist options
  • Select an option

  • Save luzpaz/890dc89b9a75a0456611441056160761 to your computer and use it in GitHub Desktop.

Select an option

Save luzpaz/890dc89b9a75a0456611441056160761 to your computer and use it in GitHub Desktop.
Check FreeCAD dependencies via python
#
# Script to pythonically determine 3rd party dependencies in FreeCAD
#
# Some code borrowed from https://github.com/jaheyns/CfdOF/blob/master/CfdTools.py
# also suggestion from https://forum.freecadweb.org/viewtopic.php?f=22&t=40522
import FreeCAD
import subprocess as sub
def checkFCVersion(term_print=True):
message = ""
# Check FreeCAD version
ver = FreeCAD.Version()
major_ver = int(ver[0])
minor_vers = ver[1].split('.')
minor_ver = int(minor_vers[0])
if minor_vers[1:] and minor_vers[1]:
patch_ver = int(minor_vers[1])
else:
patch_ver = 0
gitver = ver[2].split()
if gitver:
gitver = gitver[0]
if gitver and gitver != 'Unknown':
gitver = int(gitver)
else:
# If we don't have the git version, assume it's OK.
gitver = FC_COMMIT_REQUIRED
# fc_msg = "FreeCAD version ({}.{}.{}) ({}) ".format(
# int(ver[0]), minor_ver, patch_ver, gitver)
# print(fc_msg + '\n')
fc_version = ver[0:3]
fc_version = (".".join(fc_version))
fc_version_from = ver[4]
fc_version_date = ver[5]
output = '''\
FreeCAD version: {}
From: {}
Created: {}'''.format(fc_version, fc_version_from, fc_version_date)
print(output)
# https://stackoverflow.com/questions/32234169/sha1-string-regex-for-python
fc_version_git = ver[-1]
print("Git Hash: {}".format(fc_version_git))
# def checkOpenFoam()
# # check openfoam
# if term_print:
# print("Checking for OpenFOAM:")
# try:
# foam_dir = getFoamDir()
# except IOError as e:
# ofmsg = "Could not find OpenFOAM installation: " + str(e)
# if term_print:
# print(ofmsg)
# message += ofmsg + '\n'
# else:
# if not foam_dir:
# ofmsg = "OpenFOAM installation path not set and OpenFOAM environment neither pre-loaded before " + \
# "running FreeCAD nor detected in standard locations"
# if term_print:
# print(ofmsg)
# message += ofmsg + '\n'
# else:
# try:
# foam_ver = runFoamCommand("echo $WM_PROJECT_VERSION")
# except Exception as e:
# runmsg = "OpenFOAM installation found, but unable to run command: " + str(e)
# message += runmsg + '\n'
# if term_print:
# print(runmsg)
# raise
# else:
# foam_ver = foam_ver.rstrip().split()[-1]
# if foam_ver != 'dev' and foam_ver != 'plus':
# try:
# # Isolate major version number
# foam_ver = foam_ver.lstrip('v')
# foam_ver = int(foam_ver.split('.')[0])
# if foam_ver >= 1000: # Plus version
# if foam_ver < 1706:
# vermsg = "OpenFOAM version " + foam_ver + " is outdated:\n" + \
# "Minimum version 1706 or 4.0 required"
# message += vermsg + "\n"
# if term_print:
# print(vermsg)
# else: # Foundation version
# if foam_ver < 4:
# vermsg = "OpenFOAM version " + foam_ver + " is outdated:\n" + \
# "Minimum version 4.0 or 1706 required"
# message += vermsg + "\n"
# if term_print:
# print(vermsg)
# except ValueError:
# vermsg = "Error parsing OpenFOAM version string " + foam_ver
# message += vermsg
# if term_print:
# print(vermsg)
#
# # Check for cfMesh
# try:
# cfmesh_ver = runFoamCommand("cartesianMesh -version")
# cfmesh_ver = cfmesh_ver.rstrip().split()[-1]
# cfmesh_ver = cfmesh_ver.split('.')
# if (not cfmesh_ver or len(cfmesh_ver) != 2 or
# int(cfmesh_ver[0]) < CF_MAJOR_VER_REQUIRED or
# (int(cfmesh_ver[0]) == CF_MAJOR_VER_REQUIRED and
# int(cfmesh_ver[1]) < CF_MINOR_VER_REQUIRED)):
# vermsg = "cfMesh-CfdOF version {}.{} required".format(CF_MAJOR_VER_REQUIRED,
# CF_MINOR_VER_REQUIRED)
# message += vermsg + "\n"
# if term_print:
# print(vermsg)
# except subprocess.CalledProcessError:
# cfmesh_msg = "cfMesh (CfdOF version) not found"
# message += cfmesh_msg + '\n'
# if term_print:
# print(cfmesh_msg)
#
# # Check for HiSA
# try:
# hisa_ver = runFoamCommand("hisa -version")
# hisa_ver = hisa_ver.rstrip().split()[-1]
# hisa_ver = hisa_ver.split('.')
# if (not hisa_ver or len(hisa_ver) != 3 or
# int(hisa_ver[0]) < HISA_MAJOR_VER_REQUIRED or
# (int(hisa_ver[0]) == HISA_MAJOR_VER_REQUIRED and
# (int(hisa_ver[1]) < HISA_MINOR_VER_REQUIRED or
# (int(hisa_ver[1]) == HISA_MINOR_VER_REQUIRED and
# int(hisa_ver[2]) < HISA_PATCH_VER_REQUIRED)))):
# vermsg = "HiSA version {}.{}.{} required".format(HISA_MAJOR_VER_REQUIRED,
# HISA_MINOR_VER_REQUIRED,
# HISA_PATCH_VER_REQUIRED)
# message += vermsg + "\n"
# if term_print:
# print(vermsg)
# except subprocess.CalledProcessError:
# hisa_msg = "HiSA not found"
# message += hisa_msg + '\n'
# if term_print:
# print(hisa_msg)
#
# # Check for paraview
# if term_print:
# print("Checking for paraview:")
# paraview_cmd = "paraview"
# import distutils.spawn
# if distutils.spawn.find_executable(paraview_cmd) is None:
# # If not found, try to run from the OpenFOAM environment, in case a bundled version is
# # available from there
# pv_path = runFoamCommand("which paraview")
# if not pv_path.rstrip():
# pv_msg = "Paraview executable " + paraview_cmd + " not found in system or OpenFOAM path."
# message += pv_msg + '\n'
# if term_print:
# print(pv_msg)
# def checkExternalWorkbenches()
# print("Checking for Plot workbench:")
# try:
# from freecad.plot import Plot
# except ImportError:
# # plot_msg = "Could not load Plot workbench\nPlease install it using Tools | Addon manager"
# # message += plot_msg + '\n'
# # if term_print:
# # print(plot_msg)
# else:
# print("Plot workbench found. Get version.")
# #
#
# try:
# import matplotlib
# except ImportError:
# matplot_msg = "Could not load matplotlib package (required by Plot workbench)"
# message += matplot_msg + '\n'
# if term_print:
# print(matplot_msg)
#
# if term_print:
# print("Completed dependency check")
# return message
def checkPivy():
try:
import pivy
except:
print("pivy module didn't load")
else:
pivy_version = pivy.__version__
print("pivy: {}".format(pivy_version))
def checkVTK():
try:
import vtk
except:
print("vtk module didn't load")
else:
vtk_version = vtk.vtkVersion.GetVTKSourceVersion().split(" ")[-1]
print("VTK: {}".format(vtk_version))
def checkIfcopenshell():
try:
import ifcopenshell
except:
print("ifcopenshell module didn't load")
else:
ifcopenshell_version = ifcopenshell.version
print("ifcopenshell: {}".format(ifcopenshell_version))
def checkOCL():
try:
import ocl
except:
print("ocl module didn't load")
else:
ocl_version = ocl.version()
print("ocl: {}".format(ocl_version))
def checkCalculiX():
try:
sub.run(["ccx", "-v"], stdout=sub.PIPE).stdout.decode("utf8").strip().split(" ")[-1]
except:
print("CalculiX not available")
else:
ccx_version = sub.run(["ccx", "-v"], stdout=sub.PIPE).stdout.decode("utf8").strip().split(" ")[-1]
print("CalculiX: {}".format(ccx_version))
def checkGmsh():
try:
sub.run(["gmsh", "--version"], stderr=sub.PIPE).stderr.decode("utf8").strip()
except:
print("gmsh not available")
else:
gmsh_version = sub.run(["gmsh", "--version"], stderr=sub.PIPE).stderr.decode("utf8").strip()
print("gmsh: {}".format(gmsh_version))
checkFCVersion()
checkPivy()
checkVTK()
checkIfcopenshell()
checkOCL()
checkCalculiX()
checkGmsh()
@vocx-fc
Copy link
Copy Markdown

vocx-fc commented Dec 6, 2019

Don't import subprocess inside a function. Subprocess is a standard Python module; it should be imported at the top of the script. This is better Python style.

@luzpaz
Copy link
Copy Markdown
Author

luzpaz commented Dec 6, 2019

Don't import subprocess inside a function. Subprocess is a standard Python module; it should be imported at the top of the script. This is better Python style.

Thanks, done in https://gist.github.com/luzpaz/890dc89b9a75a0456611441056160761/revisions#diff-55b470e7f4d37ac7d4a14c0173a5e6d8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment