write_csv.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """Usage: write_csv.py -f FILE
  2. Options:
  3. -h --help Show this screen.
  4. """
  5. #!/usr/bin/python2
  6. from docopt import docopt
  7. import numpy as np
  8. import csv
  9. import json
  10. import os
  11. import os.path
  12. import subprocess
  13. def write_csv(output_file):
  14. with open('kalypso_registers.json') as registers_file:
  15. registers = json.load(registers_file)
  16. with open('kalypso_log_conf.json') as log_conf_file:
  17. log_conf = json.load(log_conf_file)
  18. def read_register(reg_key, hex=False):
  19. ## Output from 'pci -r' function in terminal is a string with (rubbish)
  20. ## + (8 chars for values, starting from [11])
  21. pcitool_string = subprocess.check_output(['pci', '-r', registers[reg_key]["address"]])
  22. if hex==True:
  23. return pcitool_string[11:19]
  24. else:
  25. value = int(pcitool_string[11:19],16) & int(registers[reg_key]["mask"],16)
  26. return value
  27. csvfile = output_file[:-3] + "csv"
  28. with open(csvfile, "w") as output:
  29. writer = csv.writer(output, lineterminator='\n')
  30. entries = []
  31. for key in log_conf["log_entries"]:
  32. entries.append([str(registers[key]["name"]),read_register(key,hex=True)])
  33. writer.writerows(entries)
  34. if __name__ == "__main__":
  35. arguments = docopt(__doc__, version="lor 0.2")
  36. output_file = arguments["FILE"]
  37. write_csv(output_file)