41
41
42
42
from .utils .commands import Commands , CommandsBase
43
43
from .utils .filelock import Timeout , FileLock
44
- from .utils .opengrok import list_indexed_projects
44
+ from .utils .opengrok import list_indexed_projects , get_config_value
45
45
from .utils .readconfig import read_config
46
46
47
47
major_version = sys .version_info [0 ]
48
48
if (major_version < 3 ):
49
49
print ("Need Python 3, you are running {}" .format (major_version ))
50
50
sys .exit (1 )
51
51
52
- __version__ = "0.4 "
52
+ __version__ = "0.5 "
53
53
54
54
55
55
def worker (base ):
@@ -73,10 +73,12 @@ def main():
73
73
74
74
# There can be only one way how to supply list of projects to process.
75
75
group1 = parser .add_mutually_exclusive_group ()
76
- group1 .add_argument ('-d' , '--directory' , default = "/var/opengrok/src" ,
76
+ group1 .add_argument ('-d' , '--directory' ,
77
77
help = 'Directory to process' )
78
78
group1 .add_argument ('-P' , '--projects' , nargs = '*' ,
79
79
help = 'List of projects to process' )
80
+ parser .add_argument ('-I' , '--indexed' , action = 'store_true' ,
81
+ help = 'Sync indexed projects only' )
80
82
81
83
group2 = parser .add_mutually_exclusive_group ()
82
84
group2 .add_argument ('-D' , '--debug' , action = 'store_true' ,
@@ -88,8 +90,6 @@ def main():
88
90
help = 'ignore errors from these projects' )
89
91
parser .add_argument ('-c' , '--config' , required = True ,
90
92
help = 'config file in JSON format' )
91
- parser .add_argument ('-I' , '--indexed' , action = 'store_true' ,
92
- help = 'Sync indexed projects only' )
93
93
parser .add_argument ('-U' , '--uri' , default = 'http://localhost:8080/source' ,
94
94
help = 'URI of the webapp with context path' )
95
95
args = parser .parse_args ()
@@ -105,12 +105,18 @@ def main():
105
105
logger = logging .getLogger (os .path .basename (sys .argv [0 ]))
106
106
107
107
uri = args .uri
108
- if not uri :
109
- logger .error ("uri of the webapp not specified" )
110
- sys .exit (1 )
108
+ logger .debug ("web application URI = {}" .format (uri ))
111
109
112
- logger .debug ("Uri = {}" .format (uri ))
110
+ # Changing working directory to root will avoid problems when running
111
+ # via sudo/su.
112
+ try :
113
+ os .chdir ("/" )
114
+ except OSError :
115
+ logger .error ("cannot change working directory to /" ,
116
+ exc_info = True )
117
+ sys .exit (1 )
113
118
119
+ # First read and validate configuration file as it is mandatory argument.
114
120
config = read_config (logger , args .config )
115
121
if config is None :
116
122
logger .error ("Cannot read config file from {}" .format (args .config ))
@@ -122,6 +128,17 @@ def main():
122
128
logger .error ("The config file has to contain key \" commands\" " )
123
129
sys .exit (1 )
124
130
131
+ directory = args .directory
132
+ if not args .directory and not args .projects and not args .indexed :
133
+ # Assume directory, get the source root value from the webapp.
134
+ directory = get_config_value (logger , 'sourceRoot' , uri )
135
+ if not directory :
136
+ logger .error ("Neither -d or -P or -I specified and cannot get "
137
+ "source root from the webapp" )
138
+ sys .exit (1 )
139
+ else :
140
+ logger .info ("Assuming directory: {}" .format (directory ))
141
+
125
142
ignore_errors = []
126
143
if args .ignore_errors :
127
144
ignore_errors = args .ignore_errors
@@ -132,13 +149,6 @@ def main():
132
149
pass
133
150
logger .debug ("Ignored projects: {}" .format (ignore_errors ))
134
151
135
- try :
136
- os .chdir ("/" )
137
- except OSError :
138
- logger .error ("cannot change working directory to /" ,
139
- exc_info = True )
140
- sys .exit (1 )
141
-
142
152
lock = FileLock (os .path .join (tempfile .gettempdir (),
143
153
"opengrok-sync.lock" ))
144
154
try :
@@ -147,8 +157,12 @@ def main():
147
157
148
158
if args .projects :
149
159
dirs_to_process = args .projects
160
+ logger .debug ("Processing directories: {}" .
161
+ format (dirs_to_process ))
150
162
elif args .indexed :
151
163
indexed_projects = list_indexed_projects (logger , uri )
164
+ logger .debug ("Processing indexed projects: {}" .
165
+ format (indexed_projects ))
152
166
153
167
if indexed_projects :
154
168
for line in indexed_projects :
@@ -157,7 +171,7 @@ def main():
157
171
logger .error ("cannot get list of projects" )
158
172
sys .exit (1 )
159
173
else :
160
- directory = args . directory
174
+ logger . debug ( "Processing directory {}" . format ( directory ))
161
175
for entry in os .listdir (directory ):
162
176
if path .isdir (path .join (directory , entry )):
163
177
dirs_to_process .append (entry )
0 commit comments