Skip to content
Snippets Groups Projects
Commit 2241e2b9 authored by Iustin Pop's avatar Iustin Pop
Browse files

Add a simple timespec parsing function

This function will be used for auto-archiving jobs via the command line.
The function is pretty simple, we only support up to weeks since months
and higher are not 'precise' entities, and dealing with them would
require us to start using calendar functions.

Reviewed-by: imsnah
parent 62c9ec92
No related branches found
No related tags found
No related merge requests found
......@@ -202,7 +202,8 @@ dist_TESTS = \
test/ganeti.workerpool_unittest.py \
test/ganeti.rapi.resources_unittest.py \
test/ganeti.http_unittest.py \
test/ganeti.constants_unittest.py
test/ganeti.constants_unittest.py \
test/ganeti.cli_unittest.py
nodist_TESTS =
......
......@@ -707,3 +707,45 @@ def FormatTimestamp(ts):
return '?'
sec, usec = ts
return time.strftime("%F %T", time.localtime(sec)) + ".%06d" % usec
def ParseTimespec(value):
"""Parse a time specification.
The following suffixed will be recognized:
- s: seconds
- m: minutes
- h: hours
- d: day
- w: weeks
Without any suffix, the value will be taken to be in seconds.
"""
value = str(value)
if not value:
raise errors.OpPrereqError("Empty time specification passed")
suffix_map = {
's': 1,
'm': 60,
'h': 3600,
'd': 86400,
'w': 604800,
}
if value[-1] not in suffix_map:
try:
value = int(value)
except ValueError:
raise errors.OpPrereqError("Invalid time specification '%s'" % value)
else:
multiplier = suffix_map[value[-1]]
value = value[:-1]
if not value: # no data left after stripping the suffix
raise errors.OpPrereqError("Invalid time specification (only"
" suffix passed)")
try:
value = int(value) * multiplier
except ValueError:
raise errors.OpPrereqError("Invalid time specification '%s'" % value)
return value
#!/usr/bin/python
#
# Copyright (C) 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
"""Script for unittesting the cli module"""
import unittest
import ganeti
import testutils
from ganeti import constants
from ganeti import cli
from ganeti.errors import OpPrereqError
class TestParseTimespec(unittest.TestCase):
"""Testing case for ParseTimespec"""
def testValidTimes(self):
"""Test valid timespecs"""
test_data = [
('1s', 1),
('1', 1),
('1m', 60),
('1h', 60 * 60),
('1d', 60 * 60 * 24),
('1w', 60 * 60 * 24 * 7),
('4h', 4 * 60 * 60),
('61m', 61 * 60),
]
for value, expected_result in test_data:
self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
def testInvalidTime(self):
"""Test invalid timespecs"""
test_data = [
'1y',
'',
'aaa',
's',
]
for value in test_data:
self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment