qemuimage-testlib-pythonhelper 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. import optparse
  3. import subprocess
  4. import sys
  5. import os
  6. parser = optparse.OptionParser(
  7. usage = """
  8. %prog [options]
  9. """)
  10. parser.add_option("-q", "--findqemu",
  11. help = "find a qemu beneath the process <pid>",
  12. action="store", dest="findqemu")
  13. options, args = parser.parse_args(sys.argv)
  14. if options.findqemu:
  15. #
  16. # Walk the process tree from the process specified looking for a qemu-system. Return its pid.
  17. #
  18. ps = subprocess.Popen(['ps', 'axww', '-o', 'pid,ppid,command'], stdout=subprocess.PIPE).communicate()[0]
  19. processes = ps.split('\n')
  20. nfields = len(processes[0].split()) - 1
  21. pids = {}
  22. commands = {}
  23. for row in processes[1:]:
  24. data = row.split(None, nfields)
  25. if len(data) != 3:
  26. continue
  27. if data[1] not in pids:
  28. pids[data[1]] = []
  29. pids[data[1]].append(data[0])
  30. commands[data[0]] = data[2]
  31. if options.findqemu not in pids:
  32. sys.stderr.write("No children found matching %s" % options.findqemu)
  33. sys.exit(1)
  34. parents = []
  35. newparents = pids[options.findqemu]
  36. while newparents:
  37. next = []
  38. for p in newparents:
  39. if p in pids:
  40. for n in pids[p]:
  41. if n not in parents and n not in next:
  42. next.append(n)
  43. if p not in parents:
  44. parents.append(p)
  45. newparents = next
  46. #print "Children matching %s:" % str(parents)
  47. for p in parents:
  48. # Need to be careful here since runqemu-internal runs "ldd qemu-system-xxxx"
  49. basecmd = commands[p].split()[0]
  50. basecmd = os.path.basename(basecmd)
  51. if "qemu-system" in basecmd:
  52. print p
  53. sys.exit(0)
  54. sys.exit(1)
  55. else:
  56. parser.print_help()