add migrate option for django 1.7
authorymh <ymh.work@gmail.com>
Thu, 19 Mar 2015 22:49:11 +0100
changeset 22 89114c0a6ad7
parent 21 57bbeb60c60b
child 23 bb36afd9ad31
add migrate option for django 1.7
src/fablib/core.py
--- a/src/fablib/core.py	Mon Nov 17 14:34:16 2014 +0100
+++ b/src/fablib/core.py	Thu Mar 19 22:49:11 2015 +0100
@@ -19,9 +19,9 @@
 
 
 __all__ = ["check_folder_access", "syncdb", "collectstatic", "do_relaunch_server",
-    "export_version", "do_sync_web", "create_config", "clean_export_folder", 
-    "sync_install_build", "do_create_virtualenv", "clean_rsync_folder", "rsync_export",    
-    "do_sync_comp", "get_comp_versions_dict", "SyncComp"] 
+    "export_version", "do_sync_web", "create_config", "clean_export_folder",
+    "sync_install_build", "do_create_virtualenv", "clean_rsync_folder", "rsync_export",
+    "do_sync_comp", "get_comp_versions_dict", "SyncComp"]
 
 def get_export_path(version):
     base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/")
@@ -37,25 +37,25 @@
 
     for export_key, version in export_keys.items():
         export_path = os.path.join(path,export_key)
-        
+
         repo_url = env.repos[export_key]['repo']
         url_part = urlparse.urlparse(repo_url)
         if url_part.scheme or url_part.netloc:
             # this is a remote repo. Let's clone first
             clone_path = os.path.join(path,'clone',export_keys)
             os.makedirs(clone_path)
-            
-            scm = "hg"            
+
+            scm = "hg"
             with settings(warn_only=True):
                 output = local('git ls-remote \"%s\"' % repo_url)
-                scm = "git" if output.failed else "hg" 
+                scm = "git" if output.failed else "hg"
             if scm == "hg":
                 output = local("hg clone \"%s\" \"%s\"" % (repo_url,clone_path))
             else:
-                local("git clone \"%s\" \"%s\"" % (repo_url,clone_path))            
+                local("git clone \"%s\" \"%s\"" % (repo_url,clone_path))
         else:
             clone_path = repo_url
-        
+
         with lcd(clone_path):
             # detetct .git or .hg subfolder
             if os.path.exists(os.path.join(clone_path,".git")):
@@ -64,10 +64,10 @@
             else:
                 cmd_str = "hg archive -r \'%s\' \"%s\""
             local(cmd_str % (str(version),export_path))
-    
+
     print("Export version %s done"%repr(export_keys))
 
-    
+
 def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key):
     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
@@ -81,32 +81,32 @@
             res = run("python < %s" % (tempfilepath))
         run("rm -f \"%s\"" % (tempfilepath))
     return res
-    
-    
-    
+
+
+
 def rsync_export(path, remotepath, filters):
     print("Rsync %s to %s",(path,remotepath))
-    
+
     filter_option_str = "--progress --stats"
     if filters:
         filter_option_str += " " + " ".join(["--filter \"%s\"" % (f) for f in filters])
-    
+
     run("mkdir -p \"%s\"" % remotepath)
     rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True)
     print("Rsync %s to %s done",(path,remotepath))
-    
+
 def clean_rsync_folder(remotepath):
     print("clean rsync folder %s" % remotepath)
     run("rm -fr \"%s\"" % remotepath)
-    
+
 def build_src(path):
     print("Build source dist at %s" % path)
     f = None
     sys.path.append(path)
     current_path = os.getcwdu()
-    try:    
-        os.chdir(path)    
-        try:    
+    try:
+        os.chdir(path)
+        try:
             f, pathname, description = imp.find_module("setup", [path])
             print("Build source dist at %s : found setup" % path)
             setup_mod = imp.load_module("setup", f, pathname, description)
@@ -118,7 +118,7 @@
         finally:
             if f:
                 f.close()
-            
+
         setup_mod.launch_setup("setup.py", ['sdist'])
     finally:
         os.chdir(current_path)
@@ -134,14 +134,14 @@
         if version[3] != 'final':
             version_str = '%s %s %s' % (version_str, version[3], version[4])
     return version_str
-    
+
 
 def get_src_version(key, path):
-        
+
     print("get src version for %s at %s" % (key,path))
-    
+
     mod_name = env.repos[key].get('module', key) or key
-    
+
     f = None
     sys.path.append(path)
     current_path = os.getcwdu()
@@ -151,7 +151,7 @@
         src_mod = imp.load_module(mod_name, f, pathname, description)
     except:
         src_mod = None
-        print("Could not import module, trying to parse")        
+        print("Could not import module, trying to parse")
     finally:
         os.chdir(current_path)
         if f:
@@ -161,7 +161,7 @@
         with open(os.path.join(path,mod_name,"__init__.py"),'r') as init_file:
             for line in init_file:
                 m = re.search('VERSION\s+=\s+\((.+)\)', line, re.I)
-                if m:                    
+                if m:
                     version = tuple([re.sub('[\s\"\']','', item) for item in m.group(1).split(',')])
                     break
     elif hasattr(src_mod, "VERSION"):
@@ -170,7 +170,7 @@
         version = src_mod.__version__
 
     print("VERSION : %s" % repr(version))
-     
+
     if version is None:
         version = ""
 
@@ -184,10 +184,10 @@
             version_str = str(version)
     else:
         version_str = version
-        
+
     print("VERSION str : %s" % repr(version_str))
-    return (version, version_str) 
-    
+    return (version, version_str)
+
 
 def sync_build(path):
     print("Sync build %s" % path)
