diff options
Diffstat (limited to 'scripts/lib/wic/ksparser.py')
| -rw-r--r-- | scripts/lib/wic/ksparser.py | 136 | 
1 files changed, 136 insertions, 0 deletions
| diff --git a/scripts/lib/wic/ksparser.py b/scripts/lib/wic/ksparser.py new file mode 100644 index 0000000000..7dbe052714 --- /dev/null +++ b/scripts/lib/wic/ksparser.py | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | #!/usr/bin/env python -tt | ||
| 2 | # ex:ts=4:sw=4:sts=4:et | ||
| 3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- | ||
| 4 | # | ||
| 5 | # Copyright (c) 2016 Intel, Inc. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or modify it | ||
| 8 | # under the terms of the GNU General Public License as published by the Free | ||
| 9 | # Software Foundation; version 2 of the License | ||
| 10 | # | ||
| 11 | # This program is distributed in the hope that it will be useful, but | ||
| 12 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 13 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | # for more details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the GNU General Public License along | ||
| 17 | # with this program; if not, write to the Free Software Foundation, Inc., 59 | ||
| 18 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 19 | # | ||
| 20 | # DESCRIPTION | ||
| 21 | # This module provides parser for kickstart format | ||
| 22 | # | ||
| 23 | # AUTHORS | ||
| 24 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> | ||
| 25 | # Ed Bartosh <ed.bartosh> (at] linux.intel.com> | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | import shlex | ||
| 30 | from argparse import ArgumentParser, ArgumentError, ArgumentTypeError | ||
| 31 | |||
| 32 | from wic.partition import Partition | ||
| 33 | |||
| 34 | class KickStartError(Exception): | ||
| 35 | pass | ||
| 36 | |||
| 37 | class KickStartParser(ArgumentParser): | ||
| 38 | """ | ||
| 39 | This class overwrites error method to throw exception | ||
| 40 | instead of producing usage message(default argparse behavior). | ||
| 41 | """ | ||
| 42 | def error(self, message): | ||
| 43 | raise ArgumentError(None, message) | ||
| 44 | |||
| 45 | def sizetype(arg): | ||
| 46 | """ | ||
| 47 | Custom type for ArgumentParser | ||
| 48 | Converts size string in <num>[K|k|M|G] format into the integer value | ||
| 49 | """ | ||
| 50 | if arg.isdigit(): | ||
| 51 | return int(arg) * 1024L | ||
| 52 | |||
| 53 | if not arg[:-1].isdigit(): | ||
| 54 | raise ArgumentTypeError("Invalid size: %r" % arg) | ||
| 55 | |||
| 56 | size = int(arg[:-1]) | ||
| 57 | if arg.endswith("k") or arg.endswith("K"): | ||
| 58 | return size | ||
| 59 | if arg.endswith("M"): | ||
| 60 | return size * 1024L | ||
| 61 | if arg.endswith("G"): | ||
| 62 | return size * 1024L * 1024L | ||
| 63 | |||
| 64 | raise ArgumentTypeError("Invalid size: %r" % arg) | ||
| 65 | |||
| 66 | def overheadtype(arg): | ||
| 67 | """ | ||
| 68 | Custom type for ArgumentParser | ||
| 69 | Converts overhead string to float and checks if it's bigger than 1.0 | ||
| 70 | """ | ||
| 71 | try: | ||
| 72 | result = float(arg) | ||
| 73 | except ValueError: | ||
| 74 | raise ArgumentTypeError("Invalid value: %r" % arg) | ||
| 75 | |||
| 76 | if result < 1.0: | ||
| 77 | raise ArgumentTypeError("Overhead factor should be > 1.0" % arg) | ||
| 78 | |||
| 79 | return result | ||
| 80 | |||
| 81 | class KickStart(object): | ||
| 82 | def __init__(self, confpath): | ||
| 83 | |||
| 84 | self.partitions = [] | ||
| 85 | self.bootloader = None | ||
| 86 | self.lineno = 0 | ||
| 87 | |||
| 88 | parser = KickStartParser() | ||
| 89 | subparsers = parser.add_subparsers() | ||
| 90 | |||
| 91 | part = subparsers.add_parser('part') | ||
| 92 | part.add_argument('mountpoint') | ||
| 93 | part.add_argument('--active', action='store_true') | ||
| 94 | part.add_argument('--align', type=int) | ||
| 95 | part.add_argument("--extra-space", type=sizetype, default=10*1024L) | ||
| 96 | part.add_argument('--fsoptions', dest='fsopts') | ||
| 97 | part.add_argument('--fstype') | ||
| 98 | part.add_argument('--label') | ||
| 99 | part.add_argument('--no-table') | ||
| 100 | part.add_argument('--ondisk', '--ondrive', dest='disk') | ||
| 101 | part.add_argument("--overhead-factor", type=overheadtype, default=1.3) | ||
| 102 | part.add_argument('--part-type') | ||
| 103 | part.add_argument('--rootfs-dir') | ||
| 104 | part.add_argument('--size', type=sizetype, default=0) | ||
| 105 | part.add_argument('--source') | ||
| 106 | part.add_argument('--sourceparams') | ||
| 107 | part.add_argument('--use-uuid', action='store_true') | ||
| 108 | part.add_argument('--uuid') | ||
| 109 | |||
| 110 | bootloader = subparsers.add_parser('bootloader') | ||
| 111 | bootloader.add_argument('--append') | ||
| 112 | bootloader.add_argument('--configfile') | ||
| 113 | bootloader.add_argument('--ptable', choices=('msdos', 'gpt'), | ||
| 114 | default='msdos') | ||
| 115 | bootloader.add_argument('--timeout', type=int) | ||
| 116 | bootloader.add_argument('--source') | ||
| 117 | |||
| 118 | with open(confpath) as conf: | ||
| 119 | lineno = 0 | ||
| 120 | for line in conf: | ||
| 121 | line = line.strip() | ||
| 122 | lineno += 1 | ||
| 123 | if line and line[0] != '#': | ||
| 124 | try: | ||
| 125 | parsed = parser.parse_args(shlex.split(line)) | ||
| 126 | except ArgumentError as err: | ||
| 127 | raise KickStartError('%s:%d: %s' % \ | ||
| 128 | (confpath, lineno, err)) | ||
| 129 | if line.startswith('part'): | ||
| 130 | self.partitions.append(Partition(parsed, lineno)) | ||
| 131 | else: | ||
| 132 | if not self.bootloader: | ||
| 133 | self.bootloader = parsed | ||
| 134 | else: | ||
| 135 | raise KickStartError("%s:%d: more than one bootloader "\ | ||
| 136 | "specified" % (confpath, lineno)) | ||
