/usr/lib/python2.7/dist-packages/pyramid/scaffolds/copydir.py is in python-pyramid 1.4.5+dfsg-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # (c) 2005 Ian Bicking and contributors; written for Paste
# (http://pythonpaste.org) Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license.php
import os
import sys
import pkg_resources
from pyramid.compat import (
input_,
native_,
url_quote as compat_url_quote,
escape,
)
fsenc = sys.getfilesystemencoding()
class SkipTemplate(Exception):
"""
Raised to indicate that the template should not be copied over.
Raise this exception during the substitution of your template
"""
def copy_dir(source, dest, vars, verbosity, simulate, indent=0,
sub_vars=True, interactive=False, overwrite=True,
template_renderer=None, out_=sys.stdout):
"""
Copies the ``source`` directory to the ``dest`` directory.
``vars``: A dictionary of variables to use in any substitutions.
``verbosity``: Higher numbers will show more about what is happening.
``simulate``: If true, then don't actually *do* anything.
``indent``: Indent any messages by this amount.
``sub_vars``: If true, variables in ``_tmpl`` files and ``+var+``
in filenames will be substituted.
``overwrite``: If false, then don't every overwrite anything.
``interactive``: If you are overwriting a file and interactive is
true, then ask before overwriting.
``template_renderer``: This is a function for rendering templates (if you
don't want to use string.Template). It should have the signature
``template_renderer(content_as_string, vars_as_dict,
filename=filename)``.
"""
def out(msg):
out_.write(msg)
out_.write('\n')
out_.flush()
# This allows you to use a leading +dot+ in filenames which would
# otherwise be skipped because leading dots make the file hidden:
vars.setdefault('dot', '.')
vars.setdefault('plus', '+')
use_pkg_resources = isinstance(source, tuple)
if use_pkg_resources:
names = sorted(pkg_resources.resource_listdir(source[0], source[1]))
else:
names = sorted(os.listdir(source))
pad = ' '*(indent*2)
if not os.path.exists(dest):
if verbosity >= 1:
out('%sCreating %s/' % (pad, dest))
if not simulate:
makedirs(dest, verbosity=verbosity, pad=pad)
elif verbosity >= 2:
out('%sDirectory %s exists' % (pad, dest))
for name in names:
if use_pkg_resources:
full = '/'.join([source[1], name])
else:
full = os.path.join(source, name)
reason = should_skip_file(name)
if reason:
if verbosity >= 2:
reason = pad + reason % {'filename': full}
out(reason)
continue # pragma: no cover
if sub_vars:
dest_full = os.path.join(dest, substitute_filename(name, vars))
sub_file = False
if dest_full.endswith('_tmpl'):
dest_full = dest_full[:-5]
sub_file = sub_vars
if use_pkg_resources and pkg_resources.resource_isdir(source[0], full):
if verbosity:
out('%sRecursing into %s' % (pad, os.path.basename(full)))
copy_dir((source[0], full), dest_full, vars, verbosity, simulate,
indent=indent+1,
sub_vars=sub_vars, interactive=interactive,
template_renderer=template_renderer, out_=out_)
continue
elif not use_pkg_resources and os.path.isdir(full):
if verbosity:
out('%sRecursing into %s' % (pad, os.path.basename(full)))
copy_dir(full, dest_full, vars, verbosity, simulate,
indent=indent+1,
sub_vars=sub_vars, interactive=interactive,
template_renderer=template_renderer, out_=out_)
continue
elif use_pkg_resources:
content = pkg_resources.resource_string(source[0], full)
else:
f = open(full, 'rb')
content = f.read()
f.close()
if sub_file:
try:
content = substitute_content(
content, vars, filename=full,
template_renderer=template_renderer
)
except SkipTemplate:
continue # pragma: no cover
if content is None:
continue # pragma: no cover
already_exists = os.path.exists(dest_full)
if already_exists:
f = open(dest_full, 'rb')
old_content = f.read()
f.close()
if old_content == content:
if verbosity:
out('%s%s already exists (same content)' %
(pad, dest_full))
continue # pragma: no cover
if interactive:
if not query_interactive(
native_(full, fsenc), native_(dest_full, fsenc),
native_(content, fsenc), native_(old_content, fsenc),
simulate=simulate, out_=out_):
continue
elif not overwrite:
continue # pragma: no cover
if verbosity and use_pkg_resources:
out('%sCopying %s to %s' % (pad, full, dest_full))
elif verbosity:
out(
'%sCopying %s to %s' % (pad, os.path.basename(full),
dest_full))
if not simulate:
f = open(dest_full, 'wb')
f.write(content)
f.close()
def should_skip_file(name):
"""
Checks if a file should be skipped based on its name.
If it should be skipped, returns the reason, otherwise returns
None.
"""
if name.startswith('.'):
return 'Skipping hidden file %(filename)s'
if name.endswith('~') or name.endswith('.bak'):
return 'Skipping backup file %(filename)s'
if name.endswith('.pyc') or name.endswith('.pyo'):
return 'Skipping %s file ' % os.path.splitext(name)[1] + '%(filename)s'
if name.endswith('$py.class'):
return 'Skipping $py.class file %(filename)s'
if name in ('CVS', '_darcs'):
return 'Skipping version control directory %(filename)s'
return None
# Overridden on user's request:
all_answer = None
def query_interactive(src_fn, dest_fn, src_content, dest_content,
simulate, out_=sys.stdout):
def out(msg):
out_.write(msg)
out_.write('\n')
out_.flush()
global all_answer
from difflib import unified_diff, context_diff
u_diff = list(unified_diff(
dest_content.splitlines(),
src_content.splitlines(),
dest_fn, src_fn))
c_diff = list(context_diff(
dest_content.splitlines(),
src_content.splitlines(),
dest_fn, src_fn))
added = len([l for l in u_diff if l.startswith('+')
and not l.startswith('+++')])
removed = len([l for l in u_diff if l.startswith('-')
and not l.startswith('---')])
if added > removed:
msg = '; %i lines added' % (added-removed)
elif removed > added:
msg = '; %i lines removed' % (removed-added)
else:
msg = ''
out('Replace %i bytes with %i bytes (%i/%i lines changed%s)' % (
len(dest_content), len(src_content),
removed, len(dest_content.splitlines()), msg))
prompt = 'Overwrite %s [y/n/d/B/?] ' % dest_fn
while 1:
if all_answer is None:
response = input_(prompt).strip().lower()
else:
response = all_answer
if not response or response[0] == 'b':
import shutil
new_dest_fn = dest_fn + '.bak'
n = 0
while os.path.exists(new_dest_fn):
n += 1
new_dest_fn = dest_fn + '.bak' + str(n)
out('Backing up %s to %s' % (dest_fn, new_dest_fn))
if not simulate:
shutil.copyfile(dest_fn, new_dest_fn)
return True
elif response.startswith('all '):
rest = response[4:].strip()
if not rest or rest[0] not in ('y', 'n', 'b'):
out(query_usage)
continue
response = all_answer = rest[0]
if response[0] == 'y':
return True
elif response[0] == 'n':
return False
elif response == 'dc':
out('\n'.join(c_diff))
elif response[0] == 'd':
out('\n'.join(u_diff))
else:
out(query_usage)
query_usage = """\
Responses:
Y(es): Overwrite the file with the new content.
N(o): Do not overwrite the file.
D(iff): Show a unified diff of the proposed changes (dc=context diff)
B(ackup): Save the current file contents to a .bak file
(and overwrite)
Type "all Y/N/B" to use Y/N/B for answer to all future questions
"""
def makedirs(dir, verbosity, pad):
parent = os.path.dirname(os.path.abspath(dir))
if not os.path.exists(parent):
makedirs(parent, verbosity, pad) # pragma: no cover
os.mkdir(dir)
def substitute_filename(fn, vars):
for var, value in vars.items():
fn = fn.replace('+%s+' % var, str(value))
return fn
def substitute_content(content, vars, filename='<string>',
template_renderer=None):
v = standard_vars.copy()
v.update(vars)
return template_renderer(content, v, filename=filename)
def html_quote(s):
if s is None:
return ''
return escape(str(s), 1)
def url_quote(s):
if s is None:
return ''
return compat_url_quote(str(s))
def test(conf, true_cond, false_cond=None):
if conf:
return true_cond
else:
return false_cond
def skip_template(condition=True, *args):
"""
Raise SkipTemplate, which causes copydir to skip the template
being processed. If you pass in a condition, only raise if that
condition is true (allows you to use this with string.Template)
If you pass any additional arguments, they will be used to
instantiate SkipTemplate (generally use like
``skip_template(license=='GPL', 'Skipping file; not using GPL')``)
"""
if condition:
raise SkipTemplate(*args)
standard_vars = {
'nothing': None,
'html_quote': html_quote,
'url_quote': url_quote,
'empty': '""',
'test': test,
'repr': repr,
'str': str,
'bool': bool,
'SkipTemplate': SkipTemplate,
'skip_template': skip_template,
}
|