-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubfilestree.py
More file actions
84 lines (68 loc) · 2.39 KB
/
githubfilestree.py
File metadata and controls
84 lines (68 loc) · 2.39 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import requests
import os
import getopt
import sys
# Sometimes you need to know what types of files are in a github repo along
# with their layout in order to prepare for some follow-on analysis.
# This script is a model for extracting a list of files in tree format
# This version is for GH environments where there are organizations
def main(argv):
if sys.version_info < (3, 10):
raise Exception("Use only with Python 3.10 or higher")
username = ''
orgname = ''
repo = ''
if not (sys.argv[1:]):
print('Get list of repo tree file names from github.com.')
print('Inputs required: githubfilestree.py -u <user> -r <repo> -o <organization>')
sys.exit(2)
try:
opts, args = getopt.getopt(argv, 'hu:r:o:', ['user=','repo=','org='])
except getopt.GetoptError as err:
print(err)
print('Get list of repo tree file names from github.com.')
print('githubfilestree.py -u <user> -r <repo> -o <organization>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('Get list of repo tree file names from github.com.')
print('githubfilestree.py -u <user> -r <repo> -o <organization>')
sys.exit()
elif opt in ("-u", "--user"):
username = arg
elif opt in ("-r", "--repo"):
repo = arg
elif opt in ("-o", "--org"):
orgname = arg
"""
username = '<UserNameHere>'
orgname = '<OrganizationNameHere>'
repo = '<RepositoryNameHere>'
token = '<PersonalTokenFromEnvironmentorSecretStoreHere>'
# or get the token from the environment:
"""
token = os.environ['GHPAT']
url = "https://api.github.com/repos/{}/{}/git/trees/main?recursive=1".format(orgname, repo)
reposeparator = "="*80
# Authenticate & create session
####
gh_session = requests.Session()
gh_session.auth = (username, token)
# Fetch repo data
####
r = gh_session.get(url)
response = r.json()
# Report
####
print(reposeparator)
numberoffiles = len(response["tree"])
print(f"Github user: {username}")
print(f"Github org: {orgname}")
print(f"Github repo: {repo}")
print(f"{str(numberoffiles)} files in {orgname}/{repo} include: ")
for file in response["tree"]:
print(file["path"])
print(reposeparator)
if __name__ == '__main__':
main(sys.argv[1:])