Monday, August 24, 2015

Python script to copy image

#!/usr/bin/env python
import os
import re
import sys

def get_label(cmd):
  label_full = os.popen(cmd).read()
  print label_full
  label_group = re.match(r'.*(BLD.*)',label_full,flags=0)
  label = label_group.group(1)
  return label
def get_image():
  label = get_label(cmd)
  if (target == 'edison'):
      cmd_image = "\/auto\/binos-tools\/bin\/mcpre_abs image -l " + label + " -u EDISON_SUPER_NOVA"
  elif (target == 'katana'):
      cmd_image = "\/auto\/binos-tools\/bin\/mcpre_abs image -l " + label + " -u KATANA_SUPER_NOVA"
  image = str(os.popen(cmd_image).read())
  image = image.split('\n')
  image = str(image[0])
  print image
  if (target == 'edison'):
      image_name = re.match(r'.*(cat3k.*)',image).group(1)
  elif (target == 'katana'):
      image_name = re.match(r'.*(ct5760.*)',image).group(1)
  return image,image_name
  #return image
def copy_tftp(tftp_loc):
  #image = get_image()
  image,image_name = get_image()
  touch_file = "touch " + tftp_loc + image_name
  copy_cmd = "cp " + image + ' ' + tftp_loc
  perm_cmd = "chmod 777 " + tftp_loc + image_name
  print touch_file
  print perm_cmd
  print copy_cmd
  os.system(touch_file)
  os.system(perm_cmd)
  os.system(copy_cmd)
if __name__ == "__main__":
    cmdargs = list(sys.argv)
    argsLen = len(sys.argv)
    print argsLen
    print cmdargs
    if (argsLen == 4 and (cmdargs[2] == 'edison' or cmdargs[2] == 'katana')):
        tftp_loc = cmdargs[1]
        target = cmdargs[2]
        branch = cmdargs[3]
        print target
        if (tftp_loc == '.'):
            print "Getting current TFTP Location..."
            tftp_loc = str(os.popen('pwd').read())
            tftp_loc = tftp_loc.split('\n')
            tftp_loc = str(tftp_loc[0])+'/'
        print tftp_loc
        if (branch == 'dev'):
            cmd = str("\/auto\/binos-tools\/bin\/mcpre_abs list -b polaris_dev -m {\"s2s accepted\",\"polarispublished\",\
"creg-ready-edison\",\"creg-ready-katana\"} -n 1 -c Daily -s DONE")
        elif (branch == 'throttle'):
            cmd = str("\/auto\/binos-tools\/bin\/mcpre_abs list -b v161_0_throttle -m {\"s2s accepted\",\"polarispublished
\",\"creg-ready-edison\",\"creg-ready-katana\"} -n 1 -c Daily -s DONE")
        else:
            print "Please give polaris_dev or v161_0_throttle branch"
        copy_tftp(tftp_loc)
    else:
        print "Please provide required/Correct Parameters"

No comments:

Post a Comment