|
23 | 23 | """ |
24 | 24 | import sys |
25 | 25 | import os |
26 | | -from threading import Lock |
| 26 | +from threading import Thread, Lock |
| 27 | +from queue import Queue |
27 | 28 |
|
28 | 29 | from cli import options |
29 | 30 | from sonar import exceptions, errcodes, utilities, version |
@@ -104,27 +105,25 @@ def __remove_chars_at_end(file: str, nb_bytes: int) -> None: |
104 | 105 | fd.truncate() |
105 | 106 |
|
106 | 107 |
|
107 | | -def __add_project_header(file: str) -> None: |
108 | | - """Writes the configuration in file""" |
109 | | - with open(file, mode="a", encoding="utf-8") as fd: |
110 | | - print(',\n "projects": {\n', file=fd) |
111 | | - |
112 | | - |
113 | | -def __add_project_footer(file: str) -> None: |
114 | | - """Closes projects section""" |
115 | | - __remove_chars_at_end(file, 2) |
116 | | - with open(file, mode="a", encoding="utf-8") as fd: |
117 | | - print("\n }\n}", file=fd) |
118 | | - |
119 | | - |
120 | | -def write_project(project_json: dict[str, any], file: str) -> None: |
| 108 | +def write_projects(queue: Queue, file: str) -> None: |
121 | 109 | """ |
122 | | - writes a project JSON in a file |
| 110 | + Thread to write projects in the JSON file |
123 | 111 | """ |
124 | | - key = project_json.pop("key") |
125 | | - with _WRITE_LOCK: |
126 | | - with utilities.open_file(file, mode="a") as fd: |
127 | | - print(f'"{key}": {utilities.json_dump(project_json)},', file=fd) |
| 112 | + done = False |
| 113 | + 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") |
128 | 127 |
|
129 | 128 |
|
130 | 129 | def __write_export(config: dict[str, str], file: str) -> None: |
@@ -179,12 +178,20 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N |
179 | 178 | # sq_settings = utilities.inline_lists(sq_settings, exceptions=("conditions",)) |
180 | 179 |
|
181 | 180 | log.info("Exporting project migration data streaming projects in '%s'", kwargs[options.REPORT_FILE]) |
182 | | - export_settings["WRITE_CALLBACK"] = write_project |
183 | 181 | __remove_chars_at_end(kwargs[options.REPORT_FILE], 3) |
184 | | - __add_project_header(kwargs[options.REPORT_FILE]) |
| 182 | + with utilities.open_file(kwargs[options.REPORT_FILE], mode="a") as fd: |
| 183 | + print(",", file=fd) |
| 184 | + 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 |
185 | 190 | projects.export(endpoint, export_settings=export_settings, key_list=key_list) |
186 | | - __add_project_footer(kwargs[options.REPORT_FILE]) |
| 191 | + q.join() |
187 | 192 | log.info("Exporting migration data from %s completed", kwargs["url"]) |
| 193 | + with utilities.open_file(kwargs[options.REPORT_FILE], mode="a") as fd: |
| 194 | + print("\n}", file=fd) |
188 | 195 |
|
189 | 196 |
|
190 | 197 | def main() -> None: |
|
0 commit comments