Skip to content
This repository was archived by the owner on Feb 4, 2020. It is now read-only.

Commit bdf41a6

Browse files
authored
Merge pull request #225 from frerich/resolve_code_duplication
Resolve code duplication
2 parents d3bc5c0 + 077b357 commit bdf41a6

File tree

1 file changed

+27
-70
lines changed

1 file changed

+27
-70
lines changed

clcache.py

Lines changed: 27 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,22 +1371,6 @@ def processCacheHit(cache, objectFile, cachekey):
13711371
return 0, cachedArtifacts.stdout, cachedArtifacts.stderr, False
13721372

13731373

1374-
def postprocessObjectEvicted(cache, objectFile, cachekey, compilerResult):
1375-
printTraceStatement("Cached object already evicted for key {} for object {}".format(cachekey, objectFile))
1376-
returnCode, compilerOutput, compilerStderr = compilerResult
1377-
1378-
cleanupRequired = False
1379-
1380-
section = cache.compilerArtifactsRepository.section(cachekey)
1381-
with section.lock, cache.statistics.lock, cache.statistics as stats:
1382-
stats.registerEvictedMiss()
1383-
if returnCode == 0 and os.path.exists(objectFile):
1384-
artifacts = CompilerArtifacts(objectFile, compilerOutput, compilerStderr)
1385-
cleanupRequired = addObjectToCache(stats, cache, section, cachekey, artifacts)
1386-
1387-
return compilerResult + (cleanupRequired,)
1388-
1389-
13901374
def createManifest(manifestHash, includePaths):
13911375
baseDir = normalizeBaseDir(os.environ.get('CLCACHE_BASEDIR'))
13921376

@@ -1407,9 +1391,14 @@ def createManifest(manifestHash, includePaths):
14071391
return manifest, cachekey
14081392

14091393

1410-
def postprocessHeaderChangedMiss(
1411-
cache, objectFile, manifestSection, manifestHash, sourceFile, compilerResult, stripIncludes):
1412-
returnCode, compilerOutput, compilerStderr = compilerResult
1394+
def postprocessUnusableManifestMiss(
1395+
cache, objectFile, manifestSection, manifestHash, sourceFile, compiler, cmdLine, reason):
1396+
stripIncludes = False
1397+
if '/showIncludes' not in cmdLine:
1398+
cmdLine = list(cmdLine)
1399+
cmdLine.insert(0, '/showIncludes')
1400+
stripIncludes = True
1401+
returnCode, compilerOutput, compilerStderr = invokeRealCompiler(compiler, cmdLine, captureOutput=True)
14131402
includePaths, compilerOutput = parseIncludesSet(compilerOutput, sourceFile, stripIncludes)
14141403

