[WebUI] Refactor server.get_scripts

* The directory list is now sorted so will always produce the same output.
 * Code is now shared with minify script, with some minor changes.
This commit is contained in:
Calum Lind 2015-09-04 14:55:47 +01:00
parent da1c07ff99
commit d898ba9333
2 changed files with 39 additions and 50 deletions

View File

@ -320,54 +320,50 @@ class ScriptResource(resource.Resource, component.Component):
:keyword type: The type of scripts to get (normal, debug, dev) :keyword type: The type of scripts to get (normal, debug, dev)
:param type: string :param type: string
""" """
scripts = []
if type not in ("dev", "debug", "normal"): if type not in ("dev", "debug", "normal"):
type = 'normal' type = 'normal'
_scripts = self.__scripts[type]["scripts"] _scripts = self.__scripts[type]["scripts"]
_order = self.__scripts[type]["order"] _order = self.__scripts[type]["order"]
scripts = []
for path in _order: for path in _order:
filepath = _scripts[path] # Index for grouping the scripts when inserting.
script_idx = len(scripts)
# this is a folder # A folder resource is enclosed in a tuple.
if isinstance(filepath, tuple): if isinstance(_scripts[path], tuple):
filepath, recurse = filepath filepath, recurse = _scripts[path]
if recurse: for root, dirnames, filenames in os.walk(filepath):
for dirpath, dirnames, filenames in os.walk(filepath, False): dirnames.sort(reverse=True)
files = fnmatch.filter(filenames, "*.js") files = fnmatch.filter(filenames, "*.js")
files.sort() files.sort()
order_file = os.path.join(dirpath, '.order') order_file = os.path.join(root, ".order")
if os.path.isfile(order_file): if os.path.isfile(order_file):
for line in open(order_file, 'rb'): with open(order_file, "r") as _file:
line = line.strip() for line in _file:
if not line or line[0] == '#': if line.startswith("+ "):
continue order_filename = line.split()[1]
try: files.pop(files.index(order_filename))
pos, filename = line.split() files.insert(0, order_filename)
files.pop(files.index(filename))
if pos == '+':
files.insert(0, filename)
else:
files.append(filename)
except:
pass
dirpath = dirpath[len(filepath) + 1:] # Ensure sub-directory scripts are top of list with root directory scripts bottom.
if dirpath: if dirnames:
scripts.extend(['js/' + path + '/' + dirpath + '/' + f for f in files]) scripts.extend(["js/" + os.path.basename(root) + "/" + f for f in files])
else: else:
scripts.extend(['js/' + path + '/' + f for f in files]) dirpath = os.path.basename(os.path.dirname(root)) + "/" + os.path.basename(root)
else: for filename in reversed(files):
files = fnmatch.filter(os.listdir('.'), "*.js") scripts.insert(script_idx, "js/" + dirpath + "/" + filename)
if not recurse:
break
else: else:
scripts.append("js/" + path) scripts.append("js/" + path)
return scripts return scripts
def getChild(self, path, request): # NOQA def getChild(self, path, request): # NOQA
if hasattr(request, "lookup_path"): if hasattr(request, "lookup_path"):
request.lookup_path += '/' + path request.lookup_path += "/" + path
else: else:
request.lookup_path = path request.lookup_path = path
return self return self

View File

@ -46,35 +46,28 @@ else:
def source_files_list(source_dir): def source_files_list(source_dir):
scripts = []
src_file_list = []
for root, dirnames, filenames in os.walk(source_dir): for root, dirnames, filenames in os.walk(source_dir):
dirnames.sort(reverse=True) dirnames.sort(reverse=True)
filenames_js = fnmatch.filter(filenames, '*.js') files = fnmatch.filter(filenames, '*.js')
filenames_js.sort() files.sort()
order_file = os.path.join(root, '.order') order_file = os.path.join(root, '.order')
if os.path.isfile(order_file): if os.path.isfile(order_file):
with open(order_file, 'r') as _file: with open(order_file, 'r') as _file:
for line in _file: for line in _file:
line = line.strip() if line.startswith('+ '):
if not line or line[0] == '#': order_filename = line.split()[1]
continue files.pop(files.index(order_filename))
order_pos, order_filename = line.split() files.insert(0, order_filename)
filenames_js.pop(filenames_js.index(order_filename))
if order_pos == '+':
filenames_js.insert(0, order_filename)
# Ensure root directory files are bottom of list. # Ensure root directory files are bottom of list.
if dirnames: if dirnames:
for filename in filenames_js: scripts.extend([os.path.join(root, f) for f in files])
src_file_list.append(os.path.join(root, filename))
else: else:
for filename in reversed(filenames_js): for filename in reversed(files):
src_file_list.insert(0, os.path.join(root, filename)) scripts.insert(0, os.path.join(root, filename))
return scripts
return src_file_list
def concat_src_files(file_list, fileout_path): def concat_src_files(file_list, fileout_path):