1 # -*- coding: utf-8 -*-
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
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\
38 Input arguments = command line arguments which are not parsed as options.\
44 Upload files to avangard.data.cod.ru and add them to the base.\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\
54 Password for uploaded files.\n\
58 Description for uploaded files.\n\
63 Add accounts to the base.\n\
64 email:password [ [free_space, -1] [decription] ]\n\
69 List accounts (with info) which are in arguments list or all if arguments are empty list.\n\
74 List files (with info) which are in arguments list or all if arguments are empty list.\n\
79 Remove accounts which are in arguments list from the base.\n\
84 Remove files which are in arguments list from base and server.\n\
89 Append file lines as arguments.\n\
94 Force accounts check on start. Or if specified twice force full (accounts and files) check on start.\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\
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
):
153 def inside(where
, what
):
159 #default option values
160 default_accounts
= os
.environ
['HOME'] + '/.azeu/accounts.conf'
161 default_files
= os
.environ
['HOME'] + '/.azeu/files.conf'
164 ava
= avangard
.Avangard()
167 optlist
, optargs
= None, None
169 optlist
, arguments
= sgetopt
.sgetopt(sys
.argv
[1:], option_ideas
)
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()
181 except sgetopt
.SGetoptError
:
182 wlog('core', 'Wrong parameters.', 3)
185 if (rclass
== 'version'):
188 elif (rclass
== 'help'):
193 t_file
= open(default_accounts
, 'rb')
194 for line
in t_file
.readlines():
195 if (line
[-1:] == '\n'):
200 tmp
= line
.split(' ', 2)
201 ep
= tmp
[0].split(':')
202 email
, password
= ep
[0], ep
[1]
205 if ((len(tmp
) > 1) and (tmp
[1] != '')):
212 ava
.append(email
, password
, space
, description
)
215 t_file
= open(default_files
, 'rb')
216 for line
in t_file
.readlines():
217 if (line
[-1:] == '\n'):
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() ):
236 description
= 'uploaded with azeu'
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'):
251 arguments
.append(line
)
253 elif ( name
== 'force'):
255 elif ( name
== 'password' ):
257 elif ( name
== 'description' ):
259 elif ( name
== 'strategy' ):
260 strategy
= int(value
)
261 if ( (strategy
> 1) or (strategy
< 0) ):
266 for acc
in ava
.accounts
:
267 if ((acc
.space
== -1) or (force
> 1)):
274 if (rclass
== 'add-files'):
275 wlog('core', 'add-files mode.')
278 for path
in arguments
:
283 res
= ava
.upload(path
, password
, description
, strategy
)
284 except KeyboardInterrupt:
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]))
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]
301 if ((len(tmp
) > 1) and (tmp
[1] != '')):
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
) ):
317 elif (rclass
== 'list-files'):
318 wlog('core', 'list-files mode.')
319 for acc
in ava
.accounts
:
321 for one
in acc
.files
:
322 if ( (len(arguments
) == 0) or (str(one
) in arguments
) ):
323 if (acc_printed
== 0):
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.')
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
:
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
:
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
:
366 print >> t_bfile
, str(acc
.email
) + ':' + str(f
.aid
) + ':' + str(f
.name
) + ':' +\
368 str(f
.date
) + ':' + str(f
.counter
) + ':' + str(f
.description
)
372 wlog('core', 'All done.')