一、Git版本管理

很多公司在使用git的tag进行版本的管理。

git tag -n                          查看本地Tag
git tag -l 'v1.4.2.*'               查看本地Tag,模糊匹配
git show v1.0                       查看git tag -a v1.0 -m '版本介绍'        本地创建Tag
git tag -d v1.0                     删除Tag
git checkout v.10                   切换taggit push origin  --tags             推送本地tag到远程
git pull origin  --tags             获取远程taggit clone -b v0.10  http://...      指定版本克隆代码

二、Python操作git

"""
基于Python实现对git仓库进行操作,使用前需要安装模块:gitpythonpip3 install gitpython
"""# ############## 1. clone下载代码 ##############
"""
import os
from git.repo import Repodownload_path = os.path.join('code', 'fuck')
Repo.clone_from('https://gitee.com/wupeiqi/fuck.git', to_path=download_path, branch='master')
"""# ############## 2. pull最新代码 ##############
"""
import os
from git.repo import Repolocal_path = os.path.join('code', 'fuck')
repo = Repo(local_path)
repo.git.pull()
"""
# ############## 3. 获取所有分支 ##############
"""
import os
from git.repo import Repolocal_path = os.path.join('code', 'fuck')
repo = Repo(local_path)branches = repo.remote().refs
for item in branches:print(item.remote_head)
"""
# ############## 4. 获取所有版本 ##############
"""
import os
from git.repo import Repolocal_path = os.path.join('code', 'fuck')
repo = Repo(local_path)for tag in repo.tags:print(tag.name)
"""# ############## 5. 获取所有commit ##############
"""
import os
from git.repo import Repolocal_path = os.path.join('code', 'fuck')
repo = Repo(local_path)commit_log = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50,date='format:%Y-%m-%d %H:%M')
log_list = commit_log.split("\n")
real_log_list = [eval(item) for item in log_list]
print(real_log_list)
"""# ############## 6. 切换分支 ##############
"""
import os
from git.repo import Repolocal_path = os.path.join('code', 'fuck')
repo = Repo(local_path)before = repo.git.branch()
print(before)
repo.git.checkout('master')
after = repo.git.branch()
print(after)
repo.git.reset('--hard', '854ead2e82dc73b634cbd5afcf1414f5b30e94a8')
"""# ############## 7. 打包代码 ##############
"""
with open(os.path.join('code', 'fuck.tar'), 'wb') as fp:repo.archive(fp)
"""

git相关操作类

import os
from git.repo import Repo
from git.repo.fun import is_git_dirclass GitRepository(object):"""git仓库管理"""def __init__(self, local_path, repo_url, branch='master'):self.local_path = local_pathself.repo_url = repo_urlself.repo = Noneself.initial(repo_url, branch)def initial(self, repo_url, branch):"""初始化git仓库:param repo_url::param branch::return:"""if not os.path.exists(self.local_path):os.makedirs(self.local_path)git_local_path = os.path.join(self.local_path, '.git')if not is_git_dir(git_local_path):self.repo = Repo.clone_from(repo_url, to_path=self.local_path, branch=branch)else:self.repo = Repo(self.local_path)def pull(self):"""从线上拉最新代码:return:"""self.repo.git.pull()def branches(self):"""获取所有分支:return:"""branches = self.repo.remote().refsreturn [item.remote_head for item in branches if item.remote_head not in ['HEAD', ]]def commits(self):"""获取所有提交记录:return:"""commit_log = self.repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',max_count=50,date='format:%Y-%m-%d %H:%M')log_list = commit_log.split("\n")return [eval(item) for item in log_list]def tags(self):"""获取所有tag:return:"""return [tag.name for tag in self.repo.tags]def change_to_branch(self, branch):"""切换分值:param branch::return:"""self.repo.git.checkout(branch)def change_to_commit(self, branch, commit):"""切换commit:param branch::param commit::return:"""self.change_to_branch(branch=branch)self.repo.git.reset('--hard', commit)def change_to_tag(self, tag):"""切换tag:param tag::return:"""self.repo.git.checkout(tag)if __name__ == '__main__':local_path = os.path.join('codes', 'luffycity')repo = GitRepository(local_path, 'https://gitee.com/wupeiqi/fuck.git')branch_list = repo.branches()print(branch_list)repo.change_to_branch('dev')repo.pull()

