Guest User

Untitled

a guest
Jan 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import datetime
  6. import subprocess
  7.  
  8. import time
  9. import gevent.pool
  10. import gevent.monkey
  11. from gevent import Timeout
  12. gevent.monkey.patch_all()
  13. from multiprocessing import Process
  14.  
  15. def shell_cmd(cmd, system=False):
  16. if system:
  17. return '', os.system(cmd)
  18. return subprocess.Popen(cmd, shell=not isinstance(cmd, list),
  19. stdout=subprocess.PIPE,
  20. stderr=subprocess.PIPE).communicate()
  21.  
  22. def slice_list(ls, n):
  23. if not isinstance(ls, list) or not isinstance(n, int):
  24. return []
  25. ls_len = len(ls)
  26. if n <= 0 or 0 == ls_len:
  27. return []
  28. if n > ls_len:
  29. return []
  30. elif n == ls_len:
  31. return [[i] for i in ls]
  32. else:
  33. j = ls_len / n
  34. k = ls_len % n
  35. ls_return = []
  36. for i in xrange(0, (n-1) * j, j):
  37. ls_return.append(ls[i:i+j])
  38. ls_return.append(ls[(n-1)*j:])
  39. return ls_return
  40.  
  41. def pull_worker(image):
  42. try:
  43. with Timeout(600):
  44. cmd = 'docker pull {0}'.format(image)
  45. shell_cmd(cmd)
  46. out, err = shell_cmd('docker images {0}'.format(image))
  47. if not err:
  48. out, err = shell_cmd('docker rmi {0}'.format(image))
  49. if err:
  50. print "[ERROR docker rmi] {0} {1}".format(image, err)
  51. with open('good_image.txt','a') as goodImageFile:
  52. goodImageFile.write(image + '\n')
  53. else:
  54. with open('error_image.txt','a') as timeout_file:
  55. timeout_file.write(image + " {0}".format(err) + '\n')
  56.  
  57. except Timeout:
  58. with open('timeout_image.txt','a') as timeout_file:
  59. timeout_file.write(image + '\n')
  60.  
  61. if __name__ == "__main__":
  62. start = time.time()
  63. all_task_list = []
  64.  
  65. if len(sys.argv) == 4:
  66. pass
  67. else:
  68. print "Need three params:\n# 1 File\n# 2 Process numbers\n# 3 Parallels numbers for each process"
  69. sys.exit(-1)
  70.  
  71. _, file, coreNum, poolNum = sys.argv
  72.  
  73. def each_process(task_object_list):
  74. pool = gevent.pool.Pool(int(poolNum))
  75. pool.map(pull_worker, task_object_list)
  76. stop = time.time()
  77. elapsed = stop - start
  78. print "End precess with {0} s".format(elapsed)
  79.  
  80. with open(file) as f:
  81. for line in f:
  82. line = line.strip()
  83. all_task_list.append(line)
  84.  
  85. print "All task: {0}".format(len(all_task_list))
  86. for sliced_task_list in slice_list(all_task_list, int(coreNum)):
  87. print "Start process with tasks: {0}".format(len(sliced_task_list))
  88. p = Process(target=each_process, args=(sliced_task_list,))
  89. p.start()
Add Comment
Please, Sign In to add comment