1.Python代码操作git

  • 安装

    pip3 install gitpython
  • 操作git

    import os
    from git.repo import Repo # gitpython def clone():
    download_path = os.path.join('codes', 'fuck') # git clone -b master https://gitee.com/wupeiqi/xxoo.git
    # git clone -b v1 https://gitee.com/wupeiqi/xxoo.git
    Repo.clone_from('https://gitee.com/wupeiqi/xxoo.git', to_path=download_path, branch='master') def pull():
    # git pull origin master
    local_path = os.path.join('codes', 'fuck')
    repo = Repo(local_path)
    repo.git.pull() def tag_list():
    local_path = os.path.join('codes', 'fuck')
    repo = Repo(local_path)
    tag_list = [item.name for item in repo.tags]
    print(tag_list) def commits_list():
    import json
    local_path = os.path.join('codes', 'fuck')
    repo = Repo(local_path)
    commit_log = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',date='format:%Y-%m-%d %H:%M',max_count=50) commit_list = [json.loads(item) for item in commit_log.split('\n') ]
    print(commit_list) def branches_list():
    pull()
    local_path = os.path.join('codes', 'fuck')
    repo = Repo(local_path) branches = repo.remote().refs
    branch_list = [item.remote_head for item in branches if item.remote_head != "HEAD"] def checkout_to_branch(branch='dev'):
    local_path = os.path.join('codes', 'fuck')
    repo = Repo(local_path) # before = repo.git.branch()
    # print('当前所在分支:',before) repo.git.checkout(branch) # after = repo.git.branch()
    # print('当前所在分支:',after) def checkout_to_commit(commit='ec1d728'):
    # commits_list()
    local_path = os.path.join('codes', 'fuck')
    repo = Repo(local_path)
    repo.git.reset('--hard', commit)
  • 封装到一个类中,以后当做工具。

    import os
    import json
    from git.repo import Repo
    from git.repo.fun import is_git_dir class GitRepository(object):
    """
    git仓库管理
    """ def __init__(self, local_path, repo_url, branch='master'):
    #本地存储代码的路径 E:\Django_issue\code_issue\codes\xxx
    self.local_path = local_path
    #远程仓库地址 https://gite.com/zhanyu/xxx.git
    self.repo_url = repo_url
    # 这个默认为空下边自动赋值
    self.repo = None
    # 调用下边的方法
    self.initial(branch) def initial(self,branch):
    """
    初始化git仓库
    :param repo_url:
    :param branch:
    :return:
    """
    # 判断本地的仓库中有没有文件,没有就创建
    if not os.path.exists(self.local_path):
    os.makedirs(self.local_path)
    # codes/luffycity/.git
    git_local_path = os.path.join(self.local_path, '.git')
    # 用来判断是不是有这个文件
    if not is_git_dir(git_local_path):
    # 第一次拉文件的时候是需要克隆的,不能直接拉取
    self.repo = Repo.clone_from(self.repo_url, to_path=self.local_path, branch=branch)
    else:
    # 如果有这个.git文件就实例化Repo这个类;
    self.repo = Repo(self.local_path) def pull(self):
    """
    从线上拉最新代码
    :return:
    """
    self.repo.git.pull() def branches(self):
    """
    获取所有分支
    :return:
    """
    branches = self.repo.remote().refs
    return [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')
    return [json.loads(item) for item in commit_log.split('\n') ] 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_object = GitRepository(local_path, 'https://gitee.com/wupeiqi/xxoo.git')

2.解压缩文件

import shutil

# 压缩文件: py2、py3
"""
abs_file_path = shutil.make_archive(
base_name="files/ww", # 压缩包文件路劲
format='tar', # “zip”, “tar”
root_dir='codes/luffycity' # 被压缩的文件目录
)
print(abs_file_path)
""" # 解压缩:py3
# shutil._unpack_zipfile('files/ww.zip', 'xxxxxx/new')
# shutil._unpack_tarfile('files/ww.zip', 'xxxxxx/new') # 解压缩:py2/py3
"""
import zipfile
z = zipfile.ZipFile('files/ww.zip', 'r')
z.extractall(path='xxxxxx/luffy')
z.close() import tarfile
tar = tarfile.TarFile('code/www.tar', 'r')
tar.extractall(path='/code/x1/') # 可设置解压地址
tar.close()
"""

3.基于paramiko操作远程服务器

import paramiko

class SSHProxy(object):

    def __init__(self, hostname, port, username, private_key_path):
self.hostname = hostname
self.port = port
self.username = username
self.private_key_path = private_key_path self.transport = None def 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.transport
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read()
ssh.close()
return result def 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 self def __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 proxy:
proxy.upload('xx','xx')
proxy.command('ifconfig')
proxy.command('ifconfig')
proxy.upload('xx', 'xx')
with SSHProxy('10.211.55.26', 22, 'root', '/Users/wupeiqi/.ssh/id_rsa') as proxy:
proxy.upload('xx','xx')
proxy.command('ifconfig')
proxy.command('ifconfig')
proxy.upload('xx', 'xx')

4.本地执行命令

import subprocess

result = subprocess.check_output('dir', cwd='D:\wupeiqi\code\ziwen\codes', shell=True)
print(result.decode('gbk'), type(result))

python的零碎知识的更多相关文章

  1. 【Python】 零碎知识积累 II

    [Python] 零碎知识积累 II ■ 函数的参数默认值在函数定义时确定并保存在内存中,调用函数时不会在内存中新开辟一块空间然后用参数默认值重新赋值,而是单纯地引用这个参数原来的地址.这就带来了一个 ...

  2. 【Python】 零碎知识积累 I

    大概也是出于初高中时学化学,积累各种反应和物质的习惯,还有大学学各种外语时一看见不认识的词就马上记下来的习惯,形成了一种能记一点是一点的零碎知识记录的癖好.这篇文章就是专门拿来记录这些零碎知识的,没事 ...

  3. python之零碎知识

    一 join方法 主要是做字符串的拼接:join后面跟的类型必须是要可迭代得到对象 for循环的对象是可迭代对象 # result = "".join(li) # print(re ...

  4. python数组相关知识

    1.np中的reshape函数,可以把矩阵重新划分成m行n列. arange(n)可以把 [0,n-1]装入数组中,一定要注意的是img.reshape()并不会改变原来的数组,所以需要另外新建一个数 ...

  5. Python 编程核心知识体系(REF)

    Python 编程核心知识体系: https://woaielf.github.io/2017/06/13/python3-all/ https://woaielf.github.io/page2/

  6. python基础----基础知识介绍

    一  编程语言的划分       编译型:将代码一次性全部编译成二进制,然后运行. 缺点:开发效率低,不能跨平台(windows与linux) 优点:执行效率高 代表语言:c语言 解释型:当程序开始运 ...

  7. 简述Python入门小知识

    如今的Python开发工程师很受企业和朋友们的青睐,现在学习Python开发的小伙伴也很多,本篇文章就和大家探讨一下Python入门小知识都有哪些. 扣丁学堂简述Python入门小知识Python培训 ...

  8. Python数据挖掘——基础知识

    Python数据挖掘——基础知识 数据挖掘又称从数据中 挖掘知识.知识提取.数据/模式分析 即为:从数据中发现知识的过程 1.数据清理 (消除噪声,删除不一致数据) 2.数据集成 (多种数据源 组合在 ...

  9. python - 那些零碎的知识点

    python - 那些零碎的知识点 一. 字符串格式化 1. "旧式字符串解析(%操作符)" 'Hello, %s' % name "Hello, Bob" ' ...

随机推荐

  1. Java篇:Docker的介绍安装 和常用命令

    文章目录 为什么 出现docker Docker的简介 容器(Container) 镜像(Image) 仓库(Repository) Docker的安装 查看容器 删除镜像 删除容器 部署应用 以my ...

  2. 百度实习生,以修仙者的角度聊聊怎么学MySQL,不来看看你的修为如何吗?

    目录 因为我个人比较喜欢看修仙类的小说,所以本文的主体部分借用修仙者的修为等级,将学习旅程划分成:练气.筑基.结丹.元婴.化神.飞升六个段位,你可以看下你大概在哪个段位上哦! 本文目录: 我为什么要写 ...

  3. iNeuOS工业互联平台,图表与数据点组合成新组件,进行项目复用

    目       录 1.      概述... 1 2.      演示信息... 2 3.      应用过程... 2 1.   概述 针对有些行业的数据已经形成了标准化的建模或者有些公司专注于某 ...

  4. 【故障公告】redis内存耗尽造成博客后台无法保存

    非常抱歉,今天上午11:00~11:30左右,由于 redis 服务器内存耗尽造成博客后台故障--保存博文时总是提示"请求太过频繁,请稍后再试",由此给您带来麻烦,请您谅解. 由于 ...

  5. (二)、vim即gvim的炫酷搜索模式与技巧

      一.进入搜索模式 1. 打开文件,狂按  <Esc> 进入normal模式,然后按  /  或者  :/  进入搜索模式,添加上关键字例如world,按回车即搜索world: :/wo ...

  6. 现代JavaScript—ES6+中的Imports,Exports,Let,Const和Promise

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者.原文出处:https://www.freecodecamp.org/news/learn-modern-jav ...

  7. 在 xunit 测试项目中使用依赖注入

    在 xunit 测试项目中使用依赖注入 Intro 之前写过几篇 xunit 依赖注入的文章,今天这篇文章将结合我在 .NET Conf 上的分享,更加系统的分享一下在测试中的应用案例. 之所以想分享 ...

  8. [leetcode299] 299. Bulls and Cows

    public String getHint(String secret, String guess) { /* 判断bull 是通过比较两个字符串的每一位,每次相同就删除该字符出现的次数,因为后边的 ...

  9. 第十九章节 BJROBOT 安卓手机 APP 导航【ROS全开源阿克曼转向智能网联无人驾驶车】

    导航前说明:一定要确保你小车在构建好地图的基础上进行! 1.把小车平放在你想要构建地图区域的地板上,打开资料里的虚拟机,打开一个终端, ssh 过去主控端启动roslaunch znjrobot br ...

  10. [开源软件] 腾讯云Linux服务器一键安装LAMP/LNMP/LANMP环境 转

    本帖最后由 我本戏子 于 2015-8-13 22:00 编辑OneinStack是非常优秀的一键PHP/JAVA安装脚本,提供以下环境:lnmp(Linux + Nginx+ MySQL+ PHP) ...