最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python調(diào)用subprocess模塊實(shí)現(xiàn)命令行操作控制SVN的方法

 更新時(shí)間:2022年09月08日 10:52:43   作者:Logintern09  
這篇文章主要介紹了使用python的subprocess模塊實(shí)現(xiàn)對(duì)SVN的相關(guān)操作,通過設(shè)置GitSvn類,在該類下自定義執(zhí)行SVN常規(guī)操作的方法,需要的朋友跟隨小編一起看看吧

使用python的subprocess模塊實(shí)現(xiàn)對(duì)SVN的相關(guān)操作。

設(shè)置GitSvn類,在該類下自定義執(zhí)行SVN常規(guī)操作的方法。

SVN的常規(guī)操作包括:
(1)獲取SVN當(dāng)前版本,通過方法get_version()實(shí)現(xiàn);

(2)下載SVN指定倉(cāng)庫(kù),通過方法download()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn checkout實(shí)現(xiàn)的下載整個(gè)倉(cāng)庫(kù)功能;

(3)獲取SVN某個(gè)倉(cāng)庫(kù)下的所有文件列表,通過方法search_dir()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn list實(shí)現(xiàn)的獲取倉(cāng)庫(kù)文件列表功能;

(4)在SVN指定位置創(chuàng)建新的文件夾,通過方法mkdir_command()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn mkdir實(shí)現(xiàn)的創(chuàng)建文件夾或者目錄功能;

(5)將本地倉(cāng)庫(kù)文件添加到SVN倉(cāng)庫(kù),通過方法upload_file()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn add實(shí)現(xiàn)的添加文件功能;

(6)將本地倉(cāng)庫(kù)已添加的文件提交SVN指定倉(cāng)庫(kù),通過方法commit_command()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn commit實(shí)現(xiàn)的提交文件功能;

(7)刪除SVN倉(cāng)庫(kù)的目錄,通過方法delete_url()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn delete實(shí)現(xiàn)的刪除目錄功能;

(8)鎖定SVN倉(cāng)庫(kù)的文件,通過方法lock_file()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn lock實(shí)現(xiàn)的鎖定文件功能;

(9)將SVN倉(cāng)庫(kù)的文件解除鎖定,通過方法unlock_file()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn unlock實(shí)現(xiàn)的解除文件鎖定功能;

(10)查看SVN倉(cāng)庫(kù)文件當(dāng)前狀態(tài),通過方法check_status()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn status實(shí)現(xiàn)的查看文件狀態(tài)功能;

(11)更新SVN倉(cāng)庫(kù)的某個(gè)文件,通過方法update_file()實(shí)現(xiàn),實(shí)際是通過調(diào)用SVN的命令行操作指令svn up實(shí)現(xiàn)的更新文件功能;

GitSvn類定義如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Logintern09


import os
import time
import subprocess
from log_manage import logger

rq = time.strftime('%Y%m%d', time.localtime(time.time()))
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "temp_files")
tempfile = os.path.join(file_path, rq + '.log')

