本文整理匯總了Python中params.Params.loadMainfile方法的典型用法代碼示例。如果您正苦於以下問題:Python Params.loadMainfile方法的具體用法?Python Params.loadMainfile怎麽用?Python Params.loadMainfile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類params.Params
的用法示例。
在下文中一共展示了Params.loadMainfile方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Install
# 需要導入模塊: from params import Params [as 別名]
# 或者: from params.Params import loadMainfile [as 別名]
class Install():
dryrun = False
params = None
answers_file = None
def __init__(self, answers, APP, nodeps = False, update = False, target_path = None, dryrun = False, **kwargs):
run_path = os.path.dirname(os.path.realpath(__file__))
self.dryrun = dryrun
app = APP #FIXME
self.params = Params(nodeps, update, target_path)
self.utils = utils.Utils(self.params)
if os.path.exists(app):
logger.info("App path is %s, will be populated to %s" % (app, target_path))
app = self.utils.loadApp(app)
else:
logger.info("App name is %s, will be populated to %s" % (app, target_path))
if not target_path:
if self.params.app_path:
self.params.target_path = self.params.app_path
else:
self.params.target_path = os.getcwd()
self.params.app = app
self.answers_file = answers
def _copyFromContainer(self, image):
image = self.utils.getImageURI(image)
name = "%s-%s" % (self.utils.getComponentName(image), ''.join(random.sample(string.letters, 6)))
logger.debug("Creating a container with name %s" % name)
create = ["docker", "create", "--name", name, image, "nop"]
subprocess.call(create)
cp = ["docker", "cp", "%s:/%s" % (name, utils.APP_ENT_PATH), self.utils.tmpdir]
logger.debug(cp)
if not subprocess.call(cp):
logger.debug("Application entity data copied to %s" % self.utils.tmpdir)
rm = ["docker", "rm", name]
subprocess.call(rm)
def _populateApp(self, src = None, dst = None):
logger.info("Copying app %s" % self.utils.getComponentName(self.params.app))
if not src:
src = os.path.join(self.utils.tmpdir, APP_ENT_PATH)
if not dst:
dst = self.params.target_path
distutils.dir_util.copy_tree(src, dst, update=(not self.params.update))
self.utils.checkAllArtifacts()
def install(self):
self.params.loadAnswers(self.answers_file)
if self.params.app_path and not self.params.target_path == self.params.app_path:
logger.info("Copying content of directory %s to %s" % (self.params.app_path, self.params.target_path))
self._populateApp(src=self.params.app_path)
mainfile_path = os.path.join(self.params.target_path, MAIN_FILE)
if not self.params.app_path and (self.params.update or not os.path.exists(self.utils.getComponentDir(self.params.app))):
self.utils.pullApp(self.params.app)
self._copyFromContainer(self.params.app)
mainfile_path = os.path.join(self.utils.getTmpAppDir(), MAIN_FILE)
logger.debug("%s path for pulled image: %s" % (MAIN_FILE, mainfile_path))
self.params.loadMainfile(mainfile_path)
logger.debug("App ID: %s" % self.params.app_id)
self._populateApp()
else:
logger.info("Component data exist in %s, skipping population..." % self.utils.getComponentDir(self.params.app))
if not self.params.mainfile_data:
self.params.loadMainfile(mainfile_path)
values = {}
if not self.params.nodeps:
logger.info("Installing dependencies for %s" % self.params.app_id)
values = self._installDependencies()
logger.debug(values)
self.params.loadAnswers(values)
logger.debug(self.params.answers_data)
if self.params.write_sample_answers:
print("blah")
self.params.writeAnswersSample()
return values
def _installDependencies(self):
values = {}
for component, graph_item in self.params.mainfile_data["graph"].iteritems():
if not self.utils.isExternal(graph_item):
values[component] = self.params.getValues(component, skip_asking = True)
logger.debug("Component %s is part of the app" % component)
#.........這裏部分代碼省略.........
示例2: Run
# 需要導入模塊: from params import Params [as 別名]
# 或者: from params.Params import loadMainfile [as 別名]
#.........這裏部分代碼省略.........
raise Exception("App path %s does not exist." % APP)
self.params = Params(target_path=self.app_path)
if "ask" in kwargs:
self.params.ask = kwargs["ask"]
self.utils = Utils(self.params)
self.answers_file = answers
self.plugin = Plugin()
self.plugin.load_plugins()
def _dispatchGraph(self):
if not "graph" in self.params.mainfile_data:
raise Exception("Graph not specified in %s" % MAIN_FILE)
for component, graph_item in self.params.mainfile_data["graph"].iteritems():
if self.utils.isExternal(graph_item):
component_run = Run(self.answers_file, self.utils.getExternalAppDir(component), self.dryrun, self.debug, **self.kwargs)
ret = component_run.run()
if self.answers_output:
self.params.loadAnswers(ret)
else:
self._processComponent(component, graph_item)
def _applyTemplate(self, data, component):
template = Template(data)
config = self.params.getValues(component)
logger.debug("Config: %s " % config)
output = None
while not output:
try:
logger.debug(config)
output = template.substitute(config)
except KeyError as ex:
name = ex.args[0]
logger.debug("Artifact contains unknown parameter %s, asking for it" % name)
config[name] = self.params._askFor(name, {"description": "Missing parameter '%s', provide the value or fix your %s" % (name, MAIN_FILE)})
if not len(config[name]):
raise Exception("Artifact contains unknown parameter %s" % name)
self.params.loadAnswers({component: {name: config[name]}})
return output
def _processComponent(self, component, graph_item):
logger.debug("Processing component %s" % component)
data = None
artifacts = self.utils.getArtifacts(component)
artifact_provider_list = []
if not self.params.provider in artifacts:
raise Exception("Data for provider \"%s\" are not part of this app" % self.params.provider)
dst_dir = os.path.join(self.utils.tmpdir, component)
for artifact in artifacts[self.params.provider]:
artifact_path = self.utils.sanitizePath(artifact)
with open(os.path.join(self.app_path, artifact_path), "r") as fp:
data = fp.read()
logger.debug("Templating artifact %s/%s" % (self.app_path, artifact_path))
data = self._applyTemplate(data, component)
artifact_dst = os.path.join(dst_dir, artifact_path)
if not os.path.isdir(os.path.dirname(artifact_dst)):
os.makedirs(os.path.dirname(artifact_dst))
with open(artifact_dst, "w") as fp:
logger.debug("Writing artifact to %s" % artifact_dst)
fp.write(data)
artifact_provider_list.append(artifact_path)
provider_class = self.plugin.getProvider(self.params.provider)
provider = provider_class(self.params.getValues(component), artifact_provider_list, dst_dir, self.dryrun)
if provider:
logger.info("Using provider %s for component %s" % (self.params.provider, component))
else:
raise Exception("Something is broken - couldn't get the provider")
provider.init()
provider.deploy()
def run(self):
self.params.loadMainfile(os.path.join(self.params.target_path, MAIN_FILE))
self.params.loadAnswers(self.answers_file)
self.utils.checkArtifacts()
config = self.params.get()
if "provider" in config:
self.provider = config["provider"]
self._dispatchGraph()
#Think about this a bit more probably - it's (re)written for all components...
if self.answers_output:
self.params.writeAnswers(self.answers_output)
return self.params.answers_data
return None