comparison piecrust/importing/piecrust.py @ 64:9ae3237365eb

PieCrust 1 import: clean empty directories and convert some config values.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 29 Aug 2014 08:06:17 -0700
parents 28958565a17b
children 46842f71f31f
comparison
equal deleted inserted replaced
63:28958565a17b 64:9ae3237365eb
1 import os 1 import os
2 import os.path 2 import os.path
3 import re 3 import re
4 import shutil 4 import shutil
5 import logging 5 import logging
6 import yaml
6 from piecrust.importing.base import FileWalkingImporter 7 from piecrust.importing.base import FileWalkingImporter
7 8
8 9
9 logger = logging.getLogger(__name__) 10 logger = logging.getLogger(__name__)
10 11
33 logger.debug("Importing PieCrust 1 site from: %s" % root_dir) 34 logger.debug("Importing PieCrust 1 site from: %s" % root_dir)
34 exclude = args.exclude or [] 35 exclude = args.exclude or []
35 exclude += ['_cache', '_counter'] 36 exclude += ['_cache', '_counter']
36 self._startWalk(root_dir, exclude, root_dir, args.upgrade) 37 self._startWalk(root_dir, exclude, root_dir, args.upgrade)
37 if args.upgrade: 38 if args.upgrade:
38 content_dir = os.path.join(root_dir, '_content') 39 self._cleanEmptyDirectories(root_dir)
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.")
46 logger.info("The PieCrust website was successfully imported.") 40 logger.info("The PieCrust website was successfully imported.")
47 41
48 def _importFile(self, full_fn, rel_fn, out_root_dir, is_move): 42 def _importFile(self, full_fn, rel_fn, out_root_dir, is_move):
49 logger.debug("- %s" % rel_fn) 43 logger.debug("- %s" % rel_fn)
50 dest_path = rel_fn 44 dest_path = rel_fn
79 "has been kept for reference." % rel_fn) 73 "has been kept for reference." % rel_fn)
80 shutil.copy2(full_fn, full_dest_path + '.orig') 74 shutil.copy2(full_fn, full_dest_path + '.orig')
81 if is_move: 75 if is_move:
82 os.remove(full_fn) 76 os.remove(full_fn)
83 77
78 def _cleanEmptyDirectories(self, root_dir):
79 for item in os.listdir(root_dir):
80 if not os.path.isdir(item):
81 continue
82
83 file_count = 0
84 item_path = os.path.join(root_dir, item)
85 for _, __, filenames in os.walk(item_path):
86 file_count += len(filenames)
87 if file_count == 0:
88 logger.debug("Deleting empty directory: %s" % item)
89 shutil.rmtree(item_path)
90
84 def convertConfig(self, content): 91 def convertConfig(self, content):
92 config = yaml.load(content)
93 sitec = config.setdefault('site', {})
94 if 'templates_dirs' in sitec:
95 tdc = sitec['templates_dirs']
96 cl = len('_content/')
97 if isinstance(tdc, str) and re.match(r'^_content[/\\]', tdc):
98 sitec['templates_dirs'] = tdc[cl:]
99 elif isinstance(tdc, list):
100 sitec['templates_dirs'] = list(map(
101 lambda d: d[cl:] if re.match(r'^_content[/\\]', d) else d,
102 tdc))
103
104 jinjac = config.setdefault('jinja', {})
105 jinjac['twig_compatibility'] = True
106
107 content = yaml.dump(config, default_flow_style=False)
85 return content 108 return content
86 109
87 def convertPage(self, content): 110 def convertPage(self, content):
88 return content 111 return content
89 112