Package linda :: Package parser :: Module clparser
[hide private]

Source Code for Module linda.parser.clparser

  1  import os, sys, re, getopt 
  2   
3 -class CLParser:
4 - def __init__(self):
5 self.arguments = {} 6 self._opts = None 7 self._args = None
8
9 - def parse_args(self):
10 try: 11 self._opts, self._args = getopt.getopt(sys.argv[1:], \ 12 'c:C:dDf:hiIl:L:m:nopPqsSt:Tu:vV', ['config=', 'checks=', \ 13 'debug', 'disable-local', 'format=', 'help', 'info', \ 14 'informational', 'lab-root=', 'list-checks=', \ 15 'more-overrides=', 'no-cull', 'show-overridden', \ 16 'print-overrides', 'profiling', 'quiet', 'show-tag', \ 17 'seteuid', 'types=', 'traceback', 'unpack=', 'verbose', \ 18 'version']) 19 except getopt.GetoptError: 20 self.usage(exitstatus=7) 21 self.parse() 22 self.set_defaults() 23 self.add_files()
24
25 - def parse(self):
26 for o, a in self._opts: 27 if o in ('-c', '--config'): 28 if os.path.exists(os.path.abspath(a)): 29 self.arguments['config'] = os.path.abspath(a) 30 else: 31 raise CLParsingException, "Config file doesn't exist" 32 elif o in ('-C', '--checks'): 33 self.arguments['checks'] = a 34 elif o in ('-d', '--debug'): 35 if self.arguments.has_key('debug'): 36 self.arguments['debug'] += 1 37 else: 38 self.arguments['debug'] = 1 39 elif o in ('-D', '--disable-local'): 40 self.arguments['disable-local'] = 1 41 elif o in ('-f', '--format'): 42 self.arguments['format'] = a 43 elif o in ('-h', '--help'): 44 self.usage() 45 elif o in ('-i', '--info'): 46 self.arguments['info'] = 1 47 elif o in ('-I', '--informational'): 48 print _("N: Informational is deprecated, use -t E,W,I") 49 elif o in ('-l', '--lab'): 50 if os.path.isdir(os.path.abspath(a)): 51 self.arguments['lab-root'] = os.path.abspath(a) 52 else: 53 raise CLParsingException, "Lab dir doesn't exist" 54 elif o in ('-L', '--list-checks'): 55 if a == 'all': 56 self.arguments['list-checks'] = (('binary', 'source'), \ 57 (1, 2)) 58 elif a in ('bin1', 'bin2', 'src1', 'src2'): 59 mapping = {'bin': 'binary', 'src': 'source'} 60 self.arguments['list-checks'] = ((mapping[a[:-1]],), \ 61 (int(a[-1]),)) 62 else: 63 raise CLParsingException, \ 64 "%s is unknown arg to list checks" % a 65 elif o in ('-m', '--more-overrides'): 66 if os.path.isfile(os.path.abspath(a)): 67 self.arguments['more-overrides'] = os.path.abspath(a) 68 else: 69 raise CLParsingException, "Overrides file doesn't exist" 70 elif o in ('-n', '--no-cull'): 71 self.arguments['no-cull'] = 1 72 elif o in ('-o', '--show-overridden'): 73 self.arguments['show-overridden'] = 1 74 elif o in ('-p', '--print-overrides'): 75 self.arguments['print-overrides'] = 1 76 elif o in ('-P', '--profiling'): 77 self.arguments['profiling'] = 1 78 elif o in ('-q', '--quiet'): 79 self.arguments['quiet'] = 1 80 elif o in ('-s', '--show-tag'): 81 self.arguments['show-tag'] = 1 82 elif o in ('-S', '--seteuid'): 83 self.arguments['seteuid'] = 1 84 elif o in ('-t', '--types'): 85 if not self.arguments.has_key('types'): 86 self.arguments['types'] = [] 87 for type in re.split(r', ?', a): 88 if type in ('E', 'W', 'X', 'I'): 89 self.arguments['types'].append(type) 90 if type == 'X' and self.arguments.has_key('quiet'): 91 print _("N: eXperimental type enabled.") 92 else: 93 raise CLParsingException, \ 94 "'%s' isn't a valid type" % type 95 elif o in ('-T', '--traceback'): 96 self.arguments['traceback'] = 1 97 elif o in ('-u', '--unpack'): 98 if a in ('1', '2'): 99 self.arguments['unpack'] = int(a) 100 self.arguments['no-cull'] = 1 101 else: 102 raise CLParsingException, "Unpack level must be 1 or 2" 103 elif o in ('-v', '--verbose'): 104 if self.arguments.has_key('verbose'): 105 self.arguments['verbose'] += 1 106 else: 107 self.arguments['verbose'] = 1 108 elif o in ('-V', '--version'): 109 print _("Linda, version %s") % "0.3.26ubuntu1" 110 sys.exit(0)
111
112 - def add_files(self):
113 usable_files = filter(os.path.exists, self._args) 114 self.arguments['files'] = [] 115 for i in usable_files: 116 extension = os.path.splitext(i)[1] 117 if extension in ('.deb', '.udeb', '.dsc', '.changes'): 118 self.arguments['files'].append(os.path.abspath(i))
119
120 - def set_defaults(self):
121 for key in ('debug', 'disable-local', 'info', 'no-cull', \ 122 'show-overridden', 'print-overrides', 'profiling', 'quiet', \ 123 'seteuid', 'traceback', 'verbose'): 124 if self[key] == '': 125 self.arguments[key] = 0 126 if not self.arguments.has_key('types'): 127 self.arguments['types'] = ['E', 'W']
128
129 - def __getitem__(self, key):
130 if self.arguments.has_key(key): 131 return self.arguments[key] 132 else: 133 return ''
134
135 - def __setitem__(self, key, data):
136 self.arguments[key] = data
137
138 - def usage(self, exitstatus=0):
139 print \ 140 _("""Linda, a Debian package checker. 141 142 Usage: linda [options] [.dsc|.deb|.udeb|.changes] ... 143 -c CONFIG, --config=CONFIG := Specify config directory 144 -C CHECK_STRING, --checks=CHECK_STRING := Specify checks to run 145 -d, --debug := Set debug 146 -D, --disable-local := Don't register local checks 147 -f FORMAT, --format=FORMAT := Set output format, -f help lists them 148 -h, --help := Show this help, and exit 149 -i, --info := Show more information about errors and warnings 150 -l LAB_ROOT, --lab=LAB_ROOT := Set the directory the lab should be in 151 -L LIST_CHECKS, --list-checks=LIST_CHECKS := List checks and exit 152 -m MORE_OVERRIDES, --more-overrides=MORE_OVERRIDES := Also parse overrides from 153 this file 154 -n, --no-cull := Don't delete the lab after tests are completed 155 -o, --show-overridden := Display errors that are overridden 156 -p, --print-overrides := Print parsed overrides and exit 157 -P, --profiling := Print some profiling stats at exit 158 -q, --quiet := Quieten me a little bit 159 -s, --show-tag := Also print out the tag when printing errors 160 -S, --seteuid := Don't seteuid() to nobody when running as root 161 -t TYPES_STRING, --types=TYPES_STRING := Specify the types to show. Default is 162 E,W,X 163 -T, --traceback := Show a traceback from any exception 164 -u UNPACK_LEVEL, --unpack=UNPACK_LEVEL := Set unpack level, implies -n 165 -v, --verbose := Set verbose 166 -V, --version := Display version 167 """) 168 sys.exit(exitstatus)
169
170 -class CLParsingException(Exception):
171 pass
172