From f1c4dc13eac954be8fba3ce04d2bc56337925f53 Mon Sep 17 00:00:00 2001 From: Thasso Griebel Date: Fri, 17 Jan 2014 15:55:02 +0100 Subject: [PATCH] Added support to marse memory patterns (Fixes #18) --- jip/cli/jip_submit.py | 4 +++- jip/profiles.py | 2 +- jip/utils.py | 28 ++++++++++++++++++++++++++++ test/test_utils.py | 6 ++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/jip/cli/jip_submit.py b/jip/cli/jip_submit.py index 907ac74..a6a2bc7 100644 --- a/jip/cli/jip_submit.py +++ b/jip/cli/jip_submit.py @@ -31,7 +31,9 @@ -E, --environment Specify an environment if your grid engine supports it. For SGE, this is translated to the parallel environment - -m, --mem Max memory assigned to the job + -m, --mem Max memory assigned to the job in MB. Also supports + suffixes like G, M or K for Gigabyte, Megabyte and + Kilobyte -n, --name Job name -o, --out Stdout log file -e, --log Stderr log file diff --git a/jip/profiles.py b/jip/profiles.py index 1f09937..0b4713f 100644 --- a/jip/profiles.py +++ b/jip/profiles.py @@ -288,7 +288,7 @@ def apply(self, job, _load_specs=True, overwrite_threads=False, if self.time is not None: job.max_time = jip.utils.parse_time(self.time) if self.mem is not None: - job.max_memory = self.mem + job.max_memory = jip.utils.parse_mem(self.mem) if self.log is not None: job.stderr = self._render(job, self.log) if self.out is not None: diff --git a/jip/utils.py b/jip/utils.py index a4e700d..2cad83f 100644 --- a/jip/utils.py +++ b/jip/utils.py @@ -161,3 +161,31 @@ def parse_time(time): minutes += 1 r = (delta.days * 1440) + (60 * hours) + minutes return r + + +def parse_mem(mem): + """Takse a string and parses a memory pattern. The supported suffixes are + G M or K both upper and lower case. + + :param mem: the memory string + :returns: memory in megabyte + """ + m = None + try: + m = int(mem) + except: + pass + if m or m == 0: + return m + + try: + # check the memory patterns + lc = mem[-1].upper() + m = int(mem[:-1]) + if lc == "G": + m = m * 1024 + elif lc == "K": + m = m / 1024 + return m + except: + raise ValueError("Unable to parse %s to memory", mem) diff --git a/test/test_utils.py b/test/test_utils.py index dc0190d..a48c650 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -11,3 +11,9 @@ def test_parse_time_minutes(data): @pytest.mark.parametrize('data', [90, '1h30m', '90m', '30m3600s', '1:30']) def test_parse_time_timestamp(data): assert utils.parse_time(data) == 90 + + +@pytest.mark.parametrize('data', [1024, '1g', '1G', '1048576k', '1048576K', + "1024m", "1024M"]) +def test_parse_mem(data): + assert utils.parse_mem(data) == 1024