build.py, projects.py: Streamline both scripts

Make build.py and projects.py even more similar in preparation for
merging them.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2018-03-07 10:17:14 +00:00
commit f7db567bff
2 changed files with 45 additions and 38 deletions

View file

@ -17,34 +17,40 @@ import datetime
import re
from os.path import expanduser
class ResultCache(object):
def __init__(self):
self.__cache = {}
def run(self, func, args):
d = self.__cache
depth = 0
keys = [ func.__name__ ] + args
l = len(keys)
for k in keys:
if k is None:
k = 'None'
else:
k = str(k)
depth += 1
#debug('depth = ', depth, 'key = ', k, 'd = ', str(d))
if k in d:
if l == depth:
return d[k]
d = d[k]
continue
if l == depth:
r = func(*args)
d[k] = r
return r
d = d[k] = {}
#d = d[k]
raise Exception("cache algorithm failed for function", func.__name__, "in depth", depth)
def debug(*objs):
if args.debug:
print("DEBUG: ", *objs, file=sys.stderr)
def build_cache_func(func, args):
global build_cache
d = build_cache
depth = 0
keys = [ func.__name__ ] + args
l = len(keys)
for k in keys:
if k is None:
k = 'None'
depth += 1
#debug('depth = ', depth, 'key = ', k, 'd = ', str(d))
if k in d:
if l == depth:
return d[k]
d = d[k]
continue
if l == depth:
r = func(*args)
d[k] = r
return r
d[k] = {}
d = d[k]
raise Exception("cache algorithm failed for function", func.__name__, "in depth", depth)
def build_find_proj_path(name):
name=name.replace("dspider-", "")
search_path=[".", "dspc/src", "dspc/src/dspcd-plugins", "dspc/src/io" ]
@ -55,10 +61,11 @@ def build_find_proj_path(name):
raise Exception("module " + name + " not found below " + projs_root)
def build_find_proj_path_cached(name):
return build_cache_func(build_find_proj_path, [ name ])
return res_cache.run(build_find_proj_path, [ name ])
def build_read_deps(cur, prereq_type):
# dep cache doesn't make a difference at all
projects_py="/usr/bin/python2 " + my_dir + "/projects.py --prefix " + projs_root + " " + os.getenv('PROJECTS_PY_EXTRA_ARGS', "")
if prereq_type in dep_cache:
if cur in dep_cache[prereq_type]:
return dep_cache[prereq_type][cur]
@ -85,7 +92,7 @@ def build_read_deps(cur, prereq_type):
return r
def build_read_deps_cached(cur, prereq_type):
return build_cache_func(build_read_deps, [ cur, prereq_type ])
return res_cache.run(build_read_deps, [ cur, prereq_type ])
def build_add_dep_tree(cur, prereq_types, tree, all_deps):
debug("adding prerequisites " + ' '.join(prereq_types) + " of module " + cur)
@ -225,7 +232,7 @@ def cmd_build(args_):
# -------------------------------------------------------------------- here we go
global_args = []
#cache = ResultCache()
#res_cache = ResultCache()
skip = 0
for a in sys.argv[1::]:
@ -255,18 +262,15 @@ args=parser.parse_args(global_args)
debug("----------------------------------------- running ", ' '.join(sys.argv))
projs_root = args.prefix
res_cache = ResultCache()
dep_cache = {}
my_dir=os.path.dirname(os.path.realpath(__file__))
if args.topdir:
topdir = args.topdir
top_name = cache.run(read_value, [topdir + '/make/project.conf', 'build', 'name'])
top_name = res_cache.run(read_value, [topdir + '/make/project.conf', 'build', 'name'])
if not top_name:
top_name = re.sub('-[0-9.-]*$', '', basename(realpath(topdir)))
# ---- global variables
build_cache = {}
dep_cache = {}
my_dir=os.path.dirname(os.path.realpath(__file__))
projects_py="/usr/bin/python2 " + my_dir + "/projects.py --prefix " + projs_root + " " + os.getenv('PROJECTS_PY_EXTRA_ARGS', "")
cmd = getattr(sys.modules[__name__], 'cmd_' + args.cmd.replace('-', '_'))
cmd(sys.argv[(len(global_args) + 1)::])

View file

@ -1,6 +1,7 @@
#!/usr/bin/python2 -u
from __future__ import print_function
import os
import sys
import argparse
from sets import Set
@ -100,7 +101,7 @@ def htdocs_dir(name):
def os_cascade():
r = [ 'os', platform.system().lower() ]
os = cache.run(get_os, [])
os = res_cache.run(get_os, [])
name = re.sub('-.*', '', os)
series = os
while True:
@ -190,7 +191,7 @@ def get_value(name, section, key):
path = proj_root + '/make/project.conf'
#print('path = ', path, 'top_name = ', top_name, 'name = ', name)
return cache.run(read_value, [path, section, key])
return res_cache.run(read_value, [path, section, key])
def collect_values(names, section, key):
r = ""
@ -206,7 +207,7 @@ def collect_values(names, section, key):
def add_modules_from_project_txt_cached(buf, visited, spec, section, key, add_self, scope,
names_only):
return cache.run(add_modules_from_project_txt, [buf, visited, spec, section, key,
return res_cache.run(add_modules_from_project_txt, [buf, visited, spec, section, key,
add_self, scope, names_only])
def add_modules_from_project_txt(buf, visited, spec, section, key, add_self, scope,
@ -606,7 +607,9 @@ def cmd_getval(args_):
# -------------------------------------------------------------------- here we go
global_args = []
cache = ResultCache()
res_cache = ResultCache()
dep_cache = {}
my_dir = os.path.dirname(os.path.realpath(__file__))
skip = 0
for a in sys.argv[1::]: