changeset 63:28958565a17b

In-place upgrade for PieCrust 1 sites.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 28 Aug 2014 16:48:31 -0700
parents 52e4d9a1f917
children 9ae3237365eb
files piecrust/commands/builtin/util.py piecrust/importing/base.py piecrust/importing/piecrust.py
diffstat 3 files changed, 33 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/commands/builtin/util.py	Wed Aug 27 17:14:44 2014 -0700
+++ b/piecrust/commands/builtin/util.py	Thu Aug 28 16:48:31 2014 -0700
@@ -127,7 +127,8 @@
             p = subparsers.add_parser(i.name, help=i.description)
             i.setupParser(p, app)
             p.set_defaults(sub_func=i.checkedImportWebsite)
+            p.set_defaults(sub_requires_website=i.requires_website)
 
-    def run(self, ctx):
+    def checkedRun(self, ctx):
         ctx.args.sub_func(ctx)
 
--- a/piecrust/importing/base.py	Wed Aug 27 17:14:44 2014 -0700
+++ b/piecrust/importing/base.py	Thu Aug 28 16:48:31 2014 -0700
@@ -12,6 +12,7 @@
     def __init__(self):
         self.name = None
         self.description = None
+        self.requires_website = True
 
     def setupParser(self, parser, app):
         raise NotImplementedError()
@@ -20,7 +21,7 @@
         raise NotImplementedError()
 
     def checkedImportWebsite(self, ctx):
-        if ctx.app.root_dir is None:
+        if ctx.app.root_dir is None and self.requires_website:
             raise SiteNotFoundError()
         self.importWebsite(ctx.app, ctx.args)
         return 0
--- a/piecrust/importing/piecrust.py	Wed Aug 27 17:14:44 2014 -0700
+++ b/piecrust/importing/piecrust.py	Thu Aug 28 16:48:31 2014 -0700
@@ -14,20 +14,38 @@
         super(PieCrust1Importer, self).__init__()
         self.name = 'piecrust1'
         self.description = "Imports content from a PieCrust 1 website."
+        self.requires_website = False
 
     def setupParser(self, parser, app):
         super(PieCrust1Importer, self).setupParser(parser, app)
-        parser.add_argument('root_dir',
+        parser.add_argument('root_dir', nargs='?',
                 help="The root directory of the PieCrust 1 website.")
+        parser.add_argument('--upgrade', action='store_true',
+                help="Upgrade the current website in place.")
 
     def importWebsite(self, app, args):
-        logger.debug("Importing PieCrust 1 site from: %s" % args.root_dir)
+        if app.root_dir and args.upgrade:
+            raise Exception("Can't specifiy both a root directory and `--upgrade`.")
+        if app.root_dir is None and not args.upgrade:
+            raise Exception("Need to specify either a root directory or `--upgrade`.")
+
+        root_dir = os.getcwd() if args.upgrade else app.root_dir
+        logger.debug("Importing PieCrust 1 site from: %s" % root_dir)
         exclude = args.exclude or []
         exclude += ['_cache', '_counter']
-        self._startWalk(args.root_dir, exclude, app)
+        self._startWalk(root_dir, exclude, root_dir, args.upgrade)
+        if args.upgrade:
+            content_dir = os.path.join(root_dir, '_content')
+            file_count = 0
+            for _, __, filenames in os.walk(content_dir):
+                file_count += len(filenames)
+            if file_count == 0:
+                shutil.rmtree(content_dir)
+            else:
+                logger.warning("Can't delete `_content` directory, files have been left.")
         logger.info("The PieCrust website was successfully imported.")
 
-    def _importFile(self, full_fn, rel_fn, app):
+    def _importFile(self, full_fn, rel_fn, out_root_dir, is_move):
         logger.debug("- %s" % rel_fn)
         dest_path = rel_fn
         convert_func = None
@@ -43,10 +61,13 @@
             dest_path = 'assets/' + rel_fn
 
         logger.debug("  %s -> %s" % (rel_fn, dest_path))
-        full_dest_path = os.path.join(app.root_dir, dest_path)
+        full_dest_path = os.path.join(out_root_dir, dest_path)
         os.makedirs(os.path.dirname(full_dest_path), 0o755, True)
         if convert_func is None:
-            shutil.copy2(full_fn, full_dest_path)
+            if is_move:
+                shutil.move(full_fn, full_dest_path)
+            else:
+                shutil.copy2(full_fn, full_dest_path)
         else:
             with open(full_fn, 'r', encoding='utf8') as fp:
                 content = fp.read()
@@ -57,6 +78,8 @@
                 logger.warning("'%s' has been modified. The original version "
                                "has been kept for reference." % rel_fn)
                 shutil.copy2(full_fn, full_dest_path + '.orig')
+            if is_move:
+                os.remove(full_fn)
 
     def convertConfig(self, content):
         return content