<commit_before># coding=utf8

import paramiko
import datetime
import telnetlib
import os
import sys
import time

HOSTS = [
    {
        'HOST':'hive1_host',
        'PORT':9092,
        'USER':'root'
    },
    {
        'HOST':'hive2_host',
        'PORT':9092,
        'USER':'root'
    }
]

BASEPATH = '/root/mpush'

MPUSH_TAR_NAME = 'mpush-jar-with-dependency.tar.gz'

PROCESS_KEY_WORD = 'mpush-cs.jar'

GITLABPATH = '/data/localgit/mpush/mpush/target/'+MPUSH_TAR_NAME

JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java'

ENV= 'daily'

class Telnet(object):
    def __init__(self, chan):
        self.chan = chan

    def send(self, cmd,isprint=True):
        self.chan.send(cmd+'\n')
        print_cmd(cmd)
        out = ''
        is_recv = False
        is_recv_err = False
        while True:
            # 结束
            if self.chan.recv_stderr_ready():
                tmp = self.chan.recv_stderr(1024)
                if isprint:
                    print_out_stream(tmp)
                out += tmp
                is_recv_err = True
            else:
                if is_recv_err:
                    return out
                else:
                    time.sleep(0.1)

            if self.chan.recv_ready():
                tmp = self.chan.recv(1024)
                if isprint:
                    print_out_stream(tmp)
                out += tmp
                is_recv = True
            else:
                if is_recv:
                    return out
                else:
                    time.sleep(0.1)

class SSH():
    def __init__(self):
        self.client = None

    def connect(self,host,port=22,username='root',password=<PASSWORD>):
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.client.connect(host, port, username=username, password=password, timeout=120)
        return self

    def exe(self,cmd,isprint=True):
        if not cmd:
            return
        print greenText(cmd)
        stdin, stdout, stderr = self.client.exec_command(cmd)
        if isprint:
            for std in stdout.readlines():
                print std,
        print stderr.read()
        return stdin, stdout, stderr

    def telnet(self, cmd,isprint=True):
        chan = self.client.get_transport().open_session(timeout=10)
        chan.exec_command(cmd)

        t = Telnet(chan)

        is_recv = False

        while True:
            if chan.recv_ready():
                if isprint:
                    print_out_stream(chan.recv(1024))
                is_recv = True
            else:
                if is_recv:
                    return t
                else:
                    time.sleep(0.1)


    def close(self):
        if self.client:
            self.client.close()

def getPid(keyword,ssh):
    stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False)
    return stdout.read().strip()
def showText(s, typ):
    if typ == 'RED':
        return redText(s)
    elif typ == 'GREEN':
        return greenText(s)
    elif typ == 'YELLOW':
        return yellowText(s)
    else:
        return s

def redText(s):
    return "\033[1;31m%s\033[0m" % s

def greenText(s):
    return "\033[1;32m%s\033[0m" % s


def yellowText(s):
    return "\033[1;33m%s\033[0m" % s

def print_cmd(s):
    """打印执行的命令"""
    print yellowText(s)


def print_out(s):
    """打印执行命令的结果"""
    print greenText(s)


def print_out_stream(s):
    """打印执行命令的结果"""
    sys.stdout.write(greenText(s))

def runShell(c):
    print c
    os.system(c)

def main():

    ##0 git pull
    runShell('git pull origin master')
    print showText('git pull master success','greenText')

    ##1 assembly
    runShell('mvn clean install  assembly:assembly -P %s'%ENV)
    print showText('assembly success','greenText')

    ##2 包创建时间
    runShell('stat -c "%%y" %s'%GITLABPATH)

    confirmPub = raw_input("确认发布(y/n)：")

    if confirmPub != 'y':
       return

    for item in HOSTS:

        pubHost = raw_input("发布 %s (y/n)："%item['HOST'])
        if pubHost != 'y':
           return

        ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER'])

        ##3 backup
        base = BASEPATH+'/'+MPUSH_TAR_NAME
        to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        ssh.exe('mv %s %s '%(base,to))
        print showText('backup mpush ok','greenText')

        ## remove zk info
        try:
            telnet = ssh.telnet('telnet 127.0.0.1 4001')
            telnet.send(' ',False)
            telnet.send('rcs',False) ## 删除zk
            telnet.send('quit',False) ## 关闭连接
            pid = getPid('telnet',ssh)
            if pid :
                ssh.exe('kill -9 %s'%pid)
        except:
            print showText('telnet exception','redText')


        print showText('start kill process','greenText')

        ##4 kill process  先kill执行。等待15秒后，如果进程还是没有杀掉，则执行kill -9
        pid = getPid(PROCESS_KEY_WORD,ssh)
        if pid :
            ssh.exe('kill %s'%pid)
            time.sleep(15)
        else:
            print showText('there is no process to kill','YELLOW')
        pid = getPid(PROCESS_KEY_WORD,ssh)
        if pid:
            ssh.exe('kill -9 %s'%pid)
<commit_msg>bug fix<commit_after># coding=utf8

import paramiko
import datetime
import telnetlib
import os
import sys
import time

HOSTS = [
    {
        'HOST':'hive1_host',
        'PORT':9092,
        'USER':'root'
    },
    {
        'HOST':'hive2_host',
        'PORT':9092,
        'USER':'root'
    }
]

