-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathTestGitTree.py
51 lines (42 loc) · 1.29 KB
/
TestGitTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年1月16日
@author: Irony
@site: https://pyqt.site https://github.com/PyQt5
@email: 892768447@qq.com
@file: Test.TestGitTree
@description:
"""
import json
import os
import requests
__Author__ = "Irony"
__Copyright__ = "Copyright (c) 2019"
# https://raw.githubusercontent.com/PyQt5/PyQt/master/.gitattributes
RepositoryTrees = {'/': []}
url = 'https://api.github.com/repos/PyQt5/PyQt/git/trees/master?recursive=1'
if not os.path.isfile('tree.json'):
req = requests.get(url)
open('tree.json', 'wb').write(req.content)
trees = req.json()['tree']
else:
trees = json.loads(open('tree.json', 'rb').read().decode())['tree']
for tree in trees:
path = tree['path']
if path.startswith('.'):
# .开头的文件或目录跳过
continue
# 根目录下的文件
if path.count('/') == 0 and tree['type'] == 'blob':
RepositoryTrees['/'].append(tree)
continue
# 提取整理所有根节点下的目录
name = path.split('/')[0]
if name not in RepositoryTrees:
RepositoryTrees[name] = []
else:
# 添加非目录
if tree['type'] != 'tree':
RepositoryTrees[name].append(tree)
print(RepositoryTrees)