2
from __future__ import with_statement
4
from random import random
8
from os.path import abspath
10
UTILS_FOLDER = '/home/'+os.environ['USER']+'/zanstaen-git/utils/'
12
def generate_random_string():
14
Simple random string of number generator.
16
return str(random())[-10:]
18
def tar_folder(folder='../client/skeleton/', tar_name='zanclient.tar.gz', mode='w:gz'):
20
tarObject = tarfile.open('../tmp/'+tar_name, mode)
21
tarObject.dereference = True
22
tarObject.add(folder, 'zanclient')
25
print 'Error creating tar file'
29
def generate_config(filename, user, site, passw):
30
config = ConfigParser.ConfigParser()
31
config.add_section('ZANSTAEN')
32
config.set('ZANSTAEN', 'user', user)
33
config.set('ZANSTAEN', 'site', site)
34
config.set('ZANSTAEN', 'pass', passw)
35
with open(filename, 'w') as f:
39
def write_archive(user, site, passw):
40
"""Creates a zanclient.tar.gz and returns a string with its abs address.
42
os.chdir(UTILS_FOLDER)
43
random_string = generate_random_string()
44
tmp_folder = '../tmp/'+random_string
46
shutil.copytree('../client/skeleton/', tmp_folder)
47
config_file = tmp_folder+'/config.zanclient'
48
if not generate_config(config_file, user, site, passw):
49
print 'Failure. Couldn\'t create config file'
51
archive_name = 'zanclient-'+user+'.tar.gz'
52
if tar_folder(folder=tmp_folder, tar_name=archive_name):
53
shutil.rmtree(tmp_folder)
54
return abspath(tmp_folder), archive_name
56
print 'Failure. Couldn\'t create tar file'
59
if __name__ == "__main__":
60
folder, archive = write_archive('test', 'test.inf', 'test')
61
print "Folder: %s" % folder
62
print "Archive: %s" % archive