class GitSvn(object):
    def __init__(self, bill_addr):
        self.bill_addr = bill_addr

    def get_version(self):
        cmd = "svn info %s" % self.bill_addr
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        logger.info(result)
        error = error.decode(encoding="gbk")
        logger.error(error)
        with open(tempfile, "w") as f:
            f.write(result)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)

    def download(self, dir_path):
        with open(tempfile, "r") as f:
            result = f.readlines()
            for line in result:
                if line.startswith("Revision"):
                    laster_version = int(line.split(":")[-1].strip())
                    logger.info(line)
                    break
        cur_path = os.path.dirname(os.path.realpath(__file__))
        id_path = os.path.join(cur_path, default_sys_name)
        cmd = "svn checkout %s %s -r r%s" % (dir_path, id_path, laster_version)
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)
            if (error.startswith("svn: E155004:")) or (error.startswith("svn: E155037:")):
                cur_path = os.path.dirname(os.path.realpath(__file__))
                file_path = os.path.join(cur_path, default_sys_name)
                cmd = "svn cleanup"
                logger.info(cmd)
                output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                          cwd=file_path)
                (result, error) = output.communicate()
                result = result.decode(encoding="gbk")
                error = error.decode(encoding="gbk")
                logger.info(result)
                logger.error(error)

    def search_dir(self):
        cmd = "svn list %s" % self.bill_addr
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        with open(tempfile, "w") as f:
            f.write(result)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)

    def mkdir_command(self, id_num):
        cmd = "svn mkdir %s" % id_num
        logger.info(cmd)
        cur_path = os.path.dirname(os.path.realpath(__file__))
        file_path = os.path.join(cur_path, default_sys_name)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                  cwd=file_path)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)

    def upload_file(self, file_path):
        cmd = "svn add %s --force" % file_path
        root_path, file_name = os.path.split(file_path)
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                  cwd=root_path)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)

    def commit_command(self, file):
        # 執(zhí)行了鎖定的用戶執(zhí)行了提交操作(提交操作將自動(dòng)解鎖)
        if os.path.isfile(file):
            file_path, file_name = os.path.split(file)
        else:
            file_path = file
        cmd = "svn commit %s -m 'update_files'" % file
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                  cwd=file_path)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)
            elif error.startswith("svn: E730053:"):
                res = "數(shù)據(jù)上傳失敗,請(qǐng)重新提交數(shù)據(jù)!??!"
                raise SystemError(res)
            else:
                res = "數(shù)據(jù)上傳失敗,請(qǐng)重新提交數(shù)據(jù)!?。?
                raise SystemError(res)

    def delete_url(self, url):
        cmd = "svn delete %s" % url
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)

    def lock_file(self, file_name):
        cur_path = os.path.dirname(os.path.realpath(__file__))
        id_path = os.path.join(cur_path, default_sys_name)
        file_path = os.path.join(id_path, file_name)
        cmd = "svn lock %s" % file_path
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)
            elif error.startswith("svn: warning: W160042:"):
                res = "系統(tǒng)資源已被其他用戶鎖定,請(qǐng)稍后重試!"
                raise SystemError(res)

    def unlock_file(self, file_name):
        # 不使用--force 參數(shù) 可以解鎖被自己鎖定的文集 即普通的release lock
        cur_path = os.path.dirname(os.path.realpath(__file__))
        id_path = os.path.join(cur_path, default_sys_name)
        file_path = os.path.join(id_path, file_name)
        cmd = "svn unlock %s --force" % file_path
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)

    def check_status(self, file_name):
        # ?:不在svn的控制中;M:內(nèi)容被修改;C:發(fā)生沖突;A:預(yù)定加入到版本庫(kù);K:被鎖定
        cur_path = os.path.dirname(os.path.realpath(__file__))
        id_path = os.path.join(cur_path, default_sys_name)
        file_path = os.path.join(id_path, file_name)
        cmd = "svn status -v %s" % file_path
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        # 獲取狀態(tài)結(jié)果解析
        status_list = result.split(" ")
        status_flag_list = []
        for i in status_list:
            if i.isalpha():
                status_flag_list.append(i)
            else:
                break
        if result.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)
        elif "C" in status_flag_list:
            return "C"
        elif "K" in status_flag_list:
            return "K"

    def update_file(self, file_name):
        cur_path = os.path.dirname(os.path.realpath(__file__))
        id_path = os.path.join(cur_path, default_sys_name)
        file_path = os.path.join(id_path, file_name)
        cmd = "svn up %s" % file_path
        logger.info(cmd)
        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (result, error) = output.communicate()
        result = result.decode(encoding="gbk")
        error = error.decode(encoding="gbk")
        logger.info(result)
        logger.error(error)
        with open(tempfile, "w") as f:
            f.write(result)
        with open(tempfile, "r") as f:
            result = f.readlines()
            count = -1
            for line in result:
                count += 1
                if (line.strip() != "") and (count == 1):
                    if line.startswith("C"):
                        res = "更新系統(tǒng)資源時(shí)發(fā)現(xiàn)存在沖突,請(qǐng)稍后重試!"
                        raise SystemError(res)
        if error.strip() != "":
            if error.startswith("svn: E170013:"):
                res = "網(wǎng)絡(luò)異常,暫時(shí)連接不上系統(tǒng),請(qǐng)檢查網(wǎng)絡(luò)或其他配置!"
                raise SystemError(res)
            elif (error.startswith("svn: E155037:")) or (error.startswith("svn: E155004:")):
                cur_path = os.path.dirname(os.path.realpath(__file__))
                file_path = os.path.join(cur_path, default_sys_name)
                cmd = "svn cleanup"
                logger.info(cmd)
                output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                          cwd=file_path)
                (result, error) = output.communicate()
                result = result.decode(encoding="gbk")
                error = error.decode(encoding="gbk")
                logger.info(result)
                logger.error(error)

上述類GitSvn的應(yīng)用實(shí)例如下:

