游走的鱼

导航

线上maven仓库是局域网,无法自动更新jar包,与测试环境jar包对比,再导入nexus种

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import pymysql
import os
import sys
import time

path='/data/maven-repository'
jarlist=[]
#创建新的jarfilelist Name
def fileName():
    now=int(time.time())
    timeArray = time.localtime(now)
    otherStyleTime = time.strftime('%Y-%m-%d',timeArray)
    newFileName=otherStyleTime
    if not os.path.exists(otherStyleTime):
        os.makedirs(otherStyleTime)
    return newFileName
#读取数据库中最新的那个jarfilelist 
def conn_mysql():
    db=pymysql.connect("10.1.130.46","jarlist","jarlist","jarlist",charset='utf8')
    cursor = db.cursor()
    cursor.execute("select jarlistName from jarInfo where id=(SELECT max(id) FROM jarInfo)")
    oldFileName = cursor.fetchone()
    #print("Database version : %s " % oldFileName)
    db.close()
    return oldFileName

#生成新的jarfilelist
def print_files(path):
    
    lsdir = os.listdir(path)
    
    dirs = [i for i in lsdir if os.path.isdir(os.path.join(path, i))]
    if dirs:
        for i in dirs:
            print_files(os.path.join(path, i))
    files = [i for i in lsdir if os.path.isfile(os.path.join(path,i))]
    for f in files:
       jarlistname=os.path.join(path, f)
       jarlist.append(jarlistname)
    return jarlist

def write_files(newFileName,jarlist):
    fi=open(newFileName,'w')
    for f in jarlist:
        fi.write(f+'\n')

       
#新的jarfilelist 和 数据库中的jarfilelist 做对比,差异的生产tar包
def create_tar(oldFileName,newFileName,newDir):
    oldf=open(oldFileName)
    newf=open(newFileName)
    oldlist=[]
    newlist=[]
    differentlist=[]
    for line in oldf.readlines():
        oldlist.append(line)
    for line in newf.readlines():
        newlist.append(line)
    for newline in newlist:
        if newline not in oldlist:
            differentlist.append(newline)
    #print(differentlist)
    for line in differentlist:
        line=line.strip('\n')
        resuLine=line[22:]
        dir=resuLine[0:resuLine.rfind('/')]
        jarpath=newDir+dir
        print('--->',jarpath,line)
        if not os.path.exists(jarpath):
            os.makedirs(newDir+dir)
            os.system('cp  %s %s'%(line,jarpath))
        else:
            os.system('cp  %s %s'%(line,jarpath))
        
#将生成新的jarlist文件名字存到数据库中
def store_mysql(newFileName):
    now=int(time.time())
    timeArray = time.localtime(now)
    otherStyleTime = time.strftime('%Y-%m-%d %H:%M:%S',timeArray)
    db=pymysql.connect("10.1.130.46","jarlist","jarlist","jarlist",charset='utf8')
    cursor = db.cursor()
    cursor.execute("INSERT INTO `jarlist`.`jarInfo`(`jarlistName`, `jarlistFilePath`, `createTime`) VALUES ('%s', '/data/liqian/', '%s')"%(newFileName,otherStyleTime))
    oldFileName = db.commit()
    #print("Database version : %s " % oldFileName)
    db.close()
    return oldFileName


#print_files(sys.argv[1])

if __name__ == "__main__":
    newFileName=fileName()+'.'+'txt'
    newDir=fileName()
    oldFileName=conn_mysql()[0]
    jarlist=print_files(path)
    write_files(newFileName,jarlist)
    create_tar(oldFileName,newFileName,newDir)
    store_mysql(newFileName)

#ceshi bat jiaoben 
 
 
 
生成差异包通过以下脚本导入
[nmcuser@hadoop-master-0 2020-06-01]$ cat mavenimport.sh 
#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
while getopts ":r:u:p:" opt; do
        case $opt in
                r) REPO_URL="$OPTARG"
                ;;
                u) USERNAME="$OPTARG"
                ;;
                p) PASSWORD="$OPTARG"
                ;;
        esac
done
 
find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;
 
执行脚本

./mavenimport.sh -u admin -p admin -r http://10.252.234.185:8081/nexus/repository/nmcdpp-repo/

 
 

posted on 2020-06-11 13:24  游走的鱼  阅读(323)  评论(0编辑  收藏  举报