Debian Codegraph @

django/core/management/utils.py

raw
1 import fnmatch
2 import os
3 import shutil
4 import subprocess
5 import sys
6 import traceback
7 from pathlib import Path
8 from subprocess import run
9
10 from django.apps import apps as installed_apps
11 from django.utils.crypto import get_random_string
12 from django.utils.encoding import DEFAULT_LOCALE_ENCODING
13
14 from .base import CommandError, CommandParser
15
16
17 def popen_wrapper(args, stdout_encoding="utf-8"):
18 """
19 Friendly wrapper around Popen.
20
21 Return stdout output, stderr output, and OS status code.
22 """
23 try:
24 p = run(args, capture_output=True, close_fds=os.name != "nt")
25 except OSError as err:
26 raise CommandError("Error executing %s" % args[0]) from err
27 return (
28 p.stdout.decode(stdout_encoding),
29 p.stderr.decode(DEFAULT_LOCALE_ENCODING, errors="replace"),
30 p.returncode,
31 )
32
33
34 def handle_extensions(extensions):
35 """
36 Organize multiple extensions that are separated with commas or passed by
37 using --extension/-e multiple times.
38
39 For example: running 'django-admin makemessages -e js,txt -e xhtml -a'
40 would result in an extension list: ['.js', '.txt', '.xhtml']
41
42 >>> handle_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py'])
43 {'.html', '.js', '.py'}
44 >>> handle_extensions(['.html, txt,.tpl'])
45 {'.html', '.tpl', '.txt'}
46 """
47 ext_list = []
48 for ext in extensions:
49 ext_list.extend(ext.replace(" ", "").split(","))
50 for i, ext in enumerate(ext_list):
51 if not ext.startswith("."):
52 ext_list[i] = ".%s" % ext_list[i]
53 return set(ext_list)
54
55
56 def find_command(cmd, path=None, pathext=None):
57 if path is None:
58 path = os.environ.get("PATH", "").split(os.pathsep)
59 if isinstance(path, str):
60 path = [path]
61 # check if there are funny path extensions for executables, e.g. Windows
62 if pathext is None:
63 pathext = os.environ.get("PATHEXT", ".COM;.EXE;.BAT;.CMD").split(os.pathsep)
64 # don't use extensions if the command ends with one of them
65 for ext in pathext:
66 if cmd.endswith(ext):
67 pathext = [""]
68 break
69 # check if we find the command on PATH
70 for p in path:
71 f = os.path.join(p, cmd)
72 if os.path.isfile(f):
73 return f
74 for ext in pathext:
75 fext = f + ext
76 if os.path.isfile(fext):
77 return fext
78 return None
79
80
81 def get_random_secret_key():
82 """
83 Return a 50 character random string usable as a SECRET_KEY setting value.
84 """
85 chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)"
86 return get_random_string(50, chars)
87
88
89 def parse_apps_and_model_labels(labels):
90 """
91 Parse a list of "app_label.ModelName" or "app_label" strings into actual
92 objects and return a two-element tuple:
93 (set of model classes, set of app_configs).
94 Raise a CommandError if some specified models or apps don't exist.
95 """
96 apps = set()
97 models = set()
98
99 for label in labels:
100 if "." in label:
101 try:
102 model = installed_apps.get_model(label)
103 except LookupError:
104 raise CommandError("Unknown model: %s" % label)
105 models.add(model)
106 else:
107 try:
108 app_config = installed_apps.get_app_config(label)
109 except LookupError as e:
110 raise CommandError(str(e))
111 apps.add(app_config)
112
113 return models, apps
114
115
116 def get_command_line_option(argv, option):
117 """
118 Return the value of a command line option (which should include leading
119 dashes, e.g. '--testrunner') from an argument list. Return None if the
120 option wasn't passed or if the argument list couldn't be parsed.
121 """
122 parser = CommandParser(add_help=False, allow_abbrev=False)
123 parser.add_argument(option, dest="value")
124 try:
125 options, _ = parser.parse_known_args(argv[2:])
126 except CommandError:
127 return None
128 else:
129 return options.value
130
131
132 def normalize_path_patterns(patterns):
133 """Normalize an iterable of glob style patterns based on OS."""
134 patterns = [os.path.normcase(p) for p in patterns]
135 dir_suffixes = {"%s*" % path_sep for path_sep in {"/", os.sep}}
136 norm_patterns = []
137 for pattern in patterns:
138 for dir_suffix in dir_suffixes:
139 if pattern.endswith(dir_suffix):
140 norm_patterns.append(pattern.removesuffix(dir_suffix))
141 break
142 else:
143 norm_patterns.append(pattern)
144 return norm_patterns
145
146
147 def is_ignored_path(path, ignore_patterns):
148 """
149 Check if the given path should be ignored or not based on matching
150 one of the glob style `ignore_patterns`.
151 """
152 path = Path(path)
153
154 def ignore(pattern):
155 return fnmatch.fnmatchcase(path.name, pattern) or fnmatch.fnmatchcase(
156 str(path), pattern
157 )
158
159 return any(ignore(pattern) for pattern in normalize_path_patterns(ignore_patterns))
160
161
162 def find_formatters():
163 return {"black_path": shutil.which("black")}
164
165
166 def run_formatters(written_files, black_path=(sentinel := object()), stderr=sys.stderr):
167 """
168 Run the black formatter on the specified files.
169 """
170 # Use a sentinel rather than None, as which() returns None when not found.
171 if black_path is sentinel:
172 black_path = shutil.which("black")
173 if black_path:
174 try:
175 subprocess.run(
176 [black_path, "--fast", "--", *written_files],
177 capture_output=True,
178 )
179 except OSError:
180 stderr.write("Formatters failed to launch:")
181 traceback.print_exc(file=stderr)