三、Python解压缩文件

在py2和py3中对文件进行解压缩稍有不同。

  • shutil 模块【压缩支持py2和py3,解压只支持py3】
  • tarfile / zipfile模块【支持py2和py3】

shutil模块

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import shutil# 文件压缩
"""
ret = shutil.make_archive(base_name="code/www",  # 压缩包文件路劲format='zip',  # “zip”, “tar”root_dir='code/fuck'  # 被压缩的文件件
)
print(ret)
"""# 解压文件
"""
shutil._unpack_zipfile('code/www.zip', 'code/new')
shutil._unpack_tarfile('code/www.tar', 'code/new')
"""

zipfile模块

import zipfile# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()# 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close()

tarfile模块

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import tarfile# 压缩
tar = tarfile.open('your.tar', 'w')
tar.add('utils/codes/luffycity/a1.py')
tar.add('utils/codes/luffycity/a3.py')
tar.close()# 解压
tar = tarfile.TarFile('code/www.tar', 'r')
tar.extractall(path='/code/x1/')  # 可设置解压地址
tar.close()

四、执行本地命令

import subprocessresult = subprocess.check_output('ls -l', cwd='/Users/wupeiqi/PycharmProjects', shell=True)
print(result.decode('utf-8'), type(result))

五、Paramiko执行远程操作

import paramikoclass SSHProxy(object):def __init__(self, hostname, port, username, private_key_path):self.hostname = hostnameself.port = portself.username = usernameself.private_key_path = private_key_pathself.transport = Nonedef open(self):private_key = paramiko.RSAKey.from_private_key_file(self.private_key_path)self.transport = paramiko.Transport((self.hostname, self.port))self.transport.connect(username=self.username, pkey=private_key)def close(self):self.transport.close()def command(self, cmd):ssh = paramiko.SSHClient()ssh._transport = self.transportstdin, stdout, stderr = ssh.exec_command(cmd)result = stdout.read()#ssh.close()return resultdef upload(self, local_path, remote_path):sftp = paramiko.SFTPClient.from_transport(self.transport)sftp.put(local_path, remote_path)sftp.close()def __enter__(self):self.open()return selfdef __exit__(self, exc_type, exc_val, exc_tb):self.close()if __name__ == '__main__':with SSHProxy('10.211.55.25', 22, 'root', '/Users/wupeiqi/.ssh/id_rsa') as ssh:# v1 = ssh.command('sudo ifconfig')# print(v1)ssh.upload('your.tar', '/data/your.tar')

六、杀进程

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import signal
import subprocessoutput = subprocess.check_output("pgrep -f python", shell=True)
pid_list = map(int, output.split())
for pid in pid_list:os.kill(pid, signal.SIGKILL)

七、其他

