本文整理汇总了Python中pykickstart.load.load_to_str函数的典型用法代码示例。如果您正苦于以下问题:Python load_to_str函数的具体用法?Python load_to_str怎么用?Python load_to_str使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了load_to_str函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: readKickstart
def readKickstart(self, f, reset=True):
"""Process a kickstart file, given by the filename f."""
if reset:
self._reset()
# an %include might not specify a full path. if we don't try to figure
# out what the path should have been, then we're unable to find it
# requiring full path specification, though, sucks. so let's make
# the reading "smart" by keeping track of what the path is at each
# include depth.
if not os.path.exists(f):
if self._includeDepth - 1 in self.currentdir:
if os.path.exists(os.path.join(self.currentdir[self._includeDepth - 1], f)):
f = os.path.join(self.currentdir[self._includeDepth - 1], f)
cd = os.path.dirname(f)
if not cd.startswith("/"):
cd = os.path.abspath(cd)
self.currentdir[self._includeDepth] = cd
try:
s = load_to_str(f)
except KickstartError as e:
raise KickstartError(formatErrorMsg(0, msg=_("Unable to open input kickstart file: %s") % str(e)))
self.readKickstartFromString(s, reset=False)
示例2: runTest
def runTest(self):
target_path = load.load_to_file(self._url, self._target_path)
self.assertEqual(target_path, self._target_path)
with open(self._target_path, 'r') as f:
self.assertEqual(self._content, f.read())
self.assertEqual(self._content, load.load_to_str(self._url))
self.assertRaises(KickstartError,
load.load_to_file,
self._url_https,
self._target_path)
示例3: preprocessKickstartToString
def preprocessKickstartToString (f):
"""Preprocess the kickstart file, given by the filename f. This
method is currently only useful for handling %ksappend lines,
which need to be fetched before the real kickstart parser can be
run. Returns the complete kickstart file as a string.
"""
try:
contents = load_to_str(f)
except KickstartError as e:
raise KickstartError(formatErrorMsg(0, msg=_("Unable to open input kickstart file: %s") % str(e)))
return _preprocessStateMachine(iter(contents.splitlines(True)))
示例4: _preprocessStateMachine
def _preprocessStateMachine (lineIter):
l = None
lineno = 0
# Now open an output kickstart file that we are going to write to one
# line at a time.
(outF, outName) = tempfile.mkstemp("-ks.cfg", "", "/tmp")
while True:
try:
l = next(lineIter)
except StopIteration:
break
# At the end of the file?
if l == "":
break
lineno += 1
ksurl = None
ll = l.strip()
if not ll.startswith("%ksappend"):
if six.PY3:
import sys
l = l.encode(sys.getdefaultencoding())
os.write(outF, l)
continue
# Try to pull down the remote file.
try:
ksurl = ll.split(' ')[1]
except:
raise KickstartParseError(formatErrorMsg(lineno, msg=_("Illegal url for %%ksappend: %s") % ll))
try:
contents = load_to_str(ksurl)
except KickstartError as e:
raise KickstartError(formatErrorMsg(lineno, msg=_("Unable to open %%ksappend file: %s") % str(e)))
# If that worked, write the remote file to the output kickstart
# file in one burst. Then close everything up to get ready to
# read ahead in the input file. This allows multiple %ksappend
# lines to exist.
if contents is not None:
os.write(outF, contents)
# All done - close the temp file and return its location.
os.close(outF)
return outName
示例5: _preprocessStateMachine
def _preprocessStateMachine (lineIter):
l = None
lineno = 0
retval = ""
if six.PY3:
retval = retval.encode(sys.getdefaultencoding())
while True:
try:
l = next(lineIter)
except StopIteration:
break
# At the end of the file?
if l == "":
break
lineno += 1
ksurl = None
ll = l.strip()
if not ll.startswith("%ksappend"):
if six.PY3:
l = l.encode(sys.getdefaultencoding())
retval += l
continue
# Try to pull down the remote file.
try:
ksurl = ll.split(' ')[1]
except:
raise KickstartParseError(formatErrorMsg(lineno, msg=_("Illegal url for %%ksappend: %s") % ll))
try:
contents = load_to_str(ksurl)
except KickstartError as e:
raise KickstartError(formatErrorMsg(lineno, msg=_("Unable to open %%ksappend file: %s") % str(e)))
# If that worked, write the remote file to the output kickstart
# file in one burst. This allows multiple %ksappend lines to
# exist.
if contents is not None:
retval += contents.encode(sys.getdefaultencoding())
return retval
示例6: versionFromFile
def versionFromFile(f):
"""Given a file or URL, look for a line starting with #version= and
return the version number. If no version is found, return DEVEL.
"""
v = DEVEL
contents = load_to_str(f)
for line in contents.splitlines(True):
if line.strip() == "":
continue
if line[:9] == "#version=":
v = stringToVersion(line[9:].rstrip())
break
return v
示例7: runTest
def runTest(self):
target_path = load.load_to_file(self._url, self._target_path)
self.assertEqual(target_path, self._target_path)
with open(self._target_path, 'r') as f:
self.assertEqual(self._content, f.read())
self.assertEqual(self._content, load.load_to_str(self._url))
# raises SSLError in _load_url()
with self.assertRaises(KickstartError):
load.load_to_file(self._url_https, self._target_path)
# raises RequestException in _load_url()
with self.assertRaises(KickstartError):
load.load_to_file('http://test.local/ks.cfg', self._target_path)
# raises IOError in load_file()
with self.assertRaises(KickstartError):
load.load_to_file(self._url, '/no/exist')
# request.status_code == 404 in _load_url()
with self.assertRaises(KickstartError):
load.load_to_file(self._url+'.TEST', '/tmp/foo')