-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.py
147 lines (121 loc) · 3.64 KB
/
shell.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import cmd, sys
import subprocess
import os
import ShieldBackend
import Authenticate
class Shieldshell(cmd.Cmd):
intro = 'Welcome to the Shield. Type help or ? to list commands.\n'
prompt = '(Shield>) '
efsbackend = None
loggedIn = False
dbxclient = None
def __init__(self):
self.authobj = Authenticate.DropBoxSDK();
self.dbxclient = self.authobj.authorize()
#super(Shieldhell, self).__init__()
cmd.Cmd.__init__(self)
def do_signup(self, arg):
'Sign-up! signup <username>'
self.username = arg
self.userdir= 'users/' + arg + '/'
if not os.path.exists(self.userdir):
os.mkdir(self.userdir)
self.loggedIn = True
self.efsbackend = ShieldBackend.ShieldBackend()
self.efsbackend.setclient(self.dbxclient)
self.efsbackend.register(arg)
def do_login(self, arg):
'Login! login <username>'
self.username = arg
self.efsbackend = ShieldBackend.ShieldBackend()
self.efsbackend.setclient(self.dbxclient)
issucc = self.efsbackend.login(arg)
if not issucc:
print "Login Failed"
else:
self.userdir= 'users/' + arg + '/'
if not os.path.exists(self.userdir):
os.mkdir(self.userdir)
self.loggedIn = True
def checklogin(self):
if self.loggedIn:
return True
print 'Must login or register'
return False
def do_create(self, arg):
'Create a file: create <filename>'
if not self.checklogin():
return
if not self.efsbackend.check_perm():
return
if not self.efsbackend.check_pathperm(arg):
return
'''check path and create directory accordingly'''
workdir = self.userdir + "/" + arg
basepath = os.path.split(workdir)[0]
if not os.path.exists(basepath):
os.makedirs(basepath)
openFileCommand = ['/usr/bin/vi', arg]
try:
process = subprocess.Popen(openFileCommand, stdout=subprocess.PIPE, cwd=self.userdir)
output = process.communicate()[0]
except:
print "Error opening file."
print 'Preparing to upload ...'
self.efsbackend.create(self.userdir + "/" + arg, arg)
return
def do_mkdir(self, arg):
'Create a file: create <dirname>'
if not self.checklogin():
return
if not self.efsbackend.check_perm():
return
self.efsbackend.mkdir(self.userdir + "/" + arg, arg)
return
def do_open(self, arg):
'Open a file: open <filename>'
if not self.checklogin():
return
self.efsbackend.open(arg, self.userdir + "/" + arg)
def do_rm(self, arg):
'delete a file: rm <relpath-filename>'
if not self.checklogin():
return
self.efsbackend.rm(arg)
def do_rmdir(self, arg):
'delete a file: rm <relpath-dirname>'
if not self.checklogin():
return
self.efsbackend.rmdir(arg)
def do_chmod(self, line):
'Share files: chmod <file/dir> <user>'
if not self.checklogin():
return
l = line.split()
fname = l[0]
user = l[1]
self.efsbackend.share(fname, user)
def do_cd(self, arg):
'Change directory. Use .. to get to parent directory: cd <dir>'
if not self.checklogin():
return
self.efsbackend.cd(arg)
def do_ls(self, arg):
'List files and directories in current directory: ls'
if not self.checklogin():
return
self.efsbackend.ls()
def do_pwd(self, arg):
'Print current directory: pwd'
if not self.checklogin():
return
self.efsbackend.pwd()
def do_bye(self, arg):
'exit: BYE'
return True
def parse(arg):
'Convert a series of zero or more numbers to an argument tuple'
return tuple(map(int, arg.split()))
if __name__ == '__main__':
efs = Shieldshell()
efs.cmdloop()