1
- from io import open
2
-
3
- import subprocess
4
- from subprocess import call
1
+ import json
5
2
import os
6
3
import shutil
4
+ import subprocess
7
5
import time
8
- import json
9
- from npm_mjs .paths import PROJECT_PATH , TRANSPILE_CACHE_PATH , STATIC_ROOT
10
- from npm_mjs .tools import set_last_run
11
-
6
+ from subprocess import call
12
7
from urllib .parse import urljoin
13
- from django . core . management . base import BaseCommand
14
- from django .contrib . staticfiles import finders
8
+
9
+ from django .apps import apps
15
10
from django .conf import settings
11
+ from django .contrib .staticfiles import finders
12
+ from django .core .management .base import BaseCommand
16
13
from django .templatetags .static import PrefixNode
17
- from django .apps import apps
18
14
19
- from .npm_install import install_npm
20
15
from .collectstatic import Command as CSCommand
16
+ from .npm_install import install_npm
21
17
from npm_mjs import signals
18
+ from npm_mjs .paths import PROJECT_PATH
19
+ from npm_mjs .paths import STATIC_ROOT
20
+ from npm_mjs .paths import TRANSPILE_CACHE_PATH
21
+ from npm_mjs .tools import set_last_run
22
22
23
23
# Run this script every time you update an *.mjs file or any of the
24
24
# modules it loads.
28
28
WEBPACK_CONFIG_JS_PATH = os .path .join (TRANSPILE_CACHE_PATH , "webpack.config.js" )
29
29
30
30
try :
31
- with open (WEBPACK_CONFIG_JS_PATH , "r" ) as file :
31
+ with open (WEBPACK_CONFIG_JS_PATH ) as file :
32
32
OLD_WEBPACK_CONFIG_JS = file .read ()
33
- except IOError :
33
+ except OSError :
34
34
pass
35
35
36
36
@@ -67,7 +67,7 @@ def handle(self, *args, **options):
67
67
if os .path .exists (transpile_path ):
68
68
files = []
69
69
for js_path in js_paths :
70
- for root , dirnames , filenames in os .walk (js_path ):
70
+ for root , _dirnames , filenames in os .walk (js_path ):
71
71
for filename in filenames :
72
72
files .append (os .path .join (root , filename ))
73
73
newest_file = max (files , key = os .path .getmtime )
@@ -90,12 +90,10 @@ def handle(self, *args, **options):
90
90
os .makedirs (out_dir )
91
91
with open (os .path .join (transpile_path , "README.txt" ), "w" ) as f :
92
92
f .write (
93
- (
94
- "These files have been automatically generated. "
95
- "DO NOT EDIT THEM! \n Changes will be overwritten. Edit "
96
- "the original files in one of the django apps, and run "
97
- "./manage.py transpile."
98
- )
93
+ "These files have been automatically generated. "
94
+ "DO NOT EDIT THEM! \n Changes will be overwritten. Edit "
95
+ "the original files in one of the django apps, and run "
96
+ "./manage.py transpile." ,
99
97
)
100
98
101
99
mainfiles = []
@@ -104,15 +102,15 @@ def handle(self, *args, **options):
104
102
for path in js_paths :
105
103
for mainfile in (
106
104
subprocess .check_output (
107
- ["find" , path , "-type" , "f" , "-name" , "*.mjs" , "-print" ]
105
+ ["find" , path , "-type" , "f" , "-name" , "*.mjs" , "-print" ],
108
106
)
109
107
.decode ("utf-8" )
110
108
.split ("\n " )[:- 1 ]
111
109
):
112
110
mainfiles .append (mainfile )
113
111
for sourcefile in (
114
112
subprocess .check_output (
115
- ["find" , path , "-type" , "f" , "-wholename" , "*js" ]
113
+ ["find" , path , "-type" , "f" , "-wholename" , "*js" ],
116
114
)
117
115
.decode ("utf-8" )
118
116
.split ("\n " )[:- 1 ]
@@ -176,7 +174,7 @@ def handle(self, *args, **options):
176
174
index_file .write (index_js )
177
175
index_file .close ()
178
176
else :
179
- index_file = open (outfile , "r" )
177
+ index_file = open (outfile )
180
178
old_index_js = index_file .read ()
181
179
index_file .close ()
182
180
if old_index_js != index_js :
@@ -227,7 +225,7 @@ def handle(self, *args, **options):
227
225
"ignore_patterns" : ["js/" , "admin/" ],
228
226
"use_default_ignore_patterns" : True ,
229
227
"post_process" : True ,
230
- }
228
+ },
231
229
)
232
230
found_files = find_static .collect ()
233
231
static_frontend_files = (
@@ -240,11 +238,11 @@ def handle(self, *args, **options):
240
238
"VERSION" : start ,
241
239
"BASE_URL" : transpile_base_url ,
242
240
"ENTRIES" : entries ,
243
- "STATIC_FRONTEND_FILES" : list (
244
- map ( lambda x : urljoin (static_base_url , x ), static_frontend_files )
245
- ) ,
241
+ "STATIC_FRONTEND_FILES" : [
242
+ urljoin (static_base_url , x ) for x in static_frontend_files
243
+ ] ,
246
244
}
247
- with open (webpack_config_template_path , "r" ) as f :
245
+ with open (webpack_config_template_path ) as f :
248
246
webpack_config_template = f .read ()
249
247
settings_dict = {}
250
248
for var in dir (settings ):
@@ -256,7 +254,8 @@ def handle(self, *args, **options):
256
254
except AttributeError :
257
255
pass
258
256
webpack_config_js = webpack_config_template .replace (
259
- "window.transpile" , json .dumps (transpile )
257
+ "window.transpile" ,
258
+ json .dumps (transpile ),
260
259
).replace ("window.settings" , json .dumps (settings_dict , default = lambda x : False ))
261
260
262
261
if webpack_config_js is not OLD_WEBPACK_CONFIG_JS :
0 commit comments