14151404
if returnCode == 0 and os.path.exists(objectFile):
@@ -1418,7 +1407,7 @@ def postprocessHeaderChangedMiss(
14181407
cleanupRequired = False
14191408
section = cache.compilerArtifactsRepository.section(cachekey)
14201409
with section.lock, cache.statistics.lock, cache.statistics as stats:
1421-
stats.registerHeaderChangedMiss()
1410+
reason(stats)
14221411
if returnCode == 0 and os.path.exists(objectFile):
14231412
artifacts = CompilerArtifacts(objectFile, compilerOutput, compilerStderr)
14241413
cleanupRequired = addObjectToCache(stats, cache, section, cachekey, artifacts)
@@ -1427,30 +1416,6 @@ def postprocessHeaderChangedMiss(
14271416
return returnCode, compilerOutput, compilerStderr, cleanupRequired
14281417

14291418

1430-
def postprocessNoManifestMiss(
1431-
cache, objectFile, manifestSection, manifestHash, sourceFile, compilerResult, stripIncludes):
1432-
returnCode, compilerOutput, compilerStderr = compilerResult
1433-
includePaths, compilerOutput = parseIncludesSet(compilerOutput, sourceFile, stripIncludes)
1434-
1435-
manifest = None
1436-
cachekey = None
1437-
1438-
if returnCode == 0 and os.path.exists(objectFile):
1439-
manifest, cachekey = createManifest(manifestHash, includePaths)
1440-
1441-
cleanupRequired = False
1442-
section = cache.compilerArtifactsRepository.section(cachekey)
1443-
with section.lock, cache.statistics.lock, cache.statistics as stats:
1444-
stats.registerSourceChangedMiss()
1445-
if returnCode == 0 and os.path.exists(objectFile):
1446-
artifacts = CompilerArtifacts(objectFile, compilerOutput, compilerStderr)
1447-
# Store compile output and manifest
1448-
cleanupRequired = addObjectToCache(stats, cache, section, cachekey, artifacts)
1449-
manifestSection.setManifest(manifestHash, manifest)
1450-
1451-
return returnCode, compilerOutput, compilerStderr, cleanupRequired
1452-
1453-
14541419
def main():
14551420
if len(sys.argv) == 2 and sys.argv[1] == "--help":
14561421
print("""
@@ -1592,13 +1557,9 @@ def processDirect(cache, objectFile, compiler, cmdLine, sourceFile):
15921557
with manifestSection.lock:
15931558
manifest = manifestSection.getManifest(manifestHash)
15941559
if manifest is None:
1595-
stripIncludes = False
1596-
if '/showIncludes' not in cmdLine:
1597-
cmdLine.insert(0, '/showIncludes')
1598-
stripIncludes = True
1599-
compilerResult = invokeRealCompiler(compiler, cmdLine, captureOutput=True)
1600-
return postprocessNoManifestMiss(
1601-
cache, objectFile, manifestSection, manifestHash, sourceFile, compilerResult, stripIncludes)
1560+
return postprocessUnusableManifestMiss(
1561+
cache, objectFile, manifestSection, manifestHash, sourceFile, compiler, cmdLine,
1562+
Statistics.registerSourceChangedMiss)
16021563

16031564
# NOTE: command line options already included in hash for manifest name
16041565
try:
@@ -1607,42 +1568,38 @@ def processDirect(cache, objectFile, compiler, cmdLine, sourceFile):
16071568
for path, contentHash in manifest.includeFiles.items()
16081569
})
16091570
except IncludeChangedException:
1610-
stripIncludes = False
1611-
if '/showIncludes' not in cmdLine:
1612-
cmdLine.insert(0, '/showIncludes')
1613-
stripIncludes = True
1614-
compilerResult = invokeRealCompiler(compiler, cmdLine, captureOutput=True)
1615-
return postprocessHeaderChangedMiss(
1616-
cache, objectFile, manifestSection, manifestHash, sourceFile, compilerResult, stripIncludes)
1571+
return postprocessUnusableManifestMiss(
1572+
cache, objectFile, manifestSection, manifestHash, sourceFile, compiler, cmdLine,
1573+
Statistics.registerHeaderChangedMiss)
16171574

16181575
cachekey = manifest.includesContentToObjectMap.get(includesContentHash)
16191576
assert cachekey is not None
16201577

1621-
artifactSection = cache.compilerArtifactsRepository.section(cachekey)
1622-
with artifactSection.lock:
1623-
if artifactSection.hasEntry(cachekey):
1624-
return processCacheHit(cache, objectFile, cachekey)
1625-
compilerResult = invokeRealCompiler(compiler, cmdLine, captureOutput=True)
1626-
return postprocessObjectEvicted(cache, objectFile, cachekey, compilerResult)
1578+
return getOrSetArtifacts(cache, cachekey, objectFile, compiler, cmdLine, Statistics.registerEvictedMiss)
16271579

16281580

16291581
def processNoDirect(cache, objectFile, compiler, cmdLine, environment):
16301582
cachekey = CompilerArtifactsRepository.computeKeyNodirect(compiler, cmdLine, environment)
1631-
section = cache.compilerArtifactsRepository.section(cachekey)
1583+
return getOrSetArtifacts(cache, cachekey, objectFile, compiler, cmdLine, Statistics.registerCacheMiss, environment)
1584+
1585+
1586+
def getOrSetArtifacts(cache, cachekey, objectFile, compiler, cmdLine, statsField, environment=None):
1587+
artifactSection = cache.compilerArtifactsRepository.section(cachekey)
16321588
cleanupRequired = False
1633-
with section.lock:
1634-
if section.hasEntry(cachekey):
1589+
with artifactSection.lock:
1590+
if artifactSection.hasEntry(cachekey):
16351591
return processCacheHit(cache, objectFile, cachekey)
16361592

16371593
compilerResult = invokeRealCompiler(compiler, cmdLine, captureOutput=True, environment=environment)
16381594
returnCode, compilerStdout, compilerStderr = compilerResult
16391595
with cache.statistics.lock, cache.statistics as stats:
1640-
stats.registerCacheMiss()
1596+
statsField(stats)
16411597
if returnCode == 0 and os.path.exists(objectFile):
16421598
artifacts = CompilerArtifacts(objectFile, compilerStdout, compilerStderr)
1643-
cleanupRequired = addObjectToCache(stats, cache, section, cachekey, artifacts)
1599+
cleanupRequired = addObjectToCache(stats, cache, artifactSection, cachekey, artifacts)
1600+
1601+
return compilerResult + (cleanupRequired,)
16441602

1645-
return returnCode, compilerStdout, compilerStderr, cleanupRequired
16461603

16471604
if __name__ == '__main__':
16481605
if 'CLCACHE_PROFILE' in os.environ:

0 commit comments

Comments
 (0)