-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
198 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import argparse | ||
import hashlib | ||
import os | ||
from .invoker import Invoker | ||
from .scm import getScm | ||
from .state import BobState | ||
from .stringparser import Env | ||
from .input import RecipeSet, Scm, YamlCache | ||
from .utils import EventLoopWrapper, processDefines | ||
from .tty import NORMAL, stepExec | ||
|
||
class LayerStepSpec: | ||
def __init__(self, path): | ||
self.__path = path | ||
|
||
@property | ||
def workspaceWorkspacePath(self): | ||
return self.__path | ||
|
||
@property | ||
def env(self): | ||
return {} | ||
|
||
@property | ||
def envWhiteList(self): | ||
return [] | ||
|
||
class Layer: | ||
def __init__(self, name, root, recipes, yamlCache, scms=None): | ||
self.__name = name | ||
self.__root = root | ||
self.__recipes = recipes | ||
self.__yamlCache = yamlCache | ||
self.__subLayers = [] | ||
self.__scms = scms | ||
|
||
async def __checkoutTask(self): | ||
for scm in self.__scms: | ||
layerSrcPath = os.path.join(self.__root, "layers", | ||
self.__name, scm.getProperties(False).get("dir", "")) | ||
invoker = Invoker(spec=LayerStepSpec(layerSrcPath), | ||
preserveEnv=True, | ||
noLogFiles=True, | ||
showStdOut=True, showStdErr=True, | ||
trace=False, redirect=False, executor=None) | ||
created = False | ||
if not os.path.isdir(layerSrcPath): | ||
os.makedirs(layerSrcPath) | ||
created = True | ||
|
||
newState = {} | ||
newState["digest"] = scm.asDigestScript(), | ||
newState["prop"] = scm.getProperties(False) | ||
oldState = BobState().getDirectoryState(layerSrcPath, False) | ||
runInvoke=True | ||
if not created and oldState is not None and \ | ||
newState != oldState and \ | ||
scm.canSwitch(Scm(oldState["prop"], Env(), | ||
overrides=self.__recipes.scmOverrides(), | ||
recipeSet=self.__recipes)): | ||
ret = await invoker.executeScmSwitch(scm, oldState["prop"]) | ||
|
||
if ret == 0: | ||
runInvoke = False | ||
if runInvoke: | ||
# inline switch failed | ||
await scm.invoke(invoker) | ||
BobState().setDirectoryState(layerSrcPath, newState) | ||
|
||
def checkout(self): | ||
with EventLoopWrapper() as (loop, executor): | ||
j = loop.create_task(self.__checkoutTask()) | ||
loop.run_until_complete(j) | ||
|
||
def getName(self): | ||
return self.__name | ||
|
||
def getScmSpec(self): | ||
return self.__scmSpec | ||
|
||
def loadYaml(self, path, schema): | ||
if os.path.exists(path): | ||
return self.__yamlCache.loadYaml(path, schema, {}, preValidate=lambda x: None) | ||
return {} | ||
|
||
def parse(self): | ||
configYaml = os.path.join(self.__root, "config.yaml") | ||
config = self.loadYaml(configYaml, (RecipeSet.STATIC_CONFIG_SCHEMA, b'')) | ||
for l in config.get('layers', []): | ||
layerScms = [] | ||
for name, scms in l.items(): | ||
for scm in scms: | ||
scm["recipe"] = configYaml | ||
layerScms.append(Scm(scm, Env(), | ||
overrides=self.__recipes.scmOverrides(), | ||
recipeSet=self.__recipes)) | ||
self.__subLayers.append(Layer(name, | ||
self.__root, | ||
self.__recipes, | ||
self.__yamlCache, | ||
layerScms)) | ||
|
||
def getSubLayers(self): | ||
return self.__subLayers | ||
|
||
class Layers: | ||
def __init__(self, recipes): | ||
self.__layers = [] | ||
self.__recipes = recipes | ||
self.__yamlCache = YamlCache() | ||
|
||
def __haveLayer(self, layer): | ||
for l in self.__layers: | ||
if l.getName() == layer.getName(): | ||
return True | ||
return False | ||
|
||
def __handleLayer(self, layer): | ||
for l in layer.getSubLayers(): | ||
if not self.__haveLayer(layer): | ||
self.__layers.append(layer) | ||
l.checkout() | ||
self.__recipes.addParseableLayer(l.getName()) | ||
l.parse() | ||
for l1 in layer.getSubLayers(): | ||
self.__handleLayer(l1) | ||
|
||
def __collect(self): | ||
# parse config of bobRoot -> add Layers for each entry | ||
recipesRoot = Layer("", os.getcwd(), self.__recipes, self.__yamlCache) | ||
|
||
recipesRoot.parse() | ||
for l in recipesRoot.getSubLayers(): | ||
self.__layers.append(l) | ||
l.checkout() | ||
self.__recipes.addParseableLayer(l.getName()) | ||
l.parse() | ||
self.__handleLayer(l) | ||
|
||
def collect(self): | ||
self.__yamlCache.open() | ||
try: | ||
self.__collect() | ||
finally: | ||
self.__yamlCache.close() | ||
|
||
def doLayers(argv, bobRoot): | ||
|
||
parser = argparse.ArgumentParser(prog="bob layers", description='Handle layers') | ||
parser.add_argument('action', type=str, choices=['update', 'status'], default="status", | ||
help="Action: [update, status]") | ||
parser.add_argument('-c', dest="configFile", default=[], action='append', | ||
help="Use config File") | ||
parser.add_argument('-v', '--verbose', default=NORMAL, action='count', | ||
help="Increase verbosity (may be specified multiple times)") | ||
parser.add_argument('-D', default=[], action='append', dest="defines", | ||
help="Override default environment variable") | ||
args = parser.parse_args(argv) | ||
|
||
defines = processDefines(args.defines) | ||
|
||
# parse without failing due to missing layers. | ||
# this is neccessary to set the scm relevant policies. | ||
recipes = RecipeSet() | ||
recipes.addParseableLayer([]) | ||
recipes.setConfigFiles(args.configFile) | ||
recipes.parse(defines) | ||
|
||
layers = Layers(recipes) | ||
layers.collect() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters