Commits

Vladimir Mihailenco  committed 226b8af

Speed up version.list_versions

  • Participants
  • Parent commits 383efd2

Comments (0)

Files changed (4)

 include MANIFEST.in
 include README.rst
+recursive-include fabdeploy/bin **
 recursive-include fabdeploy/config_templates **

File fabdeploy/bin/versions.py

+#!/usr/bin/env python
+
+import os
+import sys
+import datetime
+import argparse
+
+
+def versions(dirpath, time_format=None):
+    versions = []
+    for filename in os.listdir(dirpath):
+        filepath = os.path.join(dirpath, filename)
+        if not os.path.isdir(filepath):
+            continue
+        try:
+            datetime.datetime.strptime(filename, time_format)
+        except ValueError:
+            continue
+        is_tmp = not os.path.exists(os.path.join(filepath, '.fabdeploy'))
+        versions.append((filename, is_tmp))
+    versions.sort(reverse=True)
+    return versions
+
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser()
+    parser.add_argument('dirpath', type=str)
+    parser.add_argument('time_format', type=str)
+    args = parser.parse_args()
+
+    v = versions(args.dirpath, time_format=args.time_format)
+    sys.stdout.write(repr(v))

File fabdeploy/fabd.py

 import os
+import ast
 import shutil
 import logging
 
-from fabric.api import env, run, sudo, puts, abort
+from fabric.api import env, run, sudo, puts, abort, settings, hide
 
-from . import users, ssh
+from . import users, tar, ssh
 from .containers import conf as conf_dec
 from .task import Task
 
     'default_conf',
     'create_user',
     'create_configs',
+    'push_bin',
 ]
 
 
                 shutil.copyfile(src_filepath, target_filepath)
 
 create_configs = CreateConfigs()
+
+
+class PushBin(Task):
+    @conf_dec
+    def bin_src(self):
+        return os.path.join(os.path.dirname(__file__), 'bin')
+
+    @conf_dec
+    def bin_target(self):
+        return self.conf.fabdeploy_bin_path
+
+    def do(self):
+        tar.push.run(
+            src_path=self.conf.bin_src,
+            target_path=self.conf.bin_target)
+
+push_bin = PushBin()
+
+
+class Bin(Task):
+    @conf_dec
+    def cmd(self):
+        return 'python %(fabdeploy_bin_path)s/%(program)s.py %(args)s'
+
+    def do(self):
+        with settings(hide('everything')):
+            output = run(self.conf.cmd)
+        return ast.literal_eval(output)
+
+bin = Bin()

File fabdeploy/version.py

-import datetime
 import posixpath
 
 from fabric.api import run, puts, prompt
 from fabric.contrib import files
 
+from . import fabd
 from .containers import conf
-from .files import list_files
 from .task import Task
 
 
 
 class Activate(Task):
     def do(self):
+        # save previous version
         run('rm --force %(previous_version_link)s' % self.conf)
-        # save previous version
         if files.exists(self.conf.last_version_link):
             run('cp '
                 '--no-dereference '
 
 class ListVersions(Task):
     def versions(self):
-        versions = []
-        for file in list_files(self.conf.home_path):
-            name = posixpath.basename(file)
-            try:
-                datetime.datetime.strptime(name, self.conf.time_format)
-            except ValueError:
-                continue
-
-            f = posixpath.join(self.conf.home_path, name, '.fabdeploy')
-            is_tmp = not files.exists(f)
-            versions.append((name, is_tmp))
-
-        versions.sort(reverse=True)
-        return versions
+        return fabd.bin.run(
+            program='versions',
+            args='%(home_path)s %(time_format)s' % self.conf)
 
     def puts(self, versions):
         for i, (version, is_tmp) in enumerate(versions):