@@ -200,12 +200,12 @@
 def remove_build(path):
     print("remove build build %s" % path)
     run("rm \"%s\"" % path)
-        
+
 
 def install_build(remotepath, remotevirtualenvpath, module_to_uninstall= None):
     print("Install build %s in %s" % (remotepath, remotevirtualenvpath))
     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
-    
+
     with prefix("source %s" % activate_path):
         if module_to_uninstall:
             with settings(warn_only=True):
@@ -221,17 +221,30 @@
         run("rm -fr \"%s\"" % (remotestaticsitepath))
         run("python manage.py collectstatic --noinput %s" % ("--settings="+module_settings if module_settings else ""))
 
-def syncdb(remotepath, remotevirtualenvpath, module_settings=""):
+def migrate(remotepath, remotevirtualenvpath, module_settings=""):
+    {
+        'syncdb': do_syncdb,
+        'migrate': do_migrate,
+    }.get(getattr(env, 'migrate_method', 'syncdb'), do_syncdb)(remotepath, remotevirtualenvpath, module_settings)
+
+def do_syncdb(remotepath, remotevirtualenvpath, module_settings=""):
     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
         run("python manage.py syncdb --migrate --noinput %s" % ("--settings="+module_settings if module_settings else ""))
-        
-def create_config(export_path):    
+
+
+def do_migrate(remotepath, remotevirtualenvpath, module_settings=""):
+    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
+    with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
+        run("python manage.py migrate --noinput %s" % ("--settings="+module_settings if module_settings else ""))
+
+
+def create_config(export_path):
     print("Create config from %s" % (export_path,))
     remotepath = env.remote_path['src']
     remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
     template_path = os.path.join(export_path, "src", env.platform_web_module, "config.py.tmpl")
-    
+
     context = {
         'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/",
         'asctime': '%(asctime)s',
@@ -240,22 +253,22 @@
         'module': '%(module)s',
     }
     context.update(env.config['web'])
-    
+
     if not exists(remote_config_path, verbose=True):
         upload_template(template_path, remote_config_path, context=context)
 
 def export_version(**kwargs):
     print("export version %s" % (repr(kwargs)))
-    
+
     export_path = kwargs.get('path', None)
-    
-    if not export_path:    
+
+    if not export_path:
         export_path = get_export_path("_".join(["%s_%s" % (k,v) for k,v in kwargs.items()]))
-    
+
     clean_export_folder(export_path)
-    
+
     do_export_version(export_path,**kwargs)
-    
+
     return export_path
 
 def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
@@ -276,24 +289,24 @@
 
 def do_sync_comp(key, export_path):
     print("do_sync_comp with  path %s" % (export_path))
-    
+
     src_path = os.path.join(export_path,env.repos[key]['src_root'])
     # find setup.py
     for root, _, files in os.walk(src_path):
         if "setup.py" in files:
             src_path = root
-    
+
     build_src(src_path)
     (_,version_str) = get_src_version(key, src_path)
     build_path = os.path.join(src_path,"dist","%s-%s.tar.gz" % (key,version_str))
     sync_install_build(build_path, key)
-        
+
 
 def sync_install_build(build_path, module_to_uninstall=None):
     res_trans = None
     try:
         res_trans = sync_build(build_path)
-        install_build(res_trans[0], env.remote_path['virtualenv'], module_to_uninstall)        
+        install_build(res_trans[0], env.remote_path['virtualenv'], module_to_uninstall)
     finally:
         if res_trans:
             remove_build(res_trans[0])
@@ -302,10 +315,10 @@
 def do_sync_web(version, export_path):
     print("do_sync_web with version %s and path %s" % (version,export_path))
     #sync src
-    src_path = os.path.join(export_path,"src/") 
+    src_path = os.path.join(export_path,"src/")
     rsync_export(src_path, env.remote_path['src'], env.rsync_filters['src'])
-        
-    
+
+
 def check_folder_access():
     print("Check folder access")
     # get remote user
@@ -331,13 +344,13 @@
                 if "." in version_req:
                     version_req = "V" + ".".join(["%02d" % (int(s)) if s.isdigit() else s for s in version_req.split(".")])
                 comp_versions[key] = version_req
-    
+
     return comp_versions
 
-def do_relaunch_server(do_collectstatic, do_syncdb, do_check_folder_access):
+def do_relaunch_server(do_collectstatic, do_migrate, do_check_folder_access):
     if do_check_folder_access:
         check_folder_access()
-    if do_syncdb:
+    if do_migrate:
         syncdb(env.remote_path['src'], env.remote_path['virtualenv'], env.get('settings', ''))
     if do_collectstatic:
         collectstatic(env.remote_path['src'], env.remote_path['virtualenv'], env.platform_web_module, env.get('settings', ''))
@@ -345,18 +358,18 @@
 
 
 class SyncComp(Task):
-    
+
     def __init__(self, key):
         self.key = key
 
     def __get_name(self):
         return "sync_" + self.key
-    
+
     name = property(__get_name)
-    
+
     def run(self, version):
         print(green("sync %s with version %s" % (self.key, version)))
-        export_path_web = export_version(web=version)    
+        export_path_web = export_version(web=version)
         export_path_web_full = os.path.join(export_path_web,'web')
         comp_versions = get_comp_versions_dict(export_path_web_full)
 
@@ -365,5 +378,5 @@
         do_sync_comp(self.key, export_path_full)
         clean_export_folder(export_path)
         clean_export_folder(export_path_web)
-        
-        do_relaunch_server(do_collectstatic=True, do_syncdb=True, do_check_folder_access=True)
+
+        do_relaunch_server(do_collectstatic=True, do_migrate=True, do_check_folder_access=True)