Hash :
07a187be
Author :
Date :
2017-09-09T14:40:10
gnulib-tool.py: follow gnulib-tool changes, part 14 Follow gnulib-tool change 2015-08-20 Daiki Ueno <ueno@gnu.org> gnulib-tool: don't transform binary files with sed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
#!/usr/bin/python
# encoding: UTF-8
#===============================================================================
# Define global imports
#===============================================================================
import os
import re
import sys
import codecs
import shutil
import filecmp
import subprocess as sp
from . import constants
from .GLError import GLError
from .GLConfig import GLConfig
#===============================================================================
# Define module information
#===============================================================================
__author__ = constants.__author__
__license__ = constants.__license__
__copyright__ = constants.__copyright__
#===============================================================================
# Define global constants
#===============================================================================
PYTHON3 = constants.PYTHON3
NoneType = type(None)
APP = constants.APP
DIRS = constants.DIRS
ENCS = constants.ENCS
UTILS = constants.UTILS
MODES = constants.MODES
TESTS = constants.TESTS
compiler = constants.compiler
joinpath = constants.joinpath
cleaner = constants.cleaner
string = constants.string
isabs = os.path.isabs
isdir = os.path.isdir
isfile = os.path.isfile
normpath = os.path.normpath
relpath = os.path.relpath
#===============================================================================
# Define GLFileSystem class
#===============================================================================
class GLFileSystem(object):
'''GLFileSystem class is used to create virtual filesystem, which is based on
the gnulib directory and directory specified by localdir argument. Its main
method lookup(file) is used to find file in these directories or combine it
using Linux 'patch' utility.'''
def __init__(self, config):
'''Create new GLFileSystem instance. The only argument is localdir,
which can be an empty string too.'''
if type(config) is not GLConfig:
raise(TypeError('config must be a GLConfig, not %s' %
type(config).__name__))
self.config = config
def __repr__(self):
'''x.__repr__ <==> repr(x)'''
result = '<pygnulib.GLFileSystem %s>' % hex(id(self))
return(result)
def lookup(self, name):
'''GLFileSystem.lookup(name) -> tuple
Lookup a file in gnulib and localdir directories or combine it using Linux
'patch' utility. If file was found, method returns string, else it raises
GLError telling that file was not found. Function also returns flag which
indicates whether file is a temporary file.
GLConfig: localdir.'''
if type(name) is bytes or type(name) is string:
if type(name) is bytes:
name = name.decode(ENCS['default'])
else: # if name has not bytes or string type
raise(TypeError(
'name must be a string, not %s' % type(module).__name__))
# If name exists in localdir, then we use it
path_gnulib = joinpath(DIRS['root'], name)
path_local = joinpath(self.config['localdir'], name)
path_diff = joinpath(self.config['localdir'], '%s.diff' % name)
path_temp = joinpath(self.config['tempdir'], name)
try: # Try to create directories
os.makedirs(os.path.dirname(path_temp))
except OSError as error:
pass # Skip errors if directory exists
if isfile(path_temp):
os.remove(path_temp)
if self.config['localdir'] and isfile(path_local):
result = (path_local, False)
else: # if path_local does not exist
if isfile(path_gnulib):
if self.config['localdir'] and isfile(path_diff):
shutil.copy(path_gnulib, path_temp)
command = 'patch -s "%s" < "%s" >&2' % (path_temp, path_diff)
try: # Try to apply patch
sp.check_call(command, shell=True)
except sp.CalledProcessError as error:
raise(GLError(2, name))
result = (path_temp, True)
else: # if path_diff does not exist
result = (path_gnulib, False)
else: # if path_gnulib does not exist
raise(GLError(1, name))
return(result)
#===============================================================================
# Define GLFileAssistant class
#===============================================================================
class GLFileAssistant(object):
'''GLFileAssistant is used to help with file processing.'''
def __init__(self, config, transformers=dict()):
'''Create GLFileAssistant instance.'''
if type(config) is not GLConfig:
raise(TypeError('config must be a GLConfig, not %s' %
type(config).__name__))
if type(transformers) is not dict:
raise(TypeError('transformers must be a dict, not %s' %
type(transformers).__name__))
for key in ['lib', 'aux', 'main', 'tests']:
if key not in transformers:
transformers[key] = 's,x,x,'
else: # if key in transformers
value = transformers[key]
if type(value) is bytes or type(value) is string:
if type(value) is bytes:
transformers[key] = value.decode(ENCS['default'])
else: # if value has not bytes or string type
raise(TypeError('transformers[%s] must be a string, not %s' %
(key, type(value).__name__)))
self.original = None
self.rewritten = None
self.added = list()
self.makefile = list()
self.config = config
self.transformers = transformers
self.filesystem = GLFileSystem(self.config)
def __repr__(self):
'''x.__repr__() <==> repr(x)'''
result = '<pygnulib.GLFileAssistant %s>' % hex(id(self))
return(result)
def tmpfilename(self, path):
'''GLFileAssistant.tmpfilename() -> string
Return the name of a temporary file (file is relative to destdir).'''
if type(path) is bytes or type(path) is string:
if type(path) is bytes:
path = path.decode(ENCS['default'])
else: # if path has not bytes or string type
raise(TypeError(
'path must be a string, not %s' % (type(path).__name__)))
if not self.config['dryrun']:
# Put the new contents of $file in a file in the same directory (needed
# to guarantee that an 'mv' to "$destdir/$file" works).
result = joinpath(self.config['destdir'], '%s.tmp' % path)
dirname = os.path.dirname(result)
if dirname and not isdir(dirname):
os.makedirs(dirname)
else: # if self.config['dryrun']
# Put the new contents of $file in a file in a temporary directory
# (because the directory of "$file" might not exist).
tempdir = self.config['tempdir']
result = joinpath(tempdir, '%s.tmp' % os.path.basename(path))
dirname = os.path.dirname(result)
if not isdir(dirname):
os.makedirs(dirname)
if type(result) is bytes:
result = bytes.decode(ENCS['default'])
return(result)
def setOriginal(self, original):
'''GLFileAssistant.setOriginal(original)
Set the name of the original file which will be used.'''
if type(original) is bytes or type(original) is string:
if type(original) is bytes:
original = original.decode(ENCS['default'])
else: # if original has not bytes or string type
raise(TypeError(
'original must be a string, not %s' % (type(original).__name__)))
self.original = original
def setRewritten(self, rewritten):
'''GLFileAssistant.setRewritten(rewritten)
Set the name of the rewritten file which will be used.'''
if type(rewritten) is bytes or type(rewritten) is string:
if type(rewritten) is bytes:
rewritten = rewritten.decode(ENCS['default'])
else: # if rewritten has not bytes or string type
raise(TypeError(
'rewritten must be a string, not %s' % type(rewritten).__name__))
self.rewritten = rewritten
def addFile(self, file):
'''GLFileAssistant.addFile(file)
Add file to the list of added files.'''
if file not in self.added:
self.added += [file]
def removeFile(self, file):
'''GLFileAssistant.removeFile(file)
Remove file from the list of added files.'''
if file in self.added:
self.added.pop(file)
def getFiles(self):
'''Return list of the added files.'''
return(list(self.added))
def add(self, lookedup, tmpflag, tmpfile):
'''GLFileAssistant.add(lookedup, tmpflag, tmpfile)
This method copies a file from gnulib into the destination directory.
The destination is known to exist. If tmpflag is True, then lookedup file
is a temporary one.'''
original = self.original
rewritten = self.rewritten
destdir = self.config['destdir']
symbolic = self.config['symbolic']
lsymbolic = self.config['lsymbolic']
if original == None:
raise(TypeError('original must be set before applying the method'))
elif rewritten == None:
raise(TypeError('rewritten must be set before applying the method'))
if not self.config['dryrun']:
print('Copying file %s' % rewritten)
loriginal = joinpath(self.config['localdir'], original)
if (symbolic or (lsymbolic and lookedup == loriginal)) \
and not tmpflag and filecmp.cmp(lookedup, tmpfile):
constants.link_if_changed(
lookedup, joinpath(destdir, rewritten))
else: # if any of these conditions is not met
try: # Try to move file
shutil.move(tmpfile, joinpath(destdir, rewritten))
except Exception as error:
raise(GLError(17, original))
else: # if self.config['dryrun']
print('Copy file %s' % rewritten)
def update(self, lookedup, tmpflag, tmpfile, already_present):
'''GLFileAssistant.update(lookedup, tmpflag, tmpfile, already_present)
This method copies a file from gnulib into the destination directory.
The destination is known to exist. If tmpflag is True, then lookedup file
is a temporary one.'''
original = self.original
rewritten = self.rewritten
destdir = self.config['destdir']
symbolic = self.config['symbolic']
lsymbolic = self.config['lsymbolic']
if original == None:
raise(TypeError('original must be set before applying the method'))
elif rewritten == None:
raise(TypeError('rewritten must be set before applying the method'))
if type(lookedup) is bytes or type(lookedup) is string:
if type(lookedup) is bytes:
lookedup = lookedup.decode(ENCS['default'])
else: # if lookedup has not bytes or string type
raise(TypeError('lookedup must be a string, not %s' %
type(lookedup).__name__))
if type(already_present) is not bool:
raise(TypeError('already_present must be a bool, not %s' %
type(already_present).__name__))
basename = rewritten
backupname = string('%s~' % basename)
basepath = joinpath(destdir, basename)
backuppath = joinpath(destdir, backupname)
if not filecmp.cmp(basepath, tmpfile):
if not self.config['dryrun']:
if already_present:
print('Updating file %s (backup in %s)' %
(basename, backupname))
else: # if not already_present
message = 'Replacing file '
message += '%s (non-gnulib code backed up in ' % basename
message += '%s) !!' % backupname
print(message)
if isfile(backuppath):
os.remove(backuppath)
try: # Try to replace the given file
shutil.move(basepath, backuppath)
except Exception as error:
raise(GLError(17, original))
loriginal = joinpath(self.config['localdir'], original)
if (symbolic or (lsymbolic and lookedup == loriginal)) \
and not tmpflag and filecmp.cmp(lookedup, tmpfile):
constants.link_if_changed(lookedup, basepath)
else: # if any of these conditions is not met
try: # Try to move file
if os.path.exists(basepath):
os.remove(basepath)
shutil.copy(tmpfile, rewritten)
except Exception as error:
raise(GLError(17, original))
else: # if self.config['dryrun']
if already_present:
print('Update file %s (backup in %s)' %
(rewritten, backup))
else: # if not already_present
print('Replace file %s (backup in %s)' %
(rewritten, backup))
def add_or_update(self, already_present):
'''GLFileAssistant.add_or_update(already_present)
This method handles a file that ought to be present afterwards.'''
original = self.original
rewritten = self.rewritten
if original == None:
raise(TypeError('original must be set before applying the method'))
elif rewritten == None:
raise(TypeError('rewritten must be set before applying the method'))
if type(already_present) is not bool:
raise(TypeError('already_present must be a bool, not %s' %
type(already_present).__name__))
xoriginal = original
if original.startswith('tests=lib/'):
xoriginal = constants.substart('tests=lib/', 'lib/', original)
lookedup, tmpflag = self.filesystem.lookup(xoriginal)
tmpfile = self.tmpfilename(rewritten)
sed_transform_lib_file = self.transformers.get('lib', '')
sed_transform_build_aux_file = self.transformers.get('aux', '')
sed_transform_main_lib_file = self.transformers.get('main', '')
sed_transform_testsrelated_lib_file = self.transformers.get(
'tests', '')
try: # Try to copy lookedup file to tmpfile
shutil.copy(lookedup, tmpfile)
except Exception as error:
raise(GLError(15, lookedup))
# Don't process binary files with sed.
if not (original.endswith(".class") or original.endswith(".mo")):
transformer = string()
if original.startswith('lib/'):
if sed_transform_main_lib_file:
transformer = sed_transform_main_lib_file
elif original.startswith('build-aux/'):
if sed_transform_build_aux_file:
transformer = sed_transform_build_aux_file
elif original.startswith('tests=lib/'):
if sed_transform_testsrelated_lib_file:
transformer = sed_transform_testsrelated_lib_file
if transformer:
args = ['sed', '-e', transformer]
stdin = codecs.open(lookedup, 'rb', 'UTF-8')
try: # Try to transform file
data = sp.check_output(args, stdin=stdin, shell=False)
data = data.decode("UTF-8")
except Exception as error:
raise(GLError(16, lookedup))
with codecs.open(tmpfile, 'wb', 'UTF-8') as file:
file.write(data)
path = joinpath(self.config['destdir'], rewritten)
if isfile(path):
self.update(lookedup, tmpflag, tmpfile, already_present)
os.remove(tmpfile)
else: # if not isfile(path)
self.add(lookedup, tmpflag, tmpfile)
self.addFile(rewritten)
def super_update(self, basename, tmpfile):
'''GLFileAssistant.super_update(basename, tmpfile) -> tuple
Move tmpfile to destdir/basename path, making a backup of it.
Returns tuple, which contains basename, backupname and status.
0: tmpfile is the same as destfile;
1: tmpfile was used to update destfile;
2: destfile was created, because it didn't exist.'''
backupname = '%s~' % basename
basepath = joinpath(self.config['destdir'], basename)
backuppath = joinpath(self.config['destdir'], backupname)
if isfile(basepath):
if filecmp.cmp(basepath, tmpfile):
result_flag = 0
else: # if not filecmp.cmp(basepath, tmpfile)
result_flag = 1
if not self.config['dryrun']:
if isfile(backuppath):
os.remove(backuppath)
shutil.move(basepath, backuppath)
shutil.move(tmpfile, basepath)
else: # if self.config['dryrun']
os.remove(tmpfile)
else: # if not isfile(basepath)
result_flag = 2
if not self.config['dryrun']:
if isfile(basepath):
os.remove(basepath)
shutil.move(tmpfile, basepath)
else: # if self.config['dryrun']
os.remove(tmpfile)
result = tuple([basename, backupname, result_flag])
return(result)