# encoding: utf-8 import os import time import urllib.request import requests import json import psutil import subprocess import platform import sys import shutil def CheckEnv(key): value = os.getenv(key) if None == value: print("env not %s" % key) os._exit(-1) os.putenv(key, value) def GetP4Info(p4path): CheckEnv('P4PORT') CheckEnv('P4USER') CheckEnv('P4PASSWD') output = subprocess.Popen('p4 changes -m1 -ssubmitted %s#have' % p4path, stdout=subprocess.PIPE, shell=True) if None != os.getenv("NO_USE_HAVE"): output = subprocess.Popen('p4 changes -m1 -ssubmitted %s' % p4path, stdout=subprocess.PIPE, shell=True) code = "" sysstr = platform.system() if(sysstr =="Windows"): code = "GBK" elif(sysstr == "Linux"): code = "UTF-8" elif(sysstr == "Darwin"): code = "UTF-8" else: code = "UTF-8" str = output.stdout.readline().decode(code, "ignore") list = str.split(' ', 8) list_name=list[5].split('@', 1) return list[1], list_name[0] def GetTime(): return time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime()) def SendRobot(webhook, msg): header = {'Content-Type' : 'application/json' } body = { 'msgtype':'markdown', 'markdown':{'content':msg} } data_json = json.dumps(body) r =requests.post(webhook, data_json, header) print(r.text) def SendRobotToAuthor(webhook, msg, p4_author): header = {'Content-Type' : 'application/json' } UserId = p4_author+"@h3d.com.cn" body = { 'msgtype':'text', 'text':{'content':msg, 'mentioned_list':[UserId]} } data_json = json.dumps(body) r =requests.post(webhook, data_json, header) print(r.text) def SendRobotToAll(webhook, msg): header = {'Content-Type' : 'application/json' } body = { 'msgtype':'text', 'text':{'content':msg, 'mentioned_list':["@all"]} } data_json = json.dumps(body) r =requests.post(webhook, data_json, header) print(r.text) def UploadNexus(nexus_path, local_file, nexus_file): upload_user = os.getenv('UPLOAD_USER') upload_passwd = os.getenv('UPLOAD_PASSWD') os.system("curl -v --user %s:%s --upload-file %s %s/%s" %(upload_user, upload_passwd, local_file, nexus_path, nexus_file)) def CheckProcess(processname): pl = psutil.pids() for pid in pl: if psutil.Process(pid).name() == processname: return pid def DelFiles(path_file): if os.path.isdir(path_file) is False: return ls = os.listdir(path_file) for i in ls: f_path = os.path.join(path_file, i) if os.path.isdir(f_path): DelFiles(f_path) else: os.remove(f_path) def Copyfile(srcfile,dstpath): # 复制函数 if not os.path.isfile(srcfile): print ("%s not exist!"%(srcfile)) else: fpath,fname=os.path.split(dstpath) if not os.path.exists(fpath): os.makedirs(fpath) # 创建路径 shutil.copy(srcfile, dstpath) # 复制文件 print ("copy %s -> %s"%(srcfile, dstpath)) # 通知上传下载 def NoticeUpload(notice_webhook, upload_path, upload_name, p4number, p4author, fileType='安装包', hint_dSYM = False): upload_full_path = upload_path + "/" + upload_name job_name = os.getenv('JOB_NAME') content="{} 成功! p4:{} 提交者:@{} [点击下载 {}!]({})"\ .format(job_name, p4number, p4author, fileType, upload_full_path) if hint_dSYM == True: content = content + " [点击下载 iOS dSYM!]({})".format(upload_full_path.replace(".ipa", ".dSYM.zip")) SendRobot(notice_webhook, content) print(upload_full_path) sys.stdout.flush()