src/fablib/core.py
author ymh <ymh.work@gmail.com>
Fri, 02 Sep 2016 02:01:44 +0200
changeset 29 690b89879211
parent 28 19d1e055ff4e
child 32 800b7b8aa439
child 33 40a2d27364b2
permissions -rw-r--r--
small adaptation for iconolab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Feb 20, 2013
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from fabric.api import run, local, env, cd, put, prefix, sudo, lcd
1
35eb0cbadae0 update fablib
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     8
from fabric.colors import green
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from fabric.context_managers import settings
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from fabric.contrib.files import exists, upload_template
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from fabric.contrib.project import rsync_project
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
from fabric.tasks import Task
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import imp
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
import os.path
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
import re
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
import shutil
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    17
import sys
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
import urlparse
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
23
bb36afd9ad31 correct export
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    21
__all__ = ["check_folder_access", "migrate", "collectstatic", "do_relaunch_server",
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    22
           "export_version", "do_sync_web", "create_config", "clean_export_folder",
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    23
           "sync_install_build", "do_create_virtualenv", "clean_rsync_folder", "rsync_export",
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    24
           "do_sync_comp", "get_comp_versions_dict", "SyncComp", "get_src_version", "sync_build",
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    25
           "install_build", "do_create_virtualenv_requirement", "build_src"]
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
def get_export_path(version):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/")
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    return os.path.expanduser(base_path) + "_%s" % (str(version))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
def clean_export_folder(path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    print("Removing %s" % path)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    if os.path.isdir(path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        shutil.rmtree(path, ignore_errors=True)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
def do_export_version(path, **export_keys):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    print("Export version %s : %s " % (path,repr(export_keys)))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    for export_key, version in export_keys.items():
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        export_path = os.path.join(path,export_key)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    41
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    42
        repo_url = env.repos[export_key]['repo']
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        url_part = urlparse.urlparse(repo_url)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        if url_part.scheme or url_part.netloc:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            # this is a remote repo. Let's clone first
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            clone_path = os.path.join(path,'clone',export_keys)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            os.makedirs(clone_path)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    48
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    49
            scm = "hg"
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    50
            with settings(warn_only=True):
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    51
                output = local('git ls-remote \"%s\"' % repo_url)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    52
                scm = "git" if output.failed else "hg"
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    53
            if scm == "hg":
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    54
                output = local("hg clone \"%s\" \"%s\"" % (repo_url,clone_path))
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    55
            else:
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    56
                local("git clone \"%s\" \"%s\"" % (repo_url,clone_path))
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        else:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            clone_path = repo_url
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    59
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        with lcd(clone_path):
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    61
            # detetct .git or .hg subfolder
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    62
            if os.path.exists(os.path.join(clone_path,".git")):
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    63
                os.makedirs(export_path)
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    64
                cmd_str = "git archive \'%s\' | tar -x -C \"%s\""
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    65
            else:
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    66
                cmd_str = "hg archive -r \'%s\' \"%s\""
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    67
            local(cmd_str % (str(version),export_path))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    68
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    print("Export version %s done"%repr(export_keys))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    71
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    72
def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key, settings_module=''):
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    73
    if not settings_module:
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    74
        settings_module = '%s.%s' % (platform_web_module, 'settings')
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
1
35eb0cbadae0 update fablib
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
        tempfilepath = run("mktemp -t fablib.XXXXXX")
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        with settings(warn_only=True):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            run("echo \"import os\" > %s" % (tempfilepath))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
            map(lambda str_echo: run("echo \"%s\" >> %s" % (str_echo, tempfilepath)),
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    81
                ["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s')" % (settings_module),
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                 "from django.conf import settings",
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    83
                 "print(settings.%s)" % (settings_key)])
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            res = run("python < %s" % (tempfilepath))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        run("rm -f \"%s\"" % (tempfilepath))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    return res
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    87
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    88
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    89
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
def rsync_export(path, remotepath, filters):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
    print("Rsync %s to %s",(path,remotepath))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    92
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
    filter_option_str = "--progress --stats"
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
    if filters:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        filter_option_str += " " + " ".join(["--filter \"%s\"" % (f) for f in filters])
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    96
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
    run("mkdir -p \"%s\"" % remotepath)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
    rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
    print("Rsync %s to %s done",(path,remotepath))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   100
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
def clean_rsync_folder(remotepath):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    print("clean rsync folder %s" % remotepath)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
    run("rm -fr \"%s\"" % remotepath)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   104
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
def build_src(path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
    print("Build source dist at %s" % path)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    f = None
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   108
    sys.path.append(path)
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   109
    current_path = os.getcwdu()
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   110
    try:
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   111
        os.chdir(path)
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   112
        try:
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   113
            f, pathname, description = imp.find_module("setup", [path])
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   114
            print("Build source dist at %s : found setup" % path)
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   115
            setup_mod = imp.load_module("setup", f, pathname, description)
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   116
            print("Build source dist at %s : setup loaded" % path)
21
57bbeb60c60b add exception trace
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   117
        except:
57bbeb60c60b add exception trace
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   118
            e = sys.exc_info()[0]
57bbeb60c60b add exception trace
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   119
            print("Error when building %s : %s" % (path, str(e)))
57bbeb60c60b add exception trace
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   120
            raise
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   121
        finally:
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   122
            if f:
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   123
                f.close()
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   124
21
57bbeb60c60b add exception trace
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   125
        setup_mod.launch_setup("setup.py", ['sdist'])
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
    finally:
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   127
        os.chdir(current_path)
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
    print("Build source dist at %s done" % path)
14
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   129
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   130
def get_version(version):
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   131
    version_str = '%s.%s' % (version[0], version[1])
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   132
    if version[2]:
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   133
        version_str = '%s.%s' % (version_str, version[2])
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   134
    if version[3:] == ('alpha', 0):
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   135
        version_str = '%s pre-alpha' % version_str
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   136
    else:
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   137
        if version[3] != 'final':
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   138
            version_str = '%s %s %s' % (version_str, version[3], version[4])
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   139
    return version_str
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   140
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   142
def get_src_version(key, path):
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   143
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   144
    print("get src version for %s at %s" % (key,path))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   145
13
e07bdc4b7cf5 small correction on fablib
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   146
    mod_name = env.repos[key].get('module', key) or key
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   147
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
    f = None
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   149
    sys.path.append(path)
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   150
    current_path = os.getcwdu()
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   151
    os.chdir(path)
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
    try:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
        f, pathname, description = imp.find_module(mod_name, [path])
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   154
        src_mod = imp.load_module(mod_name, f, pathname, description)
14
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   155
    except:
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   156
        src_mod = None
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   157
        print("Could not import module, trying to parse")
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
    finally:
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   159
        os.chdir(current_path)
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
        if f:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
            f.close()
14
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   162
    version = None
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   163
    if src_mod is None:
15
7cabb2f82044 Correct path for __init__.py file
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
   164
        with open(os.path.join(path,mod_name,"__init__.py"),'r') as init_file:
14
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   165
            for line in init_file:
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   166
                m = re.search('VERSION\s+=\s+\((.+)\)', line, re.I)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   167
                if m:
14
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   168
                    version = tuple([re.sub('[\s\"\']','', item) for item in m.group(1).split(',')])
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   169
                    break
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   170
    elif hasattr(src_mod, "VERSION"):
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   171
        version = src_mod.VERSION
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   172
    elif hasattr(src_mod, "__version__"):
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   173
        version = src_mod.__version__
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   174
15
7cabb2f82044 Correct path for __init__.py file
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
   175
    print("VERSION : %s" % repr(version))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   176
14
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   177
    if version is None:
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   178
        version = ""
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   179
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   180
    if not isinstance(version, basestring):
14
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   181
        if src_mod and hasattr(src_mod, "get_version"):
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   182
            version_str = src_mod.get_version()
14
118595f0bcec try to get version by parsing file
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   183
        elif isinstance(version, tuple):
16
c0bf35ee1aed correct get version to use integer instead of str
ymh <ymh.work@gmail.com>
parents: 15
diff changeset
   184
            #convert num
c0bf35ee1aed correct get version to use integer instead of str
ymh <ymh.work@gmail.com>
parents: 15
diff changeset
   185
            version_str = get_version([int(s) if s.isdigit() else s for s in version])
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   186
        else:
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   187
            version_str = str(version)
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   188
    else:
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   189
        version_str = version
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   190
15
7cabb2f82044 Correct path for __init__.py file
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
   191
    print("VERSION str : %s" % repr(version_str))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   192
    return (version, version_str)
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   193
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
def sync_build(path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
    print("Sync build %s" % path)
2
bc05135e8050 correct remote path name
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   197
    with cd(env.remote_path['build_export']):
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
        filename = os.path.basename(path)
2
bc05135e8050 correct remote path name
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   199
        res_trans = put(path, os.path.join(env.remote_path['build_export'], filename))
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
        print("Sync build %s to %s" % (path,repr(res_trans)))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
        return res_trans
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
def remove_build(path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
    print("remove build build %s" % path)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
    run("rm \"%s\"" % path)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   206
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
def install_build(remotepath, remotevirtualenvpath, module_to_uninstall= None):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
    print("Install build %s in %s" % (remotepath, remotevirtualenvpath))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   211
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
    with prefix("source %s" % activate_path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
        if module_to_uninstall:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
            with settings(warn_only=True):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
                run("pip uninstall -y %s" % module_to_uninstall)
28
19d1e055ff4e remove cache use for pip
ymh <ymh.work@gmail.com>
parents: 27
diff changeset
   216
        run("pip install --no-cache-dir \"%s\"" % remotepath)
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   218
def collectstatic(remotepath, remotevirtualenvpath, platform_web_module, module_settings="", admin_cmd="python manage.py"):
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
    print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   220
    remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT", env.settings)
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
    with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
21
57bbeb60c60b add exception trace
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   223
        #remove old files optio -c of collect static fail !
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   224
        run("rm -fr \"%s\"/*" % (remotestaticsitepath))
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   225
        run("%s collectstatic --noinput %s" % (admin_cmd, "--settings="+module_settings if module_settings else ""))
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   227
def migrate(remotepath, remotevirtualenvpath, module_settings="", admin_cmd="python manage.py"):
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   228
    {
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   229
        'syncdb': do_syncdb,
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   230
        'migrate': do_migrate,
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   231
    }.get(getattr(env, 'migrate_method', 'migrate'), do_syncdb)(remotepath, remotevirtualenvpath, module_settings, admin_cmd)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   232
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   233
def do_syncdb(remotepath, remotevirtualenvpath, module_settings="", admin_cmd="python manage.py"):
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
    with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   236
        run("%s syncdb --migrate --noinput %s" % (admin_cmd, "--settings="+module_settings if module_settings else ""))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   237
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   238
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   239
def do_migrate(remotepath, remotevirtualenvpath, module_settings="", admin_cmd="python manage.py"):
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   240
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   241
    with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   242
        run("%s migrate --noinput %s" % (admin_cmd, "--settings="+module_settings if module_settings else ""))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   243
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   244
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   245
def create_config(export_path):
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
    print("Create config from %s" % (export_path,))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
    remotepath = env.remote_path['src']
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
    remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
    template_path = os.path.join(export_path, "src", env.platform_web_module, "config.py.tmpl")
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   250
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
    context = {
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
        'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/",
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
        'asctime': '%(asctime)s',
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
        'levelname': '%(levelname)s',
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
        'message': '%(message)s',
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
        'module': '%(module)s',
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
    }
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
    context.update(env.config['web'])
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   259
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
    if not exists(remote_config_path, verbose=True):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
        upload_template(template_path, remote_config_path, context=context)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
def export_version(**kwargs):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
    print("export version %s" % (repr(kwargs)))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   265
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
    export_path = kwargs.get('path', None)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   267
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   268
    if not export_path:
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
        export_path = get_export_path("_".join(["%s_%s" % (k,v) for k,v in kwargs.items()]))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   270
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
    clean_export_folder(export_path)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   272
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
    do_export_version(export_path,**kwargs)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   274
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
    return export_path
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
    print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
    if "remote_baseline_venv" in env and env.remote_baseline_venv:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
        prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate")
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
    else:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
        prefix_str = "echo"
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
    with settings(warn_only=True):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
        run("rm -fr \"%s\"" % remotevirtualenvpath)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
    run("mkdir -p \"%s\"" % remotevirtualenvpath)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
    with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        run("python create_python_env.py")
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
        run("python project-boot.py \"%s\"" % remotevirtualenvpath)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
    with prefix("source \"%s\"" % activate_path):
28
19d1e055ff4e remove cache use for pip
ymh <ymh.work@gmail.com>
parents: 27
diff changeset
   291
        run("pip install --no-cache-dir -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt"))
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   293
def do_create_virtualenv_requirement(remote_venv_requirement_path, remotevirtualenvpath, python_version = "2"):
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   294
    print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_requirement_path, remotevirtualenvpath))
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   295
    with settings(warn_only=True):
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   296
        run("rm -fr \"%s\"" % remotevirtualenvpath)
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   297
    run("mkdir -p \"%s\"" % remotevirtualenvpath)
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   298
    run("virtualenv -p `which python%s` %s" % (python_version, remotevirtualenvpath))
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   299
    with prefix("source \"%s\"" % os.path.join(remotevirtualenvpath, "bin/activate")):
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   300
        run("pip install -r \"%s\"" % remote_venv_requirement_path)
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   301
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   302
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   303
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
def do_sync_comp(key, export_path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
    print("do_sync_comp with  path %s" % (export_path))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   306
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   307
    src_path = os.path.join(export_path,env.repos[key]['src_root'])
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
    # find setup.py
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
    for root, _, files in os.walk(src_path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
        if "setup.py" in files:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
            src_path = root
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   312
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
    build_src(src_path)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
    (_,version_str) = get_src_version(key, src_path)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
    build_path = os.path.join(src_path,"dist","%s-%s.tar.gz" % (key,version_str))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
    sync_install_build(build_path, key)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   317
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
def sync_install_build(build_path, module_to_uninstall=None):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
    res_trans = None
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
    try:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
        res_trans = sync_build(build_path)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   323
        install_build(res_trans[0], env.remote_path['virtualenv'], module_to_uninstall)
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
    finally:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
        if res_trans:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
            remove_build(res_trans[0])
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
def do_sync_web(version, export_path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
    print("do_sync_web with version %s and path %s" % (version,export_path))
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
    #sync src
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   332
    src_path = os.path.join(export_path,"src/")
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
    rsync_export(src_path, env.remote_path['src'], env.rsync_filters['src'])
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   334
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   335
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
def check_folder_access():
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
    print("Check folder access")
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
    # get remote user
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
    for folder_path in env.folders:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
        if not os.path.isabs(folder_path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
            folder_path = env.remote_path['web'].rstrip("/")+ "/" + folder_path
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
            with settings(warn_only=True):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
                if not exists(folder_path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
                    run("mkdir -p \"%s\"" % folder_path)
1
35eb0cbadae0 update fablib
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
                run("chown -R :%s \"%s\"" % (env.web_group, folder_path))
35eb0cbadae0 update fablib
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
                run("chmod -R g+w \"%s\"" % folder_path)
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
def get_comp_versions_dict(export_path_web):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
    comp_versions = {}
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
    requirement_file_path = os.path.join(export_path_web, 'src', 'requirement.txt')
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
    if not os.path.exists(requirement_file_path):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
        return comp_versions
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
    with open(requirement_file_path) as f:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
        for line in f:
9
525a37ed8c18 various correction to add chuncked-uploads to platform
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   355
            m = re.match('^([\w-]+)\s+\(\s*\=\=\s*([\.\d\w]+)\s*\)', line)
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
            if m:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
                key, version_req = m.groups()
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
                if "." in version_req:
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
                    version_req = "V" + ".".join(["%02d" % (int(s)) if s.isdigit() else s for s in version_req.split(".")])
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
                comp_versions[key] = version_req
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   361
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
    return comp_versions
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   364
def do_relaunch_server(do_collectstatic, do_migrate, do_check_folder_access):
18
bc8e23448f4d upgrade fablib
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   365
    if do_check_folder_access:
bc8e23448f4d upgrade fablib
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   366
        check_folder_access()
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   367
    if do_migrate:
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   368
        migrate(env.remote_path['src'], env.remote_path['virtualenv'], env.get('settings', ''), env.get('admin_cmd', 'python manage.py'))
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
    if do_collectstatic:
29
690b89879211 small adaptation for iconolab
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   370
        collectstatic(env.remote_path['src'], env.remote_path['virtualenv'], env.platform_web_module, env.get('settings', ''), env.get('admin_cmd', 'python manage.py'))
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
    sudo(env.web_relaunch_cmd, shell=False)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
class SyncComp(Task):
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   375
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
    def __init__(self, key):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
        self.key = key
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
    def __get_name(self):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
        return "sync_" + self.key
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   381
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
    name = property(__get_name)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   383
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
    def run(self, version):
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
        print(green("sync %s with version %s" % (self.key, version)))
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   386
        export_path_web = export_version(web=version)
0
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
        export_path_web_full = os.path.join(export_path_web,'web')
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
        comp_versions = get_comp_versions_dict(export_path_web_full)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
        export_path = export_version(**{self.key:comp_versions[self.key]})
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
        export_path_full = os.path.join(export_path,self.key)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
        do_sync_comp(self.key, export_path_full)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
        clean_export_folder(export_path)
3820e8d1316d first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
        clean_export_folder(export_path_web)
22
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   395
89114c0a6ad7 add migrate option for django 1.7
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   396
        do_relaunch_server(do_collectstatic=True, do_migrate=True, do_check_folder_access=True)