祖传代码优化(待完善)

2019-04-10  本文已影响0人  Wei_Lai

github地址

背景

最近做的事情重复性较高,于是想按照流程,写一个脚本,来节省时间。
具体流程大概是:

  1. 在linux上执行某些操作
  2. 在windows下上传某些文件夹中的文件到linux固定文件夹中
  3. 然后再在linux上执行某些操作

为了偷懒,于是统一在windows下操作。

修改后的入口文件


from os import path
from os import listdir
import os
import sys
from ssh_connection import *
from upload import upload
from cmd_list import cmd 


def run():
    upload('installsh', '/***')

    with connection():
        stdin, stdout, stderr = ssh.exec_command(cmd[0],cmd[1],cmd[2],cmd[3])
        print(stdout.readlines())

    upload('2a', '/***')
    upload('2b', '/***')

    with connection():
        stdin, stdout, stderr = ssh.exec_command(cmd[5],cmd[6],cmd[7])
        print(stdout.readlines())

if __name__ == '__main__':
    print('start work')
    run()

原始代码

原始代码大致如下,原谅我懒得还原了(建议直接跳过)。

workflow.py

from os import path
from os import listdir
import os
import sys
from ssh_connection import connection

def run():
    path=os.getcwd()+r'\files'
    f = os.getcwd()+r'\files'+r'\start.py'
    os.chdir(root)
    os.system(f) 


    with connection():
        cmd1 = 'first.sh'
        stdin, stdout, stderr = ssh.exec_command(cmd1)
        print(stdout.readlines())
    
    path=os.getcwd()+r'\files'
    for root,dirs,files in os.walk(path):
        for f in files:
            if os.path.splitext(f)[1]== '.py':
                os.chdir(root)
                os.system(f)

    with connection():
        cmd2 = 'second.sh'
        cmd3 = 'third.sh'
        stdin, stdout, stderr = ssh.exec_command(cmd2,cmd3)

if __name__=='__main__':  
    print('start work')
    run()

ssh_connection.py

from contextlib import contextmanager
import paramiko
from host import *

ssh = paramiko.SSHClient()
@contextmanager
def connection():
    try:
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname, port, username, password)
        yield
    except Exception as e:
        raise e 
    finally:
        ssh.close()

host.py

hostname = '0.0.0.0'
username ='root'
password = '*****'
port=22

某upload.py (windows中2b文件夹上传到linux中/home/upload_b/中)

import paramiko    
import datetime    
import os
import sys
sys.path.append("..")
from host import *

def upload(local_dir,remote_dir):  
    try:  
        t=paramiko.Transport((hostname,port))  
        t.connect(username=username,password=password)  
        sftp=paramiko.SFTPClient.from_transport(t)  
        print('upload file start %s ' % datetime.datetime.now())  
        for root,dirs,files in os.walk(local_dir):  
            print('[%s][%s][%s]' % (root,dirs,files))  
            for filespath in files:  
                local_file = os.path.join(root,filespath)  
                print(11,'[%s][%s][%s][%s]' % (root,filespath,local_file,local_dir))  
                a = local_file.replace(local_dir,'').replace('\\','/').lstrip('/')  
                print('01',a,'[%s]' % remote_dir)  
                remote_file = os.path.join(remote_dir,a)  
                print(22,remote_file)  
                try:  
                    sftp.put(local_file,remote_file)  
                except Exception as e:  
                    sftp.mkdir(os.path.split(remote_file)[0])  
                    sftp.put(local_file,remote_file)  
                    print("66 upload %s to remote %s" % (local_file,remote_file))  
            for name in dirs:  
                local_path = os.path.join(root,name)  
                print(0,local_path,local_dir)  
                a = local_path.replace(local_dir,'').replace('\\','')  
                print(1,a)  
                print(1,remote_dir)  
                remote_path = os.path.join(remote_dir,a)  
                print(33,remote_path)  
                try:  
                    sftp.mkdir(remote_path)  
                    print(44,"mkdir path %s" % remote_path)  
                except Exception as e:  
                    print(55,e)  
        print('77,upload file success %s ' % datetime.datetime.now())  
        t.close()  
    except Exception as e:  
        print(88,e)

def main():
     local_dir = os.getcwd() + r'\2b'
     remote_dir = '/home/upload_b'
     upload(local_dir, remote_dir)

if __name__=='__main__':
     main()

优化过程

刚开始写出来,我自己能用,给别人,还要教别人怎么使用,然后代码我自己都懒得看,我感觉目前很多人写py只注重功能的实现,一点都不在意美观,而我也是这样,哈哈哈。

upload.py 这一文件别人写的,稍微修改一下,再导入就行。

修改的地方:

  1. 使用local_dir = os.path.join(os.getcwd(), 'files', local_dir),获取路径,稍微美观点
  2. 因为只用来导入,懒得修改了
import paramiko    
import datetime    
import os
import sys
from host import *

def upload(local_dir,remote_dir):
    local_dir = os.path.join(os.getcwd(), 'files', local_dir)
    remote_dir = remote_dir
    try:
        t=paramiko.Transport((hostname,port))  
        t.connect(username=username,password=password)  
        sftp=paramiko.SFTPClient.from_transport(t)  
        print('upload file start %s ' % datetime.datetime.now())  
        for root,dirs,files in os.walk(local_dir):  
            print('[%s][%s][%s]' % (root,dirs,files))  
            for filespath in files:  
                local_file = os.path.join(root,filespath)  
                print(11,'[%s][%s][%s][%s]' % (root,filespath,local_file,local_dir))  
                a = local_file.replace(local_dir,'').replace('\\','/').lstrip('/')  
                print('01',a,'[%s]' % remote_dir)  
                remote_file = os.path.join(remote_dir,a)  
                print(22,remote_file)  
                try:  
                    sftp.put(local_file,remote_file)  
                except Exception as e:  
                    sftp.mkdir(os.path.split(remote_file)[0])  
                    sftp.put(local_file,remote_file)  
                    print("66 upload %s to remote %s" % (local_file,remote_file))  
            for name in dirs:  
                local_path = os.path.join(root,name)  
                print(0,local_path,local_dir)  
                a = local_path.replace(local_dir,'').replace('\\','')  
                print(1,a)  
                print(1,remote_dir)  
                remote_path = os.path.join(remote_dir,a)  
                print(33,remote_path)  
                try:  
                    sftp.mkdir(remote_path)  
                    print(44,"mkdir path %s" % remote_path)  
                except Exception as e:  
                    print(55,e)  
        print('77,upload file success %s ' % datetime.datetime.now())  
        t.close()  
    except Exception as e:  
        print(88,e)
ssh_connection.py 使用上下文
from contextlib import contextmanager
import paramiko
from host import *

ssh = paramiko.SSHClient()
@contextmanager
def connection():
    try:
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname, port, username, password)
        yield
    except Exception as e:
        raise e 
    finally:
        ssh.close()

work.py 的优化

如开头的原始代码与优化后的区别。

cmd_list.py 使用枚举,更加直观(待做)。
上一篇下一篇

猜你喜欢

热点阅读