summaryrefslogtreecommitdiff
path: root/sbhs_server/pages/management/commands/zip_client.py
blob: 89263bf27b8d7a5d7e679416712c7095aa2d31cc (plain)
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
from django.core.management.base import BaseCommand
import os, zipfile, shutil, distutils.core
from sbhs_server import settings

class Command(BaseCommand):
    args = ''
    help = 'ZIP all clients for all architectures.'

    def handle(self, *args, **options):
        def zipdir(path, zip, rootname):
            for root, dirs, files in os.walk(path):
                for file in files:
                    pathname = os.path.join(root, file)
                    zip.write(pathname, rootname + "/" + pathname.replace(path, ""))

        scilab_codes_dirname = "scilab_codes"
        scilab_codes_dirpath = os.path.join(settings.SBHSCLIENT_STATIC_DIR, scilab_codes_dirname)
        zipped_dirname = "zipped"
        zipped_dirpath = os.path.join(settings.SBHSCLIENT_STATIC_DIR, zipped_dirname)

        dirs = os.listdir(settings.SBHSCLIENT_STATIC_DIR)
        print dirs
        dirs.remove(scilab_codes_dirname)
        dirs.remove(zipped_dirname)
	print dirs
        dirs = [d for d in dirs if os.path.isdir(os.path.join(settings.SBHSCLIENT_STATIC_DIR, d))]
        print dirs
        dirs = [d for d in dirs if not d.startswith(".")]

        for dirname in dirs:
            print dirname
            dirpath = os.path.join(settings.SBHSCLIENT_STATIC_DIR, dirname)
            tmp_dirpath = os.path.join(zipped_dirpath, dirname)
            common_files_dirpath = os.path.join(dirpath, "common_files")
            run_file_dirpath = os.path.join(dirpath, "run_file")

            os.mkdir(tmp_dirpath)
            for f in os.listdir(dirpath):
                if os.path.isfile(os.path.join(dirpath, f)):            
                    shutil.copy(os.path.join(dirpath, f), os.path.join(tmp_dirpath, f))

            if dirname not in ["analysis", "local"]:
                distutils.dir_util.copy_tree(scilab_codes_dirpath, tmp_dirpath)
            else:
                distutils.dir_util.copy_tree(dirpath, tmp_dirpath)
            try:
                shutil.rmtree(tmp_dirpath + "/" + scilab_codes_dirname + "/.git")
            except:
                pass
    
            try:
                for root, dirs, files in os.walk(tmp_dirpath):
                    if os.path.exists(os.path.join(root, "scilabread.sce")):
                        distutils.dir_util.copy_tree(run_file_dirpath, root)
            except:
                pass
    
            try:
                distutils.dir_util.copy_tree(common_files_dirpath, os.path.join(tmp_dirpath, "common_files"))
            except:
                pass
    
            zipf = zipfile.ZipFile(os.path.join(zipped_dirpath, "sbhsclient/scilab_codes_" + dirname + ".zip"), 'w', zipfile.ZIP_DEFLATED)
            zipdir(tmp_dirpath, zipf, "scilab_codes_" + dirname)
            zipf.close()
            shutil.rmtree(tmp_dirpath)