diff options
author | Patrik Teivonen <patrik.teivonen@qt.io> | 2022-07-05 13:43:43 +0300 |
---|---|---|
committer | Patrik Teivonen <patrik.teivonen@qt.io> | 2022-09-01 08:28:10 +0000 |
commit | 5e7dccd99a8565a1afdaaacad63f90e932a49fd5 (patch) | |
tree | ad0ff5eadf02700f22e78c2222c3e7994bfee4f5 | |
parent | 158dbfa0117d2eaddccaca41a3a8038e7d821e5a (diff) |
pylint: Add missing UTF-8 encoding parameter when opening files
Enable following warning in pylint and use encoding="utf-8" everywhere:
W1514: Using open without explicitly specifying an encoding (unspecified-encoding)
Change-Id: Iddb885a420b9953ec899c825293a04ed75a2aef0
Reviewed-by: Iikka Eklund <iikka.eklund@qt.io>
20 files changed, 41 insertions, 41 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9ca1ef82e..b8bd834ce 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -41,7 +41,7 @@ repos: fail_fast: true - id: pylint name: Analyze code (pylint) - entry: bash -c 'for x in "$@"; do pipenv run python3 -m pylint --errors-only --enable=C0209,W1202,W1203,C0325,R1705,R1720,R1721,R1723,R1724,W0104,W0105,W0107,W0612,W0613,R0205,R1711,R1727,R1719,W1503,W0402 "$x"; done' + entry: bash -c 'for x in "$@"; do pipenv run python3 -m pylint --errors-only --enable=C0209,W1202,W1203,C0325,R1705,R1720,R1721,R1723,R1724,W0104,W0105,W0107,W0612,W0613,R0205,R1711,R1727,R1719,W1503,W0402,W1514 "$x"; done' language: system types: [python] fail_fast: true diff --git a/packaging-tools/bld_ifw_tools.py b/packaging-tools/bld_ifw_tools.py index 2ef5a58b4..fcb38957f 100755 --- a/packaging-tools/bld_ifw_tools.py +++ b/packaging-tools/bld_ifw_tools.py @@ -739,13 +739,13 @@ def sign_windows_installerbase(file_name): def patch(file, dict): filedata = None print(f"Patching {file} ...") - with open(file, 'r') as f: + with open(file, 'r', encoding="utf-8") as f: filedata = f.read() for key in dict: filedata = filedata.replace(key, dict[key]) - with open(file, 'w') as f: + with open(file, 'w', encoding="utf-8") as f: f.write(filedata) diff --git a/packaging-tools/bld_lib.py b/packaging-tools/bld_lib.py index b244b3a4a..fa2a3492a 100644 --- a/packaging-tools/bld_lib.py +++ b/packaging-tools/bld_lib.py @@ -133,7 +133,7 @@ def build(qtDestDir: str, currentDir: str) -> str: assert qmakeTool, f"Could not find: {qmakeToolName} from: {qtDestDir}" # patch - with open(os.path.join(os.path.dirname(qmakeTool), "qt.conf"), "w+") as f: + with open(os.path.join(os.path.dirname(qmakeTool), "qt.conf"), "w+", encoding="utf-8") as f: f.write("[Paths]\n") f.write("Prefix=..\n") diff --git a/packaging-tools/bld_openssl.py b/packaging-tools/bld_openssl.py index 6ca28a582..3aec5ee2b 100644 --- a/packaging-tools/bld_openssl.py +++ b/packaging-tools/bld_openssl.py @@ -60,7 +60,7 @@ def archive(install_dir, archive_prefix): def check_environment(): - FNULL = open(os.devnull, 'w') + FNULL = open(os.devnull, 'w', encoding="utf-8") def check_cmd(cmd): if subprocess.call(cmd, stdout=FNULL, stderr=FNULL) != 0: diff --git a/packaging-tools/bld_utils.py b/packaging-tools/bld_utils.py index ff2d71138..47651a041 100644 --- a/packaging-tools/bld_utils.py +++ b/packaging-tools/bld_utils.py @@ -405,7 +405,7 @@ def get_commit_SHA(source_path): if not buildGitSHA: tagfile = os.path.join(source_path, '.tag') if os.path.exists(tagfile): - with open(tagfile, 'r') as f: + with open(tagfile, 'r', encoding="utf-8") as f: buildGitSHA = f.read().strip() return buildGitSHA diff --git a/packaging-tools/bldinstallercommon.py b/packaging-tools/bldinstallercommon.py index 36b8e2e98..feee0fdd7 100644 --- a/packaging-tools/bldinstallercommon.py +++ b/packaging-tools/bldinstallercommon.py @@ -122,7 +122,7 @@ def search_for_files( def _matches_rgx(path: Path): if rgx_pattern: - with open(path, 'r') as f: + with open(path, 'r', encoding="utf-8") as f: return bool(pattern.search(f.read())) return True return locate_paths(search_path, suffixes, filters=[os.path.isfile, _matches_rgx]) @@ -347,7 +347,7 @@ def is_text(s): def is_text_file(filename, blocksize=512): try: - return is_text(open(filename).read(blocksize)) + return is_text(open(filename, encoding="utf-8").read(blocksize)) except UnicodeDecodeError: return is_text(open(filename, 'rb').read(blocksize)) @@ -565,7 +565,7 @@ def git_archive_repo(repo_and_ref): # clone given repo to temp clone_repository(repository, ref, checkout_dir, full_clone=True, init_subrepos=True) # git archive repo with given name - archive_file = open(archive_name, 'w') + archive_file = open(archive_name, 'w', encoding="utf-8") check_call(f"git --no-pager archive {ref}", stdout=archive_file, stderr=STDOUT, shell=True, cwd=checkout_dir) archive_file.close() print(f"Created archive: {archive_name}") @@ -720,7 +720,7 @@ def patch_qt(qt5_path): print("##### patch Qt #####") qmake_binary = os.path.join(qt5_path, 'bin', 'qmake') # write qt.conf - qtConfFile = open(os.path.join(qt5_path, 'bin', 'qt.conf'), "w") + qtConfFile = open(os.path.join(qt5_path, 'bin', 'qt.conf'), "w", encoding="utf-8") qtConfFile.write("[Paths]" + os.linesep) qtConfFile.write("Prefix=.." + os.linesep) qtConfFile.close() diff --git a/packaging-tools/build_wrapper.py b/packaging-tools/build_wrapper.py index 62676a96b..ad7319474 100644 --- a/packaging-tools/build_wrapper.py +++ b/packaging-tools/build_wrapper.py @@ -301,7 +301,7 @@ class BuildLog: def __enter__(self): try: - self.file = open(self.log_filepath, 'w' if self.log_overwrite else 'a') + self.file = open(self.log_filepath, 'w' if self.log_overwrite else 'a', encoding="utf-8") except Exception: print(f"Failed to write log file '{self.log_filepath}'") sys.stdout.flush() @@ -311,7 +311,7 @@ class BuildLog: def __exit__(self, type, value, traceback): self.file.close() if type: # exception raised -> print the log and re-raise - with open(self.log_filepath, 'r') as f: + with open(self.log_filepath, 'r', encoding="utf-8") as f: print(f.read()) return True # re-raise @@ -418,7 +418,7 @@ def get_qtcreator_version(path_to_qtcreator_src, optionDict): ide_branding_path = ide_branding_path if ide_branding_path else os.path.join(path_to_qtcreator_src, 'cmake') ide_branding_file = os.path.join(ide_branding_path, 'QtCreatorIDEBranding.cmake') - with open(ide_branding_file, 'r') as f: + with open(ide_branding_file, 'r', encoding="utf-8") as f: for line in f: match = expr.match(line) if match: @@ -439,7 +439,7 @@ def make_QtcPlugin_from_json(plugin_json): def parse_qt_creator_plugin_conf(plugin_conf_file_path, optionDict): data = {} - with open(plugin_conf_file_path, 'r') as f: + with open(plugin_conf_file_path, 'r', encoding="utf-8") as f: data = json.load(f) plugins_json = data['Plugins'] if is_linux(): @@ -464,7 +464,7 @@ def collect_qt_creator_plugin_sha1s(plugins): work_dir = optionDict['WORK_DIR'] sha1s = [] for name in [p.name for p in plugins if p.build and os.path.isdir(os.path.join(work_dir, p.path))]: - with open(os.path.join(work_dir, name + '.7z.git_sha'), 'r') as f: + with open(os.path.join(work_dir, name + '.7z.git_sha'), 'r', encoding="utf-8") as f: sha = f.read().strip() sha1s.append(name + ': ' + sha) return sorted(sha1s) @@ -810,7 +810,7 @@ def handle_qt_creator_build(optionDict, qtCreatorPlugins): log_filepath=log_filepath) qtcreator_sha = get_commit_SHA(qtcreator_source) - with open(os.path.join(work_dir, 'QTC_SHA1'), 'w') as f: + with open(os.path.join(work_dir, 'QTC_SHA1'), 'w', encoding="utf-8") as f: f.write(qtcreator_sha + '\n') if is_linux(): @@ -820,7 +820,7 @@ def handle_qt_creator_build(optionDict, qtCreatorPlugins): if os.path.exists(licensemanaging_source): sha1s.append('license-managing: ' + get_commit_SHA(licensemanaging_source)) sha1s.append('qt-creator: ' + qtcreator_sha) - with open(os.path.join(work_dir, 'SHA1'), 'w') as f: + with open(os.path.join(work_dir, 'SHA1'), 'w', encoding="utf-8") as f: f.writelines([sha + '\n' for sha in sha1s]) # Create opensource source package diff --git a/packaging-tools/create_installer.py b/packaging-tools/create_installer.py index 5e5365b27..486f6e8e6 100644 --- a/packaging-tools/create_installer.py +++ b/packaging-tools/create_installer.py @@ -232,7 +232,7 @@ def parse_component_data(task, configuration_file, configurations_base_path): file_full_path = locate_path(allos_conf_file_dir, [configuration_file], filters=[os.path.isfile]) log.info("Reading target configuration file: %s", file_full_path) configuration = ConfigParser(interpolation=ExtendedInterpolation()) - configuration.read_file(open(file_full_path)) + configuration.read_file(open(file_full_path, encoding="utf-8")) # parse package ignore list first sdk_component_exclude_list = safe_config_key_fetch(configuration, 'PackageIgnoreList', 'packages') @@ -323,7 +323,7 @@ def get_component_sha1_file(sdk_component, sha1_file_dest): download(sdk_component.component_sha1_uri, sha1_file_dest) # read sha1 from the file - with open(sha1_file_dest, "r") as sha1_file: + with open(sha1_file_dest, "r", encoding="utf-8") as sha1_file: sdk_component.component_sha1 = sha1_file.read().strip() @@ -432,7 +432,7 @@ def get_component_data(task, sdk_component, archive, install_dir, data_dir_dest, # read sha1 from the file sha1_file_path = install_dir + os.sep + archive.component_sha1_file if os.path.exists(sha1_file_path): - with open(sha1_file_path, "r") as sha1_file: + with open(sha1_file_path, "r", encoding="utf-8") as sha1_file: sdk_component.component_sha1 = sha1_file.read().strip() else: raise CreateInstallerError(f"Component SHA1 file '{archive.component_sha1_file}' not found") @@ -920,7 +920,7 @@ class QtInstallerTask: def __init__(self, args): log.info("Parsing: %s", args.configuration_file) self.config = ConfigParser(interpolation=ExtendedInterpolation()) - self.config.read_file(open(args.configuration_file)) + self.config.read_file(open(args.configuration_file, encoding="utf-8")) self.configurations_dir = args.configurations_dir self.configuration_file = args.configuration_file diff --git a/packaging-tools/libclang_training/libclangtimings2csv.py b/packaging-tools/libclang_training/libclangtimings2csv.py index ca0e11529..299fda828 100644 --- a/packaging-tools/libclang_training/libclangtimings2csv.py +++ b/packaging-tools/libclang_training/libclangtimings2csv.py @@ -109,7 +109,7 @@ def recordsToString(records): def convert(inputFile, columnLabel=None): if not columnLabel: columnLabel = os.path.basename(inputFile) - fileContent = open(inputFile, 'r').read() + fileContent = open(inputFile, 'r', encoding="utf-8").read() records = [[columnLabel, columnLabel]] + extractRecords(fileContent) diff --git a/packaging-tools/libclang_training/mergeCsvFiles.py b/packaging-tools/libclang_training/mergeCsvFiles.py index e6f130d70..16bd42d40 100644 --- a/packaging-tools/libclang_training/mergeCsvFiles.py +++ b/packaging-tools/libclang_training/mergeCsvFiles.py @@ -49,7 +49,7 @@ class FileWithValues: def readCsv(filePath, delimiter): lines = [] - with open(filePath, 'rt') as f: + with open(filePath, 'rt', encoding="utf-8") as f: lines = f.readlines() records = [] @@ -66,7 +66,7 @@ def readCsvFiles(filePaths): files = [] for filePath in filePaths: - f = open(filePath, 'rt') + f = open(filePath, 'rt', encoding="utf-8") reader = csv.reader(f, delimiter=Global.Delimiter, quoting=csv.QUOTE_NONE) values = [] @@ -104,7 +104,7 @@ def checkConsistency(files): def mergeFilesHelper(outputFilePath, referenceIdentifiers, files): - with open(outputFilePath, 'wt') as csvfile: + with open(outputFilePath, 'wt', encoding="utf-8") as csvfile: writer = csv.writer(csvfile, delimiter=Global.Delimiter, quotechar='"', quoting=csv.QUOTE_MINIMAL) # Write header row diff --git a/packaging-tools/libclang_training/runBatchFiles.py b/packaging-tools/libclang_training/runBatchFiles.py index d5dd6e7ca..db0322aa9 100644 --- a/packaging-tools/libclang_training/runBatchFiles.py +++ b/packaging-tools/libclang_training/runBatchFiles.py @@ -188,7 +188,7 @@ def runSyncAndLogOutputWindows(args, batchFilePath, logFilePath): def runSyncAndLogOutputUnix(args, batchFilePath, logFilePath): - logFile = open(logFilePath, 'w') + logFile = open(logFilePath, 'w', encoding="utf-8") verboseStart(args) p = Popen(args, @@ -222,7 +222,7 @@ def convertLogFileToCsvFile(logFilePath, columnLabel): output = libclangtimings2csv.convert(logFilePath, columnLabel) csvFilePath = logFilePath + '.csv' - f = open(csvFilePath, 'w') + f = open(csvFilePath, 'w', encoding="utf-8") f.write(output) f.close() diff --git a/packaging-tools/release_repo_updater.py b/packaging-tools/release_repo_updater.py index 38b5f542c..8ec4c3cff 100755 --- a/packaging-tools/release_repo_updater.py +++ b/packaging-tools/release_repo_updater.py @@ -175,7 +175,7 @@ def execute_remote_cmd(remoteServer: str, remoteServerHome: str, cmd: List[str], def create_remote_script(server: str, cmd: List[str], remoteScriptPath: str, scriptFileName: str) -> str: with TemporaryDirectory(dir=os.getcwd()) as tmpBaseDir: tempFilePath = os.path.join(tmpBaseDir, scriptFileName) - with open(tempFilePath, 'w+') as f: + with open(tempFilePath, 'w+', encoding="utf-8") as f: f.write("#!/usr/bin/env bash\n") f.write(' '.join(cmd)) os.chmod(tempFilePath, 0o755) diff --git a/packaging-tools/tests/test_bld_python.py b/packaging-tools/tests/test_bld_python.py index 693e447d0..8b2866176 100755 --- a/packaging-tools/tests/test_bld_python.py +++ b/packaging-tools/tests/test_bld_python.py @@ -45,7 +45,7 @@ class TestBldPython(unittest.TestCase): tempDir = os.path.join(tmpBaseDir, "foo", "bar", "test", "dir") os.makedirs(tempDir) tempFilePath = os.path.join(tempDir, "configure") - with open(tempFilePath, 'w+') as f: + with open(tempFilePath, 'w+', encoding="utf-8") as f: f.write("\n") foundDir = locate_source_root(tmpBaseDir) diff --git a/packaging-tools/tests/test_bldinstallercommon.py b/packaging-tools/tests/test_bldinstallercommon.py index dba0a8410..32f1a6b2d 100644 --- a/packaging-tools/tests/test_bldinstallercommon.py +++ b/packaging-tools/tests/test_bldinstallercommon.py @@ -78,11 +78,11 @@ class TestCommon(unittest.TestCase): with TemporaryDirectory(dir=os.getcwd()) as tmp_base_dir: # run tag substitution with data tmp_file = Path(tmp_base_dir) / "test" - with open(tmp_file, "a") as f: + with open(tmp_file, "a", encoding="utf-8") as f: f.write(file_contents) for key, value in replacements: replace_in_files([tmp_file], key, value) - with open(tmp_file, "r") as f: + with open(tmp_file, "r", encoding="utf-8") as f: file_contents = f.read() # check that file contents match self.assertEqual(file_contents, expected_file_content) @@ -141,7 +141,7 @@ class TestCommon(unittest.TestCase): path, content = file tmp_file = Path(tmp_base_dir) / path tmp_file.parents[0].mkdir(parents=True, exist_ok=True) - with open(tmp_file, "a") as f: + with open(tmp_file, "a", encoding="utf-8") as f: f.write(content) extensions, rgx = params result = search_for_files(tmp_base_dir, extensions, rgx) diff --git a/packaging-tools/tests/test_content_cleaner.py b/packaging-tools/tests/test_content_cleaner.py index 898f04030..5e59e6ed4 100644 --- a/packaging-tools/tests/test_content_cleaner.py +++ b/packaging-tools/tests/test_content_cleaner.py @@ -47,7 +47,7 @@ class TestContentCleaner(unittest.TestCase): test_path = os.path.join(test_base_dir, test_content_path) os.makedirs(os.path.dirname(test_path), exist_ok=True) if not test_path.endswith("/"): - with open(test_path, "w+") as f: + with open(test_path, "w+", encoding="utf-8") as f: f.write("") @data( diff --git a/packaging-tools/tests/test_installer_utils.py b/packaging-tools/tests/test_installer_utils.py index 11ce56f3a..d22a174ac 100644 --- a/packaging-tools/tests/test_installer_utils.py +++ b/packaging-tools/tests/test_installer_utils.py @@ -96,7 +96,7 @@ class TestInstallerUtils(unittest.TestCase): # create tmp file tempFileName = "foobar.txt" tempFilePath = os.path.join(absoluteTempPath, tempFileName) - with open(tempFilePath, 'w+') as f: + with open(tempFilePath, 'w+', encoding="utf-8") as f: f.write("\n") self.assertTrue(os.path.isfile(tempFilePath)) diff --git a/packaging-tools/tests/test_packaging.py b/packaging-tools/tests/test_packaging.py index 411b27b71..5fba2b303 100755 --- a/packaging-tools/tests/test_packaging.py +++ b/packaging-tools/tests/test_packaging.py @@ -123,7 +123,7 @@ class TestPackaging(unittest.TestCase): tempFile = os.path.join(tempDir, "qconfig.pri") try: - with open(tempFile, "a") as f: + with open(tempFile, "a", encoding="utf-8") as f: f.write("something foo\n") f.write("QT_EDITION = foobar\n") f.write("nonsense\n") diff --git a/packaging-tools/tests/test_release_repo_meta_update.py b/packaging-tools/tests/test_release_repo_meta_update.py index b098e7ff8..63911ca78 100755 --- a/packaging-tools/tests/test_release_repo_meta_update.py +++ b/packaging-tools/tests/test_release_repo_meta_update.py @@ -100,7 +100,7 @@ class TestReleaseRepoMetaUpdate(unittest.TestCase): tmp = os.path.join(tmp_base_dir, path) os.makedirs(os.path.dirname(tmp), exist_ok=True) if tmp.endswith((".xml", ".7z")): - with open(tmp, 'w+') as f: + with open(tmp, 'w+', encoding="utf-8") as f: f.write("\n") @asyncio_test diff --git a/packaging-tools/tests/test_release_repo_updater.py b/packaging-tools/tests/test_release_repo_updater.py index a496903b2..d92a19dd5 100755 --- a/packaging-tools/tests/test_release_repo_updater.py +++ b/packaging-tools/tests/test_release_repo_updater.py @@ -65,13 +65,13 @@ from tests.testhelpers import ( def _write_dummy_file(path: str) -> None: os.makedirs(os.path.dirname(path), exist_ok=True) - with open(path, 'w+') as f: + with open(path, 'w+', encoding="utf-8") as f: f.write("\n") def _write_package_xml(path: str, version: str, releaseDate: str) -> None: os.makedirs(os.path.dirname(path), exist_ok=True) - with open(path, 'w+') as f: + with open(path, 'w+', encoding="utf-8") as f: f.write("<?xml version=\"1.0\"?>\n") f.write("<Package>\n") f.write(" <Name>qt.foo.bar1</Name>\n") @@ -84,7 +84,7 @@ def _write_package_xml(path: str, version: str, releaseDate: str) -> None: def _write_updates_xml(path: str, version: str, releaseDate: str) -> None: os.makedirs(os.path.dirname(path), exist_ok=True) - with open(path, 'w+') as f: + with open(path, 'w+', encoding="utf-8") as f: f.write("<Updates>\n") f.write(" <ApplicationName>{AnyApplication}</ApplicationName>\n") f.write(" <ApplicationVersion>1.0.0</ApplicationVersion>\n") diff --git a/packaging-tools/tests/test_runner.py b/packaging-tools/tests/test_runner.py index 67d245be5..6aa7269ba 100755 --- a/packaging-tools/tests/test_runner.py +++ b/packaging-tools/tests/test_runner.py @@ -109,9 +109,9 @@ class TestRunner(unittest.TestCase): with TemporaryDirectory(dir=os.getcwd()) as tmp_base_dir: log_file = Path(tmp_base_dir) / "log" log_file.touch() - with open(log_file, 'w') as f: + with open(log_file, 'w', encoding="utf-8") as f: do_execute_sub_process(["echo", "TEST"], tmp_base_dir, redirect_output=f) - with open(log_file, 'r') as f: + with open(log_file, 'r', encoding="utf-8") as f: self.assertEqual(f.read().strip(), "TEST") @data( |