+2024-04-13 Bruno Haible <bruno@clisp.org>
+
+ gnulib-tool.py: Stop using codecs.open.
+ * pygnulib/*.py: To open a file, consistently use
+ open(..., mode='[rwa]', newline='\n', encoding='utf-8').
+
2024-04-13 Bruno Haible <bruno@clisp.org>
gnulib-tool.py: Refactor directory tree removals.
Various other refactorings, as deemed useful:
- Use an enum for 'all', 'old', 'new', 'added', 'removed' in GLImport.py.
- - go through all the open() and codecs.open() calls and turn them into
- with open(file_name, 'r', newline='\n', encoding='utf-8') as file:
- or
- with open(file_name, 'w', newline='\n', encoding='utf-8') as file:
================================================================================
#===============================================================================
import os
import re
-import codecs
import subprocess as sp
from collections.abc import Callable
from . import constants
if makefile_name:
path = joinpath(sourcebase, 'Makefile.am')
if isfile(path):
- with codecs.open(path, 'rb', 'UTF-8') as file:
+ with open(path, mode='r', newline='\n', encoding='utf-8') as file:
data = file.read()
if pattern.findall(data):
lib_gets_installed = True
transformer = sed_transform_testsrelated_lib_file
if transformer != None:
# Read the file that we looked up.
- with open(lookedup, 'r', newline='\n', encoding='utf-8') as file:
+ with open(lookedup, mode='r', newline='\n', encoding='utf-8') as file:
src_data = file.read()
# Write the transformed data to the temporary file.
- with open(tmpfile, 'w', newline='\n', encoding='utf-8') as file:
+ with open(tmpfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(re.sub(transformer[0], transformer[1], src_data))
path = joinpath(self.config['destdir'], rewritten)
if isfile(path):
#===============================================================================
import os
import re
-import codecs
import subprocess as sp
from . import constants
from .GLError import GLError
os.rmdir(self.cache['tempdir'])
# Read configure.{ac,in}.
- with codecs.open(self.config.getAutoconfFile(), 'rb', 'UTF-8') as file:
+ with open(self.config.getAutoconfFile(), mode='r', newline='\n', encoding='utf-8') as file:
data = file.read()
# Get cached auxdir and libtool from configure.{ac,in}.
# Get other cached variables.
path = joinpath(self.config['m4base'], 'gnulib-cache.m4')
if isfile(path):
- with codecs.open(path, 'rb', 'UTF-8') as file:
+ with open(path, mode='r', newline='\n', encoding='utf-8') as file:
data = file.read()
# gl_LGPL is special, because it can occur with or without
destdir, m4base = self.config.getDestDir(), self.config.getM4Base()
path = joinpath(destdir, m4base, 'gnulib-comp.m4')
if isfile(path):
- with codecs.open(path, 'rb', 'UTF-8') as file:
+ with open(path, mode='r', newline='\n', encoding='utf-8') as file:
data = file.read()
regex = r'AC_DEFUN\(\[%s_FILE_LIST\], \[(.*?)\]\)' % self.cache['macro_prefix']
pattern = re.compile(regex, re.S | re.M)
if self.config['automake_subdir'] or self.config['automake_subdir_tests']:
found_subdir_objects = False
if self.config['destdir']:
- with open(self.config['configure_ac'], encoding='utf-8') as file:
+ with open(self.config['configure_ac'], mode='r', newline='\n', encoding='utf-8') as file:
data = file.read()
pattern = re.compile(r'^.*AM_INIT_AUTOMAKE\([\[ ]*([^])]*).*$', re.MULTILINE)
configure_ac_automake_options = pattern.findall(data)
else:
base = '.'
if isfile(joinpath(base, 'Makefile.am')):
- with open(joinpath(base, 'Makefile.am'), encoding='utf-8') as file:
+ with open(joinpath(base, 'Makefile.am'), mode='r', newline='\n', encoding='utf-8') as file:
data = file.read()
pattern = re.compile(r'^AUTOMAKE_OPTIONS[\t ]*=(.*)$', re.MULTILINE)
automake_options = pattern.findall(data)
backupname = '%s~' % srcpath
if isfile(joinpath(destdir, srcpath)):
if files_added or files_removed:
- with codecs.open(joinpath(destdir, srcpath), 'rb', 'UTF-8') as file:
+ with open(joinpath(destdir, srcpath), mode='r', newline='\n', encoding='utf-8') as file:
original_lines = file.readlines()
# Clean the newlines but not trailing whitespace.
original_lines = [ line.rstrip('\n')
new_lines = [ line
for line in new_lines
if line not in lines_to_remove ]
- with codecs.open(joinpath(destdir, srcpath), 'wb', 'UTF-8') as file:
+ with open(joinpath(destdir, srcpath), mode='w', newline='\n', encoding='utf-8') as file:
file.write(lines_to_multiline(new_lines))
else: # if self.config['dryrun']
print('Update %s (backup in %s)' % (srcpath, backupname))
if ignore == '.cvsignore':
# Automake generates Makefile rules that create .dirstamp files.
files_added = ['.deps', '.dirstamp'] + files_added
- with codecs.open(joinpath(destdir, srcpath), 'wb', 'UTF-8') as file:
+ with open(joinpath(destdir, srcpath), mode='w', newline='\n', encoding='utf-8') as file:
file.write(lines_to_multiline(files_added))
else: # if self.config['dryrun']
print('Create %s' % srcpath)
basename = joinpath(pobase, 'Makevars')
tmpfile = self.assistant.tmpfilename(basename)
emit = self.emitter.po_Makevars()
- with codecs.open(tmpfile, 'wb', 'UTF-8') as file:
+ with open(tmpfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
filename, backup, flag = self.assistant.super_update(basename, tmpfile)
if flag == 1:
# Create po makefile parameterization, part 2.
basename = joinpath(pobase, 'POTFILES.in')
tmpfile = self.assistant.tmpfilename(basename)
- with codecs.open(tmpfile, 'wb', 'UTF-8') as file:
+ with open(tmpfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(self.emitter.po_POTFILES_in(filetable['all']))
basename = joinpath(pobase, 'POTFILES.in')
filename, backup, flag = self.assistant.super_update(basename, tmpfile)
for file in os.listdir(joinpath(destdir, pobase))
if file.endswith('.po') ])
data += lines_to_multiline(files)
- with codecs.open(tmpfile, 'wb', 'UTF-8') as file:
+ with open(tmpfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(data)
filename, backup, flag = self.assistant.super_update(basename, tmpfile)
if flag == 1:
basename = joinpath(m4base, 'gnulib-cache.m4')
tmpfile = self.assistant.tmpfilename(basename)
emit = self.gnulib_cache()
- with codecs.open(tmpfile, 'wb', 'UTF-8') as file:
+ with open(tmpfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
filename, backup, flag = self.assistant.super_update(basename, tmpfile)
if flag == 1:
basename = joinpath(m4base, 'gnulib-comp.m4')
tmpfile = self.assistant.tmpfilename(basename)
emit = self.gnulib_comp(filetable, gentests)
- with codecs.open(tmpfile, 'wb', 'UTF-8') as file:
+ with open(tmpfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
filename, backup, flag = self.assistant.super_update(basename, tmpfile)
if flag == 1:
emit = sp.run([joinpath(DIRS['root'], 'build-aux/prefix-gnulib-mk'), '--from-gnulib-tool',
f'--lib-name={libname}', f'--prefix={sourcebase}/'],
input=emit, text=True, capture_output=True).stdout
- with codecs.open(tmpfile, 'wb', 'UTF-8') as file:
+ with open(tmpfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
filename, backup, flag = self.assistant.super_update(basename, tmpfile)
if flag == 1:
emit = self.emitter.tests_Makefile_am(basename, self.moduletable.getTestsModules(),
self.moduletable, self.makefiletable,
'%stests_WITNESS' % macro_prefix, for_test)
- with codecs.open(tmpfile, 'wb', 'UTF-8') as file:
+ with open(tmpfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
filename, backup, flag = self.assistant.super_update(basename, tmpfile)
if flag == 1:
% (dictionary['val'], dictionary['var'], joinpath(dictionary['dir'], 'Makefile.am')))
# Detect position_early_after.
- with codecs.open(configure_ac, 'rb', 'UTF-8') as file:
+ with open(configure_ac, mode='r', newline='\n', encoding='utf-8') as file:
data = file.read()
match_result1 = \
bool(re.compile(r'^ *AC_PROG_CC_STDC', re.M).findall(data))
#===============================================================================
import os
import re
-import codecs
import subprocess as sp
from . import constants
result = result.rstrip(os.linesep)
return result
# gnulib copy without versioning information.
- with codecs.open(os.path.join(DIRS['root'], 'ChangeLog'), 'rb', 'UTF-8') as file:
+ with open(os.path.join(DIRS['root'], 'ChangeLog'), mode='r', newline='\n', encoding='utf-8') as file:
line = file.readline()
first_changelog_line = line.rstrip()
result = re.compile(r' .*').sub(r'', first_changelog_line)
import os
import re
import sys
-import codecs
import hashlib
import subprocess as sp
from . import constants
self.filesystem = GLFileSystem(self.config)
self.modulesystem = GLModuleSystem(self.config)
# Read the module description file into memory.
- with codecs.open(path, 'rb', 'UTF-8') as file:
+ with open(path, mode='r', newline='\n', encoding='utf-8') as file:
self.content = file.read().replace('\r\n', '\n')
# Dissect it into sections.
self.sections = dict()
import os
import re
import sys
-import codecs
import subprocess as sp
from pathlib import Path
from . import constants
else: # if not single_configure
emit = self.emitter.lib_Makefile_am(destfile, modules,
moduletable, self.makefiletable, '', for_test)
- with codecs.open(destfile, 'wb', 'UTF-8') as file:
+ with open(destfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
# Create $m4base/Makefile.am.
file = constants.substart('m4/', '', file)
emit += 'EXTRA_DIST += %s\n' % file
emit = constants.nlconvert(emit)
- with codecs.open(destfile, 'wb', 'UTF-8') as file:
+ with open(destfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
subdirs = [sourcebase, m4base]
witness_macro = '%stests_WITNESS' % macro_prefix
emit = self.emitter.tests_Makefile_am(destfile, tests_modules, moduletable,
self.makefiletable, witness_macro, for_test)
- with codecs.open(destfile, 'wb', 'UTF-8') as file:
+ with open(destfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
else: # if not single_configure
# Create $testsbase/Makefile.am.
self.config.setLibtests(False)
emit = self.emitter.tests_Makefile_am(destfile, modules, moduletable,
self.makefiletable, '', for_test)
- with codecs.open(destfile, 'wb', 'UTF-8') as file:
+ with open(destfile, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
# Viewed from the $testsbase subdirectory, $auxdir is different.
emit = ''
emit += 'AC_OUTPUT\n'
emit = constants.nlconvert(emit)
path = joinpath(self.testdir, testsbase, 'configure.ac')
- with codecs.open(path, 'wb', 'UTF-8') as file:
+ with open(path, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
# Restore changed variables.
emit += 'ACLOCAL_AMFLAGS = -I %s\n' % m4base
emit = constants.nlconvert(emit)
path = joinpath(self.testdir, 'Makefile.am')
- with codecs.open(path, 'wb', 'UTF-8') as file:
+ with open(path, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
# Create configure.ac
emit += 'AC_CONFIG_FILES([%s])\n' % ' '.join(makefiles)
emit += 'AC_OUTPUT\n'
path = joinpath(self.testdir, 'configure.ac')
- with codecs.open(path, 'wb', 'UTF-8') as file:
+ with open(path, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
# Create autogenerated files.
# Need to run configure and make once, to create built files that are to be
# distributed (such as parse-datetime.c).
path = joinpath(self.testdir, sourcebase, 'Makefile.am')
- with codecs.open(path, 'rb', 'UTF-8') as file:
+ with open(path, mode='r', newline='\n', encoding='utf-8') as file:
snippet = file.read()
snippet = combine_lines(snippet)
if inctests:
# Likewise for built files in the $testsbase directory.
path = joinpath(self.testdir, testsbase, 'Makefile.am')
- with codecs.open(path, 'rb', 'UTF-8') as file:
+ with open(path, mode='r', newline='\n', encoding='utf-8') as file:
snippet = file.read()
snippet = combine_lines(snippet)
sp.call('./configure')
if distributed_built_sources:
os.chdir(sourcebase)
- with codecs.open('Makefile', 'ab', 'UTF-8') as file:
+ with open('Makefile', mode='a', newline='\n', encoding='utf-8') as file:
file.write('built_sources: $(BUILT_SOURCES)\n')
args = [UTILS['make'],
'AUTOCONF=%s' % UTILS['autoconf'],
os.chdir('..')
if tests_distributed_built_sources:
os.chdir(testsbase)
- with codecs.open('Makefile', 'ab', 'UTF-8') as file:
+ with open('Makefile', mode='a', newline='\n', encoding='utf-8') as file:
file.write('built_sources: $(BUILT_SOURCES)\n')
args = [UTILS['make'],
'AUTOCONF=%s' % UTILS['autoconf'],
emit += 'done\n'
emit = constants.nlconvert(emit)
path = joinpath(self.megatestdir, 'do-autobuild')
- with codecs.open(path, 'wb', 'UTF-8') as file:
+ with open(path, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
# Create Makefile.am.
emit += 'EXTRA_DIST = do-autobuild\n'
emit = constants.nlconvert(emit)
path = joinpath(self.megatestdir, 'Makefile.am')
- with codecs.open(path, 'wb', 'UTF-8') as file:
+ with open(path, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
emit = '# Process this file with autoconf '
emit += 'AC_OUTPUT\n'
emit = constants.nlconvert(emit)
path = joinpath(self.megatestdir, 'configure.ac')
- with codecs.open(path, 'wb', 'UTF-8') as file:
+ with open(path, mode='w', newline='\n', encoding='utf-8') as file:
file.write(emit)
# Create autogenerated files.
import platform
import shutil
import tempfile
-import codecs
import subprocess as sp
import __main__ as interpreter
os.remove(temp)
else:
print('executing %s' % ' '.join(args))
- with codecs.open(temp, 'rb') as file:
+ with open(temp, mode='r', newline='\n', encoding='utf-8') as file:
cmdout = file.read()
print(cmdout)
os.remove(temp)
import os
import re
import sys
-import codecs
import random
import argparse
import subprocess as sp
config.setAutoconfFile(configure_ac)
# Analyze configure.ac.
- with open(configure_ac, 'r', encoding='utf-8') as file:
+ with open(configure_ac, mode='r', newline='\n', encoding='utf-8') as file:
configure_ac_data = file.read()
guessed_m4dirs = []
dirisnext = False
filepath = joinpath(destdir, 'Makefile.am')
if isfile(filepath):
- with codecs.open(filepath, 'rb', 'UTF-8') as file:
+ with open(filepath, mode='r', newline='\n', encoding='utf-8') as file:
data = file.read()
data = data.split('ACLOCAL_AMFLAGS')[1]
data = data[data.find('=') + 1:data.find('\n')]
filepath = joinpath(destdir, 'aclocal.m4')
if isfile(filepath):
pattern = re.compile(r'm4_include\(\[(.*?)]\)')
- with codecs.open(filepath, 'rb', 'UTF-8') as file:
+ with open(filepath, mode='r', newline='\n', encoding='utf-8') as file:
m4dirs = pattern.findall(file.read())
m4dirs = [ os.path.dirname(m4dir)
for m4dir in m4dirs ]
incompatibilities += ' %s' % pair[1]
incompatibilities += constants.NL
tempname = mktemp()
- with codecs.open(tempname, 'wb', 'UTF-8') as file:
+ with open(tempname, mode='w', newline='\n', encoding='utf-8') as file:
file.write(incompatibilities)
sed_table = 's,^\\([^ ]*\\) ,\\1' + ' ' * 51 + ',\n'
sed_table += 's,^\\(' + '.' * 49 + '[^ ]*\\) *,' + ' ' * 17 + '\\1 ,'