1- import os , glob
1+ import glob
2+ import os
3+ import subprocess
24import sys
3-
45from argparse import ArgumentParser , RawTextHelpFormatter
6+
7+ import dotbot
8+
59from .config import ConfigReader , ReadingError
610from .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
913from .util import module
1014
11- import dotbot
12- import os
13- import subprocess
1415
1516def 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+
4779
4880def read_config (config_file ):
4981 reader = ConfigReader (config_file )
5082 return reader .get_config ()
5183
84+
5285def main ():
5386 log = Messenger ()
5487 try :
@@ -58,12 +91,15 @@ def main():
5891 if options .version :
5992 try :
6093 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 ]
64100 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 ))
67103 exit (0 )
68104 if options .super_quiet :
69105 log .set_level (Level .WARNING )
@@ -82,43 +118,50 @@ def main():
82118 else :
83119 log .use_color (sys .stdout .isatty ())
84120
121+ plugins = []
85122 plugin_directories = list (options .plugin_dirs )
86123 if not options .disable_built_in_plugins :
87- from . plugins import Clean , Create , Link , Shell , Plugins
124+ plugins . extend ([ Clean , Create , Link , Plugins , Shell ])
88125 plugin_paths = []
89126 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 )
92129 for plugin_path in options .plugins :
93130 plugin_paths .append (plugin_path )
94131 for plugin_path in plugin_paths :
95132 abspath = os .path .abspath (plugin_path )
96- module .load (abspath )
133+ plugins . extend ( module .load (abspath ) )
97134 if not options .config_file :
98- log .error (' No configuration file specified' )
135+ log .error (" No configuration file specified" )
99136 exit (1 )
100137 tasks = read_config (options .config_file )
101138 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" )
103140 tasks = []
104141 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" )
106143 if options .base_directory :
107144 base_directory = os .path .abspath (options .base_directory )
108145 else :
109146 # default to directory of config file
110147 base_directory = os .path .dirname (os .path .abspath (options .config_file ))
111148 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+ )
114157 success = dispatcher .dispatch (tasks )
115158 if success :
116- log .info (' \n ==> All tasks executed successfully' )
159+ log .info (" \n ==> All tasks executed successfully" )
117160 else :
118- raise DispatchError (' \n ==> Some tasks were not executed successfully' )
161+ raise DispatchError (" \n ==> Some tasks were not executed successfully" )
119162 except (ReadingError , DispatchError ) as e :
120- log .error ('%s' % e )
163+ log .error ("%s" % e )
121164 exit (1 )
122165 except KeyboardInterrupt :
123- log .error (' \n ==> Operation aborted' )
166+ log .error (" \n ==> Operation aborted" )
124167 exit (1 )
0 commit comments