diff options
| author | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2024-08-27 16:10:37 +0200 |
|---|---|---|
| committer | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2024-09-05 16:17:17 +0200 |
| commit | 5e913b5975be387721124d72841efce46274def7 (patch) | |
| tree | e9e83ce497230bb7757c02bddf54c225de3513ee /sources/pyside-tools/deploy_lib/config.py | |
| parent | 6d813fb2b4a418a13dc2bb0e46ecbeaf29ac440c (diff) | |
Deployment: Fix formatting when writing back to config file
- Configparser does not preserve the formatting of the original file.
As a result when writing into the config file, the formatting is lost.
In the case of pysidedeploy.spec, the blank lines are lost making
the entire config file rather hard to read.
- This patch adds a blank line before the comment lines thus retaining
the original formatting of the file.
Pick-to: 6.7
Task-number: PYSIDE-1612
Change-Id: Icd1ebe52d364f4dc9197220161ed6daa6ed577a0
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/pyside-tools/deploy_lib/config.py')
| -rw-r--r-- | sources/pyside-tools/deploy_lib/config.py | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/sources/pyside-tools/deploy_lib/config.py b/sources/pyside-tools/deploy_lib/config.py index de19b0a1d..180c4f7e5 100644 --- a/sources/pyside-tools/deploy_lib/config.py +++ b/sources/pyside-tools/deploy_lib/config.py @@ -5,6 +5,7 @@ from __future__ import annotations import sys import configparser import logging +import tempfile import warnings from configparser import ConfigParser from pathlib import Path @@ -42,8 +43,26 @@ class BaseConfig: def update_config(self): logging.info(f"[DEPLOY] Creating {self.config_file}") - with open(self.config_file, "w+") as config_file: - self.parser.write(config_file, space_around_delimiters=True) + + # This section of code is done to preserve the formatting of the original deploy.spec + # file where there is blank line before the comments + with tempfile.NamedTemporaryFile('w+', delete=False) as temp_file: + self.parser.write(temp_file, space_around_delimiters=True) + temp_file_path = temp_file.name + + # Read the temporary file and write back to the original file with blank lines before + # comments + with open(temp_file_path, 'r') as temp_file, open(self.config_file, 'w') as config_file: + previous_line = None + for line in temp_file: + if (line.lstrip().startswith('#') and previous_line is not None + and not previous_line.lstrip().startswith('#')): + config_file.write('\n') + config_file.write(line) + previous_line = line + + # Clean up the temporary file + Path(temp_file_path).unlink() def set_value(self, section: str, key: str, new_value: str, raise_warning: bool = True): try: |
