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)
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)
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
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)))
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
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
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
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
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')
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')
def runTest(self): self.assertEqual(self._content, load.load_to_str(self._url)) self.assertRaises(KickstartError, load.load_to_str, self._url_https)
def runTest(self): self.assertEqual(self._content, load.load_to_str(self._path))