@@ -98,50 +98,44 @@ def __parse_args(desc):
9898 return args
9999
100100
101- def __remove_chars_at_end (file : str , nb_bytes : int ) -> None :
102- """Writes the configuration in file"""
103- with open (file , mode = "rb+" ) as fd :
104- fd .seek (- nb_bytes , os .SEEK_END )
105- fd .truncate ()
106-
107-
108- def write_projects (queue : Queue , file : str ) -> None :
101+ def write_objects (queue : Queue , fd , object_type : str ) -> None :
109102 """
110103 Thread to write projects in the JSON file
111104 """
112105 done = False
113106 prefix = ""
114- with utilities .open_file (file , mode = "a" ) as fd :
115- print ('" projects": {' , file = fd )
116- while not done :
117- project_json = queue .get ()
118- done = project_json is None
119- if not done :
120- log .info ("Writing project '%s'" , project_json ["key" ])
121- key = project_json .pop ("key" )
122- print (f'{ prefix } "{ key } ": { utilities .json_dump (project_json )} ' , end = "" , file = fd )
123- prefix = ",\n "
124- queue .task_done ()
125- print ("\n }" , file = fd , end = "" )
126- log .info ("Writing projects complete" )
127-
128-
129- def __write_export (config : dict [str , str ], file : str ) -> None :
130- """Writes the configuration in file"""
131- with utilities .open_file (file ) as fd :
132- print (utilities .json_dump (config ), file = fd )
107+ log .info ("Waiting %s to write..." , object_type )
108+ print (f'"{ object_type } ": ' + "{" , file = fd )
109+ while not done :
110+ obj_json = queue .get ()
111+ done = obj_json is None
112+ if not done :
113+ if object_type in ("projects" , "applications" , "portfolios" , "users" ):
114+ if object_type == "users" :
115+ key = obj_json .pop ("login" , None )
116+ else :
117+ key = obj_json .pop ("key" , None )
118+ log .debug ("Writing %s key '%s'" , object_type [:- 1 ], key )
119+ print (f'{ prefix } "{ key } ": { utilities .json_dump (obj_json )} ' , end = "" , file = fd )
120+ else :
121+ log .debug ("Writing %s" , object_type )
122+ print (f"{ prefix } { utilities .json_dump (obj_json )[2 :- 1 ]} " , end = "" , file = fd )
123+ prefix = ",\n "
124+ queue .task_done ()
125+ print ("\n }" , file = fd , end = "" )
126+ log .info ("Writing %s complete" , object_type )
133127
134128
135129def __export_config (endpoint : platform .Platform , what : list [str ], ** kwargs ) -> None :
136130 """Exports a platform configuration in a JSON file"""
131+ file = kwargs [options .REPORT_FILE ]
137132 export_settings = {
138133 "INLINE_LISTS" : False ,
139134 "EXPORT_DEFAULTS" : True ,
140135 # "FULL_EXPORT": kwargs["fullExport"],
141136 "FULL_EXPORT" : False ,
142137 "MODE" : "MIGRATION" ,
143138 "THREADS" : kwargs [options .NBR_THREADS ],
144- options .REPORT_FILE : kwargs [options .REPORT_FILE ],
145139 "SKIP_ISSUES" : kwargs ["skipIssues" ],
146140 }
147141 if "projects" in what and kwargs [options .KEYS ]:
@@ -154,7 +148,7 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
154148 options .WHAT_RULES : [__JSON_KEY_RULES , rules .export ],
155149 options .WHAT_PROFILES : [__JSON_KEY_PROFILES , qualityprofiles .export ],
156150 options .WHAT_GATES : [__JSON_KEY_GATES , qualitygates .export ],
157- # options.WHAT_PROJECTS: [__JSON_KEY_PROJECTS, projects.export],
151+ options .WHAT_PROJECTS : [__JSON_KEY_PROJECTS , projects .export ],
158152 options .WHAT_APPS : [__JSON_KEY_APPS , applications .export ],
159153 options .WHAT_PORTFOLIOS : [__JSON_KEY_PORTFOLIOS , portfolios .export ],
160154 options .WHAT_USERS : [__JSON_KEY_USERS , users .export ],
@@ -164,34 +158,31 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
164158 log .info ("Exporting configuration from %s" , kwargs [options .URL ])
165159 key_list = kwargs [options .KEYS ]
166160 sq_settings = {__JSON_KEY_PLATFORM : endpoint .basics ()}
167- for what_item , call_data in calls .items ():
168- if what_item not in what :
169- continue
170- ndx , func = call_data
171- try :
172- sq_settings [ndx ] = func (endpoint , export_settings = export_settings , key_list = key_list )
173- __write_export (sq_settings , kwargs [options .REPORT_FILE ])
174- except exceptions .UnsupportedOperation as e :
175- log .warning (e .message )
176- sq_settings = utilities .remove_empties (sq_settings )
177- # if not kwargs.get("dontInlineLists", False):
178- # sq_settings = utilities.inline_lists(sq_settings, exceptions=("conditions",))
179-
180- log .info ("Exporting project migration data streaming projects in '%s'" , kwargs [options .REPORT_FILE ])
181- __remove_chars_at_end (kwargs [options .REPORT_FILE ], 3 )
182- with utilities .open_file (kwargs [options .REPORT_FILE ], mode = "a" ) as fd :
183- print ("," , file = fd )
161+ is_first = True
184162 q = Queue (maxsize = 0 )
185- worker = Thread (target = write_projects , args = (q , kwargs [options .REPORT_FILE ]))
186- worker .setDaemon (True )
187- worker .setName ("WriteThread" )
188- worker .start ()
189- export_settings ["WRITE_QUEUE" ] = q
190- projects .export (endpoint , export_settings = export_settings , key_list = key_list )
191- q .join ()
192- log .info ("Exporting migration data from %s completed" , kwargs ["url" ])
193- with utilities .open_file (kwargs [options .REPORT_FILE ], mode = "a" ) as fd :
163+ with utilities .open_file (file , mode = "w" ) as fd :
164+ print ("{" , file = fd )
165+ for what_item , call_data in calls .items ():
166+ if what_item not in what :
167+ continue
168+ ndx , func = call_data
169+ try :
170+ if not is_first :
171+ print ("," , file = fd )
172+ is_first = False
173+ worker = Thread (target = write_objects , args = (q , fd , ndx ))
174+ worker .daemon = True
175+ worker .name = f"Write{ ndx [:1 ].upper ()} { ndx [1 :10 ]} "
176+ worker .start ()
177+ sq_settings [ndx ] = func (endpoint , export_settings = export_settings , key_list = key_list , write_q = q )
178+ q .join ()
179+ except exceptions .UnsupportedOperation as e :
180+ log .warning (e .message )
181+ sq_settings = utilities .remove_empties (sq_settings )
182+ # if not kwargs.get("dontInlineLists", False):
183+ # sq_settings = utilities.inline_lists(sq_settings, exceptions=("conditions",))
194184 print ("\n }" , file = fd )
185+ log .info ("Exporting migration data from %s completed" , kwargs ["url" ])
195186
196187
197188def main () -> None :
0 commit comments