【发布时间】:2018-06-27 18:27:41
【问题描述】:
好的,我一直致力于从远程 git 存储库中提取数据,并使用 Python 脚本根据文件的最后修改日期生成 csv 报告列表文件。我已经能够使用 subprocess 获取最新代码,并且还能够生成报告。这两个函数的代码sn-p如下:
> import subprocess
> process = subprocess.Popen("git pull",stdout=subprocess.PIPE)
> output = process.communicate()[0]
用于生成 csv
> with open('excelout1.csv', 'w') as csv_file:
> wr = csv.writer(csv_file, delimiter=',')
> for row in myfilelist:
> wr.writerow(row)
所以现在,我得到了所有文件的最后修改日期,但问题是,生成的日期是我本地 repo 中的文件更新的时间,即当我接受最新的 pull 时,很明显。我想要的是远程存储库中每个文件的最后修改日期和作者。
使用 Git bash 生成上次修改日期的命令是 git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort。我想知道如何在 python 脚本中使用这个命令。我对 python 还很陌生,任何形式的帮助都会受到赞赏。
编辑:根据 Mufeed 的建议使用当前代码
import os, csv, glob, time
import pandas as pd
import subprocess
process = subprocess.Popen("git pull", stdout=subprocess.PIPE)
output = process.communicate()[0]
p = subprocess.check_output(['git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort'],cwd = "C:\Users\sherin.sunny\git\ng-ui",shell=True)
print(p)
print ('-'*60) # just vanity
date_file_list = []
for dirpath, dirs, files in os.walk(".\src\\"):
# select the type of file, for instance *.jpg or all files *.*
for file in glob.glob(dirpath + '/*.component.ts'):
stats = os.stat(file)
lastmod_date = time.localtime(stats[8])
date_file_tuple = lastmod_date, file
date_file_list.append(date_file_tuple)
#print date_file_list # test
date_file_list.sort()
date_file_list.reverse() # newest mod date now first
print ("%-40s %s" % ("filename:", "last modified:"))
myfilelist = []
for file in date_file_list:
# extract just the filename
folder, file_name = os.path.split(file[1])
# convert date tuple to MM/DD/YYYY HH:MM:SS format
file_date = time.strftime("%m/%d/%y %H:%M:%S", file[0])
myfilelist.append([file_name, file_date])
with open('excelout1.csv', 'w') as csv_file:
wr = csv.writer(csv_file, delimiter=',')
for row in myfilelist:
wr.writerow(row)
【问题讨论】:
-
为什么不使用子流程模块本身呢? subprocess.check_output(['git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {}'],shell=True)
-
@mufeed 默认不使用 git bash
-
不知道我理解的对不对。但是要得到他想要的结果,我提到的代码就足够了吧?执行该代码后,我得到了正确的输出。如果我理解错了,请告诉我。
-
感谢@Peter 的澄清。我现在明白了。