-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkkcmd
executable file
·224 lines (190 loc) · 4.36 KB
/
kkcmd
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
#!/usr/bin/python
#!/usr/bin/python
# vim: set syntax=python:
from __future__ import print_function
import os,sys, getopt
#== credit__
#=== parameters_
#=== def_mygetcwd()
#=== command_arg
#=== read_temp_k
#== list__
#=== get_the_keyName and KeyDir
#=== isEnv_
#== credit__
# Created by benyang tang
# Requirements:
# python
# aria2c
# Feb 2019
#
#=== parameters_
kkDir = os.environ['KKDIR']
temp_kFile = '%s/temp_k' %(kkDir)
temp_k2File = '%s/temp_k2' %(kkDir)
width1 = 10
#=== def_mygetcwd()
def mygetcwd():
t1 = 'pwd'
return os.popen(t1,'r').read()[:-1]
#=== command_arg
optStr = 'cduvl'
try:
opts, args1 = getopt.getopt(sys.argv[1:],optStr)
except getopt.error as msg:
print('wrong options. exiting.')
sys.exit(1)
isDelete = 0
isEnv = 0
isList = 0
isCd = 0
for o,a in opts:
if o=='-d':
isDelete = 1
if o=='-v':
isEnv = 1
if o=='-u':
isEnv = 2
if o=='-l':
isList = 1
if o=='-c':
isCd = 1
#=== read_temp_k
try:
lines = open(temp_kFile).readlines()
if lines[-1] == '\n':
del lines[-1]
except: # initialize the kk system
print('initialize the kk file.')
print(temp_kFile)
if not os.path.isdir(kkDir):
os.mkdir(kkDir)
if not os.path.isfile(temp_kFile):
os.system('touch %s' %(temp_kFile))
lines = []
#== list__
if isList:
if len(args1)==0:
myNum1 = '0'
myNum2 = 'z'
else:
myNum1 = args1[0]
if len(args1)==1:
myNum2 = myNum1
elif len(args1)==2:
myNum2 = args1[1]
len1 = len(myNum1)
len2 = len(myNum2)
#=== list lines
dashes = '-'*width1
dots = '.'*width1
print('')
for line in lines:
temp1 = str.split(line)
kkName = temp1[0]
kkDir = temp1[1]
if kkName[:len1]>=myNum1 and kkName[:len2]<=myNum2:
if kkDir[-1]=='/':
kkName1 = kkName + ' ' + dashes[:(width1-len(kkName)-1)]
print(('%s %s' %(kkName1,kkDir)))
else:
kkName1 = kkName + ' ' + dots[:(width1-len(kkName)-1)]
print(('%s %s' %(kkName1,kkDir)))
print(' ')
sys.exit(0)
# end of kk -l
#=== get_the_keyName and KeyDir
myNum = None
if len(args1)>0:
myNum = args1[0]
if len(myNum)>10:
myNum = myNum[:10]
file0 = None
if len(args1)>1:
file0 = args1[1]
#=== isCd_
if isCd:
#=== which one matches
i2 = None
for i in range(len(lines)):
temp1 = str.split(lines[i])
# print(temp1)
if temp1[0]==myNum:
i2 = i
print((temp1[1]))
sys.exit(0)
if type(i2)==type(None):
#print('Dir shortcut %s does not exit. Please check.' %(myNum))
print(' .')
sys.exit(1)
# end of kk -c
#=== isEnv_
if isEnv:
letterDigit = 'abcdefghijklmnopqrstuvwxyz0123456789_'
lines1 = []
for line in lines:
temp1 = str.split(line);
kName = temp1[0]
isCheckName = 1
if myNum is None or kName==myNum:
#--- No symbols (.,#$%, etc) are allowed in env names
isName = 1
for i in str.lower(kName):
if not i in letterDigit:
isName = 0
break
#--- make line of variable setting
if isName:
if isEnv==1:
kDir = temp1[1]
if kDir[-1]=='/': kDir = kDir[:-1]
temp2 = "kk%s='%s'\n" %(temp1[0], kDir)
# for unset the variable
else:
temp2 = "unset kk%s\n" %(temp1[0])
lines1.append(temp2)
if lines1:
print('\nWill run the following shell variables:')
for line in lines1:
print(line, end=' ')
print('' )
else:
lines1 = 'echo "Nothing to be set"'
#--- write to a file to be executed
open(temp_k2File,'w').writelines(lines1)
sys.exit(0)
#=== Does myNum already exist?
i2 = None
for i in range(len(lines)):
temp1 = str.split(lines[i])
if temp1[0]==myNum:
if isDelete:
i2 = i
del lines[i2]
open(temp_kFile,'w').writelines(lines)
sys.exit(0)
i2 = i
break
if type(i2)==type(None):
lines.append('')
i2 = len(lines)-1
#=== the line in temp_k
if file0:
kLine = '%s %s\n' %(str.ljust(myNum,width1), mygetcwd() + '/' + file0)
else:
kLine = '%s %s\n' %(str.ljust(myNum,width1), mygetcwd() + '/')
print(('kName assigned: \n%s' %(kLine)))
lines[i2] = kLine
#=== backup temp_k
fn = temp_kFile + '.bak'
isBackup = 0
if not os.path.isfile(fn):
isBackup = 1
elif (os.path.getmtime(temp_kFile) - os.path.getmtime(fn)) > 24*3600:
isBackup = 1
if isBackup:
import shutil
shutil.copyfile(temp_kFile, fn)
#=== write temp_k
lines.sort()
open(temp_kFile,'w').writelines(lines)