forked from alanndz/build_kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select
executable file
·209 lines (191 loc) · 7 KB
/
select
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
#!/usr/bin/env python
#
# Copyright (C) 2019 @alanndz (Telegram and Github)
# SPDX-License-Identifier: GPL-3.0-or-later
import sys, getopt, os, subprocess
cwd = os.getcwd()
config = os.path.join(cwd, 'config') # config/
which = lambda: open(os.path.join(config, 'folder')).read() # config/folder
kernel = 'aLn'
status = lambda idk, codename, version, type, branch: '''
>>> %s :
Codename: %s
Kernel Version: %s
Scheduler Kernel: %s
Branch: %s ''' %(idk, codename, version, type, branch)
__help__ = '''Usage: select [Command] [Argument]
Commands:
Simple\tLong\t\tDetail
-h\t--help\t\tShow this text
-n\t--new\t\tCreate new
-u\t--update\tUpdate Codename, Type, and other (need argument)
-r\t--release\tRelease Kernel or Not
-s\t--switch\tSwitch to another stuff (need argument)
-l\t--list\t\tShow list stuff
-d\t--delete\tDelete stuff (need argument)
-c\t--commit\tCommit default True, if want false just add this
A command is needed.'''
def sh(cmd, input="", cwd=cwd, silence = False):
rst = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, input=input.encode("utf-8"), cwd=cwd)
assert rst.returncode == 0, rst.stderr.decode("utf-8")
if silence:
return rst.stdout.decode("utf-8")
return print (rst.stdout.decode("utf-8"))
def rmdir(dir):
for i in os.listdir(dir):
os.remove(os.path.join(dir, i))
os.rmdir(dir)
class Main:
def __init__(a, argv):
unixOpt = "hnu:rs:ld:c"
gnuOpt = ["help", "new", "update=",
"release", "switch=",
"list", "delete=",
"commit"]
a.commit = True
if len(argv) == 0:
print (__help__)
sys.exit()
try:
opts, args = getopt.getopt(argv, unixOpt, gnuOpt)
except getopt.GetoptError as err:
print (str(err))
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print (__help__)
sys.exit()
elif opt in ("-n", "--new"):
a.new()
elif opt in ("-u", "--update"):
l = a.list()
if not arg in l:
print ('Your value is not exist or not availabe, check use -l or --list')
sys.exit()
a.update(arg)
elif opt in ("-r", "--release"):
if a.upRelease():
a.addCommit('RELEASE: True')
else:
a.addCommit('RELEASE: False')
elif opt in ("-s", "--switch"):
if not arg in a.list():
print ('Your value is not exist or not available, check using -l or --list')
sys.exit()
open(os.path.join(config, 'folder'), 'w').write(arg)
a.addCommit('SWITCH: %s' %(arg))
elif opt in ("-l", "--list"):
a.status()
elif opt in ("-c", "--commit"):
a.commit = False
elif opt in ("-d", "--delete"):
if not arg in a.list():
print ('Your value is not exist or not available, check using -l or --list')
sys.exit()
rmdir(os.path.join(config, arg))
a.addCommit('DELETE: %s' %(arg))
print ('Success delete: %s' % arg)
def addCommit(a, value):
if a.commit:
#print (value)
sh('git add . && git commit -asm "%s"' %(value))
def new(a):
while 1:
fol = input('Enter a folder/name: ')
if fol: break
try:
os.mkdir(os.path.join(config, fol))
except OSError as err:
print ('Creation folder error, exit')
print (str(err))
sys.exit()
else:
print ('Creation foler %s Success, next' % fol)
folder = os.path.join(config, fol)
open(os.path.join(config, 'folder'), 'w').write(fol)
a.writeConfig(folder, 'Codename', 'codename')
a.writeConfig(folder, 'Branch', 'branch')
a.upRelease()
while 1:
i = input('Enter Version Kernel: ')
if i: break
open(os.path.join(folder, 'version'), 'w').write(i)
a.upType(folder)
dict = a.getVal(folder)
commit = 'NEW: %s: %s: %s %s' %(dict['type'], fol, kernel, dict['codename'])
a.addCommit(commit)
del (dict)
def list(a):
files = os.listdir(config)
dir = []
for name in files:
if os.path.isdir(os.path.join(config, name)):
dir.append(name)
return dir
def upRelease(a):
print ('Release Kernel? \n\n[1] False \n[2] True \n')
while 1:
i = input('Enter a Value: ')
i = int(i)-1
if i == 0 or i == 1: break
open(os.path.join(config, 'release'), 'w').write(str(i))
if i:
return True
else:
return False
def upType(a, folder):
print ('Choose Scheduler Kernel: \n\n[1] HMP\n[2] EAS\n[3] CFS\n')
while 1:
i = input('Enter a Value: ')
i = int(i)
if i == 1 or i == 2 or i == 3: break
w = open(os.path.join(folder, 'type'), 'w')
v = ['HMP', 'EAS', 'CFS'][i-1]
w.write(v)
w.close()
def update(a, arg):
dir = os.path.join(config, arg)
print ('Editing folder %s :\n\n[1]Codename\n[2]Branch\n[3]Kernel Version\n[4]Scheduler Kernel\n' % arg)
list = ['codename', 'branch', 'version', 'type']
i = input('Enter Choose: ')
i = int(i)-1
if i == 3:
a.upType(dir)
dict = a.getVal(dir)
a.addCommit('UPDATE: Scheduler: %s: %s' %(arg, dict['type']))
return
r = open(os.path.join(dir, list[i])).read()
print ('\nOld %s is %s \n' %(list[i], r))
j = input('Enter new value: ')
open(os.path.join(dir, list[i]), 'w').write(j)
commit = 'UPDATE: %s: %s: from %s to %s' %(arg, list[i], r, j)
a.addCommit(commit)
def status(a):
print ('Global Release: %s' %(open(os.path.join(config, 'release')).read()))
print ('Folder will be compile: %s' %(open(os.path.join(config, 'folder')).read()))
list = ['codename', 'version', 'type', 'branch']
data = []
l = a.list()
for i in l:
for j in list:
dir = os.path.join(config, i)
r = open(os.path.join(dir, j)).read()
data.append(r)
print (status(i, data[0], data[1], data[2], data[3]))
data = []
def getVal(a, dir):
list = ['codename', 'type', 'version', 'branch']
data = {}
for i in list:
r = open(os.path.join(dir, i)).read()
data[i] = r
return data
def writeConfig(a, dir, ask, conf):
while 1:
i = input('Enter a %s: ' % ask)
if i: break
w = open(os.path.join(dir, conf), 'w')
w.write(i)
w.close()
if __name__ == "__main__":
Main(sys.argv[1:])