Skip to content

Instantly share code, notes, and snippets.

@hamburger1984
Created December 3, 2020 15:21
Show Gist options
  • Save hamburger1984/7e23e3de3965ba0ca3954c948eacd140 to your computer and use it in GitHub Desktop.
Save hamburger1984/7e23e3de3965ba0ca3954c948eacd140 to your computer and use it in GitHub Desktop.
A little python script to analyze nix build logs to aid understanding dependencies
# Author: Andreas Krohn
# Intended to help understanding dependencies by looking at (verbose) nix build outputs
import sys
import getopt
import re
def analyze(logfile):
hashNames = dict()
references = dict()
scanningSection = ''
scanning = re.compile(r'^scanning\sfor\sreferences\sinside\s\'/nix/store/([^-]+)-([^\']+)\'')
scanEntry = re.compile(r'^found\sreference\sto\s\'([^\']+)\'')
inputs = re.compile(r'^(u?n?referenced\sinput:|building\sof)\s\'/nix/store/([^-]+)-([^\']+)\'')
for line in logfile:
if scanningSection:
m = scanEntry.match(line)
if m:
references[scanningSection].append(m.group(1))
continue
scanningSection = ''
m = inputs.match(line)
if m:
hashNames[m.group(2)] = m.group(3)
continue
m = scanning.match(line)
if m:
key = m.group(1)
if key in references:
print(key, " already in references")
continue
references[key] = []
hashNames[m.group(1)] = m.group(2)
scanningSection = key
continue
for p,c in references.items():
print(hashNames[p] if p in hashNames else p)
for d in c:
print(" -> ", hashNames[d] if d in hashNames else d)
def print_help():
print("analyze_build.py -l <logfile>")
def run(argv):
logfilepath = ""
try:
opts, args = getopt.getopt(argv, "hl:", ["logfile="])
except getopt.GetoptError:
print_help()
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print_help()
sys.exit()
elif opt in ("-l", "--logfile"):
logfilepath = arg
if not logfilepath:
print_help()
sys.exit(2)
with open(logfilepath) as f:
analyze(f)
if __name__ == '__main__':
run(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment