2021-07-24 08:09:03 +08:00
|
|
|
import argparse
|
2011-12-23 06:38:02 +08:00
|
|
|
import mimetypes
|
|
|
|
import os
|
|
|
|
import posixpath
|
|
|
|
import shutil
|
|
|
|
import stat
|
|
|
|
import tempfile
|
2017-05-30 15:55:38 +08:00
|
|
|
from importlib import import_module
|
2021-11-22 21:08:24 +08:00
|
|
|
from urllib.request import build_opener
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
import django
|
2016-09-29 21:05:23 +08:00
|
|
|
from django.conf import settings
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2022-08-24 17:38:22 +08:00
|
|
|
from django.core.management.utils import (
|
|
|
|
find_formatters,
|
|
|
|
handle_extensions,
|
|
|
|
run_formatters,
|
|
|
|
)
|
2015-02-23 02:14:05 +08:00
|
|
|
from django.template import Context, Engine
|
2016-12-01 18:38:01 +08:00
|
|
|
from django.utils import archive
|
2022-05-10 18:12:17 +08:00
|
|
|
from django.utils.http import parse_header_parameters
|
2014-12-25 20:30:37 +08:00
|
|
|
from django.utils.version import get_docs_version
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TemplateCommand(BaseCommand):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Copy either a Django application layout template or a Django project
|
2011-12-23 06:38:02 +08:00
|
|
|
layout template into the specified directory.
|
|
|
|
|
|
|
|
:param style: A color style object (see django.core.management.color).
|
|
|
|
:param app_or_project: The string 'app' or 'project'.
|
|
|
|
:param name: The name of the application or project.
|
|
|
|
:param directory: The directory to which the template should be copied.
|
|
|
|
:param options: The additional variables passed to project or app templates
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2020-05-14 06:00:41 +08:00
|
|
|
requires_system_checks = []
|
2011-12-23 06:38:02 +08:00
|
|
|
# The supported URL schemes
|
|
|
|
url_schemes = ["http", "https", "ftp"]
|
2016-01-24 17:06:01 +08:00
|
|
|
# Rewrite the following suffixes when determining the target filename.
|
|
|
|
rewrite_template_suffixes = (
|
|
|
|
# Allow shipping invalid .py files without byte-compilation.
|
|
|
|
(".py-tpl", ".py"),
|
|
|
|
)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument("name", help="Name of the application or project.")
|
|
|
|
parser.add_argument(
|
|
|
|
"directory", nargs="?", help="Optional destination directory"
|
|
|
|
)
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--template", help="The path or URL to load the template from."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--extension",
|
|
|
|
"-e",
|
|
|
|
dest="extensions",
|
2014-06-07 04:39:33 +08:00
|
|
|
action="append",
|
|
|
|
default=["py"],
|
|
|
|
help='The file extension(s) to render (default: "py"). '
|
|
|
|
"Separate multiple extensions with commas, or use "
|
2016-03-29 06:33:29 +08:00
|
|
|
"-e multiple times.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--name",
|
|
|
|
"-n",
|
|
|
|
dest="files",
|
2014-06-07 04:39:33 +08:00
|
|
|
action="append",
|
|
|
|
default=[],
|
2018-01-02 08:31:27 +08:00
|
|
|
help="The file name(s) to render. Separate multiple file names "
|
2016-03-29 06:33:29 +08:00
|
|
|
"with commas, or use -n multiple times.",
|
|
|
|
)
|
2021-07-24 08:09:03 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--exclude",
|
|
|
|
"-x",
|
|
|
|
action="append",
|
|
|
|
default=argparse.SUPPRESS,
|
|
|
|
nargs="?",
|
|
|
|
const="",
|
|
|
|
help=(
|
|
|
|
"The directory name(s) to exclude, in addition to .git and "
|
|
|
|
"__pycache__. Can be used multiple times."
|
|
|
|
),
|
|
|
|
)
|
2014-06-07 04:39:33 +08:00
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
def handle(self, app_or_project, name, target=None, **options):
|
2022-02-08 19:38:43 +08:00
|
|
|
self.written_files = []
|
2011-12-23 06:38:02 +08:00
|
|
|
self.app_or_project = app_or_project
|
2019-03-30 22:14:07 +08:00
|
|
|
self.a_or_an = "an" if app_or_project == "app" else "a"
|
2011-12-23 06:38:02 +08:00
|
|
|
self.paths_to_remove = []
|
2014-06-07 04:39:33 +08:00
|
|
|
self.verbosity = options["verbosity"]
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2019-03-30 22:14:07 +08:00
|
|
|
self.validate_name(name)
|
2011-12-30 03:06:57 +08:00
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
# if some directory is given, make sure it's nicely expanded
|
|
|
|
if target is None:
|
2019-08-19 19:27:13 +08:00
|
|
|
top_dir = os.path.join(os.getcwd(), name)
|
2012-01-05 07:55:34 +08:00
|
|
|
try:
|
|
|
|
os.makedirs(top_dir)
|
2017-01-25 23:13:08 +08:00
|
|
|
except FileExistsError:
|
|
|
|
raise CommandError("'%s' already exists" % top_dir)
|
2012-04-29 00:09:37 +08:00
|
|
|
except OSError as e:
|
2017-01-25 23:13:08 +08:00
|
|
|
raise CommandError(e)
|
2011-12-23 06:38:02 +08:00
|
|
|
else:
|
2019-08-19 19:27:13 +08:00
|
|
|
top_dir = os.path.abspath(os.path.expanduser(target))
|
2021-05-11 18:39:28 +08:00
|
|
|
if app_or_project == "app":
|
|
|
|
self.validate_name(os.path.basename(top_dir), "directory")
|
2012-03-22 06:29:32 +08:00
|
|
|
if not os.path.exists(top_dir):
|
|
|
|
raise CommandError(
|
|
|
|
"Destination directory '%s' does not "
|
|
|
|
"exist, please create it first." % top_dir
|
|
|
|
)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2022-08-24 17:38:22 +08:00
|
|
|
# Find formatters, which are external executables, before input
|
|
|
|
# from the templates can sneak into the path.
|
|
|
|
formatter_paths = find_formatters()
|
|
|
|
|
2014-11-17 16:24:56 +08:00
|
|
|
extensions = tuple(handle_extensions(options["extensions"]))
|
2012-02-04 21:01:30 +08:00
|
|
|
extra_files = []
|
2021-07-24 08:09:03 +08:00
|
|
|
excluded_directories = [".git", "__pycache__"]
|
2014-06-07 04:39:33 +08:00
|
|
|
for file in options["files"]:
|
2012-02-04 21:01:30 +08:00
|
|
|
extra_files.extend(map(lambda x: x.strip(), file.split(",")))
|
2021-07-24 08:09:03 +08:00
|
|
|
if exclude := options.get("exclude"):
|
|
|
|
for directory in exclude:
|
|
|
|
excluded_directories.append(directory.strip())
|
2011-12-23 06:38:02 +08:00
|
|
|
if self.verbosity >= 2:
|
2020-04-25 21:51:17 +08:00
|
|
|
self.stdout.write(
|
|
|
|
"Rendering %s template files with extensions: %s"
|
|
|
|
% (app_or_project, ", ".join(extensions))
|
|
|
|
)
|
|
|
|
self.stdout.write(
|
|
|
|
"Rendering %s template files with filenames: %s"
|
|
|
|
% (app_or_project, ", ".join(extra_files))
|
|
|
|
)
|
2011-12-23 06:38:02 +08:00
|
|
|
base_name = "%s_name" % app_or_project
|
|
|
|
base_subdir = "%s_template" % app_or_project
|
|
|
|
base_directory = "%s_directory" % app_or_project
|
2015-06-13 21:41:56 +08:00
|
|
|
camel_case_name = "camel_case_%s_name" % app_or_project
|
|
|
|
camel_case_value = "".join(x for x in name.title() if x != "_")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2017-12-11 20:08:45 +08:00
|
|
|
context = Context(
|
|
|
|
{
|
|
|
|
**options,
|
2011-12-23 06:38:02 +08:00
|
|
|
base_name: name,
|
|
|
|
base_directory: top_dir,
|
2015-06-13 21:41:56 +08:00
|
|
|
camel_case_name: camel_case_value,
|
2014-12-25 20:30:37 +08:00
|
|
|
"docs_version": get_docs_version(),
|
2014-11-25 03:11:55 +08:00
|
|
|
"django_version": django.__version__,
|
2017-12-11 20:08:45 +08:00
|
|
|
},
|
|
|
|
autoescape=False,
|
|
|
|
)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
# Setup a stub settings environment for template rendering
|
|
|
|
if not settings.configured:
|
|
|
|
settings.configure()
|
2016-09-29 21:05:23 +08:00
|
|
|
django.setup()
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
template_dir = self.handle_template(options["template"], base_subdir)
|
2011-12-23 06:38:02 +08:00
|
|
|
prefix_length = len(template_dir) + 1
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(template_dir):
|
|
|
|
|
|
|
|
path_rest = root[prefix_length:]
|
|
|
|
relative_dir = path_rest.replace(base_name, name)
|
|
|
|
if relative_dir:
|
2019-08-19 19:27:13 +08:00
|
|
|
target_dir = os.path.join(top_dir, relative_dir)
|
2019-01-31 23:12:55 +08:00
|
|
|
os.makedirs(target_dir, exist_ok=True)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
for dirname in dirs[:]:
|
2021-07-24 08:09:03 +08:00
|
|
|
if "exclude" not in options:
|
|
|
|
if dirname.startswith(".") or dirname == "__pycache__":
|
|
|
|
dirs.remove(dirname)
|
|
|
|
elif dirname in excluded_directories:
|
2011-12-23 06:38:02 +08:00
|
|
|
dirs.remove(dirname)
|
|
|
|
|
|
|
|
for filename in files:
|
|
|
|
if filename.endswith((".pyo", ".pyc", ".py.class")):
|
|
|
|
# Ignore some files as they cause various breakages.
|
|
|
|
continue
|
2019-08-19 19:27:13 +08:00
|
|
|
old_path = os.path.join(root, filename)
|
|
|
|
new_path = os.path.join(
|
|
|
|
top_dir, relative_dir, filename.replace(base_name, name)
|
|
|
|
)
|
2016-01-24 17:06:01 +08:00
|
|
|
for old_suffix, new_suffix in self.rewrite_template_suffixes:
|
|
|
|
if new_path.endswith(old_suffix):
|
|
|
|
new_path = new_path[: -len(old_suffix)] + new_suffix
|
|
|
|
break # Only rewrite once
|
|
|
|
|
2019-08-19 19:27:13 +08:00
|
|
|
if os.path.exists(new_path):
|
2019-03-30 22:14:07 +08:00
|
|
|
raise CommandError(
|
|
|
|
"%s already exists. Overlaying %s %s into an existing "
|
|
|
|
"directory won't replace conflicting files."
|
|
|
|
% (
|
|
|
|
new_path,
|
|
|
|
self.a_or_an,
|
|
|
|
app_or_project,
|
|
|
|
)
|
|
|
|
)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
# Only render the Python files, as we don't want to
|
|
|
|
# accidentally render Django templates files
|
2016-01-24 17:06:01 +08:00
|
|
|
if new_path.endswith(extensions) or filename in extra_files:
|
2019-01-28 00:30:20 +08:00
|
|
|
with open(old_path, encoding="utf-8") as template_file:
|
2016-10-20 22:59:01 +08:00
|
|
|
content = template_file.read()
|
2015-02-23 02:14:05 +08:00
|
|
|
template = Engine().from_string(content)
|
2011-12-23 06:38:02 +08:00
|
|
|
content = template.render(context)
|
2017-01-19 05:05:41 +08:00
|
|
|
with open(new_path, "w", encoding="utf-8") as new_file:
|
2016-10-20 22:59:01 +08:00
|
|
|
new_file.write(content)
|
|
|
|
else:
|
|
|
|
shutil.copyfile(old_path, new_path)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2022-02-08 19:38:43 +08:00
|
|
|
self.written_files.append(new_path)
|
2011-12-23 06:38:02 +08:00
|
|
|
if self.verbosity >= 2:
|
2020-04-25 21:51:17 +08:00
|
|
|
self.stdout.write("Creating %s" % new_path)
|
2011-12-23 06:38:02 +08:00
|
|
|
try:
|
2021-11-19 22:15:24 +08:00
|
|
|
self.apply_umask(old_path, new_path)
|
2011-12-23 06:38:02 +08:00
|
|
|
self.make_writeable(new_path)
|
|
|
|
except OSError:
|
2012-05-19 19:51:54 +08:00
|
|
|
self.stderr.write(
|
2011-12-23 06:38:02 +08:00
|
|
|
"Notice: Couldn't set permission bits on %s. You're "
|
|
|
|
"probably using an uncommon filesystem setup. No "
|
2012-05-19 19:51:54 +08:00
|
|
|
"problem." % new_path,
|
|
|
|
self.style.NOTICE,
|
|
|
|
)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
if self.paths_to_remove:
|
|
|
|
if self.verbosity >= 2:
|
2020-04-25 21:51:17 +08:00
|
|
|
self.stdout.write("Cleaning up temporary files.")
|
2011-12-23 06:38:02 +08:00
|
|
|
for path_to_remove in self.paths_to_remove:
|
2019-08-19 19:27:13 +08:00
|
|
|
if os.path.isfile(path_to_remove):
|
2011-12-23 06:38:02 +08:00
|
|
|
os.remove(path_to_remove)
|
|
|
|
else:
|
2015-02-23 00:49:29 +08:00
|
|
|
shutil.rmtree(path_to_remove)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2022-08-24 17:38:22 +08:00
|
|
|
run_formatters(self.written_files, **formatter_paths)
|
2022-02-08 19:38:43 +08:00
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
def handle_template(self, template, subdir):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Determine where the app or project templates are.
|
|
|
|
Use django.__path__[0] as the default because the Django install
|
|
|
|
directory isn't known.
|
2011-12-23 06:38:02 +08:00
|
|
|
"""
|
|
|
|
if template is None:
|
2019-08-19 19:27:13 +08:00
|
|
|
return os.path.join(django.__path__[0], "conf", subdir)
|
2011-12-23 06:38:02 +08:00
|
|
|
else:
|
|
|
|
if template.startswith("file://"):
|
|
|
|
template = template[7:]
|
2019-08-19 19:27:13 +08:00
|
|
|
expanded_template = os.path.expanduser(template)
|
|
|
|
expanded_template = os.path.normpath(expanded_template)
|
|
|
|
if os.path.isdir(expanded_template):
|
2011-12-23 06:38:02 +08:00
|
|
|
return expanded_template
|
|
|
|
if self.is_url(template):
|
|
|
|
# downloads the file and returns the path
|
|
|
|
absolute_path = self.download(template)
|
|
|
|
else:
|
2019-08-19 19:27:13 +08:00
|
|
|
absolute_path = os.path.abspath(expanded_template)
|
|
|
|
if os.path.exists(absolute_path):
|
2011-12-23 06:38:02 +08:00
|
|
|
return self.extract(absolute_path)
|
|
|
|
|
|
|
|
raise CommandError(
|
|
|
|
"couldn't handle %s template %s." % (self.app_or_project, template)
|
|
|
|
)
|
|
|
|
|
2019-04-25 10:28:31 +08:00
|
|
|
def validate_name(self, name, name_or_dir="name"):
|
2012-10-13 22:05:34 +08:00
|
|
|
if name is None:
|
2017-05-30 15:55:38 +08:00
|
|
|
raise CommandError(
|
|
|
|
"you must provide {an} {app} name".format(
|
2019-03-30 22:14:07 +08:00
|
|
|
an=self.a_or_an,
|
|
|
|
app=self.app_or_project,
|
2017-05-30 15:55:38 +08:00
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2017-05-30 15:55:38 +08:00
|
|
|
# Check it's a valid directory name.
|
2016-12-01 18:38:01 +08:00
|
|
|
if not name.isidentifier():
|
|
|
|
raise CommandError(
|
2019-04-25 10:28:31 +08:00
|
|
|
"'{name}' is not a valid {app} {type}. Please make sure the "
|
|
|
|
"{type} is a valid identifier.".format(
|
2017-05-30 15:55:38 +08:00
|
|
|
name=name,
|
2019-03-30 22:14:07 +08:00
|
|
|
app=self.app_or_project,
|
2019-04-25 10:28:31 +08:00
|
|
|
type=name_or_dir,
|
2017-05-30 15:55:38 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
# Check it cannot be imported.
|
|
|
|
try:
|
|
|
|
import_module(name)
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise CommandError(
|
|
|
|
"'{name}' conflicts with the name of an existing Python "
|
2019-04-25 10:28:31 +08:00
|
|
|
"module and cannot be used as {an} {app} {type}. Please try "
|
|
|
|
"another {type}.".format(
|
2017-05-30 15:55:38 +08:00
|
|
|
name=name,
|
2019-03-30 22:14:07 +08:00
|
|
|
an=self.a_or_an,
|
|
|
|
app=self.app_or_project,
|
2019-04-25 10:28:31 +08:00
|
|
|
type=name_or_dir,
|
2017-05-30 15:55:38 +08:00
|
|
|
)
|
2016-12-01 18:38:01 +08:00
|
|
|
)
|
2012-10-13 22:05:34 +08:00
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
def download(self, url):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Download the given URL and return the file name.
|
2011-12-23 06:38:02 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2012-01-02 01:45:40 +08:00
|
|
|
def cleanup_url(url):
|
|
|
|
tmp = url.rstrip("/")
|
|
|
|
filename = tmp.split("/")[-1]
|
|
|
|
if url.endswith("/"):
|
2013-10-22 21:31:43 +08:00
|
|
|
display_url = tmp + "/"
|
2012-01-02 01:45:40 +08:00
|
|
|
else:
|
|
|
|
display_url = url
|
|
|
|
return filename, display_url
|
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
prefix = "django_%s_template_" % self.app_or_project
|
|
|
|
tempdir = tempfile.mkdtemp(prefix=prefix, suffix="_download")
|
|
|
|
self.paths_to_remove.append(tempdir)
|
2012-01-02 01:45:40 +08:00
|
|
|
filename, display_url = cleanup_url(url)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
if self.verbosity >= 2:
|
2020-04-25 21:51:17 +08:00
|
|
|
self.stdout.write("Downloading %s" % display_url)
|
2021-11-22 21:08:24 +08:00
|
|
|
|
|
|
|
the_path = os.path.join(tempdir, filename)
|
|
|
|
opener = build_opener()
|
|
|
|
opener.addheaders = [("User-Agent", f"Django/{django.__version__}")]
|
2011-12-23 06:38:02 +08:00
|
|
|
try:
|
2021-11-22 21:08:24 +08:00
|
|
|
with opener.open(url) as source, open(the_path, "wb") as target:
|
|
|
|
headers = source.info()
|
|
|
|
target.write(source.read())
|
2019-01-28 23:01:35 +08:00
|
|
|
except OSError as e:
|
2011-12-23 06:38:02 +08:00
|
|
|
raise CommandError(
|
|
|
|
"couldn't download URL %s to %s: %s" % (url, filename, e)
|
|
|
|
)
|
|
|
|
|
2011-12-30 01:03:38 +08:00
|
|
|
used_name = the_path.split("/")[-1]
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
# Trying to get better name from response headers
|
2021-11-22 21:08:24 +08:00
|
|
|
content_disposition = headers["content-disposition"]
|
2011-12-23 06:38:02 +08:00
|
|
|
if content_disposition:
|
2022-05-10 18:12:17 +08:00
|
|
|
_, params = parse_header_parameters(content_disposition)
|
2011-12-23 06:38:02 +08:00
|
|
|
guessed_filename = params.get("filename") or used_name
|
|
|
|
else:
|
|
|
|
guessed_filename = used_name
|
|
|
|
|
|
|
|
# Falling back to content type guessing
|
|
|
|
ext = self.splitext(guessed_filename)[1]
|
2021-11-22 21:08:24 +08:00
|
|
|
content_type = headers["content-type"]
|
2011-12-23 06:38:02 +08:00
|
|
|
if not ext and content_type:
|
|
|
|
ext = mimetypes.guess_extension(content_type)
|
|
|
|
if ext:
|
|
|
|
guessed_filename += ext
|
|
|
|
|
|
|
|
# Move the temporary file to a filename that has better
|
2014-03-02 22:25:53 +08:00
|
|
|
# chances of being recognized by the archive utils
|
2011-12-23 06:38:02 +08:00
|
|
|
if used_name != guessed_filename:
|
2019-08-19 19:27:13 +08:00
|
|
|
guessed_path = os.path.join(tempdir, guessed_filename)
|
2011-12-30 01:03:38 +08:00
|
|
|
shutil.move(the_path, guessed_path)
|
2011-12-23 06:38:02 +08:00
|
|
|
return guessed_path
|
|
|
|
|
|
|
|
# Giving up
|
2011-12-30 01:03:38 +08:00
|
|
|
return the_path
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2011-12-30 01:03:38 +08:00
|
|
|
def splitext(self, the_path):
|
2011-12-23 06:38:02 +08:00
|
|
|
"""
|
|
|
|
Like os.path.splitext, but takes off .tar, too
|
|
|
|
"""
|
2011-12-30 01:03:38 +08:00
|
|
|
base, ext = posixpath.splitext(the_path)
|
2011-12-23 06:38:02 +08:00
|
|
|
if base.lower().endswith(".tar"):
|
|
|
|
ext = base[-4:] + ext
|
|
|
|
base = base[:-4]
|
|
|
|
return base, ext
|
|
|
|
|
|
|
|
def extract(self, filename):
|
|
|
|
"""
|
2020-06-25 03:46:01 +08:00
|
|
|
Extract the given file to a temporary directory and return
|
2011-12-23 06:38:02 +08:00
|
|
|
the path of the directory with the extracted content.
|
|
|
|
"""
|
|
|
|
prefix = "django_%s_template_" % self.app_or_project
|
|
|
|
tempdir = tempfile.mkdtemp(prefix=prefix, suffix="_extract")
|
|
|
|
self.paths_to_remove.append(tempdir)
|
|
|
|
if self.verbosity >= 2:
|
2020-04-25 21:51:17 +08:00
|
|
|
self.stdout.write("Extracting %s" % filename)
|
2011-12-23 06:38:02 +08:00
|
|
|
try:
|
|
|
|
archive.extract(filename, tempdir)
|
|
|
|
return tempdir
|
2019-01-28 23:01:35 +08:00
|
|
|
except (archive.ArchiveException, OSError) as e:
|
2011-12-23 06:38:02 +08:00
|
|
|
raise CommandError(
|
|
|
|
"couldn't extract file %s to %s: %s" % (filename, tempdir, e)
|
|
|
|
)
|
|
|
|
|
|
|
|
def is_url(self, template):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Return True if the name looks like a URL."""
|
2011-12-23 06:38:02 +08:00
|
|
|
if ":" not in template:
|
|
|
|
return False
|
|
|
|
scheme = template.split(":", 1)[0].lower()
|
|
|
|
return scheme in self.url_schemes
|
|
|
|
|
2021-11-19 22:15:24 +08:00
|
|
|
def apply_umask(self, old_path, new_path):
|
|
|
|
current_umask = os.umask(0)
|
|
|
|
os.umask(current_umask)
|
|
|
|
current_mode = stat.S_IMODE(os.stat(old_path).st_mode)
|
|
|
|
os.chmod(new_path, current_mode & ~current_umask)
|
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
def make_writeable(self, filename):
|
|
|
|
"""
|
|
|
|
Make sure that the file is writeable.
|
|
|
|
Useful if our source is read-only.
|
|
|
|
"""
|
|
|
|
if not os.access(filename, os.W_OK):
|
|
|
|
st = os.stat(filename)
|
|
|
|
new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR
|
|
|
|
os.chmod(filename, new_permissions)
|