diff options
author | Ryan Petrello <lists@ryanpetrello.com> | 2012-03-14 23:13:00 -0700 |
---|---|---|
committer | Ryan Petrello <lists@ryanpetrello.com> | 2012-03-15 14:55:49 -0700 |
commit | abea6937943062f6cc667d5c2bfad835f1f7a20f (patch) | |
tree | cce1faf7f4c8c7fb55cddfd9f4d2bfa31debfa76 /pecan/scaffolds | |
parent | 37bc24e7c0ff5f19d23528f3cc152db478fcd436 (diff) | |
download | pecan-abea6937943062f6cc667d5c2bfad835f1f7a20f.tar.gz |
Fixing a few bugs in `pecan create`.
Diffstat (limited to 'pecan/scaffolds')
-rw-r--r-- | pecan/scaffolds/__init__.py | 57 |
1 files changed, 11 insertions, 46 deletions
diff --git a/pecan/scaffolds/__init__.py b/pecan/scaffolds/__init__.py index 304d0c6..d802784 100644 --- a/pecan/scaffolds/__init__.py +++ b/pecan/scaffolds/__init__.py @@ -11,29 +11,17 @@ _bad_chars_re = re.compile('[^a-zA-Z0-9_]') class PecanScaffold(object): - @property - def template_dir(self): - if isinstance(self._scaffold_dir, tuple): - return self._scaffold_dir - else: - return os.path.join(self.module_dir, self._scaffold_dir) - - @property - def module_dir(self): - mod = sys.modules[self.__class__.__module__] - return os.path.dirname(mod.__file__) - def copy_to(self, dest, **kwargs): output_dir = os.path.abspath(os.path.normpath(dest)) pkg_name = _bad_chars_re.sub('', dest.lower()) - copy_dir(self.template_dir, output_dir, {'package': pkg_name}) + copy_dir(self._scaffold_dir, output_dir, {'package': pkg_name}) class BaseScaffold(PecanScaffold): - _scaffold_dir = 'base' + _scaffold_dir = ('pecan', 'scaffolds/base') -def copy_dir(source, dest, variables, out_=sys.stdout): +def copy_dir(source, dest, variables, out_=sys.stdout, i=0): """ Copies the ``source`` directory to the ``dest`` directory. @@ -42,30 +30,21 @@ def copy_dir(source, dest, variables, out_=sys.stdout): ``out_``: File object to write to """ def out(msg): - out_.write(msg) + out_.write('%s%s' % (' ' * (i * 2), msg)) out_.write('\n') out_.flush() - use_pkg_resources = isinstance(source, tuple) - - if use_pkg_resources: - names = sorted(pkg_resources.resource_listdir(source[0], source[1])) - else: - names = sorted(os.listdir(source)) + names = sorted(pkg_resources.resource_listdir(source[0], source[1])) if not os.path.exists(dest): out('Creating %s' % dest) makedirs(dest) else: - out('Directory %s already exists' % dest) + out('%s already exists' % dest) return for name in names: - if use_pkg_resources: - full = '/'.join([source[1], name]) - else: - full = os.path.join(source, name) - + full = '/'.join([source[1], name]) dest_full = os.path.join(dest, substitute_filename(name, variables)) sub_file = False @@ -73,33 +52,19 @@ def copy_dir(source, dest, variables, out_=sys.stdout): dest_full = dest_full[:-5] sub_file = True - if use_pkg_resources and pkg_resources.resource_isdir(source[0], full): - out('Recursing into %s' % os.path.basename(full)) - copy_dir((source[0], full), dest_full, variables, out_) - continue - elif not use_pkg_resources and os.path.isdir(full): + if pkg_resources.resource_isdir(source[0], full): out('Recursing into %s' % os.path.basename(full)) - copy_dir(full, dest_full, variables, out_) + copy_dir((source[0], full), dest_full, variables, out_, i + 1) continue - elif use_pkg_resources: - content = pkg_resources.resource_string(source[0], full) else: - f = open(full, 'rb') - content = f.read() - f.close() + content = pkg_resources.resource_string(source[0], full) if sub_file: content = render_template(content, variables) if content is None: continue # pragma: no cover - if use_pkg_resources: - out('Copying %s to %s' % (full, dest_full)) - else: - out('Copying %s to %s' % ( - os.path.basename(full), - dest_full) - ) + out('Copying %s to %s' % (full, dest_full)) f = open(dest_full, 'wb') f.write(content) |