-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathacdrecycle.py
executable file
·171 lines (147 loc) · 5.54 KB
/
acdrecycle.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
#
# Copyright (c) 2011 anatanokeitai.com(sakurai_youhei)
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# The Software shall be used for Younger than you, not Older.
#
"""
administrator@Tualatin ~/svn/pyacd $ ./acdrecycle.py --help
Usage: acdrecycle.py [Options] path1 path2 - ...('-' means STDIN)
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-e EMAIL, --email=EMAIL
email address for Amazon.com
-p PASSWORD, --password=PASSWORD
password for Amazon.com
-s FILE, --session=FILE
save or load login session to/from FILE
-v, --verbose show debug infomation
-q, --quiet quiet mode
This command move file(s) or dir(s) to Recycle of your Amazon Cloud Drive.
administrator@Tualatin ~/svn/pyacd $ ./acdrecycle.py -s ~/.session README.TXT
Logining to Amazon.com ... Done
Updating /home/administrator/.session ... Done
Moving /README.TXT to Recycle ... Done
"""
import sys
import os
from optparse import OptionParser
import pickle
pyacd_lib_dir=os.path.dirname(os.__file__)+os.sep+"pyacd"
if os.path.exists(pyacd_lib_dir) and os.path.isdir(pyacd_lib_dir):
sys.path.insert(0, pyacd_lib_dir)
import pyacd
parser=OptionParser(epilog="This command move file(s) or dir(s) to Recycle "+
"of your Amazon Cloud Drive. ",
usage="%prog [Options] path1 path2 - ...('-' means STDIN)",version="%prog 0.2")
parser.add_option("-e","--email",dest="email",action="store",default=None,
help="email address for Amazon.com")
parser.add_option("-p","--password",dest="password",action="store",default=None,
help="password for Amazon.com")
parser.add_option("-s","--session",dest="session",action="store",default=None,
metavar="FILE",help="save or load login session to/from FILE")
parser.add_option("-v","--verbose",dest="verbose",action="store_true",default=False,
help="show debug infomation")
parser.add_option("-q","--quiet",dest="quiet",action="store_true",default=False,
help="quiet mode")
def main():
opts,args=parser.parse_args(sys.argv[1:])
# Check options of authentication
if opts.session and os.path.exists(opts.session) and not os.path.isdir(opts.session):
pass
elif not opts.email or not opts.password:
sys.stderr.write("!! email and password are required !!\n")
parser.print_help()
sys.exit(2)
args=list(set(args))
if "-" in args:
args.remove("-")
args += [x.strip() for x in sys.stdin.readlines()]
if 0==len(args):
sys.stderr.write("!! no path selected !!\n")
parser.print_help()
sys.exit(2)
else:
pass
# Login to Amazon.com
session=None
try:
if not opts.quiet:
sys.stderr.write("Logining to Amazon.com ... ")
if opts.email and opts.password:
session=pyacd.login(opts.email,opts.password)
else:
fp=open(opts.session,"rb")
session=pickle.load(fp)
fp.close()
session=pyacd.login(session=session)
except:
pass
# Check login status
if not session:
sys.stderr.write("Unexpected error occured.\n")
sys.exit(2)
elif not session.is_valid():
sys.stderr.write("Session is invalid.\n%s\n"%session)
sys.exit(2)
elif not session.is_logined():
sys.stderr.write("Login failed.\n%s\n"%session)
sys.exit(2)
if not opts.quiet:
sys.stderr.write("Done\n")
if opts.session:
if not opts.quiet:
sys.stderr.write("Updating %s ... "%opts.session)
fp=open(opts.session,"wb")
fp.truncate()
pickle.dump(session,fp)
fp.close()
if not opts.quiet:
sys.stderr.write("Done\n")
for path in args:
if path[0]!='/':path='/'+path
if not opts.quiet:
sys.stderr.write("Moving %s to Recycle ... "%(path))
# get path
if opts.verbose:
sys.stderr.write("get ")
try:
pathobj = pyacd.api.get_info_by_path(path)
except pyacd.PyAmazonCloudDriveApiException,e:
sys.stderr.write("Aborted. ('%s')\n"%e.message)
continue
if opts.verbose:
sys.stderr.write("-> ")
if pathobj.Type!= pyacd.types.FILE and pathobj.Type!= pyacd.types.FOLDER :
sys.stderr.write("Aborted. ('%s<%s>' is special entity.)"%(path,pathobj.Type))
continue
# move
if opts.verbose:
sys.stderr.write("move ")
pyacd.api.recycle_bulk_by_id([pathobj.object_id,])
if opts.verbose:
sys.stderr.write("-> ")
if not opts.quiet:
sys.stderr.write("Done\n")
if __name__=="__main__":
main()