python - 使用python提取最后修改日期,git存储库中文件的作者
问题描述
好的,所以我一直致力于从远程 git 存储库中提取数据,并使用 Python 脚本根据文件的最后修改日期生成 csv 报告列表文件。我已经能够使用 subprocess 获得最新的代码,并且我也能够生成报告。这两个函数的代码片段如下:
> 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)
解决方案
我不知道我是否正确理解了你的问题。检查下面的代码片段。相同的子流程模块将输出作为问题描述。
import subprocess
p = subprocess.check_output(['git ls-files -z | xargs -0 -n1 -I{} -- git
log -1 --format="%ai {}" {} | sort'],cwd = "path\to\directory",shell=True)
#cwd = change working directory
print(p)
输出
b'2018-06-23 09:42:40 -0700 CONTRIBUTING.md\n
2018-06-23 09:42:40 -0700 data_reader.py\n
2018-06-23 09:42:40 -0700 LICENSE\n
2018-06-23 09:43:37 -0700 README.md\n'
subprocess.check_output 用于将输出存储到变量中,以便您可以从中提取所需的值。
推荐阅读
- c# - 通过 GetComponentInChildren 访问 Unity3D 可配置关节组件
- sqlite - 从 sqflite 按值获取数据
- php - PayPal沙盒订阅付款错误
- python - 如何将 JSON 列表转换为 CSV 文件中的两个不同列?
- java - AndroidStudio - 活动和布局问题
- python - 使用请求从 9gag 抓取图像,但图像链接不会显示
- mongoose - 如何在具有特定 ID 的猫鼬模型上发表评论
- php - 在 WooCommerce 中显示 B2B 交易的反向收费文本
- firebase - 在flutter中从firestore中检索特定于文档的图像
- javascript - 彩色拦截区