Skip to content

Commit

Permalink
Added support to marse memory patterns (Fixes #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
thasso committed Jan 17, 2014
1 parent 82b6ad1 commit f1c4dc1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 3 additions & 1 deletion jip/cli/jip_submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
-E, --environment <pe> Specify an environment if your grid engine supports
it. For SGE, this is translated to the parallel
environment
-m, --mem <mem> Max memory assigned to the job
-m, --mem <mem> Max memory assigned to the job in MB. Also supports
suffixes like G, M or K for Gigabyte, Megabyte and
Kilobyte
-n, --name <name> Job name
-o, --out <out> Stdout log file
-e, --log <err> Stderr log file
Expand Down
2 changes: 1 addition & 1 deletion jip/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions jip/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 6 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f1c4dc1

Please sign in to comment.