1 |
From: Alec Warner <antarus@g.o> |
2 |
|
3 |
We want a default for the config file. We can handle this in main() by simply |
4 |
setting myconfig to the default. If the user over-rides it with -c, we will take |
5 |
the user provided value. |
6 |
|
7 |
Delete a bunch of duplicate code. |
8 |
Add docstring for parse_config. |
9 |
Add warning that parse_config modifies global state. |
10 |
Add TODO's about bad habits, like randomly calling sys.exit() in your code. |
11 |
|
12 |
Calling stuff 'myFOO' is really annoying, particularly when you have a function such |
13 |
as this and you have variables like: |
14 |
|
15 |
myconfig |
16 |
myconf |
17 |
conf_values |
18 |
|
19 |
and you have to tell them apart ;) |
20 |
|
21 |
-A |
22 |
--- |
23 |
catalyst | 50 +++++++++++++++++++++----------------------------- |
24 |
1 file changed, 21 insertions(+), 29 deletions(-) |
25 |
|
26 |
diff --git a/catalyst b/catalyst |
27 |
index edc0b24..47efb90 100755 |
28 |
--- a/catalyst |
29 |
+++ b/catalyst |
30 |
@@ -57,11 +57,18 @@ def version(): |
31 |
print "Copyright 2008-2012 various authors" |
32 |
print "Distributed under the GNU General Public License version 2.1\n" |
33 |
|
34 |
-def parse_config(myconfig): |
35 |
- # search a couple of different areas for the main config file |
36 |
- myconf={} |
37 |
- config_file="" |
38 |
- |
39 |
+def parse_config(config_path): |
40 |
+ """Parse the catalyst config file and generate a targetmap. |
41 |
+ |
42 |
+ TODO(antarus): DANGER: This file modifies global state (conf_values). |
43 |
+ |
44 |
+ Args: |
45 |
+ config_path - A path to a catalyst config file. |
46 |
+ Returns: |
47 |
+ None |
48 |
+ """ |
49 |
+ |
50 |
+ # TODO(antarus): Move this outside of this function. |
51 |
confdefaults = { |
52 |
"distdir": "/usr/portage/distfiles", |
53 |
"hash_function": "crc32", |
54 |
@@ -75,33 +82,18 @@ def parse_config(myconfig): |
55 |
"storedir": "/var/tmp/catalyst", |
56 |
} |
57 |
|
58 |
- # first, try the one passed (presumably from the cmdline) |
59 |
- if myconfig: |
60 |
- if os.path.exists(myconfig): |
61 |
- print "Using command line specified Catalyst configuration file, "+myconfig |
62 |
- config_file=myconfig |
63 |
- |
64 |
- else: |
65 |
- print "!!! catalyst: Could not use specified configuration file "+\ |
66 |
- myconfig |
67 |
- sys.exit(1) |
68 |
- |
69 |
- # next, try the default location |
70 |
- elif os.path.exists("/etc/catalyst/catalyst.conf"): |
71 |
- print "Using default Catalyst configuration file, /etc/catalyst/catalyst.conf" |
72 |
- config_file="/etc/catalyst/catalyst.conf" |
73 |
|
74 |
- # can't find a config file (we are screwed), so bail out |
75 |
- else: |
76 |
- print "!!! catalyst: Could not find a suitable configuration file" |
77 |
+ try: |
78 |
+ print "Trying config file '%s'" % config_path |
79 |
+ cfg = modules.catalyst.config.ConfigParser(config_path) |
80 |
+ except EnvironmentError as e: |
81 |
+ print "Could not read config file '%s', got error '%s'" % (config_path, e) |
82 |
+ # TODO(antarus): it should be discouraged to call sys.exit() from functions. |
83 |
+ # One should call sys.exit() from main()..this isn't main(). |
84 |
sys.exit(1) |
85 |
|
86 |
- # now, try and parse the config file "config_file" |
87 |
try: |
88 |
-# execfile(config_file, myconf, myconf) |
89 |
- myconfig = modules.catalyst.config.ConfigParser(config_file) |
90 |
- myconf.update(myconfig.get_values()) |
91 |
- |
92 |
+ myconf.update(cfg.get_values()) |
93 |
except: |
94 |
print "!!! catalyst: Unable to parse configuration file, "+myconfig |
95 |
sys.exit(1) |
96 |
@@ -258,7 +250,7 @@ if __name__ == "__main__": |
97 |
debug=False |
98 |
verbose=False |
99 |
fetch=False |
100 |
- myconfig="" |
101 |
+ myconfig="/etc/catalyst/catalyst.conf" |
102 |
myspecfile="" |
103 |
mycmdline=[] |
104 |
myopts=[] |
105 |
-- |
106 |
1.8.1.2 |