1
- import os , glob
1
+ import glob
2
+ import os
3
+ import subprocess
2
4
import sys
3
-
4
5
from argparse import ArgumentParser , RawTextHelpFormatter
6
+
7
+ import dotbot
8
+
5
9
from .config import ConfigReader , ReadingError
6
10
from .dispatcher import Dispatcher , DispatchError
7
- from .messenger import Messenger
8
- from .messenger import Level
11
+ from .messenger import Level , Messenger
12
+ from .plugins import Clean , Create , Link , Plugins , Shell
9
13
from .util import module
10
14
11
- import dotbot
12
- import os
13
- import subprocess
14
15
15
16
def add_options (parser ):
16
- parser .add_argument ('-Q' , '--super-quiet' , action = 'store_true' ,
17
- help = 'suppress almost all output' )
18
- parser .add_argument ('-q' , '--quiet' , action = 'store_true' ,
19
- help = 'suppress most output' )
20
- parser .add_argument ('-v' , '--verbose' , action = 'count' , default = 0 ,
21
- help = 'enable verbose output\n '
22
- '-v: typical verbose\n '
23
- '-vv: also, set shell commands stderr/stdout to true' )
24
- parser .add_argument ('-d' , '--base-directory' ,
25
- help = 'execute commands from within BASEDIR' ,
26
- metavar = 'BASEDIR' )
27
- parser .add_argument ('-c' , '--config-file' ,
28
- help = 'run commands given in CONFIGFILE' , metavar = 'CONFIGFILE' )
29
- parser .add_argument ('-p' , '--plugin' , action = 'append' , dest = 'plugins' , default = [],
30
- help = 'load PLUGIN as a plugin' , metavar = 'PLUGIN' )
31
- parser .add_argument ('--disable-built-in-plugins' ,
32
- action = 'store_true' , help = 'disable built-in plugins' )
33
- parser .add_argument ('--plugin-dir' , action = 'append' , dest = 'plugin_dirs' , default = [],
34
- metavar = 'PLUGIN_DIR' , help = 'load all plugins in PLUGIN_DIR' )
35
- parser .add_argument ('--only' , nargs = '+' ,
36
- help = 'only run specified directives' , metavar = 'DIRECTIVE' )
37
- parser .add_argument ('--except' , nargs = '+' , dest = 'skip' ,
38
- help = 'skip specified directives' , metavar = 'DIRECTIVE' )
39
- parser .add_argument ('--force-color' , dest = 'force_color' , action = 'store_true' ,
40
- help = 'force color output' )
41
- parser .add_argument ('--no-color' , dest = 'no_color' , action = 'store_true' ,
42
- help = 'disable color output' )
43
- parser .add_argument ('--version' , action = 'store_true' ,
44
- help = 'show program\' s version number and exit' )
45
- parser .add_argument ('-x' , '--exit-on-failure' , dest = 'exit_on_failure' , action = 'store_true' ,
46
- help = 'exit after first failed directive' )
17
+ parser .add_argument (
18
+ "-Q" , "--super-quiet" , action = "store_true" , help = "suppress almost all output"
19
+ )
20
+ parser .add_argument ("-q" , "--quiet" , action = "store_true" , help = "suppress most output" )
21
+ parser .add_argument (
22
+ "-v" ,
23
+ "--verbose" ,
24
+ action = "count" ,
25
+ default = 0 ,
26
+ help = "enable verbose output\n "
27
+ "-v: typical verbose\n "
28
+ "-vv: also, set shell commands stderr/stdout to true" ,
29
+ )
30
+ parser .add_argument (
31
+ "-d" , "--base-directory" , help = "execute commands from within BASEDIR" , metavar = "BASEDIR"
32
+ )
33
+ parser .add_argument (
34
+ "-c" , "--config-file" , help = "run commands given in CONFIGFILE" , metavar = "CONFIGFILE"
35
+ )
36
+ parser .add_argument (
37
+ "-p" ,
38
+ "--plugin" ,
39
+ action = "append" ,
40
+ dest = "plugins" ,
41
+ default = [],
42
+ help = "load PLUGIN as a plugin" ,
43
+ metavar = "PLUGIN" ,
44
+ )
45
+ parser .add_argument (
46
+ "--disable-built-in-plugins" , action = "store_true" , help = "disable built-in plugins"
47
+ )
48
+ parser .add_argument (
49
+ "--plugin-dir" ,
50
+ action = "append" ,
51
+ dest = "plugin_dirs" ,
52
+ default = [],
53
+ metavar = "PLUGIN_DIR" ,
54
+ help = "load all plugins in PLUGIN_DIR" ,
55
+ )
56
+ parser .add_argument (
57
+ "--only" , nargs = "+" , help = "only run specified directives" , metavar = "DIRECTIVE"
58
+ )
59
+ parser .add_argument (
60
+ "--except" , nargs = "+" , dest = "skip" , help = "skip specified directives" , metavar = "DIRECTIVE"
61
+ )
62
+ parser .add_argument (
63
+ "--force-color" , dest = "force_color" , action = "store_true" , help = "force color output"
64
+ )
65
+ parser .add_argument (
66
+ "--no-color" , dest = "no_color" , action = "store_true" , help = "disable color output"
67
+ )
68
+ parser .add_argument (
69
+ "--version" , action = "store_true" , help = "show program's version number and exit"
70
+ )
71
+ parser .add_argument (
72
+ "-x" ,
73
+ "--exit-on-failure" ,
74
+ dest = "exit_on_failure" ,
75
+ action = "store_true" ,
76
+ help = "exit after first failed directive" ,
77
+ )
78
+
47
79
48
80
def read_config (config_file ):
49
81
reader = ConfigReader (config_file )
50
82
return reader .get_config ()
51
83
84
+
52
85
def main ():
53
86
log = Messenger ()
54
87
try :
@@ -58,12 +91,15 @@ def main():
58
91
if options .version :
59
92
try :
60
93
with open (os .devnull ) as devnull :
61
- git_hash = subprocess .check_output (['git' , 'rev-parse' , 'HEAD' ],
62
- cwd = os .path .dirname (os .path .abspath (__file__ )), stderr = devnull )
63
- hash_msg = ' (git %s)' % git_hash [:10 ]
94
+ git_hash = subprocess .check_output (
95
+ ["git" , "rev-parse" , "HEAD" ],
96
+ cwd = os .path .dirname (os .path .abspath (__file__ )),
97
+ stderr = devnull ,
98
+ )
99
+ hash_msg = " (git %s)" % git_hash [:10 ]
64
100
except (OSError , subprocess .CalledProcessError ):
65
- hash_msg = ''
66
- print (' Dotbot version %s%s' % (dotbot .__version__ , hash_msg ))
101
+ hash_msg = ""
102
+ print (" Dotbot version %s%s" % (dotbot .__version__ , hash_msg ))
67
103
exit (0 )
68
104
if options .super_quiet :
69
105
log .set_level (Level .WARNING )
@@ -82,43 +118,50 @@ def main():
82
118
else :
83
119
log .use_color (sys .stdout .isatty ())
84
120
121
+ plugins = []
85
122
plugin_directories = list (options .plugin_dirs )
86
123
if not options .disable_built_in_plugins :
87
- from . plugins import Clean , Create , Link , Shell , Plugins
124
+ plugins . extend ([ Clean , Create , Link , Plugins , Shell ])
88
125
plugin_paths = []
89
126
for directory in plugin_directories :
90
- for plugin_path in glob .glob (os .path .join (directory , ' *.py' )):
91
- plugin_paths .append (plugin_path )
127
+ for plugin_path in glob .glob (os .path .join (directory , " *.py" )):
128
+ plugin_paths .append (plugin_path )
92
129
for plugin_path in options .plugins :
93
130
plugin_paths .append (plugin_path )
94
131
for plugin_path in plugin_paths :
95
132
abspath = os .path .abspath (plugin_path )
96
- module .load (abspath )
133
+ plugins . extend ( module .load (abspath ) )
97
134
if not options .config_file :
98
- log .error (' No configuration file specified' )
135
+ log .error (" No configuration file specified" )
99
136
exit (1 )
100
137
tasks = read_config (options .config_file )
101
138
if tasks is None :
102
- log .warning (' Configuration file is empty, no work to do' )
139
+ log .warning (" Configuration file is empty, no work to do" )
103
140
tasks = []
104
141
if not isinstance (tasks , list ):
105
- raise ReadingError (' Configuration file must be a list of tasks' )
142
+ raise ReadingError (" Configuration file must be a list of tasks" )
106
143
if options .base_directory :
107
144
base_directory = os .path .abspath (options .base_directory )
108
145
else :
109
146
# default to directory of config file
110
147
base_directory = os .path .dirname (os .path .abspath (options .config_file ))
111
148
os .chdir (base_directory )
112
- dispatcher = Dispatcher (base_directory , only = options .only , skip = options .skip ,
113
- exit_on_failure = options .exit_on_failure , options = options )
149
+ dispatcher = Dispatcher (
150
+ base_directory ,
151
+ only = options .only ,
152
+ skip = options .skip ,
153
+ exit_on_failure = options .exit_on_failure ,
154
+ options = options ,
155
+ plugins = plugins ,
156
+ )
114
157
success = dispatcher .dispatch (tasks )
115
158
if success :
116
- log .info (' \n ==> All tasks executed successfully' )
159
+ log .info (" \n ==> All tasks executed successfully" )
117
160
else :
118
- raise DispatchError (' \n ==> Some tasks were not executed successfully' )
161
+ raise DispatchError (" \n ==> Some tasks were not executed successfully" )
119
162
except (ReadingError , DispatchError ) as e :
120
- log .error ('%s' % e )
163
+ log .error ("%s" % e )
121
164
exit (1 )
122
165
except KeyboardInterrupt :
123
- log .error (' \n ==> Operation aborted' )
166
+ log .error (" \n ==> Operation aborted" )
124
167
exit (1 )
0 commit comments