def from_env_task(**kwargs): '''Get some set of variables from the environment. We look for those defined in kwargs, but also any in the environment that start with the prefix WATCHMENEV_. They are saved to files that are equivalently named, and the idea is that we can track a changing (single) result, meaning that the timestamp is meaningful, or we can track values for many different results, so the timestamps just serve to record when each was recorded. Parameters ========== *: any number of parameters from the task configuration that will be looked for in the environment. ''' results = [] # Create a temporary directory for results tmpdir = get_tmpdir() # First extract variables from the environment environ = get_watchme_env() for key, value in environ.items(): # Write the result to file (don't include extension) filename = os.path.join(tmpdir, key) write_file(filename, value) results.append(filename) # If no results, return None if len(results) == 0: shutil.rmtree(tmpdir) return results
def _save_text(self, result, repo, file_name=None): """save a general text object to file. Parameters ========== result: the result object to save, should be path to a file repo: the repository base with the task folder """ file_name = self.params.get("file_name", file_name) or "result.txt" task_folder = os.path.join(repo, self.name) destination = os.path.join(task_folder, file_name) write_file(destination, result) return destination
def write_timestamp(repo, task, filename="TIMESTAMP"): """write a file that includes the last run timestamp. This should be written in each task folder after a run. Parameters ========== repo: the repository to write the TIMESTAMP file to task: the name of the task folder to write the file to filename: the filename (defaults to TIMESTAMP) """ ts = time.time() strtime = datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S") filename = os.path.join(repo, task, filename) write_file(filename, strtime) return filename
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 watchme.utils import write_file tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_file(tmpfile, "mocos!") self.assertTrue(os.path.exists(tmpfile)) print("Testing utils.read_file...") from watchme.utils import read_file content = read_file(tmpfile)[0] self.assertEqual("mocos!", content) from watchme.utils import write_json print("Testing utils.write_json.") print("...Case 1: Providing bad json") bad_json = {"IWuvWaffles?'}": [{True}, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) with self.assertRaises(TypeError): write_json(bad_json, tmpfile) print("...Case 2: Providing good json") good_json = {"IWuvWaffles!": [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("IWuvWaffles!" in content) print("Testing utils.print_json") from watchme.utils import print_json result = print_json({1: 1}) self.assertEqual('{\n "1": 1\n}', result)