2222 Exports SonarQube platform configuration as JSON
2323"""
2424import sys
25+ import os
26+ from threading import Lock
2527
2628from cli import options
2729from sonar import exceptions , errcodes , utilities , version
6567 options .WHAT_PORTFOLIOS : __JSON_KEY_PORTFOLIOS ,
6668}
6769
70+ _WRITE_LOCK = Lock ()
6871
6972def __parse_args (desc ):
7073 parser = options .set_common_args (desc )
@@ -93,6 +96,35 @@ def __parse_args(desc):
9396 return args
9497
9598
99+ def __remove_chars_at_end (file : str , nb_bytes : int ) -> None :
100+ """Writes the configuration in file"""
101+ with open (file , mode = "rb+" ) as fd :
102+ fd .seek (- nb_bytes , os .SEEK_END )
103+ fd .truncate ()
104+
105+
106+ def __add_project_header (file : str ) -> None :
107+ """Writes the configuration in file"""
108+ with open (file , mode = "a" , encoding = "utf-8" ) as fd :
109+ print (',\n "projects": {\n ' , file = fd )
110+
111+
112+ def __add_project_footer (file : str ) -> None :
113+ """Closes projects section"""
114+ __remove_chars_at_end (file , 2 )
115+ with open (file , mode = "a" , encoding = "utf-8" ) as fd :
116+ print ("\n }\n }" , file = fd )
117+
118+
119+ def write_project (project_json : dict [str , any ], file : str ) -> None :
120+ """
121+ writes a project JSON in a file
122+ """
123+ key = project_json .pop ("key" )
124+ with _WRITE_LOCK :
125+ with utilities .open_file (file , mode = "a" ) as fd :
126+ print (f'"{ key } ": { utilities .json_dump (project_json )} ,' , file = fd )
127+
96128def __write_export (config : dict [str , str ], file : str ) -> None :
97129 """Writes the configuration in file"""
98130 with utilities .open_file (file ) as fd :
@@ -110,7 +142,6 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
110142 "THREADS" : kwargs [options .NBR_THREADS ],
111143 options .REPORT_FILE : kwargs [options .REPORT_FILE ],
112144 "SKIP_ISSUES" : kwargs ["skipIssues" ],
113- options .REPORT_FILE : kwargs [options .REPORT_FILE ],
114145 }
115146 if "projects" in what and kwargs [options .KEYS ]:
116147 non_existing_projects = [key for key in kwargs [options .KEYS ] if not projects .exists (key , endpoint )]
0 commit comments