]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/command/migrate.py
Make the commit not get call errors subsumed.
[wizard.git] / wizard / command / migrate.py
index 420564ebf2303f82917d0ee6beaee6b4499048d2..259d2e747861ae5728f061d3b2ff9923819b88ec 100644 (file)
@@ -1,15 +1,15 @@
 import os
-import itertools
 import shutil
 import logging
-import errno
-import sys
 
 from wizard import command, deploy, shell, util
 
 def main(argv, baton):
     options, args = parse_args(argv, baton)
-    dir = args[0]
+    if args:
+        dir = args[0]
+    else:
+        dir = os.getcwd()
 
     shell.drop_priviledges(dir, options.log_file)
 
@@ -19,7 +19,7 @@ def main(argv, baton):
     logging.info("Migrating %s" % dir)
     logging.debug("uid is %d" % os.getuid())
 
-    deployment = deploy.Deployment(".")
+    deployment = deploy.ProductionCopy(".")
 
     # deal with old-style migration, remove this later
     if os.path.isfile(".scripts/old-version") and not os.path.isfile(".scripts-version"):
@@ -41,29 +41,31 @@ def main(argv, baton):
     if options.force_version:
         version = deployment.application.makeVersion(options.force_version)
     else:
-        deployment.verifyVersion()
-        version = deployment.app_version
-    repo    = version.application.repository(options.srv_path)
-    tag     = version.scripts_tag
-
-    # XXX: turn this into a context
-    try:
         try:
-            os.open(".scripts-migrate-lock", os.O_CREAT | os.O_EXCL)
-        except OSError as e:
-            if e.errno == errno.EEXIST:
-                raise DirectoryLockedError
-            elif e.errno == errno.EACCES:
-                raise command.PermissionsError(dir)
-            raise
-        if options.force: perform_force(options)
-        make_repository(sh, options, repo, tag)
-        check_variables(deployment, options)
-    finally:
+            deployment.verifyVersion()
+            version = deployment.app_version
+        except deploy.VersionMismatchError as e:
+            # well, we'll use that then
+            version = deployment.application.makeVersion(str(e.real_version))
+    repo = version.application.repository(options.srv_path)
+    tag = version.scripts_tag
+    try:
+        sh.call("git", "--git-dir=%s" % repo, "rev-parse", tag)
+    except shell.CallError:
+        raise UnsupportedVersion(version.version)
+
+    with util.LockDirectory(".scripts-migrate-lock"):
         try:
-            os.unlink(".scripts-migrate-lock")
-        except OSError:
-            pass
+            if options.force:
+                perform_force(options)
+            make_repository(sh, options, repo, tag)
+            check_variables(deployment, options)
+        except KeyboardInterrupt:
+            # revert it; barring zany race conditions this is safe
+            if os.path.exists(".scripts"):
+                shutil.rmtree(".scripts")
+            if os.path.exists(".git"):
+                shutil.rmtree(".git")
 
 def parse_args(argv, baton):
     usage = """usage: %prog migrate [ARGS] DIR
@@ -88,8 +90,6 @@ NOT run this command as root."""
     options, args = parser.parse_all(argv)
     if len(args) > 1:
         parser.error("too many arguments")
-    elif not args:
-        parser.error("must specify directory")
     return (options, args)
 
 def perform_force(options):
@@ -156,23 +156,25 @@ class Error(command.Error):
     pass
 
 class AlreadyMigratedError(Error):
+    quiet = True
     def __init__(self, dir):
         self.dir = dir
     def __str__(self):
         return """
 
-ERROR: Directory already contains a .git and
-.scripts directory.  If you force this migration,
-both of these directories will be removed.
+This autoinstall is already migrated; move along, nothing to
+see here.  (If you really want to, you can force a re-migration
+with --force, but this will blow away the existing .git and
+.scripts directories (i.e. your history and Wizard configuration).)
 """
 
-class DirectoryLockedError(Error):
-    def __init__(self, dir):
-        self.dir = dir
+class UnsupportedVersion(Error):
+    def __init__(self, version):
+        self.version = version
     def __str__(self):
         return """
 
-ERROR: Could not acquire lock on directory.  Maybe there is
-another migration process running?
-"""
-
+ERROR: This autoinstall is presently on %s, which is unsupported by
+Wizard.  Please manually upgrade it to one that is supported,
+and then retry the migration; usually the latest version is supported.
+""" % self.version