Lines 15-125
except ImportError:
Link Here
|
15 |
|
15 |
|
16 |
from portage.output import * |
16 |
from portage.output import * |
17 |
|
17 |
|
18 |
from getopt import getopt, GetoptError |
18 |
from optparse import OptionGroup, OptionParser |
19 |
|
19 |
|
20 |
__program__ = "glsa-check" |
20 |
__program__ = "glsa-check" |
21 |
__author__ = "Marius Mauch <genone@gentoo.org>" |
21 |
__author__ = "Marius Mauch <genone@gentoo.org>" |
22 |
__version__ = "1.0" |
22 |
__version__ = "1.0" |
23 |
|
23 |
|
24 |
optionmap = [ |
24 |
def cb_version(*args, **kwargs): |
25 |
["-l", "--list", "list all unapplied GLSA"], |
25 |
"""Callback for --version""" |
26 |
["-d", "--dump", "--print", "show all information about the given GLSA"], |
26 |
sys.stderr.write("\n"+ __program__ + ", version " + __version__ + "\n") |
27 |
["-t", "--test", "test if this system is affected by the given GLSA"], |
27 |
sys.stderr.write("Author: " + __author__ + "\n") |
28 |
["-p", "--pretend", "show the necessary commands to apply this GLSA"], |
28 |
sys.stderr.write("This program is licensed under the GPL, version 2\n\n") |
29 |
["-f", "--fix", "try to auto-apply this GLSA (experimental)"], |
29 |
sys.exit(0) |
30 |
["-i", "--inject", "inject the given GLSA into the checkfile"], |
|
|
31 |
["-n", "--nocolor", "disable colors (option)"], |
32 |
["-e", "--emergelike", "do not use a least-change algorithm (option)"], |
33 |
["-h", "--help", "show this help message"], |
34 |
["-V", "--version", "some information about this tool"], |
35 |
["-v", "--verbose", "print more information (option)"], |
36 |
["-c", "--cve", "show CAN ids in listing mode (option)"], |
37 |
["-m", "--mail", "send a mail with the given GLSAs to the administrator"] |
38 |
] |
39 |
|
30 |
|
40 |
# option parsing |
31 |
# option parsing |
41 |
args = [] |
32 |
parser = OptionParser(usage="%prog <option> [glsa-list]", |
42 |
params = [] |
33 |
version="%prog "+ __version__) |
43 |
try: |
34 |
parser.epilog = "glsa-list can contain an arbitrary number of GLSA ids," \ |
44 |
args, params = getopt(sys.argv[1:], "".join([o[0][1] for o in optionmap]), \ |
35 |
" filenames containing GLSAs or the special identifiers" \ |
45 |
[x[2:] for x in reduce(lambda x,y: x+y, [z[1:-1] for z in optionmap])]) |
36 |
" 'all', 'new' and 'affected'" |
46 |
# ["dump", "print", "list", "pretend", "fix", "inject", "help", "verbose", "version", "test", "nocolor", "cve", "mail"]) |
37 |
|
47 |
args = [a for a,b in args] |
38 |
modes = OptionGroup(parser, "Modes") |
48 |
|
39 |
modes.add_option("-l", "--list", action="store_const", |
49 |
for option in ["--nocolor", "-n"]: |
40 |
const="list", dest="mode", |
50 |
if option in args: |
41 |
help="List all unapplied GLSA") |
51 |
nocolor() |
42 |
modes.add_option("-d", "--dump", action="store_const", |
52 |
args.remove(option) |
43 |
const="dump", dest="mode", |
53 |
|
44 |
help="Show all information about the given GLSA") |
54 |
verbose = False |
45 |
modes.add_option("", "--print", action="store_const", |
55 |
for option in ["--verbose", "-v"]: |
46 |
const="dump", dest="mode", |
56 |
if option in args: |
47 |
help="Alias for --dump") |
57 |
verbose = True |
48 |
modes.add_option("-t", "--test", action="store_const", |
58 |
args.remove(option) |
49 |
const="test", dest="mode", |
59 |
|
50 |
help="Test if this system is affected by the given GLSA") |
60 |
list_cve = False |
51 |
modes.add_option("-p", "--pretend", action="store_const", |
61 |
for option in ["--cve", "-c"]: |
52 |
const="pretend", dest="mode", |
62 |
if option in args: |
53 |
help="Show the necessary commands to apply this GLSA") |
63 |
list_cve = True |
54 |
modes.add_option("-f", "--fix", action="store_const", |
64 |
args.remove(option) |
55 |
const="fix", dest="mode", |
65 |
|
56 |
help="Try to auto-apply this GLSA (experimental)") |
66 |
least_change = True |
57 |
modes.add_option("-i", "--inject", action="store_const", dest="mode", |
67 |
for option in ["--emergelike", "-e"]: |
58 |
help="Inject the given GLSA into the checkfile") |
68 |
if option in args: |
59 |
modes.add_option("-m", "--mail", action="store_const", |
69 |
least_change = False |
60 |
const="mail", dest="mode", |
70 |
args.remove(option) |
61 |
help="Send a mail with the given GLSAs to the administrator") |
71 |
|
62 |
parser.add_option_group(modes) |
72 |
# sanity checking |
63 |
|
73 |
if len(args) <= 0: |
64 |
parser.remove_option("--version") |
74 |
sys.stderr.write("no option given: what should I do ?\n") |
65 |
parser.add_option("-V", "--version", action="callback", |
75 |
mode="help" |
66 |
callback=cb_version, help="Some information about this tool") |
76 |
elif len(args) > 1: |
67 |
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", |
77 |
sys.stderr.write("please use only one command per call\n") |
68 |
help="Print more information") |
78 |
mode = "help" |
69 |
parser.add_option("-n", "--nocolor", action="callback", |
79 |
else: |
70 |
callback=lambda *args, **kwargs: nocolor(), |
80 |
# in what mode are we ? |
71 |
help="Disable colors") |
81 |
args = args[0] |
72 |
parser.add_option("", "--nocolour", action="callback", |
82 |
for m in optionmap: |
73 |
callback=lambda *args, **kwargs: nocolor(), |
83 |
if args in [o for o in m[:-1]]: |
74 |
help="Alias for --nocolor") |
84 |
mode = m[1][2:] |
75 |
parser.add_option("-e", "--emergelike", action="store_false", dest="least_change", |
85 |
|
76 |
help="Do not use a least-change algorithm") |
86 |
except GetoptError, e: |
77 |
parser.add_option("-c", "--cve", action="store_true", dest="list_cve", |
87 |
sys.stderr.write("unknown option given: ") |
78 |
help="Show CAN ids in listing mode") |
88 |
sys.stderr.write(str(e)+"\n") |
79 |
|
89 |
mode = "help" |
80 |
options, params = parser.parse_args() |
90 |
|
81 |
|
91 |
# we need a set of glsa for most operation modes |
82 |
mode = options.mode |
92 |
if len(params) <= 0 and mode in ["fix", "test", "pretend", "dump", "inject", "mail"]: |
83 |
least_change = options.least_change |
|
|
84 |
list_cve = options.list_cve |
85 |
verbose = options.verbose |
86 |
|
87 |
# Sanity checking |
88 |
if mode is None: |
89 |
sys.stderr.write("No mode given: what should I do?\n") |
90 |
parser.print_help() |
91 |
sys.exit(1) |
92 |
elif mode != "list" and not params: |
93 |
sys.stderr.write("\nno GLSA given, so we'll do nothing for now. \n") |
93 |
sys.stderr.write("\nno GLSA given, so we'll do nothing for now. \n") |
94 |
sys.stderr.write("If you want to run on all GLSA please tell me so \n") |
94 |
sys.stderr.write("If you want to run on all GLSA please tell me so \n") |
95 |
sys.stderr.write("(specify \"all\" as parameter)\n\n") |
95 |
sys.stderr.write("(specify \"all\" as parameter)\n\n") |
96 |
mode = "help" |
96 |
parser.print_help() |
97 |
elif len(params) <= 0 and mode == "list": |
|
|
98 |
params.append("new") |
99 |
|
100 |
# show help message |
101 |
if mode == "help": |
102 |
sys.stderr.write("\nSyntax: glsa-check <option> [glsa-list]\n\n") |
103 |
for m in optionmap: |
104 |
sys.stderr.write(m[0] + "\t" + m[1] + " \t: " + m[-1] + "\n") |
105 |
for o in m[2:-1]: |
106 |
sys.stderr.write("\t" + o + "\n") |
107 |
sys.stderr.write("\nglsa-list can contain an arbitrary number of GLSA ids, \n") |
108 |
sys.stderr.write("filenames containing GLSAs or the special identifiers \n") |
109 |
sys.stderr.write("'all', 'new' and 'affected'\n") |
110 |
sys.exit(1) |
97 |
sys.exit(1) |
111 |
|
98 |
elif mode in ["fix", "inject"] and os.geteuid() != 0: |
112 |
# we need root priviledges for write access |
99 |
# we need root priviledges for write access |
113 |
if mode in ["fix", "inject"] and os.geteuid() != 0: |
100 |
sys.stderr.write("\nThis tool needs root access to "+options.mode+" this GLSA\n\n") |
114 |
sys.stderr.write("\nThis tool needs root access to "+mode+" this GLSA\n\n") |
|
|
115 |
sys.exit(2) |
101 |
sys.exit(2) |
116 |
|
102 |
elif mode == "list" and not params: |
117 |
# show version and copyright information |
103 |
params.append("new") |
118 |
if mode == "version": |
|
|
119 |
sys.stderr.write("\n"+ __program__ + ", version " + __version__ + "\n") |
120 |
sys.stderr.write("Author: " + __author__ + "\n") |
121 |
sys.stderr.write("This program is licensed under the GPL, version 2\n\n") |
122 |
sys.exit(0) |
123 |
|
104 |
|
124 |
# delay this for speed increase |
105 |
# delay this for speed increase |
125 |
from portage.glsa import * |
106 |
from portage.glsa import * |