annotate pygnulib/GLFileSystem.py @ 40186:8964917f9574

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