salt操作

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
SaltAPI推送文件
"""# #### 基于SSH:API ####
"""
from salt.client.ssh.client import SSHClient
client = SSHClient()# 执行命令
# result = client.cmd('*', 'cmd.run', ('ls',))# 调用grains
# ret = client.cmd('*','grains.items')# 调用pillar
# ret = client.cmd('*','pillar.items')# 执行 state
# ret = client.cmd('*','state.sls',('fengfeng','pillar={"xxxx":"luffy"}'))# 发送文件
# ret = client.cmd('*','cp.get_file',('salt://fengfeng/files/test.conf','/data/s1.conf'))# 发送文件
# ret = client.cmd('*','cp.get_url',('http://www.pythonav.com/allstatic/imgs/mv/picture/2.jpeg','/data/s1.jpeg'))
"""
# #### 基于Master:API ####
"""
import salt.client
local = salt.client.LocalClient()# 执行命令
# result = client.cmd('*', 'cmd.run', ('ls',))# 调用grains
# ret = client.cmd('*','grains.items')# 调用pillar
# ret = client.cmd('*','pillar.items')# 执行 state
# ret = client.cmd('*','state.sls',('fengfeng','pillar={"xxxx":"luffy"}'))# 发送文件
# ret = client.cmd('*','cp.get_file',('salt://fengfeng/files/test.conf','/data/s1.conf'))# 发送文件
# ret = client.cmd('*','cp.get_url',('http://www.pythonav.com/allstatic/imgs/mv/picture/2.jpeg','/data/s1.jpeg'))
"""

发布功能

########  1. 执行命令
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import subprocess
import commandsresult = subprocess.check_output('ls', cwd='/Users/wupeiqi/PycharmProjects', shell=True)
print(result, type(result))ret = commands.getoutput("pgrep -f python")
print(ret)########  2. 解压缩文件
# !/usr/bin/env python
# -*- coding:utf-8 -*-import shutil# 文件压缩
"""
ret = shutil.make_archive(base_name="/Users/wupeiqi/PycharmProjects/deploy27/前戏/wwwwwwwwww",format='gztar', # “zip”, “tar”, “bztar”,“gztar”root_dir='/Users/wupeiqi/PycharmProjects/deploy27/deploy'
)
print(ret)
"""# 文件解压
import tarfile
import zipfile# shutil._unpack_zipfile(file.stream, upload_path)
"""
tar = tarfile.open('/Users/wupeiqi/PycharmProjects/deploy27/前戏/wwwwwwwwww.tar.gz', 'r')
tar.extractall(path='/Users/wupeiqi/PycharmProjects/deploy27/前戏/dp/')  # 可设置解压地址
tar.close()
"""########  3. 遍历文件夹下的所有文件
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import osfor item in os.listdir('/Users/wupeiqi/PycharmProjects/deploy27/deploy'):print(item)for item in os.walk('/Users/wupeiqi/PycharmProjects/deploy27/deploy'):print(item)########  4. 重命名和删除
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import shutil# shutil.move('/Users/wupeiqi/PycharmProjects/deploy27/deploy1','/Users/wupeiqi/PycharmProjects/deploy27/deploy')# shutil.rmtree('/Users/wupeiqi/PycharmProjects/deploy27/t')########  5. 杀进程
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import signal
import subprocess
import commandsoutput = subprocess.check_output("pgrep -f python", shell=True)
pid_list = map(int, output.split())
for pid in pid_list:os.kill(pid, signal.SIGKILL)########  6. salt推送文件
# !/usr/bin/env python
# -*- coding:utf-8 -*-
"""
SaltAPI推送文件
"""# #### 基于SSH:API ####
"""
from salt.client.ssh.client import SSHClient
client = SSHClient()# 执行命令
# result = client.cmd('*', 'cmd.run', ('ls',))# 调用grains
# ret = client.cmd('*','grains.items')# 调用pillar
# ret = client.cmd('*','pillar.items')# 执行 state
# ret = client.cmd('*','state.sls',('fengfeng','pillar={"xxxx":"luffy"}'))# 发送文件
# ret = client.cmd('*','cp.get_file',('salt://fengfeng/files/test.conf','/data/s1.conf'))# 发送文件
# ret = client.cmd('*','cp.get_url',('http://www.pythonav.com/allstatic/imgs/mv/picture/2.jpeg','/data/s1.jpeg'))
"""
# #### 基于Master:API ####
"""
import salt.client
local = salt.client.LocalClient()# 执行命令
# result = client.cmd('*', 'cmd.run', ('ls',))# 调用grains
# ret = client.cmd('*','grains.items')# 调用pillar
# ret = client.cmd('*','pillar.items')# 执行 state
# ret = client.cmd('*','state.sls',('fengfeng','pillar={"xxxx":"luffy"}'))# 发送文件
# ret = client.cmd('*','cp.get_file',('salt://fengfeng/files/test.conf','/data/s1.conf'))# 发送文件
# ret = client.cmd('*','cp.get_url',('http://www.pythonav.com/allstatic/imgs/mv/picture/2.jpeg','/data/s1.jpeg'))
"""

Python操作git相关推荐

  1. 如何使用 Python 操作 Git 代码?GitPython 入门介绍

    点击上方"视学算法",选择"置顶或者星标" 第一时间阅读精彩文章! 作者:匿蟒 链接:https://note.qidong.name/2018/01/gitp ...

  2. python做项目管理代码_代码发布项目(三)——python操作git、代码发布流程(服务器管理、项目管理)...

    一.python如何操作git 如果你想用python代码操作git需要下载一个模块 安装 pip install gitpython 基本使用 #从远处仓库下载代码到本地 importosfrom ...

  3. python中git_利用python代码操作git

    python操作git 安装模块 pip3 install gitpython 基本使用 import os from git.repo import Repo # 创建本地路径用来存放远程仓库下载的 ...

  4. Python操作数据库之 MySQL

    Python操作数据库之MySQL 一.安装Python-MySQLdb模块 Python-MySQLdb是一个操作数据库的模块,Python 通过它对 mysql 数据实现各种操作. 如果要源码安装 ...

  5. Python 操作数据库(1)

    在关系数据库中,数据库表是一系列二维数组的集合,用来代表和储存数据对象之间的关系.它由纵向的列和横向的行组成,例如一个有关作者信息的名为 authors 的表中,每个列包含的是所有作者的某个特定类型的 ...

  6. python keyboard模块_[python] PyMouse、PyKeyboard用python操作鼠标和键盘

    1.PyUserInput 简介 PyUserInput是一个使用python的跨平台的操作鼠标和键盘的模块,非常方便使用.支持的平台及依赖如下: Linux - Xlib Mac - Quartz, ...

  7. python操作word文档(python-docx)

    python操作word文档(python-docx) 1. 效果图 1.1 python-docx文档标题段落(等级.加粗.斜体.居中)效果图 1.2 python-docx字体(加粗.斜体.居中. ...

  8. Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  9. Python 操作 MongoDB 数据库!

    作者 |黄伟呢 来源 |数据分析与统计学之美 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 先来看看MySQL与MongoDB 概念区别 ...

最新文章

  1. 热力图和相关系数分析
  2. linux相等路径,关于linux:如何检查Bash中两条路径是否相等?
  3. junit - no runnable methods
  4. HTML DOM之标签操作方法
  5. 阻止页面双击选中文本
  6. 探讨SEO与前端:使用display:none对seo的影响
  7. Python爬虫案例演示:Python多线程、多进程、协程
  8. HDU 3652 B-number (数位DP)
  9. 策略→需求→建模→规划→执行
  10. P1702 突击考试
  11. 推荐一款免费,不限流量的内网穿透软件
  12. Struck: Structured Output Tracking with Kernels
  13. PLC实验:定时器/计数器功能实验
  14. Java 中的JButton按钮事件,ActionListener
  15. win7计算机怎么远程桌面连接不上,Win7系统连接不上远程桌面的解决方法
  16. 山西省高中计算机考试要点,(山西省普通高中信息技术学业水平考试标准.doc
  17. Aquaforest OCR SDK 支持超过100种语言
  18. Objective-c——UI进阶开发第一天(UIPickerView和UIDatePicker)
  19. unity中字体Canvas的字体模糊与清晰
  20. c++ vector删除多个元素方法

热门文章

  1. 关于Lambda和匿名内部类
  2. 北信源携手天津麒麟共建国产信息安全
  3. 二、StreamAPI
  4. 【USACO 3.1】Contact(01子串按出现次数排序)
  5. 如何给DPM服务器附加磁盘?
  6. [unix shell笔记] - 和find命令结合使用xargs命令
  7. java月份去0_java – 使用月份解析日期而不是前导0
  8. 【MM模块】Vendor Consignment 供应商寄售
  9. 凭证 90000000 保存(帐户确定出错)
  10. VF01 BAPI :BAPI_BILLINGDOC_CREATEMULTIPLE