代码如下:
#!/usr/bin/env python# -*- coding: utf-8 -*-#
@name : find_t.py# @author : cat#
@date : 2017/8/2.import osimport timedef bash_shell(bash_command):
"""
python 中执行 bash 命令 :param bash_command:
:return: bash 命令执行后的控制台输出
"""
try:
return os.popen(bash_command).read().strip()
except: return Nonedef find_target(target_path="./../", key='.git'):
"""
查找目标目录所在的目录 : 如
/aa/bb/.git --> return /aa/bb/
:param target_path:
:param key: target
:return:
"""
walk = os.walk(target_path)
for super_dir, dir_names, file_names in walk:
for dir_name in dir_names:
if dir_name == key:
dir_full_path = os.path.join(super_dir, dir_name)
# print(dir_full_path, super_dir, dir_name, sep=" ## ")
yield super_dirif __name__ == '__main__':
print("start execute bash ...........")
st = time.time()
cwd = os.getcwd()
# this for repo
f
or repo_path in find_target(os.getcwd(), key='.repo'):
os.chdir(repo_path)
if repo_path == os.getcwd():
print('find repo in -->', repo_path)
print(bash_shell('pwd'))
print(bash_shell('repo forall -c git config core.fileMode false --replace-all'))
else:
print('error in chdir 2 {}'.format(repo_path))
if os.getcwd() != cwd:
os.chdir(cwd)
if os.getcwd() != cwd:
print('change 2 cwd FAIL !!! {}'.format(cwd))
# this for git
for git_path in find_target(os.getcwd(), key='.git'):
os.chdir(git_path)
if git_path == os.getcwd():
print('find git in -->', git_path)
print(bash_shell('pwd'))
print(bash_shell('git config --global core.filemode false'))
else:
print('error in chdir 2 {}'.format(git_path))
if os.getcwd() != cwd:
os.chdir(cwd)
if os.getcwd() != cwd:
print('change 2 cwd FAIL !!! {}'.format(cwd))
et = time.time()
print('
#### execute finished in {:.3f} seconds ####'.format(et - st))
print('
') # test for bash_command
# print(bash_shell('git init'))
# print(bash_shell('ls -al'))
2. 关于 Git 和 python 的问题:使用limejs的过程中要使用python去编译,并且要用Git下载一些东西,出错如下:
are you trying to run a linux program on windows?
"
D:\Program Files\Git/libexec/git-core\git-svn
"
is not a valid windows path.
3. gitpython如何修改文件内容不影响格式
三种方法
_弧⑿薷脑募绞?
_ef alter(file,old_str,new_str):
?
?"""
_婊晃募械淖址?
?:param file:文件名
?:param old_str:就字符串
?:param new_str:新字符串
?:return:
?
?"""
_ile_data = ""
?
_ith open(file, "r", encoding="utf-8") as f:
?
_or line in f:
?
_f old_str in line:
?
_ine = line.replace(old_str,new_str)
?
_ile_data += line
?
_ith open(file,"w",encoding="utf-8") as f:
?
_.write(file_data)
?
_lter("file1", "09876", "python")
?
__言募谌莺鸵薷牡哪谌菪吹叫挛募薪写娲⒌姆绞?
?
?2.1 python字符串替换的方法,修改文件内容
?
_mport os
?
_ef alter(file,old_str,new_str):
?
?"""
?
_婊坏淖址吹揭桓鲂碌奈募校缓蠼募境挛募奈次募拿?
?
?:param file: 文件路径
?
?:param old_str: 需要替换的字符串
?
?:param new_str: 替换的字符串
?
?:return: None
?
?"""
?
_ith open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
?
_or line in f1:
?
_f old_str in line:
?
_ine = line.replace(old_str, new_str)
?
_2.write(line)
?
_s.remove(file)
?
_s.rename("%s.bak" % file, file)
?
_lter("file1", "python", "测试")
?
?2.2 python 使用正则表达式 替换文件内容 re.sub 方法替换
?
_mport re,os
?
_ef alter(file,old_str,new_str):
?
_ith open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
?
_or line in f1:
?
_2.write(re.sub(old_str,new_str,line))
?
_s.remove(file)
?
_s.rename("%s.bak" % file, file)
4. python通过git push上传文件
你可以使用subpocess直接调用git命令就可以了。
importshlex如果解决了您的问题请采纳!
importsubprocess
#push本地的test分支到服务器上的master分支
cmd="gitpushorigintest:master"
cwd="你项目的路径"
subprocess.check_output(shlex.split(cmd),cwd=cwd)
如果未解决请继续追问
5. 怎么用python去git commit
git是分为三部分,一部分是你自己的文件,另外一个是缓存区,最后一个是本地库。当你修改了自己的文件后,你会git add xx将修改保存到缓存区,然后再用commit推送修改到本地库中。
6. python怎么直接操作git mvn命令
一般这类对象文件和存档文件都是编译过程中出现的,我们用不着跟踪它们的版本。第二行告诉Git忽略所有以波浪符(~)结尾的文件,许多文本编辑软件(比如Emacs)都用这样的文件名保存副本。此外,你可能还需要忽略log,tmp或者pid目录,以及自动生成的文档等等。要养成一开始就设置好.gitignore文件的习惯,以免将来误提交这类无用的文件。
7. python处理git日志文本
数据量有多大,可以有偿帮你处理
8. python能使用gitgunicorn不能
可以使用。
python能使用git,gunicorn。国外的大家都在使用github,很多的开源项目都在github上面,Python使用gunicorn部署Flask项目。
Python由荷兰数学和计算机科学研究学会的吉多·范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。