Commit ceaa8d30 authored by Hanchuan Wu's avatar Hanchuan Wu
Browse files

[bin][util][moduleinfo] Rename script of dune-module information

parent 7ea7a7c1
import os import os
from common import runCommand from util.common import runCommand
from common import callFromPath from util.common import callFromPath
# extract information (for the given keys) from a module file
def extractModuleInfos(moduleFile, keys): def extractModuleInfos(moduleFile, keys):
results = {} results = {}
with open(moduleFile, 'r') as modFile: with open(moduleFile, 'r') as modFile:
...@@ -21,48 +21,70 @@ def extractModuleInfos(moduleFile, keys): ...@@ -21,48 +21,70 @@ def extractModuleInfos(moduleFile, keys):
return results return results
# get info file of a module
def getModuleFile(modulePath): def getModuleFile(modulePath):
modFile = os.path.join(modulePath, 'dune.module') modFile = os.path.join(modulePath, 'dune.module')
if not os.path.exists(modFile): if not os.path.exists(modFile):
raise RuntimeError("Could not find module file") raise RuntimeError("Could not find module file")
return modFile return modFile
# retrieve single information from a module
def getModuleInfo(modulePath, key): def getModuleInfo(modulePath, key):
return extractModuleInfos(getModuleFile(modulePath), [key])[key] return extractModuleInfos(getModuleFile(modulePath), [key])[key]
# get the dependencies of a dune module located in the given directory
def getDependencies(modulePath, verbose=False): def getDependencies(modulePath, verbose=False, includeSelf=False):
modName = getModuleInfo(modulePath, 'Module') modName = getModuleInfo(modulePath, 'Module')
parentPath = os.path.join(modulePath, '../') parentPath = os.path.join(modulePath, '../')
duneControlPath = os.path.join(parentPath, 'dune-common/bin/dunecontrol') duneControlPath = os.path.join(parentPath, 'dune-common/bin/dunecontrol')
if not os.path.exists(duneControlPath): if not os.path.exists(duneControlPath):
raise RuntimeError('Could not find dunecontrol, expected it to be in {}'.format(duneControlPath)) raise RuntimeError(
'Could not find dunecontrol, expected it to be in {}'
.format(duneControlPath)
)
run = callFromPath(parentPath)(runCommand) dcOutput = callFromPath(parentPath)(runCommand)(
for line in run('./dune-common/bin/dunecontrol --module={}'.format(modName)).split('\n'): './dune-common/bin/dunecontrol --module={}'.format(modName)
)
if not dcOutput:
raise RuntimeError("Error: call to dunecontrol failed.")
for line in dcOutput.split('\n'):
if "going to build" in line: if "going to build" in line:
line = line.replace('going to build', '').replace('---', '').replace('done', '') line = line.replace('going to build', '').strip('-')
line = line.strip('\n').strip() line = line.strip('\n').strip()
line = line.split(' ') line = line.split(' ')
deps = line deps = line
# Now we look for the folders with the modules if not includeSelf:
deps.remove(modName)
if verbose: if verbose:
print("Determined the following dependencies: " + ", ".join(deps)) print(" -- Determined the following dependencies: " + ", ".join(deps))
print(" -- Searching the respective directories...")
result = [] result = []
for dir in [d for d in os.listdir(parentPath) if os.path.isdir(os.path.join(parentPath, d))]: parentFiles = [os.path.join(parentPath, d) for d in os.listdir(parentPath)]
try: depModName = getModuleInfo(os.path.join(parentPath, dir), 'Module') for path in filter(os.path.isdir, parentFiles):
except: print('--- Note: skipping folder "' + dir + '" as it could not be identifed as dune module') try:
depModName = getModuleInfo(path, 'Module')
except Exception:
if verbose:
print(f" --- skipping folder '{path}' "
"as it could not be identifed as dune module")
else: else:
if verbose:
print(" --- visited module '{}'".format(depModName))
if depModName in deps: if depModName in deps:
result.append({'name': depModName, 'folder': dir}) result.append({
'name': depModName,
'folder': os.path.basename(path),
'path': path
})
if len(result) != len(deps): if len(result) != len(deps):
raise RuntimeError("Could not find the folders of all dependencies") raise RuntimeError("Could not find the folders of all dependencies")
elif verbose: elif verbose:
print("Found all module folders of the dependencies.") print(" -- Found all module folders of the dependencies.")
return result return result
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment