-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitapy.py
226 lines (191 loc) · 6.93 KB
/
gitapy.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import os
import sys
import getopt
import requests
import json
import inspect
import src.reading
import src.commits
import src.pr
import src.db
import src.utils
import src.caching
from src.reading import *
from src.commits import *
from src.pr import *
from src.db import *
from src.utils import *
from src.fork import *
from src.caching import _get_owner_list, _update_owner_list
from src.cachy import *
from src.watcher import *
# import _ functions also
from src.reading import _get_owner_repositories
from src.utils import _prettify
from rich import print as rprint
import subprocess
def _get_xdg_config_home():
if os.environ.get("XDG_CONFIG_HOME") != None:
XDG_CONFIG_HOME = os.environ["XDG_CONFIG_HOME"] + "/gitapy/"
else:
XDG_CONFIG_HOME = os.environ["HOME"] + "/.config/gitapy/"
return XDG_CONFIG_HOME
def _get_cache(cache_kind):
cache_path = _get_xdg_config_home() + "cache/" + cache_kind + ".txt"
with open(cache_path, "r") as f:
cache = f.read()
# split lines and make a list encoded to gum
cache = cache.splitlines()
cache = "\n".join(cache)
return cache
# read config file
def read_config():
# get config from XDG_CONFIG_HOME
if os.environ.get("XDG_CONFIG_HOME") != None:
config_path = os.environ["XDG_CONFIG_HOME"] + "/gitapy/config"
else:
config_path = os.environ["HOME"] + "/.config/gitapy/config"
# read definitions from .env file
with open(config_path, "r") as f:
for line in f:
if line.startswith("#"):
continue
else:
key, value = line.strip().split("=")
# remove whitespaces
key = key.strip()
value = value.strip()
os.environ[key] = value
# make command line arguments and help
def _cmd():
# get command line arguments
args = sys.argv[1:]
# read config file
read_config()
# check if there are any arguments
if len(args) == 0 or args[0] in ("-h", "--help"):
rprint("GitAPy - A Python wrapper for the GitHub API", ":vampire:")
rprint("Usage: gitapy.py [options] [arguments]")
rprint("")
rprint('Use "gitapy.py --help" for more information.')
rprint("Author: Laio O. Seman")
rprint("Email: [email protected]")
if len(args) == 0:
sys.exit(0)
rprint("")
rprint("Usage: gitapy.py [options] [arguments]")
rprint("")
rprint("Options:")
rprint(" -h, --help: show this help message and exit")
rprint(" -v, --version: show version information and exit")
rprint(" -l, --list: list repository contents")
rprint(" -a --create: create a new file")
rprint(" -u, --update: update an existing file")
rprint(" -d, --delete: delete an existing file")
rprint(" -t, --tarball: get repository tarball")
rprint(" -p, --pulls: list pull requests")
rprint(" -c, --commits: list commits")
rprint(" -s, --sync: sync fork with upstream")
rprint("")
rprint("Arguments:")
rprint(" -o, --owner: repository owner")
rprint(" -r, --repo: repository name")
rprint(" -p, --path: repository path")
rprint(" -m, --message: commit message")
rprint(" -f, --file: file name")
rprint(" -s, --sha: file SHA")
rprint(" -r, --ref: reference name")
rprint(" -j, --json: output in JSON format")
rprint(" -z, --zip: output in ZIP format")
rprint(" -r, --tar: output in TAR format")
rprint(" -z, --extra options: extra options")
sys.exit(1)
# create getopt function
all_options = ["help", "version", "list", "add", "update", "delete", "commits", "pulls", "tarball"]
all_arguments = [
"owner",
"repo",
"number",
"path",
"message",
"file",
"sha",
"branch",
"ref",
"archive",
"token",
"version",
"json",
"zip",
"tar",
"xtra",
]
all_arguments_cmd = ["-" + str[0] for str in all_arguments]
options = sys.argv[1:]
option = args[0]
# to find owner, the index which value is -o or --owner
# for each arguments in argument, create a variable with the item name and the value of the next item
for i in range(len(args)):
if args[i] in all_arguments_cmd:
# get index of args[i] in all_arguments_cmd
index = all_arguments_cmd.index(args[i])
globals()[all_arguments[index]] = args[i + 1]
if args[i] == "-o" or args[i] == "--owner":
_update_owner_list(args[i + 1])
else:
continue
if option in ("-l", "--list"):
list_repository_contents(owner, repo)
elif option in ("-c", "--commits"):
list_repository_commits(owner, repo)
elif option in ("-a", "--add"):
add_repository_content(owner, repo, path, message, file)
elif option in ("-u", "--update"):
update_repository_content(owner, repo, path, message, file, sha)
elif option in ("-d", "--delete"):
delete_repository_content(owner, repo, path, message, sha)
elif option in ("-t", "--tarball"):
get_repository_tarball(owner, repo, ref, archive)
elif option in ("-p", "--pulls"):
list_pull_requests(owner, repo)
elif option in ("-i", "--info"):
get_pull_request(owner, repo, number, xtra)
elif option in ("-s", "--sync"):
merge_fork_upstream(owner, repo, branch)
else:
# handle other options or show an error
rprint("Error: Invalid o;ption")
if __name__ == "__main__":
# check if no arguments were passed, if so, start iteractive mode
# if len(sys.argv) >= 2:
if (len(sys.argv) >= 2) and (sys.argv[1] == "cachy"):
# call cachy function
rprint("GitAPy - A Python wrapper for the GitHub API", ":vampire:")
rprint("v0.2 - CODNOME: vlad eater")
rprint("Usage: gitapy.py [options] [arguments]")
rprint("")
rprint('Use "gitapy.py --help" for more information.')
rprint("Author: Laio O. Seman")
rprint("Email: [email protected]")
if len(sys.argv) == 3:
if sys.argv[2] == "-debug":
read_config()
cachy_update()
sys.exit(0)
if (len(sys.argv) >= 2) and (sys.argv[1] == "watcher"):
# call cachy function
rprint("GitAPy - A Python wrapper for the GitHub API", ":vampire:")
rprint("v0.2 - CODNOME: vlad eater")
rprint("Usage: gitapy.py [options] [arguments]")
rprint("")
rprint('Use "gitapy.py --help" for more information.')
rprint("Author: Laio O. Seman")
rprint("Email: [email protected]")
if len(sys.argv) == 3:
if sys.argv[2] == "-debug":
read_config()
do_watch()
sys.exit(0)
else:
# call main function
_cmd()