]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/command/migrate.py
Implement MediaWiki scaffolding for auto conflict resolution; untested.
[wizard.git] / wizard / command / migrate.py
index 89c8f2a85ce280c6ef7b44310173474116d1139c..3949ae8ced0dd1b4d8242009dd5582aa5b8ec159 100644 (file)
@@ -11,7 +11,7 @@ def main(argv, baton):
     options, args = parse_args(argv, baton)
     dir = args[0]
 
-    shell.drop_priviledges(dir, options)
+    shell.drop_priviledges(dir, options.log_file)
 
     util.chdir(dir)
     sh = shell.Shell(options.dry_run)
@@ -33,32 +33,32 @@ def main(argv, baton):
     except deploy.NotMigratedError:
         pass
     except (deploy.CorruptedAutoinstallError, AlreadyMigratedError):
-        if options.force:
-            perform_force(options)
-        else:
+        if not options.force:
             raise
 
     deployment.verifyTag(options.srv_path)
 
-    version = deployment.app_version
+    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
 
-    try:
+    # XXX: turn this into a context
+    with util.LockDirectory(".scripts-migrate-lock"):
         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)
-        make_repository(sh, options, repo, tag)
-        check_variables(deployment, options)
-    finally:
-        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 a particularly zany race condition
+            # 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
@@ -75,7 +75,11 @@ NOT run this command as root."""
     parser.add_option("--dry-run", dest="dry_run", action="store_true",
             default=False, help="Prints would would be run without changing anything")
     parser.add_option("--force", "-f", dest="force", action="store_true",
-            default=False, help="If .git or .scripts directory already exists, delete them and migrate")
+            default=False, help="If .git or .scripts directory already exists,"
+            "delete them and migrate")
+    parser.add_option("--force-version", dest="force_version",
+            default=None, help="If .scripts-version tells lies, explicitly specify"
+            "a version to migrate to.")
     options, args = parser.parse_all(argv)
     if len(args) > 1:
         parser.error("too many arguments")
@@ -89,24 +93,13 @@ def perform_force(options):
 
     if has_git:
         logging.warning("Force removing .git directory")
-        if not options.dry_run: backup = safe_unlink(".git")
+        if not options.dry_run: backup = util.safe_unlink(".git")
         logging.info(".git backed up to %s" % backup)
     if has_scripts:
         logging.warning("Force removing .scripts directory")
-        if not options.dry_run: backup = safe_unlink(".scripts")
+        if not options.dry_run: backup = util.safe_unlink(".scripts")
         logging.info(".scripts backed up to %s" % backup)
 
-def safe_unlink(file):
-    """Moves a file to a backup location."""
-    prefix = "%s.bak" % file
-    name = None
-    for i in itertools.count():
-        name = "%s.%d" % (prefix, i)
-        if not os.path.exists(name):
-            break
-    os.rename(file, name)
-    return name
-
 def make_repository(sh, options, repo, tag):
     sh.call("git", "init") # create repository
     # configure our alternates (to save space and make this quick)
@@ -168,13 +161,3 @@ ERROR: Directory already contains a .git and
 both of these directories will be removed.
 """
 
-class DirectoryLockedError(Error):
-    def __init__(self, dir):
-        self.dir = dir
-    def __str__(self):
-        return """
-
-ERROR: Could not acquire lock on directory.  Maybe there is
-another migration process running?
-"""
-