-
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
4 changed files
with
238 additions
and
13 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,210 @@ | ||
import argparse | ||
import datetime | ||
import hashlib | ||
import os | ||
from .errors import BuildError, ParseError | ||
from .invoker import CmdFailedError, Invoker | ||
from .scm import getScm | ||
from .state import BobState | ||
from .stringparser import Env | ||
from .input import RecipeSet, Scm, YamlCache | ||
from .utils import EventLoopWrapper, processDefines, INVALID_CHAR_TRANS | ||
from .tty import DEBUG, EXECUTED, INFO, NORMAL, SKIPPED, WARNING, colorize, log, setVerbosity | ||
|
||
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 | ||
self.__layerDir = os.path.join(self.__root, "layers", self.__name) if len(self.__name) else self.__root | ||
|
||
async def __checkoutTask(self, verbose): | ||
if self.__scms is None: | ||
return | ||
for scm in self.__scms: | ||
dir = scm.getProperties(False).get("dir") | ||
layerSrcPath = os.path.join(self.__root, "layers", | ||
self.__name) | ||
if dir != '.': | ||
layerSrcPath = os.path.join(layerSrcPath, dir) | ||
invoker = Invoker(spec=LayerStepSpec(layerSrcPath), | ||
preserveEnv=True, | ||
noLogFiles = True, | ||
showStdOut = verbose >= INFO, | ||
showStdErr = verbose >= INFO, | ||
trace = verbose >= DEBUG, | ||
redirect=False, executor=None) | ||
newState = {} | ||
newState["digest"] = scm.asDigestScript(), | ||
newState["prop"] = {k:v for k,v in scm.getProperties(False).items() if v is not None} | ||
|
||
oldState = BobState().getDirectoryState(layerSrcPath, False) | ||
|
||
created = False | ||
if not os.path.isdir(layerSrcPath): | ||
os.makedirs(layerSrcPath) | ||
created = True | ||
|
||
if not created \ | ||
and oldState is not None \ | ||
and oldState["digest"] == newState["digest"]: | ||
log(colorize("CHECKOUT: Layer " + | ||
"{} skipped (up to date)".format(layerSrcPath), SKIPPED), INFO) | ||
continue | ||
|
||
if not created and oldState is not None and \ | ||
newState["digest"] != oldState["digest"] and \ | ||
scm.canSwitch(Scm(oldState["prop"], Env(), | ||
overrides=self.__recipes.scmOverrides(), | ||
recipeSet=self.__recipes)): | ||
ret = await invoker.executeScmSwitch(scm, oldState["prop"]) | ||
|
||
if ret == 0: | ||
BobState().setDirectoryState(layerSrcPath, newState) | ||
continue | ||
ret = os.path.exists(layerSrcPath) | ||
if os.path.exists(layerSrcPath) and not created: | ||
atticName = datetime.datetime.now().isoformat().translate(INVALID_CHAR_TRANS) + "_" + \ | ||
self.__name | ||
log(colorize("ATTIC: Layer " + | ||
"{} (move to layers.attic/{})".format(layerSrcPath, atticName), WARNING), WARNING) | ||
atticPath = os.path.join(self.__root, "layers.attic") | ||
if not os.path.isdir(atticPath): | ||
os.makedirs(atticPath) | ||
atticPath = os.path.join(atticPath, atticName) | ||
os.rename(layerSrcPath, atticPath) | ||
BobState().setAtticDirectoryState(atticPath, layerSrcPath) | ||
|
||
if not os.path.isdir(layerSrcPath): | ||
os.makedirs(layerSrcPath) | ||
await scm.invoke(invoker) | ||
log(colorize("CHECKOUT: Layer " + | ||
"{} .. ok".format(self.getName()), EXECUTED), INFO) | ||
BobState().setDirectoryState(layerSrcPath, newState) | ||
|
||
def checkout(self, verbose): | ||
try: | ||
with EventLoopWrapper() as (loop, executor): | ||
j = loop.create_task(self.__checkoutTask(verbose)) | ||
loop.run_until_complete(j) | ||
except CmdFailedError as e: | ||
raise BuildError(f"Failed to checkoutout Layer {self.getName()}") | ||
|
||
def getName(self): | ||
return self.__name | ||
|
||
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.__layerDir, "config.yaml") | ||
config = self.loadYaml(configYaml, (RecipeSet.STATIC_CONFIG_SCHEMA, b'')) | ||
for l in config.get('layers', []): | ||
if not isinstance(l, dict): continue | ||
layerScms = [] | ||
for name, scms in l.items(): | ||
for scm in scms.get("checkoutSCM"): | ||
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.__scmUpdate = False | ||
self.__recipes = recipes | ||
self.__yamlCache = YamlCache() | ||
|
||
def __haveLayer(self, layer): | ||
for depth,layers in self.__layers.items(): | ||
for l in layers: | ||
if l.getName() == layer.getName(): | ||
return True | ||
return False | ||
|
||
def __collect(self, depth, verbose): | ||
self.__layers[depth+1] = [] | ||
newLevel = False | ||
for l in self.__layers[depth]: | ||
if self.__scmUpdate: | ||
l.checkout(verbose) | ||
l.parse() | ||
for subLayer in l.getSubLayers(): | ||
if not self.__haveLayer(subLayer): | ||
self.__layers[depth+1].append(subLayer) | ||
newLevel = True | ||
if newLevel: | ||
self.__collect(depth + 1, verbose) | ||
|
||
def collect(self, verbose): | ||
self.__yamlCache.open() | ||
try: | ||
rootLayers = Layer("", os.getcwd(), self.__recipes, self.__yamlCache) | ||
rootLayers.parse() | ||
self.__layers[0] = rootLayers.getSubLayers(); | ||
self.__collect(0, verbose) | ||
finally: | ||
self.__yamlCache.close() | ||
|
||
def setUpdateMode(self, onoff): | ||
self.__scmUpdate = onoff | ||
|
||
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) | ||
|
||
setVerbosity(args.verbose) | ||
|
||
defines = processDefines(args.defines) | ||
|
||
recipes = RecipeSet() | ||
recipes.setConfigFiles(args.configFile) | ||
try: | ||
recipes.parse(defines) | ||
except ParseError: | ||
pass | ||
|
||
layers = Layers(recipes) | ||
if args.action == "update": | ||
layers.setUpdateMode(True) | ||
layers.collect(args.verbose) | ||
|
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