1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
from __future__ import absolute_import, division, print_function
import logging
import os
import codetree
from .exceptions import (
ConfigNotFoundException,
InvalidConfigException,
)
from .manifest import Manifest
class Spec(object):
"""Representation for an on disk spec
The spec is initialized to the proper state when an object is created and then a number of helper
methods for navigating the specs contents are provided.
"""
def __init__(self, spec_dir, url=None, stage=None, manifest_file=None):
if spec_dir[-1] == "/": # No trailing / helps with codetree relative paths
self.dir = spec_dir[:-1]
else:
self.dir = spec_dir
self.url = url
self.stage = stage
self.manifest_file = manifest_file
self._manifest = None
if self.url is None or self.dir == self.url: # if dir and url are the same, skip codetree
return
# Initialize the spec dir using codetree
self.update_from_source()
@property
def manifest(self):
logging.info("Retrieve the spec's manifest")
if self._manifest:
return self._manifest
self._manifest = Manifest(self, manifest_file=self.manifest_file, stage=self.stage)
return self._manifest
def _remove_duplicates(self, mylist):
"""Remove duplicates while preserving order.
See http://stackoverflow.com/a/480227/523729
"""
seen = set()
seen_add = seen.add
return [x for x in mylist if not (x in seen or seen_add(x))]
def iterate_subpaths(self, path):
"""Iterate subpaths of a path, from most to least explicit.
e.g. input of 'a/b/c' will yield ('a/b/c', 'b/c', 'c')"""
if path:
yield path
parts = path.split("/", 1)
if len(parts) == 1:
yield parts[0]
else:
for _path in self.iterate_subpaths(parts[1]):
yield _path
def get_configs(self, base, stage=None):
"""Locate a list of stage-qualified configuration files.
Configs are returned relative to the spec dir from
least specific to most specfic in this order:
* <base>
* <stage>/../<base>
* <stage>/<base>"""
if base[0] == "/":
raise InvalidConfigException("Invalid fully qualified config path specified: {}".format(base))
config_names = []
if stage:
# Common edge case $MOJO_STAGE/../$CONFIG
config_names.append(os.path.join(stage, "..", base))
for path in self.iterate_subpaths(os.path.join(stage, base)):
config_names.append(path)
else:
for path in self.iterate_subpaths(base):
config_names.append(path)
config_names = [os.path.normpath(config) for config in config_names]
config_names = self._remove_duplicates(config_names)
# Order is important here
# Least specific to most specific
# Sort by string length
config_names.sort(key=len)
configs = [config for config in config_names if os.path.isfile(os.path.join(self.dir, config))]
if configs:
return configs
raise ConfigNotFoundException(
"Config file '{}' not found.\n"
"Is the MOJO_STAGE environment variable or the --stage option "
"set?\nIs {} a valid spec?".format(base, self.url),
config_names,
)
def get_config(self, base, stage=None):
"""Locate a stage-qualified configuration file.
The absolute path to the most specific match,
the first of the following configs is returned:
* <stage>/<base>
* <stage>/../<base>
* <base>"""
# The last element is the most specific matching path
return os.path.join(self.dir, self.get_configs(base, stage)[-1])
def update_from_source(self):
"""Update the spec dir from source using codetree"""
url = self.url
if url.startswith("file://"):
url = url.replace("file://", "")
self.ct = codetree.config.Directive.from_raw_line("{} {}".format(self.dir, url))
# Process the codetree directive, i.e. branch, pull, etc..
self.ct.run()
|