-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpycmd
More file actions
executable file
·76 lines (59 loc) · 1.92 KB
/
Copy pathpycmd
File metadata and controls
executable file
·76 lines (59 loc) · 1.92 KB
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
#!/usr/bin/env python
import sys
import os
from os.path import expanduser
import shutil
import pprint
home = expanduser("~")
args = sys.argv
def create():
file_name = args[2]
# ファイルを読み込んで
with open(file_name , mode="r") as f:
s = f.read()
# 登録するコマンド名を入力
cmd_name = input("New command name ->")
flag = os.path.exists(home+"/.pycmd-scripts/"+cmd_name) or shutil.which(cmd_name)
while flag:
cmd_name = input("Same command name exists!!\nNew command name ->")
flag = os.path.exists(home+"/.pycmd-scripts/"+cmd_name) or shutil.which(cmd_name)
# コマンドのメモ
cmd_des = input("Command description ->")
cmd_des = '# Command description : {} \n'.format(cmd_des.replace("\n"," "))
# 文字列結合
s = "#!/usr/bin/env python\n"+cmd_des+s
# ファイル書き込み
save_name = home+"/.pycmd-scripts/"+cmd_name
with open(save_name, mode='a') as f:
f.write(s)
os.chmod(save_name, 0o755)
def ls():
files = [filename for filename in os.listdir(home+"/.pycmd-scripts/") if not filename.startswith('.')]
l =[]
for i in files:
with open(home+"/.pycmd-scripts/"+i , mode="r") as f:
s = f.read()
description = s.split("\n")[1].split("# Command description : ")[1]
#print(i, description)
l.append({"Command name":i, "Description":description})
pprint.pprint(l)
def delete():
cmd_name = args[2]
files = os.listdir(home+"/.pycmd-scripts/")
if cmd_name in files:
os.remove(home+"/.pycmd-scripts/"+cmd_name)
else :
print("pycmd: command not found: {} ".format(cmd_name))
try:
second_args = args[1]
except:
print("pycmd: args not found")
sys.exit()
if second_args=="create":
create()
elif second_args=="ls":
ls()
elif second_args=="del":
delete()
else :
print("pycmd: option not found")