-
Notifications
You must be signed in to change notification settings - Fork 392
/
git_base.py
124 lines (108 loc) · 3.21 KB
/
git_base.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
from datetime import datetime
import subprocess
import traceback
import locale
from sys import argv as sys_argv
def fuck_windows(std):
lines = std.readlines()
try:
return ''.join(line.decode('utf-8') for line in lines)
except UnicodeDecodeError:
encoding = locale.getpreferredencoding()
return ''.join(line.decode(encoding) for line in lines)
def fuck_cmd(cmd):
child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
child.stdin.close()
ret = child.wait()
val = fuck_windows(child.stdout)
if val:
val += '\n' + fuck_windows(child.stderr)
else:
val = fuck_windows(child.stderr)
return ret, val
subprocess.getstatusoutput = fuck_cmd
def try_call(call, *arg, **kw):
try:
call(*arg, **kw)
except:
print(traceback.format_exc())
def run_pythonfile(f):
ret, val = subprocess.getstatusoutput(f"python3 ./{f}")
print(ret, val)
if ret:
ret, val = subprocess.getstatusoutput(f"python ./{f}")
print(ret, val)
return ret
def cmd_lines(lines):
for line in lines:
ret, val = subprocess.getstatusoutput(line)
if ret:
print(ret, val)
return ret
def git_setIdentity():
lines = [
'git config --global user.name "github-actions"',
'git config --global user.email "[email protected]"'
]
return cmd_lines(lines)
def git_rm_upstream():
lines = [
"git remote rm upstream"
]
return cmd_lines(lines)
def git_add_upstream(url):
try_call(git_rm_upstream)
lines = [
"git config --global pull.rebase true",
"git config --global merge.ours.driver true",
f"git remote add upstream {url}"
]
return cmd_lines(lines)
def git_revoke():
lines = [
'git reset --hard "HEAD^"',
'git push origin main --force'
]
return cmd_lines(lines)
def git_c2upstream():
lines = [
'git fetch upstream --depth=1',
'git checkout upstream/main'
]
return cmd_lines(lines)
def git_push():
ret, val = subprocess.getstatusoutput("git status")
if ret or "nothing to commit" in val:
print(ret, val)
return 1
lines = [
"git add .",
'git commit -m "autocreated by git_base.py"',
"git push origin main --force"
]
return cmd_lines(lines)
def run_pythonfile_arg(f, arg):
ret, val = subprocess.getstatusoutput(f'python3 ./{f} "{arg}"')
print(ret, val)
if ret:
ret, val = subprocess.getstatusoutput(f'python ./{f} "{arg}"')
print(ret, val)
return ret
def get_arg():
if (len(sys_argv) != 2) or (not sys_argv[1]):
print('no arg')
return ''
return sys_argv[1]
def pip_install():
lines = [
'python3 -m pip install --upgrade pip',
'pip3 install -r requirements.txt'
]
ret = cmd_lines(lines)
if ret:
lines = [
'python -m pip install --upgrade pip',
'pip install -r requirements.txt'
]
return cmd_lines(lines)
try_call(git_setIdentity)