Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Non-atomic checkin: everything should be working (it gets alpha statu…

…s, anyway)

Signed-off-by: Peter Baumgartner <pete@lincolnloop.com>
  • Loading branch information...
commit 2e88b25d7bf8bbf032859b2ee61f98db81da8ea4 1 parent 54289c4
Chris Beaven authored ipmb committed
View
3  .gitignore
@@ -0,0 +1,3 @@
+*.pyc
+django_startproject.egg-info
+build
View
9 bin/django-startproject.py
@@ -0,0 +1,9 @@
+#!/usr/bin/env python
+
+def main():
+ from django_startproject.management import start_project
+ start_project()
+
+
+if __name__ == '__main__':
+ main()
View
48 django_startproject/management.py
@@ -0,0 +1,48 @@
+from django_startproject import utils
+import optparse
+import os
+import sys
+
+
+TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.realpath(utils.__file__)),
+ 'project_template')
+
+
+def start_project():
+ usage = "usage: %prog [options] project_name [base_destination_dir]"
+ parser = optparse.OptionParser(usage=usage)
+ parser.add_option('-t', '--template-dir', dest='src_dir',
+ help='project template directory to use',
+ default=TEMPLATE_DIR)
+ options, args = parser.parse_args()
+ if len(args) not in (1, 2):
+ parser.print_help()
+ sys.exit(1)
+ project_name = args[0]
+
+ src = options.src_dir
+ if len(args) > 1:
+ base_dest_dir = args[1]
+ else:
+ base_dest_dir = ''
+ dest = os.path.join(base_dest_dir, project_name)
+
+ # Get any boilerplate replacement variables:
+ replace = {}
+ for var, help in utils.get_boilerplate(src):
+ if var in ('project_name', 'myproject'):
+ default = project_name
+ elif var in ('myauthor',):
+ default = 'Lincoln Loop'
+ else:
+ default = None
+ if default is not None:
+ prompt = '%s [%s]: ' % (help, default)
+ else:
+ prompt = '%s: ' % help
+ value = None
+ while not value:
+ value = raw_input(prompt) or default
+ replace[var] = value
+
+ utils.copy_template(src, dest, replace)
View
4 django_startproject/project_template/.startproject_boilerplate
@@ -0,0 +1,4 @@
+myproject Project name
+myauthor Project author
+mydevhost Development server
+myrepohost Repository location
View
20 django_startproject/show_boilerplate.py
@@ -8,9 +8,9 @@
import os
import re
+from django_startproject import settings
+
-ROOT = os.path.join(os.path.dirname(os.path.realpath(__file__)),
- 'project_template')
EXCLUDE_FILES = []
EXCLUDE_DIRS = []
BOILERPLATE = ['myproject', 'myauthor', 'mydevhost', 'myrepohost']
@@ -25,14 +25,16 @@
print '-' * len(title)
print
- for path, dirs, files in os.walk(ROOT):
+ for path, dirs, files in os.walk(settings.TEMPLATE_DIR):
for d in dirs:
if d in EXCLUDE_DIRS:
dirs.remove(d)
for f in files:
- if f not in EXCLUDE_FILES:
- file_path = os.path.join(path, f)
- contents = open(file_path, 'rb').read()
- bp = re.findall(word, contents)
- if bp:
- print len(bp), file_path.replace(ROOT, '.')
+ if f in EXCLUDE_FILES:
+ continue
+ file_path = os.path.join(path, f)
+ contents = open(file_path, 'r').read()
+ bp = re.findall(word, contents)
+ if bp:
+ print '%2d .%s' % (len(bp),
+ file_path[len(settings.TEMPLATE_DIR):])
View
76 django_startproject/utils.py
@@ -0,0 +1,76 @@
+import os
+import re
+import shutil
+import stat
+
+
+def copy_template(src, dest, replace=None):
+ """
+ Copy all files in the source path to the destination path.
+
+ To replace boilerplate strings in the source data, pass a dictionary to the
+ ``replace`` argument where each key is the boilerplate string and the
+ corresponding value is the string which should replace it.
+ """
+ for path, dirs, files in os.walk(src):
+ relative_path = path[len(src):].lstrip('/')
+ os.mkdir(os.path.join(dest, relative_path))
+ for i, subdir in enumerate(dirs):
+ if subdir.startswith('.'):
+ del dirs[i]
+ for f in files:
+ if f.startswith('.startproject') or f.endswith('.pyc'):
+ continue
+ src_file_path = os.path.join(path, f)
+ dest_file_path = os.path.join(dest, relative_path, f)
+ copy_template_file(src_file_path, dest_file_path, replace)
+
+
+def copy_template_file(src, dest, replace=None):
+ """
+ Copy a source file to a new destination file.
+
+ To replace boilerplate strings in the source data, pass a dictionary to the
+ ``replace`` argument where each key is the boilerplate string and the
+ corresponding value is the string which should replace it.
+ """
+ replace = replace or {}
+ # Read the data from the source file.
+ src_file = open(src, 'r')
+ data = src_file.read()
+ src_file.close()
+ # Replace boilerplate strings.
+ for old_val, new_val in replace.items():
+ data = data.replace(old_val, new_val)
+ # Write the data to the destination file.
+ dest_file = open(dest, 'w')
+ dest_file.write(data)
+ dest_file.close()
+ # Copy permissions from source file.
+ shutil.copymode(src, dest)
+ # Make new file writable.
+ if os.access(dest, os.W_OK):
+ st = os.stat(dest)
+ new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR
+ os.chmod(dest, new_permissions)
+
+
+def get_boilerplate(path):
+ """
+ Look for a ``.startproject_boilerplate`` file the given path and parse it.
+
+ Return a 2-tuple list of the boilerplate variables and optional
+ descriptions.
+
+ If no file was found (or no lines contained boilerplate variables), return
+ an empty list.
+ """
+ boilerplate = []
+ boilerplate_path = os.path.join(path, '.startproject_boilerplate')
+ if os.path.isfile(boilerplate_path):
+ boilerplate_file = open(boilerplate_path, 'r')
+ for line in boilerplate_file:
+ match = re.match(r'\s*(\w+)\s*(.*)$', line)
+ if match:
+ boilerplate.append(match.groups())
+ return boilerplate
View
36 setup.py
@@ -0,0 +1,36 @@
+from distutils.core import setup
+import os
+
+
+ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
+DATA_DIR = os.path.join(ROOT_DIR, 'django_startproject', 'project_template')
+STARTPROJECT_DATA = []
+for path, dirs, filenames in os.walk(DATA_DIR):
+ # Ignore directories that start with '.'
+ for i, dir in enumerate(dirs):
+ if dir.startswith('.'):
+ del dirs[i]
+ path = path[len(DATA_DIR)+1:]
+ STARTPROJECT_DATA.append(os.path.join('project_template', path, '*.*'))
+ # Get files starting with '.' too (they are excluded from the *.* glob).
+ STARTPROJECT_DATA.append(os.path.join('project_template', path, '.*'))
+
+
+setup(name = 'django-startproject',
+ version = '1.0a',
+ author = 'Lincoln Loop',
+ author_email = 'info@lincolnloop.com',
+ description = ('Create a standard Django project layout based on '
+ 'Lincoln Loop standards.'),
+ packages = ['django_startproject'],
+ package_data={'django_startproject': STARTPROJECT_DATA},
+ scripts = ['bin/django-startproject.py'],
+ classifiers = [
+ 'Development Status :: 3 - Alpha',
+ 'Environment :: Web Environment',
+ 'Framework :: Django',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: MIT License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Topic :: Software Development :: Libraries :: Python Modules'])
Please sign in to comment.
Something went wrong with that request. Please try again.