-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patht
executable file
·127 lines (113 loc) · 5.08 KB
/
t
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
#!/usr/bin/env python
from __future__ import print_function
# Config:
tinycmd_host = "tinycmd.alwaysdata.net" # The web server host
tinycmd_user = "" # user on tinycmd_host
import os, sys
import socket
import optparse
from getpass import getpass
if sys.version_info[0] == 2: import httplib
elif sys.version_info[0] == 3: from http import client as httplib
else:
print("Wrong version of Python")
sys.exit(1)
__prog__ = "tinycmd"
__version__ = "0.2"
__website__ = "http://tinycmd.alwaysdata.net/"
__description__ = "The command string shortening service"
__usage__ = "%prog [options] [command_string_id_list]"
if __name__ == "__main__":
opt = optparse.OptionParser(description=__description__, usage=__usage__,
version = __prog__ + " v" + __version__,
epilog="")
opt.add_option("-r", "--run", help="Run without questions",
dest="noquestions", action="store_true", default=False)
opt.add_option("-s", "--showonly", help="Only show command, don't run",
dest="showonly", action="store_true", default=False)
opt.add_option("-u", "--user", dest="user", default=tinycmd_user,
type="string", help="Run sript for this user of tinycmd")
opt.add_option("--host", "--server", dest="host", default=tinycmd_host,
type="string", help="adress of tinycmd-server",
metavar="SERVER")
opt.add_option("-a", "--addcmd", dest="addcmd", metavar="COMMAND",
help="Add this command to tinycmd-server", type="string")
opt.add_option("-f", "--addfile", dest="addfile", metavar="FILE",
help="Add this script to tinycmd-server", type="string")
opt.add_option("-l", "--listcmd", dest="listcmd", default=False,
action="store_true", help="List commands for current user")
opt.add_option("-n", "--nocache", dest="nocache", default=False,
action="store_true", help="Don't use cache")
(options, args) = opt.parse_args()
# add command to server
if options.addcmd or options.addfile:
if len(args) > 1:
opt.exit(1, "Wrong count of arguments")
user = options.user
pswd = getpass("User: " + user + "\nPassword: ") if user else ""
# =============================
# add-command-code will be here
# =============================
exit(0)
# list of command for user
if options.listcmd:
if not options.user:
opt.exit(1, "List of command may be showed only for concrete user")
user = "/" + options.user if options.user else ""
try:
conn = httplib.HTTPConnection(options.host)
conn.request("GET", user + "/list/text/")
except (httplib.HTTPResponse, socket.error) as ex:
opt.exit(1, "Unable to connect to the server:" + str(ex))
r1 = conn.getresponse()
if r1.status == 404:
print("Command string not found.")
elif r1.status == 500:
print("The server gives the 500 error. Please try again later.")
elif r1.status == 200:
print(r1.read())
conn.close()
exit(0)
if len(args) == 0:
print("Wrong count of arguments\n")
print(opt.get_usage())
print(opt.get_description(),"\n")
print(opt.format_option_help())
exit(1)
user = "/" + options.user if options.user else ""
try: conn = httplib.HTTPConnection(options.host)
except (httplib.HTTPResponse, socket.error) as ex:
opt.exit(1, "Unable to connect to the server:" + str(ex))
cachedir = os.path.join(os.path.expanduser("~"), ".tinycmd" + options.user + "/")
for arg in args:
data = None
cachefn = os.path.join(cachedir, arg)
if os.path.exists(cachefn) and not options.nocache:
data = open(cachefn).read()
else:
try: conn.request("GET", user + "/cs/" + arg + "/text/")
except (httplib.HTTPResponse, socket.error) as ex:
opt.exit(1, "Unable to connect to the server:" + str(ex))
r1 = conn.getresponse()
if r1.status == 404:
print("Command string not found.")
elif r1.status == 500:
print("The server gives the 500 error. Please try again later.")
elif r1.status == 200:
data = r1.read()
if not options.nocache:
if not os.path.exists(cachedir): os.makedirs(cachedir)
with open(cachefn, "w+") as cachefile: cachefile.write(data)
if data:
print("Command: ")
print(data)
if options.showonly:
exit(0)
else:
allow_run = True
if not options.noquestions:
answer = raw_input("You are really want run this command [y|n]? Answer: ")
allow_run = answer[0].lower() == 'y' if answer else False
if allow_run:
os.system(data)
conn.close()