first commit
[archive.git] / OXIj / azeu / src / core.py
blob78ead316a63a0c734c35b89b478473b6b8d23c15
1 # -*- coding: utf-8 -*-
2 ###
3 #This file is part of azeu project
5 #azeu is automatic upload manager
6 #Copyright (C) 2007 Jan Trofimov (OXIj)
8 #This program is free software; you can redistribute it and/or
9 #modify it under the terms of the GNU General Public License
10 #as published by the Free Software Foundation; either version 2
11 #of the License, or (at your option) any later version.
13 #This program is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU General Public License for more details.
18 #You should have received a copy of the GNU General Public License
19 #along with this program; if not, write to the Free Software
20 #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #You can contact author by email jan.trof@gmail.com
23 ###
25 import sys, os, time
26 import sgetopt
28 from utils import *
29 import avangard
31 version_num = '0.3pre5'
32 version_string='azeu uploader ' + version_num + '\n\
33 Copyright (C) 2007 Jan Trofimov (OXIj)\n\
34 This is free software; see the source for copying conditions. There is NO\n\
35 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n'
36 help_string= version_string + '\n\
37 \n\
38 Input arguments = command line arguments which are not parsed as options.\
39 \n\
40 Options:\n\
41 \n\
42 -af\n\
43 --add-files\n\
44 Upload files to avangard.data.cod.ru and add them to the base.\n\
45 \n\
46 -s\n\
47 --strategy\n\
48 Uploading strategy.\n\
49 0) Less space unused on accounts. Slower while updationg accounts.\n\
50 1) More space unused on accounts. Faster while updationg accounts.\n\
51 \n\
52 -p\n\
53 --password\n\
54 Password for uploaded files.\n\
55 \n\
56 -d\n\
57 --description\n\
58 Description for uploaded files.\n\
59 \n\
60 \n\
61 -aa\n\
62 --add-accounts\n\
63 Add accounts to the base.\n\
64 email:password [ [free_space, -1] [decription] ]\n\
65 \n\
66 \n\
67 -la\n\
68 --list-accounts\n\
69 List accounts (with info) which are in arguments list or all if arguments are empty list.\n\
70 \n\
71 \n\
72 -lf\n\
73 --list-files\n\
74 List files (with info) which are in arguments list or all if arguments are empty list.\n\
75 \n\
76 \n\
77 -ra\n\
78 --remove-accounts\n\
79 Remove accounts which are in arguments list from the base.\n\
80 \n\
81 \n\
82 -rf\n\
83 --remove-files\n\
84 Remove files which are in arguments list from base and server.\n\
85 \n\
86 \n\
87 -i\n\
88 --input\n\
89 Append file lines as arguments.\n\
90 \n\
91 \n\
92 -f\n\
93 --force\n\
94 Force accounts check on start. Or if specified twice force full (accounts and files) check on start.\n\
95 \n\
96 \n\
97 -V\n\
98 --version\n\
99 Show azeu version.\n\
102 -h\n\
103 --help\n\
104 Show this help.\n\
107 Typical usage:\n\
108 Upload some files:\n\
109 azeu -af /some/file1 /some/file2\n\
110 Upload some files from lists:\n\
111 azeu -af -i file_with_list_of_some_files1 -i file_with_list_of_some_files2\n\
112 Remove file with id 123456789:\n\
113 azeu -rf 123456789\n\
114 Remove files with ids from files:\n\
115 azeu -rf -i file_with_list_of_some_file_ids1 -i file_with_list_of_some_file_ids2\n\
116 Just force check without uploading something:\n\
117 azeu -af -ff\n\
120 option_ideas = [
121 ['add-files', 'af', 0, ['add-files']],
122 ['strategy', 's', 1, ['add-files']],
123 ['password', 'p', 1, ['add-files']],
124 ['description', 'd', 1, ['add-files']],
125 ['add-accounts', 'aa', 0, ['add-accounts']],
127 ['list-accounts', 'la', 0, ['list-accounts']],
128 ['list-files', 'lf', 0, ['list-files']],
130 ['remove-accounts', 'ra', 0, ['remove-accounts']],
131 ['remove-files', 'rf', 0, ['remove-files']],
133 ['input', 'i', 1, ['add-files', 'add-accounts',\
134 'list-accounts', 'list-files',\
135 'remove-accounts', 'remove-files']],
137 ['force', 'f', 0, ['add-files', 'add-accounts',\
138 'list-accounts', 'list-files',\
139 'remove-accounts', 'remove-files']],
140 ['help', 'h', 0, ['help']],
141 ['version', 'V', 0, ['version']],
144 def cross (array1, array2):
145 result = []
146 for one in array1:
147 for two in array2:
148 if (one == two):
149 result.append(one)
151 return result
153 def inside(where, what):
154 for one in where:
155 if (one[0] == what):
156 return 1
157 return 0
159 #default option values
160 default_accounts= os.environ['HOME'] + '/.azeu/accounts.conf'
161 default_files= os.environ['HOME'] + '/.azeu/files.conf'
163 #main 'plugin'
164 ava = avangard.Avangard()
166 #option parceing
167 optlist, optargs = None, None
168 try:
169 optlist, arguments = sgetopt.sgetopt(sys.argv[1:], option_ideas )
171 rclass = []
172 if (len(optlist) > 0):
173 rclass = optlist[0][0][3]
174 for one in optlist[1:]:
175 rclass = cross(rclass, one[0][3])
177 if (len(rclass) != 1):
178 raise sgetopt.SGetoptError()
180 rclass = rclass[0]
181 except sgetopt.SGetoptError:
182 wlog('core', 'Wrong parameters.', 3)
183 sys.exit(1)
185 if (rclass == 'version'):
186 print version_string
187 sys.exit(0)
188 elif (rclass == 'help'):
189 print help_string
190 sys.exit(0)
192 #config reading
193 t_file = open(default_accounts, 'rb')
194 for line in t_file.readlines():
195 if (line[-1:] == '\n'):
196 line = line [:-1]
197 if ( line == '' ):
198 continue
200 tmp = line.split(' ', 2)
201 ep = tmp[0].split(':')
202 email, password = ep[0], ep[1]
204 space = -1
205 if ((len(tmp) > 1) and (tmp[1] != '')):
206 space = int(tmp[1])
208 description = ''
209 if (len(tmp) > 2):
210 description = tmp[2]
212 ava.append(email, password, space, description)
213 t_file.close()
215 t_file = open(default_files, 'rb')
216 for line in t_file.readlines():
217 if (line[-1:] == '\n'):
218 line = line [:-1]
219 if ( line == '' ):
220 continue
222 email, aid, name, size, date, counter, description = line.split(':', 6)
224 for acc in ava.accounts:
225 if (acc.email == email):
226 acc.append(int(aid), name, int(size), float(date), int(counter), description)
228 if ( float(date) <= time.time() ):
229 acc.space = -1
231 break
232 t_file.close()
234 #prepareing values
235 password = ''
236 description = 'uploaded with azeu'
237 strategy = 0
239 force = 0
241 for opt in optlist:
242 name, value = opt[0][0], opt[1]
244 if ( name == 'input'):
245 t_file = open(value, 'rb')
246 for line in t_file.readlines():
247 if (line[-1:] == '\n'):
248 line = line [:-1]
249 if (line == ''):
250 continue
251 arguments.append(line)
252 t_file.close()
253 elif ( name == 'force'):
254 force += 1
255 elif ( name == 'password' ):
256 password = value
257 elif ( name == 'description' ):
258 description = value
259 elif ( name == 'strategy' ):
260 strategy = int(value)
261 if ( (strategy > 1) or (strategy < 0) ):
262 strategy = 0
264 #updating
265 if (force):
266 for acc in ava.accounts:
267 if ((acc.space == -1) or (force > 1)):
268 need = 0
269 if (force > 2):
270 need = 1
271 acc.update(need)
273 #doing things
274 if (rclass == 'add-files'):
275 wlog('core', 'add-files mode.')
277 cancelled = 0
278 for path in arguments:
279 res = None
281 if (cancelled == 0):
282 try:
283 res = ava.upload(path, password, description, strategy)
284 except KeyboardInterrupt:
285 cancelled = 1
287 if (res != None):
288 wlog('core', 'File ' + str(res[0]) + ' ("' + str(res[1]) + '") uploaded to server and added to the base.')
289 wout('success ' + str(res[0]) + ' ' + str(res[2]) + ' ' + str(res[1]))
290 else:
291 wout('fail ' + path)
293 elif (rclass == 'add-accounts'):
294 wlog('core', 'add-accounts mode.')
295 for account in arguments:
296 tmp = account.split(' ', 2)
297 ep = tmp[0].split(':')
298 email, password = ep[0], ep[1]
300 space = -1
301 if ((len(tmp) > 1) and (tmp[1] != '')):
302 space = int(tmp[1])
304 description = ''
305 if (len(tmp) > 2):
306 description = tmp[2]
308 wlog('core', 'Account "' + str(email) + '" with password "' + str(password) + '" added to the base.')
309 ava.append(email, password, space, description)
311 elif (rclass == 'list-accounts'):
312 wlog('core', 'list-accounts mode.')
313 for acc in ava.accounts:
314 if ( (len(arguments) == 0) or (str(acc.email) in arguments) ):
315 wout(str(acc))
317 elif (rclass == 'list-files'):
318 wlog('core', 'list-files mode.')
319 for acc in ava.accounts:
320 acc_printed = 0
321 for one in acc.files:
322 if ( (len(arguments) == 0) or (str(one) in arguments) ):
323 if (acc_printed == 0):
324 wout(str(acc))
325 acc_printed = 1
326 wout(str(acc.files[one]))
328 elif (rclass == 'remove-accounts'):
329 wlog('core', 'remove-accounts mode.')
330 for acc in ava.accounts[:]:
331 if (str(acc.email) in arguments):
332 wlog('core', 'Account "' + str(acc.email) + '" removed from the base.')
333 ava.accounts.remove(acc)
335 elif (rclass == 'remove-files'):
336 wlog('core', 'remove-files mode.')
338 acc_to_update = []
339 for acc in ava.accounts:
340 for one in acc.files:
341 if ( str(one) in arguments ):
342 if (acc not in acc_to_update):
343 acc_to_update.append(acc)
345 for acc in acc_to_update:
346 acc.update()
348 for acc in ava.accounts:
349 for one in acc.files:
350 if ( str(one) in arguments ):
351 wlog('core', 'File ' + str(acc.files[one].aid) + ' ("' + str(acc.files[one].name) + '") removed from base and server.')
352 acc.files[one].delete()
354 sleep(avangard.timeout)
356 for acc in acc_to_update:
357 acc.update()
359 t_afile = open(default_accounts, 'wb')
360 t_bfile = open(default_files, 'wb')
361 for acc in ava.accounts:
362 print >> t_afile, str(acc.email) + ':' + str(acc.password) + ' ' + str(acc.space) + ' ' + str(acc.description)
363 for one in acc.files:
364 f = acc.files[one]
365 if (not f.deleted):
366 print >> t_bfile, str(acc.email) + ':' + str(f.aid) + ':' + str(f.name) + ':' +\
367 str(f.size) + ':' +\
368 str(f.date) + ':' + str(f.counter) + ':' + str(f.description)
369 t_bfile.close()
370 t_afile.close()
372 wlog('core', 'All done.')