BASEPATH = '/root/mpush'

MPUSH_TAR_NAME = 'mpush-jar-with-dependency.tar.gz'

PROCESS_KEY_WORD = 'mpush-cs.jar'

GITLABPATH = '/data/localgit/mpush/mpush/target/'+MPUSH_TAR_NAME

JAVA_PATH = '/opt/shinemo/jdk1.7.0_40/bin/java'

ENV= 'daily'

class Telnet(object):
    def __init__(self, chan):
        self.chan = chan

    def send(self, cmd,isprint=True):
        self.chan.send(cmd+'\n')
        print_cmd(cmd)
        out = ''
        is_recv = False
        is_recv_err = False
        while True:
            # 结束
            if self.chan.recv_stderr_ready():
                tmp = self.chan.recv_stderr(1024)
                if isprint:
                    print_out_stream(tmp)
                out += tmp
                is_recv_err = True
            else:
                if is_recv_err:
                    return out
                else:
                    time.sleep(0.1)

            if self.chan.recv_ready():
                tmp = self.chan.recv(1024)
                if isprint:
                    print_out_stream(tmp)
                out += tmp
                is_recv = True
            else:
                if is_recv:
                    return out
                else:
                    time.sleep(0.1)

class SSH():
    def __init__(self):
        self.client = None

    def connect(self,host,port=22,username='root',password=<PASSWORD>):
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.client.connect(host, port, username=username, password=password, timeout=120)
        return self

    def exe(self,cmd,isprint=True):
        if not cmd:
            return
        print greenText(cmd)
        stdin, stdout, stderr = self.client.exec_command(cmd)
        if isprint:
            for std in stdout.readlines():
                print std,
        print stderr.read()
        return stdin, stdout, stderr

    def telnet(self, cmd,isprint=True):
        chan = self.client.get_transport().open_session(timeout=10)
        chan.exec_command(cmd)

        t = Telnet(chan)

        is_recv = False

        while True:
            if chan.recv_ready():
                if isprint:
                    print_out_stream(chan.recv(1024))
                is_recv = True
            else:
                if is_recv:
                    return t
                else:
                    time.sleep(0.1)


    def close(self):
        if self.client:
            self.client.close()

def getPid(keyword,ssh):
    stdin, stdout, stderr = ssh.exe(' ps aux|grep %s |grep -v "grep"|awk \'{print $2}\' '%keyword,False)
    return stdout.read().strip()
def showText(s, typ):
    if typ == 'RED':
        return redText(s)
    elif typ == 'GREEN':
        return greenText(s)
    elif typ == 'YELLOW':
        return yellowText(s)
    else:
        return s

def redText(s):
    return "\033[1;31m%s\033[0m" % s

def greenText(s):
    return "\033[1;32m%s\033[0m" % s


def yellowText(s):
    return "\033[1;33m%s\033[0m" % s

def print_cmd(s):
    """打印执行的命令"""
    print yellowText(s)


def print_out(s):
    """打印执行命令的结果"""
    print greenText(s)


def print_out_stream(s):
    """打印执行命令的结果"""
    sys.stdout.write(greenText(s))

def runShell(c):
    print c
    os.system(c)

def main():

    ##0 git pull
    runShell('git pull origin master')
    print showText('git pull master success','greenText')

    ##1 assembly
    runShell('mvn clean install  assembly:assembly -P %s'%ENV)
    print showText('assembly success','greenText')

    ##2 包创建时间
    runShell('stat -c "%%y" %s'%GITLABPATH)

    confirmPub = raw_input("确认发布(y/n)：")

    if confirmPub != 'y':
       return

    for item in HOSTS:

        pubHost = raw_input("发布 %s (y/n)："%item['HOST'])
        if pubHost != 'y':
           return

        ssh = SSH().connect(item['HOST'],item['PORT'],username=item['USER'])

        ##3 backup
        base = BASEPATH+'/'+MPUSH_TAR_NAME
        to = BASEPATH+'/back/'+MPUSH_TAR_NAME+'.'+datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        ssh.exe('mv %s %s '%(base,to))
        print showText('backup mpush ok','greenText')

        ## remove zk info
        try:
            telnet = ssh.telnet('telnet 127.0.0.1 4001')
            telnet.send(' ',False)
            telnet.send('rcs') ## 删除zk
            telnet.send('quit') ## 关闭连接
        except:
            print showText('telnet exception','redText')


        print showText('start kill process','greenText')

        ##4 kill process  先kill执行。等待15秒后，如果进程还是没有杀掉，则执行kill -9
        pid = getPid(PROCESS_KEY_WORD,ssh)
        if pid :
            ssh.exe('kill %s'%pid)
            time.sleep(15)
        else:
            print showText('there is no process to kill','YELLOW')
        pid = getPid(PROCESS_KEY_WORD,ssh)
        if pid:
            ssh.exe('kill -9 %s'%pid)

{'fraction_non_alphanumeric': 0.12960475825019185, 'fraction_numerical': 0.01793937068303914, 'mean_word_length': 1.8229082047116165, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3568598', 'n_tokens_mistral': 3720, 'n_tokens_neox': 3522, 'n_words': 708}