if __name__ == '__main__':
    default_url = svn:***  # 用戶本地SVN客戶端的URL地址
    git_class = GitSvn(default_url)
    git_class.get_version()
    git_class.download()
    # 驗(yàn)證查看目錄文件列表功能
    git_class.search_dir()
    # 驗(yàn)證刪除目錄功能
    cur_path = os.path.dirname(os.path.realpath(__file__))
    file_path = os.path.join(cur_path, default_sys_name)
    id_path = os.path.join(file_path, 'SCR202202100002')
    git_class.delete_url(id_path)
    # 驗(yàn)證文件加鎖功能
    git_class.lock_file(file_name="單號(hào)總表.xlsx")
    # 驗(yàn)證文件解鎖功能
    git_class.unlock_file(file_name="單號(hào)總表.xlsx")
    # 檢查文件狀態(tài)
    git_class.check_status(file_name="單號(hào)總表.xlsx")
    # 驗(yàn)證創(chuàng)建目錄功能
    git_class.mkdir_command("SCR202203280001")
    # 驗(yàn)證提交文件功能
    cur_path = os.path.dirname(os.path.realpath(__file__))
    sys_path = os.path.join(cur_path, default_sys_name)
    target_dir = os.path.join(sys_path, "SCR202203280001")
    git_class.upload_file(target_dir)
    git_class.commit_command(target_dir)

到此這篇關(guān)于python調(diào)用subprocess模塊實(shí)現(xiàn)命令行操作控制SVN的文章就介紹到這了,更多相關(guān)python subprocess模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python調(diào)用C語(yǔ)言開發(fā)的共享庫(kù)方法實(shí)例

    Python調(diào)用C語(yǔ)言開發(fā)的共享庫(kù)方法實(shí)例

    這篇文章主要介紹了Python調(diào)用C語(yǔ)言開發(fā)的共享庫(kù)方法實(shí)例,本文同時(shí)給出了C語(yǔ)言和Python調(diào)用簡(jiǎn)單實(shí)例,需要的朋友可以參考下
    2015-03-03
  • Sublime開發(fā)python程序的示例代碼

    Sublime開發(fā)python程序的示例代碼

    本篇文章主要介紹了Sublime開發(fā)python程序的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Python3實(shí)現(xiàn)將文件樹中所有文件和子目錄歸檔到tar壓縮文件的方法

    Python3實(shí)現(xiàn)將文件樹中所有文件和子目錄歸檔到tar壓縮文件的方法

    這篇文章主要介紹了Python3實(shí)現(xiàn)將文件樹中所有文件和子目錄歸檔到tar壓縮文件的方法,涉及Python3使用tarfile模塊實(shí)現(xiàn)tar壓縮文件的技巧,需要的朋友可以參考下
    2015-05-05
  • python中 * 的用法詳解

    python中 * 的用法詳解

    這篇文章主要介紹了python中 * 的用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python Tkinter之事件處理詳解

    Python Tkinter之事件處理詳解

    事件處理,是 GUI 程序中不可或缺的重要組成部分,相比來說,控件只是組成一臺(tái)機(jī)器的零部件。本文我們將對(duì) Tkinter 中的事件處理機(jī)制做詳細(xì)的介紹,需要的可以參考一下
    2022-01-01
  • python微信跳一跳系列之棋子定位像素遍歷

    python微信跳一跳系列之棋子定位像素遍歷

    這篇文章主要為大家詳細(xì)介紹了python微信跳一跳系列之棋子定位之像素遍歷,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • python生成不重復(fù)隨機(jī)數(shù)和對(duì)list亂序的解決方法

    python生成不重復(fù)隨機(jī)數(shù)和對(duì)list亂序的解決方法

    下面小編就為大家分享一篇python生成不重復(fù)隨機(jī)數(shù)和對(duì)list亂序的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python中json格式處理和字典的關(guān)系

    python中json格式處理和字典的關(guān)系

    今天我們先講一下編寫python腳本處理json的核心功能,有些散亂,后期在進(jìn)行整體腳本的編寫,對(duì)python?json字典關(guān)系相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-06-06
  • python實(shí)現(xiàn)碑帖圖片橫向拼接

    python實(shí)現(xiàn)碑帖圖片橫向拼接

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)碑帖圖片橫向拼接,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • python爬蟲入門教程之點(diǎn)點(diǎn)美女圖片爬蟲代碼分享

    python爬蟲入門教程之點(diǎn)點(diǎn)美女圖片爬蟲代碼分享

    這篇文章主要介紹了python爬蟲入門教程之點(diǎn)點(diǎn)美女圖片爬蟲代碼分享,本文以采集抓取點(diǎn)點(diǎn)網(wǎng)美女圖片為例,需要的朋友可以參考下
    2014-09-09

最新評(píng)論

华坪县| 鸡西市| 鞍山市| 普洱| 镇江市| 永善县| 兴化市| 湖南省| 铁岭市| 老河口市| 舞钢市| 堆龙德庆县| 镶黄旗| 庐江县| 贞丰县| 弥勒县| 建平县| 寿阳县| 义乌市| 密山市| 赞皇县| 呼图壁县| 台湾省| 芒康县| 天全县| 华蓥市| 富蕴县| 清水县| 衡山县| 浦江县| 贡山| 墨竹工卡县| 扶绥县| 高唐县| 霸州市| 陵水| 定日县| 阆中市| 鸡东县| 金沙县| 晋中市|