浠g爜鍙戝竷绯诲垪1
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浠g爜鍙戝竷绯诲垪1相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/ranch' title='ranch'>ranch pcl ls -l nload 鍒濆 涓嬭浇 date else check
1.Python鎿嶄綔git
"""
鍩轰簬Python瀹炵幇瀵筭it浠撳簱杩涜鎿嶄綔锛屼娇鐢ㄥ墠闇€瑕佸畨瑁呮ā鍧楋細gitpython
pip3 install gitpython
"""
# ############## 1. clone涓嬭浇浠g爜 ##############
"""
import os
from git.repo import Repo
download_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 Repo
local_path = os.path.join('code', 'fuck')
repo = Repo(local_path)
repo.git.pull()
"""
# ############## 3. 鑾峰彇鎵€鏈夊垎鏀?##############
"""
import os
from git.repo import Repo
local_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 Repo
local_path = os.path.join('code', 'fuck')
repo = Repo(local_path)
for tag in repo.tags:
print(tag.name)
"""
# ############## 5. 鑾峰彇鎵€鏈塩ommit ##############
"""
import os
from git.repo import Repo
local_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("
")
real_log_list = [eval(item) for item in log_list]
print(real_log_list)
"""
# ############## 6. 鍒囨崲鍒嗘敮 ##############
"""
import os
from git.repo import Repo
local_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. 鎵撳寘浠g爜 ##############
"""
with open(os.path.join('code', 'fuck.tar'), 'wb') as fp:
repo.archive(fp)
"""
1.2鎶奼it鎿嶄綔灏佽鎴愪竴涓被
import os
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'):
self.local_path = local_path
self.repo_url = repo_url
self.repo = None
self.initial(repo_url, branch)
def initial(self, repo_url, branch):
"""
鍒濆鍖杇it浠撳簱
: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().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')
log_list = commit_log.split("
")
return [eval(item) for item in log_list]
def tags(self):
"""
鑾峰彇鎵€鏈塼ag
: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()
2.python鍘嬬缉鏂囦欢鐩稿叧
鍦╬y2鍜宲y3涓鏂囦欢杩涜瑙e帇缂╃◢鏈変笉鍚屻€?/p>
- shutil 妯″潡銆愬帇缂╂敮鎸乸y2鍜宲y3锛岃В鍘嬪彧鏀寔py3銆?/li>
tarfile / zipfile妯″潡銆愭敮鎸乸y2鍜宲y3銆?/p>
shutil妯″潡绀轰緥
import shutil
# 鏂囦欢鍘嬬缉
"""
ret = shutil.make_archive(
base_name="code/www", # 鍘嬬缉鍖呮枃浠惰矾鍔? format='zip', # 鈥渮ip鈥? 鈥渢ar鈥? root_dir='code/fuck' # 琚帇缂╃殑鏂囦欢浠?)
print(ret)
"""
# 瑙e帇鏂囦欢
"""
shutil._unpack_zipfile('code/www.zip', 'code/new')
shutil._unpack_tarfile('code/www.tar', 'code/new')
"""
shutil妯″潡
zipfile妯″潡绀轰緥
import zipfile
# 鍘嬬缉
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()
# 瑙e帇
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close()
zipfile妯″潡
tarfile妯″潡绀轰緥
import tarfile
# 鍘嬬缉
tar = tarfile.open('your.tar', 'w')
tar.add('utils/codes/luffycity/a1.py')
tar.add('utils/codes/luffycity/a3.py')
tar.close()
# 瑙e帇
tar = tarfile.TarFile('code/www.tar', 'r')
tar.extractall(path='/code/x1/') # 鍙缃В鍘嬪湴鍧€
tar.close()
tarfile妯″潡
3.鎵ц鏈湴鍛戒护
import subprocess
result = subprocess.check_output('ls -l', cwd='/Users/wupeiqi/PycharmProjects', shell=True)
print(result.decode('utf-8'), type(result))
4.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')
以上是关于浠g爜鍙戝竷绯诲垪1的主要内容,如果未能解决你的问题,请参考以下文章