figures/v-dep: Remove UnFill from plot legend addition
[thesis.git] / site_cons / site_init.py
1 import os.path
2 import SCons.Node.FS
3
4
5 def subdirs(env, *args, **kwargs):
6     for subdir in env.fs.Glob(env.subst(args[0]), *args[1:], **kwargs):
7         if isinstance(subdir, SCons.Node.FS.Dir):
8             yield subdir
9
10 def include_child_SConscripts(env, SConscript, first=[]):
11     """Get all the nested SConscripts in that may alter and pass back
12     the environment.  They may also add thesis subdependencies to that
13     environment.
14     """
15     sdirs = list(subdirs(env, '*'))
16     for subdir in reversed(first): # move first subdirs to the front
17         if subdir in sdirs:
18             sdirs.remove(subdir)
19             sdirs.insert(0, subdir)
20     for subdir in sdirs:
21         var_sconscript_path = os.path.join(subdir.abspath,
22                                            'SConscript')
23         src_sconscript_path = os.path.join(subdir.srcnode().abspath,
24                                            'SConscript')
25         if os.path.exists(src_sconscript_path):
26             if env.GetOption('silent') is False:
27                 # ideally 'silent' should be -Q, not the current -s/--silent/--quiet
28                 print 'Including', var_sconscript_path
29             env = SConscript(var_sconscript_path, 'env')
30     return env
31
32 def check_exec(context, executible):
33     # TODO: context.env vs. context.vardict
34     context.Message('Checking for %s executible...' % executible)
35     if executible.upper() in context.env:
36         context.Result('yes')
37         return 'yes'
38     result = 'no'
39     for p in context.env.Dictionary()['ENV']['PATH']:
40         path = os.path.join(p, executible)
41         if os.path.exists(path):
42             result = 'yes'
43             break
44     context.Result(result)
45     if result is 'yes':
46         context.env[executible.upper()] = path
47     return result
48
49 def _recursive_glob_dir(env, *args, **kwargs):
50     assert len(args) > 0
51     glob_results = env.fs.Glob(env.subst(args[0]), *args[1:], **kwargs)
52     sds = list(subdirs(env, *(['*']+list(args[1:])), **kwargs))
53     return (sds, glob_results)
54
55 def recursive_glob(env, *args, **kwargs):
56     dir_stack = [kwargs.pop('cwd', None)]
57     glob_results = []
58     while len(dir_stack) > 0:
59         kwargs['cwd'] = dir_stack.pop(0)
60         subdirs,results = _recursive_glob_dir(env, *args, **kwargs)
61         dir_stack.extend(subdirs)
62         glob_results.extend(results)
63     return glob_results
64
65 def link_wtk_graph(env):
66     return env.Command('wtk_graph.asy', '../asy/wtk_graph.asy',
67                        SCons.Script.Copy('$TARGET', '$SOURCE'))
68
69 def link_pyfit(env):
70     return env.Command('pyfit', '../script/pyfit',
71                        SCons.Script.Copy('$TARGET', '$SOURCE'))