Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3.  
  4.  
  5. EMAIL = "user@university.edu"
  6. HOST = "255.255.255.255"
  7. PASS = "SuperSecurePassword"
  8. STATUSCHECK_FREQ = 2
  9.  
  10.  
  11. import sys, os
  12. from plistlib import writePlist
  13.  
  14. if len(sys.argv) == 1:
  15.     print """
  16.  
  17. xgrid_batch.py by Jordan Perr - 2011
  18.  
  19. Usage:
  20. \t./xgrid_batch taskName numberOfTasks outputDirectory command arg1 arg2...
  21.  
  22. Special Arguments:
  23. \tXG_SEQ:  A unique integer for this task, sequential, starting at 0
  24. \tXG_SEED: Seconds since the unix epoch + XG_SEQ. Guaranteed to be unique.
  25.  
  26. Configuration:
  27. \tInfrequently changed parameters such as your email address, the xgrid host, and xgrid password are located at the head of this script and should be modified by the user.
  28.  
  29. Dependencies:
  30. \tPython2.6-2.7 (probably won't work with 3.x)
  31. \txgrid command line utility
  32.  
  33. Notes:
  34. \t-Use relative paths to CWD. This is true for command, as well as any file arguments.
  35.  
  36. \t-Any files written by your simulation MUST HAVE UNIQUE NAMES ACCROSS ALL JOBS. You should use XG_SEQ or XG_SEED in all file names written by your simulation. This is just how xgrid's batch command works. If two tasks produce files with identical names, one of them will be overwritten by xgrid's result collection.
  37.  
  38. \t-xgrid.plist will be written to outputDirectory. You can ignore this file and delete it once the simulation is complete.
  39.  
  40.  
  41. """
  42.     sys.exit(0)
  43.  
  44. # Create XGRID batch plist file in output directory
  45. import time
  46.  
  47. email = EMAIL
  48. task_name = sys.argv[1]
  49. num_tasks = int(sys.argv[2])
  50. output_path = os.path.abspath(sys.argv[3])
  51. command_path = sys.argv[4]
  52. arguments = sys.argv[5:]
  53.  
  54. tasks = dict()
  55. t = int(time.time())
  56. for i in range(num_tasks):
  57.     tasks["task%d"%(i)] = {
  58.         "command": command_path,
  59.         "arguments": [b.replace("XG_SEED", str(t+i)) for b in [a.replace("XG_SEQ", str(i)) for a in arguments]]
  60.     }
  61.  
  62. files = dict()
  63. files[command_path] = {"filePath": os.path.abspath(command_path)}
  64. for arg in arguments:
  65.     if os.path.exists(os.path.abspath(arg)):
  66.         files[arg] = {"filePath": os.path.abspath(arg)}
  67.  
  68. p = [{
  69.     "name": task_name,
  70.     "notificationEmail": email,
  71.     "inputFiles": files,
  72.     "taskSpecifications": tasks
  73. }]
  74.  
  75. writePlist(p, os.path.join(output_path, "xgrid.plist"))
  76.  
  77.  
  78. # Spawn XGRID job using the batch file
  79. import subprocess, re
  80.  
  81. xgrid = ["xgrid", "-h", HOST, "-p", PASS, "-job"]
  82. batch, error = subprocess.Popen(xgrid+["batch", os.path.join(output_path, "xgrid.plist")], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  83. jobId = re.findall("\d+", batch)[0]
  84. print "jobid = %s"%(jobId)
  85. print "status will be checked every %d seconds."%(STATUSCHECK_FREQ)
  86.  
  87.  
  88. # Keep checking status until it either fails or finishes
  89. while True:
  90.     attributes, error = subprocess.Popen(xgrid+["attributes", "-id", jobId], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  91.     match = re.search(r"jobStatus = (\w+);", attributes)
  92.     print "%s -- Job %s status: %s"%(time.asctime(), jobId, match.group(1))
  93.     if match.group(1) == "Finished":
  94.         break
  95.     time.sleep(STATUSCHECK_FREQ)
  96.  
  97.  
  98. # Collect results
  99. print "Collecting results... ",
  100. collecting, error = subprocess.Popen(xgrid+["results", "-out", output_path, "-id", jobId], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  101. print "Done"
  102.  
  103. # Delete job
  104. print "Deleting job... ",
  105. delete, error = subprocess.Popen(xgrid+["delete", "-id", jobId], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  106. print "Done."
  107.  
  108.  
  109. print "I hope you find something interesting in there!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement