# -*- coding: utf-8
'''
pub2nexus.py - publish to nexus server
Example:
./pub2nexus.py --action publish
--dir /home/jenkins/workspace/AP-SCG_3.4.0.7_H500/tftpboot/ap-11ac-wasp-wsg
--keyfile /home/jenkins/nexus.key
--server 172.16.200.63
--target ap/brian/ML/3.4.0.11/
--run
'''
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
import os.path
import os
import ConfigParser
import sys
import argparse
import logging
import json
import codecs
import urllib
import urllib2
import urlparse
import base64
import subprocess
import re
import getpass
import traceback
import locale
import pprint
from collections import defaultdict
import signal
from subprocess import Popen, PIPE
import sets
import uuid
import tarfile
import shutil
import hashlib
MIN_CRUCIBLE_VERSION = '3.0.0'
SCRIPT_NAME = os.path.basename(__file__)
SCRIPT_VERSION='b4f0c4942ed8404a9a137f441d0768b6'
VERSION_DATE='Aug 11, 2016 07:00 PM'
def subprocess_cmd2(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = p.communicate()
return stdout, stderr
def lsDirs(cmdOpts, folder):
if cmdOpts.trace:
print('In lsDirs')
j = 0
trace = True
for item in os.listdir(folder):
fpn = os.path.join(folder, item)
if os.path.isdir(fpn) and item[0] != '.':
j += 1
dirFullPathName = folder + '/' + item
###print('%3d (%s) -> %s , item[0]=(%s)' %(j, item, dirFullPathName, item[0] ))
# cd to dir & create tar.gz
tarName = item + '.tar.gz'
folderFileList = lsFiles(cmdOpts, dirFullPathName)
###print(' folderFileList -> %s' %(folderFileList))
curDir = os.getcwd()
srcTarname = curDir + '/' + tarName
dstDirname = folder + '/' + tarName
###print(' curDir: %s' %(curDir))
createTar = False
if len(folderFileList) != 0:
# if the tar is exist on cur directory, remove it first.
if os.path.isfile(srcTarname):
print(' * found & remove %s' %(srcTarname))
os.remove(srcTarname)
#os.chdir(dirFullPathName)
tar = tarfile.open(tarName, 'w:gz')
print(' Creating tar: %s' %(tarName))
for fullPathFolderFileName in folderFileList:
tar.add(fullPathFolderFileName)
tar.close()
#os.chdir(curDir)
#shutil.move(src, dst)
createTar = True
elif len(folderFileList) == 0:
if trace:
print('Noted, the Folder is empty -> %s' %(dirFullPathName))
if createTar:
dstDirname = folder + '/' + tarName
if trace:
print(' srcTarname -> %s' %(srcTarname))
print(' dstDirname -> %s' %(dstDirname))
if os.path.isfile(dstDirname):
if trace:
print(' * found & remove %s' %(dstDirname))
os.remove(dstDirname)
shutil.move(srcTarname, dstDirname)
def lsFiles(cmdOpts, folder):
if cmdOpts.trace:
print('In lsFiles')
fileList = []
j=0
for item in os.listdir(folder):
fpn = os.path.join(folder, item)
if os.path.isfile(fpn):
j += 1
fpn = folder + '/' + item
#print('%3d %s -> %s' %(j,item, fpn))
fileList.append(fpn)
return fileList
def touch(path):
with open(path, 'a'):
os.utime(path, None)
def get_md5(fpn):
md5_val = ''
with open(fpn) as file_handle:
data = file_handle.read()
md5_val = hashlib.md5(data).hexdigest()
return md5_val
def writeMd5(cmdOpts, destDir, fpnList):
md5StrFileOut = destDir + '/' + 'MD5SUMS'
md5Str=''
if len(fpnList) == 0:
return
j=0
trace = False
for fpn in fpnList:
j += 1
if trace:
print('%3d -> %s' %(j, fpn))
# hashlib.md5(open('filename.exe','rb').read()).hexdigest()
#md5sumStr = hashlib.md5(open(fpn,'rb').read()).hexdigest()
md5sumStr = get_md5(fpn)
md5Str += md5sumStr + ' ' + fpn + '\n'
if md5Str != '':
f = open(md5StrFileOut, 'w')
f.write(md5Str)
f.close()
print('Write out %s' %(md5StrFileOut))
def createMD5file(destDir):
md5StrFileOut = destDir + '/' + 'MD5SUMS'
touch(md5StrFileOut)
def do_publish(cmdOpts):
if cmdOpts.trace:
print('In publish')
# get list of dir first
trace = cmdOpts.trace
destdir = cmdOpts.dir
# working files version
#print('destdir: %s' %(destdir))
#files = [ f for f in os.listdir(destdir) if os.path.isfile(os.path.join(destdir,f)) ]
#print('files: %s' %(files))
lsDirs(cmdOpts, destdir)
createMD5file(destdir)
fpnList = lsFiles(cmdOpts, destdir)
writeMd5(cmdOpts, destdir, fpnList)
j=0
commaSepStr=''
trace = False
for fpn in fpnList:
j += 1
if trace:
print('%3d -> %s' %(j, fpn))
commaSepStr = ",".join(fpnList )
#print('commaSepStr=(%s)' %(commaSepStr))
httpStr = 'http://'
nexusPath=':8081/nexus/content/repositories/releases/ruckus/official/'
httpPath = httpStr + cmdOpts.server + nexusPath + cmdOpts.target
cmd = 'curl -sL --write-out %{http_code}, --upload-file {' + commaSepStr + '} ' + httpPath + ' --config ' + cmdOpts.keyfile
#print('cmd=(%s)' %(cmd))
if cmdOpts.run:
print('exec %s' %(cmd))
output, stderr = subprocess_cmd2(cmd)
lineList = output.split('\n')
j = 0
for line in lineList:
j += 1
if trace:
print('%d (%s)' %(j, line))
else:
print('--run disabled.')
def get_unique_id(cmdOpts):
unique_id = str(uuid.uuid4())
print('unique_id: ', unique_id)
unique_id_without_dash = unique_id.replace("-", '')
print('unique_id_without_dash 1 : ', unique_id_without_dash)
#return unique_id_without_dash
def do_get_id(cmdOpts):
get_unique_id(cmdOpts)
def help(cmdOpts):
print('Usage')
print(' %s --action get_id' %(SCRIPT_NAME))
print(' %s --action publish --dir xxx ---server 172.16.200.63 --target ap/Xclaim/ML/2.2.0.0/Xi-3/2.2.0.0.6/ --keyfile /home/jenkins/nexus.key [--run] [--trace]' %(SCRIPT_NAME))
print(' ./pub2nexus.py --action publish --dir /Users/brianpang/crucible --keyfile /home/jenkins/nexus.key --target ap/Xclaim/ML/2.2.0.0/Xi-3/2.2.0.0.6/ --server 172.16.200.63')
sys.exit(1)
def selectCmd():
fn = os.path.basename(__file__)
parser = argparse.ArgumentParser(description='arg parser for %s' % fn)
parser.add_argument('--action', action='store', dest='action', help='Store a action value')
parser.add_argument('--dir', action='store', dest='dir', help='Store a dir value')
parser.add_argument('--server', action='store', dest='server', help='Store a server value')
parser.add_argument('--keyfile', action='store', dest='keyfile', help='Store a keyfile value')
parser.add_argument('--target', action='store', dest='target', help='Store a target value')
parser.add_argument('--run', action='store_true', default=False, dest='run', help='Set run to true')
parser.add_argument('--trace', action='store_true', default=False, dest='trace', help='Set trace to true')
parser.add_argument('--verbose', action='store_true', default=False, dest='verbose', help='Set verbose to true')
parser.add_argument('--debug', action='store_true', default=False, dest='debug', help='Set debug to true')
cmdOpts = parser.parse_args()
return cmdOpts
def main():
cmdOpts = selectCmd();
if cmdOpts.action is None:
help(cmdOpts)
elif cmdOpts.action == 'help':
help(cmdOpts)
elif cmdOpts.action == 'get_id':
do_get_id(cmdOpts)
elif cmdOpts.action == 'publish':
do_publish(cmdOpts)
else:
print('No match found.')
help(cmdOpts)
if __name__ == '__main__':
main()
No comments:
Post a Comment