|
4 | 4 | import os |
5 | 5 | import re |
6 | 6 | import logging |
| 7 | +import fnmatch |
7 | 8 | import glob |
8 | 9 |
|
9 | 10 | logging.basicConfig() |
@@ -48,6 +49,19 @@ def dicts(dst, add): |
48 | 49 | dst[key] = add[key] |
49 | 50 | return conflicts |
50 | 51 |
|
| 52 | +def recursive_glob(rootdir='.', pattern='*'): |
| 53 | + """Search recursively for files matching a specified pattern. |
| 54 | + Adapted from http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python |
| 55 | + """ |
| 56 | + |
| 57 | + matches = [] |
| 58 | + for root, dirnames, filenames in os.walk(rootdir): |
| 59 | + for filename in fnmatch.filter(filenames, pattern): |
| 60 | + matches.append(os.path.join(root, filename)) |
| 61 | + |
| 62 | + return matches |
| 63 | + |
| 64 | + |
51 | 65 | def mergeNpmDeps(base, module, modulename): |
52 | 66 | """ |
53 | 67 | Adds dependencies from module into base and reports any conflicts that happen. |
@@ -169,63 +183,76 @@ def updateModuleList(moduleList, config, modulename): |
169 | 183 | return True |
170 | 184 |
|
171 | 185 | def createSymlink(src, dst): |
| 186 | + log.debug("Symlinking src: %s, dst: %s" % (src, dst)) |
172 | 187 | try: |
173 | 188 | # Using lexists to detect and remove broken symlinks as well as removing symlinks that |
174 | 189 | # will be overwritten. |
175 | 190 | if os.path.lexists(dst): |
176 | 191 | os.remove(dst) |
177 | 192 | os.symlink(src, dst) |
178 | 193 | except (OSError, IOError) as e: # Insufficient privileges |
179 | | - log.warn(str(e)) |
| 194 | + log.warn("Cannot create symlink (target: %s) %s" % (src, str(e))) |
180 | 195 |
|
181 | 196 | def bootstrapModules(basedeps, moduleList): |
182 | 197 | """ |
183 | 198 | Build moduleList, update basedeps with module specific dependencies and create symlinks into |
184 | 199 | module folders of both backend and frontend. |
185 | 200 | """ |
186 | 201 | #modules = getImmediateSubdirs('modules') |
187 | | - cfgfiles = glob.glob(os.path.join(BASE_PATH, 'modules', '*.config.json')) |
188 | | - for cfgpath in cfgfiles: |
189 | | - #cfgpath = os.path.join(BASE_PATH, 'modules', module, 'config.json') |
190 | | - |
191 | | - config = None |
| 202 | + cfgfiles = glob.glob(os.path.join(BASE_PATH, 'modules', '*/*config.json')) |
192 | 203 |
|
| 204 | + for cfgpath in cfgfiles: |
193 | 205 | try: |
194 | | - with open(cfgpath, 'r') as fh: |
195 | | - config = json.load(fh) |
196 | | - except (OSError, IOError): |
197 | | - log.warn(module + ': Cannot find ' + cfgpath + ', skipping module') |
198 | | - continue |
| 206 | + config = None |
| 207 | + |
| 208 | + try: |
| 209 | + with open(cfgpath, 'r') as fh: |
| 210 | + config = json.load(fh) |
| 211 | + except (OSError, IOError): |
| 212 | + log.warn(module + ': Cannot find ' + cfgpath + ', skipping module') |
| 213 | + continue |
| 214 | + |
| 215 | + try: |
| 216 | + name = config['module']['name'] |
| 217 | + except KeyError as e: |
| 218 | + log.warn("Cannot find name in module, skipping config %s" % cfgpath) |
| 219 | + |
| 220 | + if not updateModuleList(moduleList, config, name): |
| 221 | + continue |
| 222 | + |
| 223 | + module_dir = os.path.dirname(cfgpath) |
| 224 | + |
| 225 | + # Module might not have dependencies, their absence is not an error |
| 226 | + if 'dependencies' in config: |
| 227 | + updateDeps(basedeps, config['dependencies'], name) |
| 228 | + |
| 229 | + if 'backend' in config['module']: |
| 230 | + src = os.path.join(module_dir, config['module']['backend']) |
| 231 | + dst = os.path.join(BASE_PATH, 'backend/liberouterapi/modules', name) |
| 232 | + createSymlink(src, dst) |
| 233 | + |
| 234 | + if 'assets' in config['module']: |
| 235 | + """ |
| 236 | + Link assets for frontend to frontend/src/assets folder |
| 237 | + Each module must contain key 'name' and 'assets', after importing assets are available |
| 238 | + via /name/ path on frontend |
| 239 | + """ |
| 240 | + if not 'name' in config['module']: |
| 241 | + log.warn("No 'name' specified, skipping inclusion of assets.") |
| 242 | + break |
| 243 | + src = os.path.join(module_dir, config['module']['assets']) |
| 244 | + dst = os.path.join(BASE_PATH, 'frontend/src/assets', name) |
| 245 | + createSymlink(src, dst) |
| 246 | + |
| 247 | + # Frontend key presence tested by updateModuleList |
| 248 | + src = os.path.join(module_dir, config['module']['frontend']) |
| 249 | + dst = os.path.join(BASE_PATH, 'frontend/src/app/modules', name) |
199 | 250 |
|
200 | | - if not updateModuleList(moduleList, config, config['name']): |
201 | | - continue |
202 | | - |
203 | | - # Module might not have dependencies, their absence is not an error |
204 | | - if 'dependencies' in config: |
205 | | - updateDeps(basedeps, config['dependencies'], config['name']) |
206 | | - |
207 | | - if 'backend' in config['module']: |
208 | | - src = os.path.join(BASE_PATH, 'modules', config['module']['backend']) |
209 | | - dst = os.path.join(BASE_PATH, 'backend/liberouterapi/modules', config['name']) |
210 | 251 | createSymlink(src, dst) |
211 | | - |
212 | | - if 'assets' in config['module']: |
213 | | - """ |
214 | | - Link assets for frontend to frontend/src/assets folder |
215 | | - Each module must contain key 'name' and 'assets', after importing assets are available |
216 | | - via /name/ path on frontend |
217 | | - """ |
218 | | - if not 'name' in config['module']: |
219 | | - log.warn("No 'name' specified, skipping inclusion of assets.") |
220 | | - break |
221 | | - src = os.path.join(BASE_PATH, 'modules', config['name'], config['module']['assets']) |
222 | | - dst = os.path.join(BASE_PATH, 'frontend/src/assets', config['module']['name']) |
223 | | - createSymlink(src, dst) |
224 | | - |
225 | | - # Frontend key presence tested by updateModuleList |
226 | | - src = os.path.join(BASE_PATH, 'modules', config['name'], config['module']['frontend']) |
227 | | - dst = os.path.join(BASE_PATH, 'frontend/src/app/modules', config['name']) |
228 | | - createSymlink(src, dst) |
| 252 | + except Exception as e: |
| 253 | + log.warn("Skipping configuration in {0}. Reason: {1} ({2})" |
| 254 | + .format(cfgpath, str(e), type(e))) |
| 255 | + continue |
229 | 256 |
|
230 | 257 | def registerModules(modules): |
231 | 258 | """ |
|
0 commit comments