-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_repo.py
121 lines (103 loc) · 3.88 KB
/
get_repo.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import sys
from github import Github
from github import Auth
from github.GithubException import UnknownObjectException, GithubException
lsts = []
reponaming = " Listing contents of "
countrepolen = " Listing repos of "
warninglen = " Warning!: Repository not found! "
maindir = os.path.dirname(os.path.abspath(__file__))
# Read the token from the file
try:
with open(f"{maindir}/mykey.txt", "r") as file:
key = file.read().strip()
except FileNotFoundError:
print(
"\033[93mError: Token file 'mykey.txt' not found.\033[0m"
) # Yellow text for error
sys.exit(1)
auth = Auth.Token(key)
g = Github(auth=auth)
user = g.get_user()
# Color codes
GREEN = "\033[92m"
YELLOW = "\033[93m"
RESET = "\033[0m"
BLUE = "\033[94m"
def ls_inside_repo(lsts):
for con in lsts:
filecnt = 0
dircnt = 0
correct = True
try:
mycontent = g.get_user().get_repo(con)
print()
print(f"{BLUE}*{'='*(len(reponaming)+len(con)+1)}*{RESET}")
print(f"{BLUE}| Listing contents of {con} |{RESET}")
print(f"{BLUE}*{'-'*(len(reponaming)+len(con)+1)}*{RESET}")
print()
contents = mycontent.get_contents("")
if not contents:
print(f"{YELLOW} Warning: Repository '{con}' is empty.{RESET}")
print(f"{YELLOW}*{'-'*(len(warninglen)+len(con))}*{RESET}")
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
dircnt += 1
print(f"{BLUE} {file_content.path}/ {RESET}")
else:
print(f"{GREEN} {file_content.path} {RESET}")
filecnt += 1
except UnknownObjectException:
correct = False
print(f"\n{YELLOW} Warning!: Repository '{con}' not found! {RESET}")
print(f"{YELLOW}*{'-'*(len(warninglen)+len(con)+1)}*{RESET}")
except GithubException as e:
correct = False
if e.status == 404 and "empty" in e.data.get("message", "").lower():
print(f"\n{YELLOW} Warning!: Repository '{con}' is empty!{RESET}")
print(f"{YELLOW}*{'-'*(len(warninglen)+len(con))}*{RESET}")
else:
print(
f"\n{YELLOW} Warning!: An unexpected error occurred while accessing '{con}' - {e}{RESET}"
)
print(f"{YELLOW}*{'-'*(len(warninglen)+len(con)+1)}*{RESET}")
except Exception as e:
correct = False
print(
f"\n{YELLOW} Warning!: An unexpected error occurred while accessing '{con}' - {e}{RESET}"
)
print(f"{YELLOW}*{'-'*(len(warninglen)+len(con)+1)}*{RESET}")
if correct:
print()
print(f"{BLUE} Dir: {dircnt} {GREEN} File: {filecnt}")
print()
def ls_repos():
try:
myrepos = g.get_user().get_repos()
print()
print(f"{BLUE}*{'='*(len(countrepolen)+len(user.login)+1)}*{RESET}")
print(f"{BLUE}| Listing repos of {user.login} |{RESET}")
print(f"{BLUE}*{'-'*(len(countrepolen)+len(user.login)+1)}*{RESET}")
print()
# Set to track repositories (ignoring forks)
repo_names = set()
cnt = 0
for repo in myrepos:
if repo.name not in repo_names: # Only count non-fork repositories
cnt += 1
print(f"{BLUE} {repo.name}{RESET}")
repo_names.add(repo.name)
# Printing Total Repos in bold blue color
print()
print(f"{BLUE} \033[1mTotal Repos: {cnt}{RESET}")
print()
except Exception as e:
print(f"{YELLOW}Error: Unable to list repositories - {e}{RESET}")
# Main logic
lsts = sys.argv[1:]
if len(lsts) > 0:
ls_inside_repo(lsts)
else:
ls_repos()