sbin/sync/core.py
author ymh <ymh.work@gmail.com>
Fri, 05 Jul 2019 15:58:28 +0200
changeset 37 7e614ca2431a
parent 12 8895d41be7e2
permissions -rw-r--r--
remove experiment message on home page, and define new version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Feb 20, 2013
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
# from fabric.api import run, local, env, cd, put, prefix, sudo, lcd
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
# from fabric.colors import green
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
# from fabric.context_managers import settings
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
# from fabric.contrib.files import exists, upload_template
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
# from fabric.contrib.project import rsync_project
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
# from fabric.tasks import Task
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
from fabric import Connection
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
import imp
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
import os.path
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
import re
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
import shutil
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
import sys
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
import urllib.parse
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
import requirements
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
# __all__ = ["check_folder_access", "migrate", "collectstatic", "do_relaunch_server",
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
#            "export_version", "do_sync_web", "create_config", "clean_export_folder",
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
#            "sync_install_build", "do_create_virtualenv", "clean_rsync_folder", "rsync_export",
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
#            "do_sync_comp", "get_comp_versions_dict", "SyncComp", "get_src_version", "sync_build",
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
#            "install_build", "do_create_virtualenv_requirement", "build_src"]
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
def get_export_path(env, version):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/")
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    return os.path.expanduser(base_path) + "_%s" % (str(version))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
def clean_export_folder(path):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    print("Removing %s" % path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    if os.path.isdir(path):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        shutil.rmtree(path, ignore_errors=True)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
def do_export_version(c, path, **export_keys):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    print("Export version %s : %s" % (path,repr(export_keys)))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    env = c.env
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    for export_key, version in export_keys.items():
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        export_path = os.path.join(path,export_key)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        repo_url = env.repos[export_key]['repo']
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        url_part = urllib.parse.urlparse(repo_url)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        if url_part.scheme or url_part.netloc:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            # this is a remote repo. Let's clone first
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
            clone_path = os.path.join(path,'clone',export_keys[export_key])
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
            os.makedirs(clone_path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            output = c.run('git ls-remote \"%s\"' % repo_url, warn=True)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            print("OUTPUT %r" % output)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            scm = "hg" if output.failed else "git"
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            if scm == "hg":
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
                output = c.run("hg clone \"%s\" \"%s\"" % (repo_url,clone_path))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            else:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
                c.run("git clone \"%s\" \"%s\"" % (repo_url,clone_path))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        else:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            clone_path = repo_url
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        with c.cd(clone_path):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            # detetct .git or .hg subfolder
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            if os.path.exists(os.path.join(clone_path,".git")):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
                os.makedirs(export_path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
                cmd_str = "git archive \'%s\' | tar -x -C \"%s\""
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
            else:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
                cmd_str = "hg archive -r \'%s\' \"%s\""
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            c.run(cmd_str % (str(version),export_path))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
    print("Export version %s done"%repr(export_keys))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
def launch_setup_command(c, command_array, path):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    f = None
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    sys.path.append(path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    current_path = os.getcwd()
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    try:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        os.chdir(path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
        try:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            f, pathname, description = imp.find_module("setup", [path])
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            print("launch_setup_command at %s : found setup" % path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            setup_mod = imp.load_module("setup", f, pathname, description)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            print("launch_setup_command at %s : setup loaded" % path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        except:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
            e = sys.exc_info()[0]
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
            print("Error launching commands %s : %s" % (path, str(e)))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            raise
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        finally:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
            if f:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
                f.close()
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        return setup_mod.launch_setup("setup.py", command_array)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
    finally:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        os.chdir(current_path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
def get_src_dependencies(c, pkg_name, path):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
    print("Get source dependencies at %s" % path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
    launch_setup_command(c, ['egg_info'], path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
    egg_requirement_file = os.path.join(path, "%s.egg-info" % pkg_name, "requires.txt")
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    res = []
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
    with open(egg_requirement_file) as f:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        for req in requirements.parse(f):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
            if req.name in c.env['repos']:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
                r_version = req.specs[0][1] if req.specs else 'tip'
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
                res.append((req.name, r_version))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
    print("Build source dist at %s done : %r" % (path, res))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
    return res
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
def get_remote_env(c, remotepath, remotevirtualenvpath, application_module, settings_key, settings_module=''):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
    if not settings_module:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        settings_module = '%s.%s' % (application_module, 'settings')
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
    env = c.env
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
    with Connection(env['hosts'][0]) as rconnection:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
        with rconnection.prefix("echo $SHELL && . \"%s\"" % os.path.join(remotevirtualenvpath, "bin/activate")), rconnection.prefix("export PYTHONPATH=\"%s\"" % remotepath):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
            return rconnection.run("DJANGO_SETTINGS_MODULE=%s python -c 'import django.conf;print(django.conf.settings.%s)'" % (settings_module, settings_key)).stdout
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
# def rsync_export(path, remotepath, filters):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
#     print("Rsync %s to %s",(path,remotepath))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
#     filter_option_str = "--progress --stats"
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
#     if filters:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
#         filter_option_str += " " + " ".join(["--filter \"%s\"" % (f) for f in filters])
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
#     run("mkdir -p \"%s\"" % remotepath)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
#     rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
#     print("Rsync %s to %s done",(path,remotepath))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
# def clean_rsync_folder(remotepath):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
#     print("clean rsync folder %s" % remotepath)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
#     run("rm -fr \"%s\"" % remotepath)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
def build_src(c, path):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
    print("Build source dist at %s" % path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
    launch_setup_command(c, ['sdist'], path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
    print("Build source dist at %s done" % path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
def get_src_version(c, key, path):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
    print("get src version for %s at %s" % (key,path))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
    env = c.env
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
    mod_name = env.repos[key].get('module', key) or key
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
    f = None
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
    sys.path.append(path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
    current_path = os.getcwd()
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
    os.chdir(path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
    try:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
        f, pathname, description = imp.find_module(mod_name, [path])
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
        src_mod = imp.load_module(mod_name, f, pathname, description)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
    except:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
        src_mod = None
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        print("Could not import module, trying to parse")
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
    finally:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        os.chdir(current_path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
        if f:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
            f.close()
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
    version = None
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
    if src_mod is None:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
        with open(os.path.join(path,mod_name,"__init__.py"),'r') as init_file:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
            for line in init_file:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
                m = re.search('VERSION\s+=\s+\((.+)\)', line, re.I)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
                if m:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
                    version = tuple([re.sub('[\s\"\']','', item) for item in m.group(1).split(',')])
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
                    break
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
    elif hasattr(src_mod, "VERSION"):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
        version = src_mod.VERSION
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
    elif hasattr(src_mod, "__version__"):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
        version = src_mod.__version__
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
    print("VERSION : %s" % repr(version))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
    if version is None:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
        version = ""
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    if not isinstance(version, str):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
        if src_mod and hasattr(src_mod, "get_version"):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
            version_str = src_mod.get_version()
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
        elif isinstance(version, tuple):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
            #convert num
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
            version_str = get_version([int(s) if s.isdigit() else s for s in version])
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        else:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
            version_str = str(version)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
    else:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
        version_str = version
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
    print("VERSION str : %s" % repr(version_str))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
    return (version, version_str)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
def sync_build(c, path):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
    print("Sync build %s" % path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
    env = c.env
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
    with Connection(env['hosts'][0]) as host_connection:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
        with host_connection.cd(env.remote_path['build_export']):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
            filename = os.path.basename(path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
            res_trans = host_connection.put(path, os.path.join(env.remote_path['build_export'], filename))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
            print("Sync build %s to %s" % (path,res_trans.remote))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
            return res_trans
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
def collectstatic(c, remotepath, remotevirtualenvpath, platform_web_module, module_settings="", admin_cmd="python manage.py"):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
    print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
    remotestaticsitepath = get_remote_env(c, remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT", c.env.settings)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
    with Connection(c.env['hosts'][0]) as rconnection:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
        with rconnection.prefix("source \"%s\"" % activate_path), rconnection.prefix("export PYTHONPATH=\"%s\"" % remotepath), rconnection.cd(remotepath):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
            #remove old files optio -c of collect static fail !
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
            rconnection.run("rm -fr \"%s\"/*" % (remotestaticsitepath))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
            rconnection.run("%s collectstatic --noinput %s" % (admin_cmd, "--settings="+module_settings if module_settings else ""))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
def migrate(c, remotepath, remotevirtualenvpath, module_settings="", admin_cmd="python manage.py"):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
    with Connection(c.env['hosts'][0]) as rconnection:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
        with rconnection.prefix("source \"%s\"" % activate_path), rconnection.prefix("export PYTHONPATH=\"%s\"" % remotepath), rconnection.cd(remotepath):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
            rconnection.run("%s migrate --noinput %s" % (admin_cmd, "--settings="+module_settings if module_settings else ""))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
def export_version(c, **kwargs):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
    print("export version %s" % (repr(kwargs)))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
    export_path = kwargs.get('path', None)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
    if not export_path:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
        export_path = get_export_path(c.env, "_".join(["%s_%s" % (k,v) for k,v in kwargs.items()]))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
    clean_export_folder(export_path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
    do_export_version(c, export_path,**kwargs)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
    return export_path
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
def do_create_virtualenv(c, remote_venv_export_path, remotevirtualenvpath):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
    print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
    env = c.env
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
    if env.get('remote_baseline_venv'):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
        prefix_str = "source \"%s\"" % os.path.join(env.get('remote_baseline_venv'), "bin/activate")
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
    else:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        prefix_str = "echo"
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
    with Connection(env['hosts'][0]) as rconnection:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
        rconnection.run("rm -fr \"%s\"" % remotevirtualenvpath, warn=True)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
        run("mkdir -p \"%s\"" % remotevirtualenvpath)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
        with rconnection.prefix(prefix_str), rconnection.cd(os.path.join(remote_venv_export_path,"virtualenv","web")):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
            rconnection.run("python create_python_env.py")
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
            rconnection.run("python project-boot.py \"%s\"" % remotevirtualenvpath)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
        with rconnection.prefix("source \"%s\"" % activate_path):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
            rconnection.run("pip install --no-cache-dir -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt"))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
def do_create_virtualenv_requirement(c, remote_venv_requirement_path, remotevirtualenvpath, python_version = "2"):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
    print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_requirement_path, remotevirtualenvpath))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
    env = c.env
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
    with Connection(env['hosts'][0]) as rconnection:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
        rconnection.run("rm -fr \"%s\"" % remotevirtualenvpath, warn=True)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        rconnection.run("mkdir -p \"%s\"" % remotevirtualenvpath)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        # rconnection.run("virtualenv -p `which python%s` %s" % (python_version, remotevirtualenvpath))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        rconnection.run("python%s -m venv %s" % (python_version, remotevirtualenvpath))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
        with rconnection.prefix("echo $SHELL && . \"%s\"" % os.path.join(remotevirtualenvpath, "bin/activate")):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
            rconnection.run("pip install -r \"%s\"" % remote_venv_requirement_path)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
def do_relaunch_server(c, do_collectstatic, do_migrate):
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
    env = c.env
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
    if do_migrate:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
        migrate(c, env.remote_path['src'], env.remote_path['virtualenv'], env.get('settings', ''), env.get('admin_cmd', 'python manage.py'))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
    if do_collectstatic:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
        collectstatic(c, env.remote_path['src'], env.remote_path['virtualenv'], env.platform_web_module, env.get('settings', ''), env.get('admin_cmd', 'python manage.py'))
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
    with Connection(env['hosts'][0]) as rconnection:
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
        rconnection.sudo(env.web_relaunch_cmd, shell=False)
8895d41be7e2 upgrade distribution script and define version
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281