Fixed #30693 -- Removed separate import of os.path in django.core.management.templates.

This commit is contained in:
Maaz Bin Tahir Saeed 2019-08-19 16:27:13 +05:00 committed by Carlton Gibson
parent 10528a81ed
commit e9f74f53cc
1 changed files with 17 additions and 17 deletions

View File

@ -6,7 +6,6 @@ import shutil
import stat import stat
import tempfile import tempfile
from importlib import import_module from importlib import import_module
from os import path
from urllib.request import urlretrieve from urllib.request import urlretrieve
import django import django
@ -66,7 +65,7 @@ class TemplateCommand(BaseCommand):
# if some directory is given, make sure it's nicely expanded # if some directory is given, make sure it's nicely expanded
if target is None: if target is None:
top_dir = path.join(os.getcwd(), name) top_dir = os.path.join(os.getcwd(), name)
try: try:
os.makedirs(top_dir) os.makedirs(top_dir)
except FileExistsError: except FileExistsError:
@ -76,7 +75,7 @@ class TemplateCommand(BaseCommand):
else: else:
if app_or_project == 'app': if app_or_project == 'app':
self.validate_name(os.path.basename(target), 'directory') self.validate_name(os.path.basename(target), 'directory')
top_dir = os.path.abspath(path.expanduser(target)) top_dir = os.path.abspath(os.path.expanduser(target))
if not os.path.exists(top_dir): if not os.path.exists(top_dir):
raise CommandError("Destination directory '%s' does not " raise CommandError("Destination directory '%s' does not "
"exist, please create it first." % top_dir) "exist, please create it first." % top_dir)
@ -122,7 +121,7 @@ class TemplateCommand(BaseCommand):
path_rest = root[prefix_length:] path_rest = root[prefix_length:]
relative_dir = path_rest.replace(base_name, name) relative_dir = path_rest.replace(base_name, name)
if relative_dir: if relative_dir:
target_dir = path.join(top_dir, relative_dir) target_dir = os.path.join(top_dir, relative_dir)
os.makedirs(target_dir, exist_ok=True) os.makedirs(target_dir, exist_ok=True)
for dirname in dirs[:]: for dirname in dirs[:]:
@ -133,15 +132,16 @@ class TemplateCommand(BaseCommand):
if filename.endswith(('.pyo', '.pyc', '.py.class')): if filename.endswith(('.pyo', '.pyc', '.py.class')):
# Ignore some files as they cause various breakages. # Ignore some files as they cause various breakages.
continue continue
old_path = path.join(root, filename) old_path = os.path.join(root, filename)
new_path = path.join(top_dir, relative_dir, new_path = os.path.join(
filename.replace(base_name, name)) top_dir, relative_dir, filename.replace(base_name, name)
)
for old_suffix, new_suffix in self.rewrite_template_suffixes: for old_suffix, new_suffix in self.rewrite_template_suffixes:
if new_path.endswith(old_suffix): if new_path.endswith(old_suffix):
new_path = new_path[:-len(old_suffix)] + new_suffix new_path = new_path[:-len(old_suffix)] + new_suffix
break # Only rewrite once break # Only rewrite once
if path.exists(new_path): if os.path.exists(new_path):
raise CommandError( raise CommandError(
"%s already exists. Overlaying %s %s into an existing " "%s already exists. Overlaying %s %s into an existing "
"directory won't replace conflicting files." % ( "directory won't replace conflicting files." % (
@ -176,7 +176,7 @@ class TemplateCommand(BaseCommand):
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 path.isfile(path_to_remove): if os.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)
@ -188,20 +188,20 @@ class TemplateCommand(BaseCommand):
directory isn't known. directory isn't known.
""" """
if template is None: if template is None:
return path.join(django.__path__[0], 'conf', subdir) return os.path.join(django.__path__[0], 'conf', subdir)
else: else:
if template.startswith('file://'): if template.startswith('file://'):
template = template[7:] template = template[7:]
expanded_template = path.expanduser(template) expanded_template = os.path.expanduser(template)
expanded_template = path.normpath(expanded_template) expanded_template = os.path.normpath(expanded_template)
if path.isdir(expanded_template): if os.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 = os.path.abspath(expanded_template)
if path.exists(absolute_path): if os.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." %
@ -261,7 +261,7 @@ class TemplateCommand(BaseCommand):
if self.verbosity >= 2: if self.verbosity >= 2:
self.stdout.write("Downloading %s\n" % display_url) self.stdout.write("Downloading %s\n" % display_url)
try: try:
the_path, info = urlretrieve(url, path.join(tempdir, filename)) the_path, info = urlretrieve(url, os.path.join(tempdir, filename))
except OSError as e: except OSError as 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))
@ -287,7 +287,7 @@ class TemplateCommand(BaseCommand):
# Move the temporary file to a filename that has better # Move the temporary file to a filename that has better
# chances of being recognized by the archive utils # chances of being recognized by the archive utils
if used_name != guessed_filename: if used_name != guessed_filename:
guessed_path = path.join(tempdir, guessed_filename) guessed_path = os.path.join(tempdir, guessed_filename)
shutil.move(the_path, guessed_path) shutil.move(the_path, guessed_path)
return guessed_path return guessed_path