config: CacheDir()
[matrix.git] / matrix / config.py
1 # Copyright (C) 2007-2008 Movial Oy
2 # Authors: Timo Savola <tsavola@movial.fi>
3 #          Kalle Vahlman <kalle.vahlman@movial.fi>
4
5 import sre
6 from glob import glob
7 from os import getenv
8 from os.path import basename, exists, isabs, isdir, join
9 from sets import Set as set
10
11 Error = RuntimeError
12
13 exclude = sre.compile(r'^[.#].*|.*[~#]$')
14
15 def _make_list(arg):
16         if arg is None:
17                 return []
18
19         if isinstance(arg, basestring):
20                 return arg.split()
21
22         return list(arg)
23
24 def config_dir(path=None, url=None, branch='master', optional=False):
25         from repositories import Repository
26
27         if not path:
28                 if not url:
29                         raise Error('No path or URL specified')
30
31                 path = basename(url)
32                 if path.endswith('.git'):
33                         path = path[:-4]
34
35         if optional and path not in config.initial_config_dirs:
36                 if config.debug:
37                         print 'Skipping', path
38                 return
39
40         if url:
41                 name = basename(path)
42                 repo = Repository(name=name, path=path, url=url, branch=branch)
43                 if not repo.exists():
44                         repo.clone()
45         else:
46                 if not isdir(path):
47                         raise Error('Config dir does not exist: %s' % path)
48
49         if path not in config.initial_config_dirs:
50                 config.config_dirs.append(path)
51
52 def root(url, branch='master'):
53         config.roots.append((url, branch))
54
55 class Board(object):
56         def __init__(self, name, arch, gcc_march,
57                      gcc_mcpu=None,
58                      gcc_mfpu=None,
59                      gcc_options=None,
60                      gnu_host=None,
61                      flash_erase_size=65536,
62                      flash_pad_size=0,
63                      flash_compression=None,
64                      ubifs_leb_size=0,
65                      ubifs_max_leb_count=0,
66                      ubifs_min_io_size=0,
67                      ubinize_config_vol_size=0):
68
69                 self.name = name
70                 self.arch = arch
71                 self.gcc_march = gcc_march
72                 self.gcc_mcpu = gcc_mcpu or ''
73                 self.gcc_mfpu = gcc_mfpu or ''
74                 self.gcc_options = _make_list(gcc_options)
75                 self.gnu_host = gnu_host or ''
76                 self.flash_erase_size = flash_erase_size
77                 self.flash_pad_size = flash_pad_size
78                 self.flash_compression = flash_compression or ''
79                 self.ubifs_leb_size = ubifs_leb_size
80                 self.ubifs_max_leb_count = ubifs_max_leb_count
81                 self.ubifs_min_io_size = ubifs_min_io_size
82                 self.ubinize_config_vol_size = ubinize_config_vol_size
83
84                 config.boards[name] = self
85
86 def component(*args, **kwargs):
87         import components
88
89         c = components.Component(*args, **kwargs)
90         config.components[c.name] = c
91         return c
92
93 def provide_component(name):
94         c = config.components.get(name)
95         if not c:
96                 c = component(name)
97         c.cached = True
98         return c
99
100 def parse(pattern, require=True):
101         if isabs(pattern):
102                 dirs = [None]
103         else:
104                 config_dirs = config.config_dirs + config.initial_config_dirs
105                 dirs = [join(config.top_dir, dir) for dir in config_dirs]
106                 dirs.append(config.top_dir)
107
108         found = False
109
110         for dir in dirs:
111                 for path in glob(join(dir, pattern)):
112                         if exclude.match(basename(path)):
113                                 continue
114
115                         found = True
116
117                         if config.debug:
118                                 print 'Parsing', path
119
120                         execfile(path, actions.copy(), {})
121
122         if not found:
123                 msg = 'Config file(s) not found: %s' % pattern
124                 if require:
125                         raise Error(msg)
126                 elif config.debug:
127                         print msg
128
129 def set_flag(name):
130         config.flags.add(name)
131
132 def clear_flag(name):
133         config.flags.discard(name)
134
135 def use_board(name):
136         config.board = name
137
138 def cache_dir(path):
139         config.cache_dir = path
140
141 def sb2_target(name):
142         config.sb2_target = name
143
144 def sb2_compiler(path):
145         config.sb2_compiler = path
146
147 def sb2_init_options(args):
148         config.sb2_init_options = list(args)
149
150 def get_variables():
151         return config
152
153 def rootfs_root(path, target = None):
154         rootfs.path = path
155         rootfs.target = target
156
157 def include_paths(paths):
158         rootfs.include_paths.extend(paths)
159
160 def include_files(files):
161         rootfs.include_files.extend(files)
162
163 def exclude_paths(paths):
164         rootfs.exclude_paths.extend(paths)
165
166 def exclude_files(files):
167         rootfs.exclude_files.extend(files)
168
169 def exclude_expressions(expressions):
170         rootfs.exclude_expressions.extend(expressions)
171
172 def created_paths(paths):
173         rootfs.created_paths.extend(paths)
174
175 def directory_owners(owners):
176         rootfs.directory_owners.update(owners)
177
178 def device_nodes(devices):
179         rootfs.devices.update(devices)
180
181 class RootFS(object):
182         def __init__(self):
183                 self.path = None
184                 self.include_paths = []
185                 self.include_files = []
186                 self.exclude_paths = []
187                 self.exclude_files = []
188                 self.exclude_expressions = []
189                 self.created_paths = []
190                 self.devices = {}
191                 self.directory_owners = {}
192
193 class Config(object):
194         def __init__(self):
195                 self.initial_config_dirs = []
196                 self.config_dirs = []
197
198                 self.top_dir = getenv('MATRIX_TOPDIR', '')
199                 __cache_dir = join(self.top_dir, 'build-cache')
200                 self.cache_dir = getenv('MATRIX_CACHEDIR', __cache_dir)
201                 self.script_dir = None
202
203                 self.boards = {}
204                 self.board = None
205                 self.components = {}
206                 self.roots = []
207                 self.flags = set()
208                 self.verbose = False
209                 self.debug = False
210                 self.force = False
211                 self.keep_going = False
212                 self.jobs = 1
213                 self.make_jobs = 1
214
215                 self.sb2_target = None
216                 self.sb2_compiler = None
217                 self.sb2_init_options = []
218
219 actions = dict(
220         Board = Board,
221         CacheDir = cache_dir,
222         ClearFlag = clear_flag,
223         Component = component,
224         ConfigDir = config_dir,
225         GetVariables = get_variables,
226         Parse = parse,
227         ProvideComponent = provide_component,
228         Root = root,
229         SB2Compiler = sb2_compiler,
230         SB2InitOptions = sb2_init_options,
231         SB2Target = sb2_target,
232         SetFlag = set_flag,
233         UseBoard = use_board,
234         RootFSRootPath = rootfs_root,
235         RootFSPaths = include_paths,
236         RootFSFiles = include_files,
237         RootFSExcludePaths = exclude_paths,
238         RootFSExcludeFiles = exclude_files,
239         RootFSExcludeExpressions = exclude_expressions,
240         RootFSCreatePaths = created_paths,
241         RootFSDirectoryOwners = directory_owners,
242         RootFSDeviceNodes = device_nodes,
243 )
244
245 config = Config()
246 rootfs = RootFS()
247