Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python -tt |
| 2 | # |
| 3 | # Copyright (c) 2010, 2011 Intel Inc. |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify it |
| 6 | # under the terms of the GNU General Public License as published by the Free |
| 7 | # Software Foundation; version 2 of the License |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, but |
| 10 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 11 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | # for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License along |
| 15 | # with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 16 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | |
| 18 | import os |
| 19 | import time |
| 20 | |
| 21 | def build_name(kscfg, release=None, prefix=None, suffix=None): |
| 22 | """Construct and return an image name string. |
| 23 | |
| 24 | This is a utility function to help create sensible name and fslabel |
| 25 | strings. The name is constructed using the sans-prefix-and-extension |
| 26 | kickstart filename and the supplied prefix and suffix. |
| 27 | |
| 28 | kscfg -- a path to a kickstart file |
| 29 | release -- a replacement to suffix for image release |
| 30 | prefix -- a prefix to prepend to the name; defaults to None, which causes |
| 31 | no prefix to be used |
| 32 | suffix -- a suffix to append to the name; defaults to None, which causes |
| 33 | a YYYYMMDDHHMM suffix to be used |
| 34 | |
| 35 | Note, if maxlen is less then the len(suffix), you get to keep both pieces. |
| 36 | |
| 37 | """ |
| 38 | name = os.path.basename(kscfg) |
| 39 | idx = name.rfind('.') |
| 40 | if idx >= 0: |
| 41 | name = name[:idx] |
| 42 | |
| 43 | if release is not None: |
| 44 | suffix = "" |
| 45 | if prefix is None: |
| 46 | prefix = "" |
| 47 | if suffix is None: |
| 48 | suffix = time.strftime("%Y%m%d%H%M") |
| 49 | |
| 50 | if name.startswith(prefix): |
| 51 | name = name[len(prefix):] |
| 52 | |
| 53 | prefix = "%s-" % prefix if prefix else "" |
| 54 | suffix = "-%s" % suffix if suffix else "" |
| 55 | |
| 56 | ret = prefix + name + suffix |
| 57 | |
| 58 | return ret |