66import time
77import signal
88import shutil
9- from art import text2art
109from git import Repo
1110
12- from .log import Log
11+ from .log import setup_logging
1312from .tree import Tree
1413from .process import Process
1514from .arguments import Arguments
1615from .progress import CloneProgress
1716from .base import GitLabBase , CloneMethod
1817from . import __version__ as VERSION
19-
20- # ==============================================================================
21- # GLOBAL
22- # ==============================================================================
23-
24- logger = Log ("/var/log/gitlabrc" , "file.log" , "INFO" , "GitLabRC" ).logger
18+ from loguru import logger
2519
2620# ==============================================================================
2721# FUNCTIONS
2822# ==============================================================================
2923
30-
3124def pname ():
3225 return f"[gitlabrc - { str (os .getpid ())} ]"
3326
@@ -38,7 +31,7 @@ def signal_handler(sig, frame):
3831
3932
4033def check_git ():
41- if shutil .which ("git" ) == " None" :
34+ if shutil .which ("git" ) is None :
4235 logger .error ("Error: git executable not installed or not in $PATH" )
4336 exit (1 )
4437
@@ -62,7 +55,7 @@ def get_projects(gl, group, root=False):
6255
6356
6457def get_all_projects (gl , namespace ):
65- logger .info ("getting all projects" )
58+ logger .info ("Getting all projects" )
6659 projects = list ()
6760 root_projects = get_projects (gl , namespace , root = True )
6861 root_subgroups = get_subgroups (gl , namespace , root = True )
@@ -105,11 +98,10 @@ def create_directories(path, folders, namespace, noroot):
10598
10699def clone_project (project , clone_path , project_url ):
107100 if not os .path .isdir (clone_path ):
108- print ( text2art ( "Cloning" ) )
101+ logger . info ( f "Cloning { project . path_with_namespace } " )
109102 try :
110103 if project .empty_repo :
111104 logger .warning (f"Repository is empty: { project .path_with_namespace } " )
112- print (f"Warning: Repository is empty: { project .path_with_namespace } " )
113105 os .makedirs (clone_path , exist_ok = True )
114106 Repo .init (clone_path )
115107 repo = Repo (clone_path )
@@ -130,9 +122,6 @@ def clone_project(project, clone_path, project_url):
130122 logger .warning (
131123 f"Failed to clone { project .path_with_namespace } with branch 'main': { e_main } " ,
132124 )
133- print (
134- f"Warning: Failed to clone { project .path_with_namespace } with branch 'main': { e_main } " ,
135- )
136125 try :
137126 Repo .clone_from (
138127 project_url ,
@@ -145,41 +134,38 @@ def clone_project(project, clone_path, project_url):
145134 logger .error (
146135 f"Failed to clone { project .path_with_namespace } with branch 'master': { e_master } " ,
147136 )
148- print (
149- f"Failed to clone { project .path_with_namespace } with branch 'master': { e_master } " ,
150- )
151137 except Exception as e :
152138 logger .error (f"Failed to clone { project .path_with_namespace } : { e } " )
153- print (f"Failed to clone { project .path_with_namespace } : { e } " )
154139
155140
156141def fetch_project (clone_path ):
157- print ( text2art ( "Fetching" ) )
142+ logger . info ( f "Fetching updates for { clone_path } " )
158143 Process ().run_command (f"git -C { clone_path } fetch --all" )
159144
160145
161146def handle_tree_option (projects , t ):
162- print ( text2art ( "Tree" ) )
147+ logger . info ( "Generating tree representation" )
163148 tree = Tree ()
164149 parse = [project .path_with_namespace for project in projects ]
165150 parse_content = [
166151 [value + " " for value in elemento .split ("/" )] for elemento in parse
167152 ]
168153 tree .show (tree .make (parse_content ))
169- logger .info (f"mission accomplished in { str (round (time .time () - t , 2 ))} s" )
154+ logger .info (f"Mission accomplished in { str (round (time .time () - t , 2 ))} s" )
170155 exit (0 )
171156
172157
173158def main ():
174- print (text2art ("GitLabRC" ))
175- check_git ()
176159 args = Arguments (argv = None if sys .argv [1 :] else ["--help" ]).args
160+ setup_logging (log_level = args .log_level )
161+ logger .info ("Starting GitLabRC" )
162+ check_git ()
177163 if args .version :
178164 print (f"Version: { VERSION } " )
179165 exit (1 )
180166 if args .signal :
181167 signal .signal (signal .SIGINT , signal_handler )
182- print (f"Version: { VERSION } \n " )
168+ logger . info (f"Version: { VERSION } " )
183169 run (args )
184170
185171
@@ -194,25 +180,25 @@ def run(options):
194180
195181 if path :
196182 if not os .path .isdir (path ):
197- logger .error (f"error : destination path does not exist { options .path } " )
183+ logger .error (f"Error : destination path does not exist { options .path } " )
198184 exit (1 )
199185
200186 if not namespace :
201- logger .error ("error : we need a namespace" )
187+ logger .error ("Error : we need a namespace" )
202188 exit (1 )
203189
204190 projects = get_all_projects (gl , namespace )
205191
206192 if options .tree :
207193 handle_tree_option (projects , t )
208194
209- print ( text2art ( "Projects" ) )
195+ logger . info ( "Listing all projects" )
210196 for project in projects :
211- print (f"{ pname () } found { project .path_with_namespace } " )
197+ logger . info (f"Found { project .path_with_namespace } " )
212198
213199 if not options .dryrun :
214200 for index , project in enumerate (projects , start = 1 ):
215- print (f"{ pname () } clone /fetch project { project .path_with_namespace } " )
201+ logger . info (f"Clone /fetch project { project .path_with_namespace } " )
216202 folders = [
217203 f .strip ().lower () for f in project .path_with_namespace .split ("/" )
218204 ]
@@ -222,7 +208,7 @@ def run(options):
222208 clone_path = path + "/" + "/" .join (str (x ) for x in folders )
223209 clone_path = re .sub ("/+" , "/" , clone_path )
224210
225- print (f"{ pname () } folder { clone_path } " )
211+ logger . info (f"Folder { clone_path } " )
226212
227213 project_url = (
228214 project .http_url_to_repo
@@ -234,5 +220,5 @@ def run(options):
234220 clone_project (project , clone_path , project_url )
235221 fetch_project (clone_path )
236222
237- logger .info (f"mission accomplished in { str (round (time .time () - t , 2 ))} s" )
223+ logger .info (f"Mission accomplished in { str (round (time .time () - t , 2 ))} s" )
238224 exit (0 )
0 commit comments