Skip to content

Commit

Permalink
Made sure the new project template functionality works when the template
Browse files Browse the repository at this point in the history
path specified has a trailing path separator.

Thanks Alex for the report.

Fixes #17475.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17287 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Dec 29, 2011
1 parent 3367913 commit f185024
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
23 changes: 12 additions & 11 deletions django/core/management/templates.py
Expand Up @@ -160,7 +160,7 @@ def handle(self, app_or_project, name, target=None, **options):
if self.verbosity >= 2: if self.verbosity >= 2:
self.stdout.write("Cleaning up temporary files.\n") self.stdout.write("Cleaning up temporary files.\n")
for path_to_remove in self.paths_to_remove: for path_to_remove in self.paths_to_remove:
if os.path.isfile(path_to_remove): if path.isfile(path_to_remove):
os.remove(path_to_remove) os.remove(path_to_remove)
else: else:
shutil.rmtree(path_to_remove, shutil.rmtree(path_to_remove,
Expand All @@ -178,14 +178,15 @@ def handle_template(self, template, subdir):
if template.startswith('file://'): if template.startswith('file://'):
template = template[7:] template = template[7:]
expanded_template = path.expanduser(template) expanded_template = path.expanduser(template)
if os.path.isdir(expanded_template): expanded_template = path.normpath(expanded_template)
if path.isdir(expanded_template):
return expanded_template return expanded_template
if self.is_url(template): if self.is_url(template):
# downloads the file and returns the path # downloads the file and returns the path
absolute_path = self.download(template) absolute_path = self.download(template)
else: else:
absolute_path = path.abspath(expanded_template) absolute_path = path.abspath(expanded_template)
if os.path.exists(absolute_path): if path.exists(absolute_path):
return self.extract(absolute_path) return self.extract(absolute_path)


raise CommandError("couldn't handle %s template %s." % raise CommandError("couldn't handle %s template %s." %
Expand All @@ -203,13 +204,13 @@ def download(self, url):
if self.verbosity >= 2: if self.verbosity >= 2:
self.stdout.write("Downloading %s\n" % url) self.stdout.write("Downloading %s\n" % url)
try: try:
path, info = urllib.urlretrieve(url, the_path, info = urllib.urlretrieve(url,
os.path.join(tempdir, filename)) path.join(tempdir, filename))
except IOError, e: except IOError, e:
raise CommandError("couldn't download URL %s to %s: %s" % raise CommandError("couldn't download URL %s to %s: %s" %
(url, filename, e)) (url, filename, e))


used_name = path.split('/')[-1] used_name = the_path.split('/')[-1]


# Trying to get better name from response headers # Trying to get better name from response headers
content_disposition = info.get('content-disposition') content_disposition = info.get('content-disposition')
Expand All @@ -230,18 +231,18 @@ def download(self, url):
# Move the temporary file to a filename that has better # Move the temporary file to a filename that has better
# chances of being recognnized by the archive utils # chances of being recognnized by the archive utils
if used_name != guessed_filename: if used_name != guessed_filename:
guessed_path = os.path.join(tempdir, guessed_filename) guessed_path = path.join(tempdir, guessed_filename)
shutil.move(path, guessed_path) shutil.move(the_path, guessed_path)
return guessed_path return guessed_path


# Giving up # Giving up
return path return the_path


def splitext(self, path): def splitext(self, the_path):
""" """
Like os.path.splitext, but takes off .tar, too Like os.path.splitext, but takes off .tar, too
""" """
base, ext = posixpath.splitext(path) base, ext = posixpath.splitext(the_path)
if base.lower().endswith('.tar'): if base.lower().endswith('.tar'):
ext = base[-4:] + ext ext = base[-4:] + ext
base = base[:-4] base = base[:-4]
Expand Down
12 changes: 12 additions & 0 deletions tests/regressiontests/admin_scripts/tests.py
Expand Up @@ -1414,6 +1414,18 @@ def test_custom_project_template(self):
self.assertTrue(os.path.isdir(testproject_dir)) self.assertTrue(os.path.isdir(testproject_dir))
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir'))) self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))


def test_template_dir_with_trailing_slash(self):
"Ticket 17475: Template dir passed has a trailing path separator"
template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template' + os.sep)
args = ['startproject', '--template', template_path, 'customtestproject']
testproject_dir = os.path.join(test_dir, 'customtestproject')

out, err = self.run_django_admin(args)
self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))

def test_custom_project_template_from_tarball_by_path(self): def test_custom_project_template_from_tarball_by_path(self):
"Make sure the startproject management command is able to use a different project template from a tarball" "Make sure the startproject management command is able to use a different project template from a tarball"
template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template.tgz') template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template.tgz')
Expand Down
2 changes: 1 addition & 1 deletion tests/urls.py
Expand Up @@ -27,7 +27,7 @@
# admin custom URL tests # admin custom URL tests
(r'^custom_urls/', include('regressiontests.admin_custom_urls.urls')), (r'^custom_urls/', include('regressiontests.admin_custom_urls.urls')),


# admin custom URL tests # admin scripts tests
(r'^admin_scripts/', include('regressiontests.admin_scripts.urls')), (r'^admin_scripts/', include('regressiontests.admin_scripts.urls')),


) )

0 comments on commit f185024

Please sign in to comment.