def test_write_read_files(self): '''test_write_read_files will test the functions write_file and read_file ''' print("Testing utils.write_file...") from spython.utils import write_file import json tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_file(tmpfile, "hello!") self.assertTrue(os.path.exists(tmpfile)) print("Testing utils.read_file...") from spython.utils import read_file content = read_file(tmpfile)[0] self.assertEqual("hello!", content) from spython.utils import write_json print("Testing utils.write_json...") print("...Case 1: Providing bad json") bad_json = {"Wakkawakkawakka'}": [{True}, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) with self.assertRaises(TypeError) as cm: write_json(bad_json, tmpfile) print("...Case 2: Providing good json") good_json = {"Wakkawakkawakka": [True, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_json(good_json, tmpfile) with open(tmpfile, 'r') as filey: content = json.loads(filey.read()) self.assertTrue(isinstance(content, dict)) self.assertTrue("Wakkawakkawakka" in content)
def __init__(self, filename, load=True): '''a generic recipe parser holds the original file, and provides shared functions for interacting with files. If the subclass has a parse function defined, we parse the filename Parameters ========== filename: the recipe file to parse. load: if True, load the filename into the Recipe. If not loaded, the user can call self.parse() at a later time. ''' self.filename = filename self._run_checks() self.lines = [] self.recipe = Recipe(self.filename) if self.filename: # Read in the raw lines of the file self.lines = read_file(self.filename) # If parsing function defined, parse the recipe if load: self.parse()
def __init__(self, filename, load=True): """a generic recipe parser holds the original file, and provides shared functions for interacting with files. If the subclass has a parse function defined, we parse the filename Parameters ========== filename: the recipe file to parse. load: if True, load the filename into the Recipe. If not loaded, the user can call self.parse() at a later time. """ self.filename = filename self._run_checks() self.lines = [] # Arguments can be used internally, active layer name and number self.args = {} self.active_layer = "spython-base" self.active_layer_num = 1 # Support multistage builds self.recipe = {"spython-base": Recipe(self.filename)} if self.filename: # Read in the raw lines of the file self.lines = read_file(self.filename) # If parsing function defined, parse the recipe if load: self.parse()
def test_write_read_files(tmp_path): """test_write_read_files will test the functions write_file and read_file""" print("Testing utils.write_file...") from spython.utils import write_file tmpfile = str(tmp_path / "written_file.txt") assert not os.path.exists(tmpfile) write_file(tmpfile, "hello!") assert os.path.exists(tmpfile) print("Testing utils.read_file...") from spython.utils import read_file content = read_file(tmpfile)[0] assert content == "hello!"
def parse(self): '''parse is the base function for parsing the recipe, whether it be a Dockerfile or Singularity recipe. The recipe is read in as lines, and saved to a list if needed for the future. If the client has it, the recipe type specific _parse function is called. Instructions for making a client subparser: It should have a main function _parse that parses a list of lines from some recipe text file into the appropriate sections, e.g., self.fromHeader self.environ self.labels self.install self.files self.test self.entrypoint ''' self.cmd = None self.comments = [] self.entrypoint = None self.environ = [] self.files = [] self.install = [] self.labels = [] self.ports = [] self.test = None self.volumes = [] if self.recipe: # Read in the raw lines of the file self.lines = read_file(self.recipe) # If properly instantiated by Docker or Singularity Recipe, parse if hasattr(self, '_parse'): self._parse()