comparison piecrust/importing/piecrust.py @ 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
comparison
equal deleted inserted replaced
62:52e4d9a1f917 63:28958565a17b
12 class PieCrust1Importer(FileWalkingImporter): 12 class PieCrust1Importer(FileWalkingImporter):
13 def __init__(self): 13 def __init__(self):
14 super(PieCrust1Importer, self).__init__() 14 super(PieCrust1Importer, self).__init__()
15 self.name = 'piecrust1' 15 self.name = 'piecrust1'
16 self.description = "Imports content from a PieCrust 1 website." 16 self.description = "Imports content from a PieCrust 1 website."
17 self.requires_website = False
17 18
18 def setupParser(self, parser, app): 19 def setupParser(self, parser, app):
19 super(PieCrust1Importer, self).setupParser(parser, app) 20 super(PieCrust1Importer, self).setupParser(parser, app)
20 parser.add_argument('root_dir', 21 parser.add_argument('root_dir', nargs='?',
21 help="The root directory of the PieCrust 1 website.") 22 help="The root directory of the PieCrust 1 website.")
23 parser.add_argument('--upgrade', action='store_true',
24 help="Upgrade the current website in place.")
22 25
23 def importWebsite(self, app, args): 26 def importWebsite(self, app, args):
24 logger.debug("Importing PieCrust 1 site from: %s" % args.root_dir) 27 if app.root_dir and args.upgrade:
28 raise Exception("Can't specifiy both a root directory and `--upgrade`.")
29 if app.root_dir is None and not args.upgrade:
30 raise Exception("Need to specify either a root directory or `--upgrade`.")
31
32 root_dir = os.getcwd() if args.upgrade else app.root_dir
33 logger.debug("Importing PieCrust 1 site from: %s" % root_dir)
25 exclude = args.exclude or [] 34 exclude = args.exclude or []
26 exclude += ['_cache', '_counter'] 35 exclude += ['_cache', '_counter']
27 self._startWalk(args.root_dir, exclude, app) 36 self._startWalk(root_dir, exclude, root_dir, args.upgrade)
37 if args.upgrade:
38 content_dir = os.path.join(root_dir, '_content')
39 file_count = 0
40 for _, __, filenames in os.walk(content_dir):
41 file_count += len(filenames)
42 if file_count == 0:
43 shutil.rmtree(content_dir)
44 else:
45 logger.warning("Can't delete `_content` directory, files have been left.")
28 logger.info("The PieCrust website was successfully imported.") 46 logger.info("The PieCrust website was successfully imported.")
29 47
30 def _importFile(self, full_fn, rel_fn, app): 48 def _importFile(self, full_fn, rel_fn, out_root_dir, is_move):
31 logger.debug("- %s" % rel_fn) 49 logger.debug("- %s" % rel_fn)
32 dest_path = rel_fn 50 dest_path = rel_fn
33 convert_func = None 51 convert_func = None
34 if rel_fn.replace('\\', '/') == '_content/config.yml': 52 if rel_fn.replace('\\', '/') == '_content/config.yml':
35 dest_path = 'config.yml' 53 dest_path = 'config.yml'
41 convert_func = self.convertPage 59 convert_func = self.convertPage
42 else: 60 else:
43 dest_path = 'assets/' + rel_fn 61 dest_path = 'assets/' + rel_fn
44 62
45 logger.debug(" %s -> %s" % (rel_fn, dest_path)) 63 logger.debug(" %s -> %s" % (rel_fn, dest_path))
46 full_dest_path = os.path.join(app.root_dir, dest_path) 64 full_dest_path = os.path.join(out_root_dir, dest_path)
47 os.makedirs(os.path.dirname(full_dest_path), 0o755, True) 65 os.makedirs(os.path.dirname(full_dest_path), 0o755, True)
48 if convert_func is None: 66 if convert_func is None:
49 shutil.copy2(full_fn, full_dest_path) 67 if is_move:
68 shutil.move(full_fn, full_dest_path)
69 else:
70 shutil.copy2(full_fn, full_dest_path)
50 else: 71 else:
51 with open(full_fn, 'r', encoding='utf8') as fp: 72 with open(full_fn, 'r', encoding='utf8') as fp:
52 content = fp.read() 73 content = fp.read()
53 converted_content = convert_func(content) 74 converted_content = convert_func(content)
54 with open(full_dest_path, 'w', encoding='utf8') as fp: 75 with open(full_dest_path, 'w', encoding='utf8') as fp:
55 fp.write(converted_content) 76 fp.write(converted_content)
56 if converted_content != content: 77 if converted_content != content:
57 logger.warning("'%s' has been modified. The original version " 78 logger.warning("'%s' has been modified. The original version "
58 "has been kept for reference." % rel_fn) 79 "has been kept for reference." % rel_fn)
59 shutil.copy2(full_fn, full_dest_path + '.orig') 80 shutil.copy2(full_fn, full_dest_path + '.orig')
81 if is_move:
82 os.remove(full_fn)
60 83
61 def convertConfig(self, content): 84 def convertConfig(self, content):
62 return content 85 return content
63 86
64 def convertPage(self, content): 87 def convertPage(self, content):