/usr/share/pyshared/paste/script/filemaker.py is in python-pastescript 1.7.5-1.
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 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 | # (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 glob
import pkg_resources
from paste.script import pluginlib, copydir
from paste.script.command import BadCommand
difflib = None
try:
import subprocess
except ImportError:
from paste.script.util import subprocess24 as subprocess
class FileOp(object):
"""
Enhance the ease of file copying/processing from a package into a target
project
"""
def __init__(self, simulate=False,
verbose=True,
interactive=True,
source_dir=None,
template_vars=None):
"""
Initialize our File operation helper object
source_dir
Should refer to the directory within the package
that contains the templates to be used for the other copy
operations. It is assumed that packages will keep all their
templates under a hierarchy starting here.
This should be an absolute path passed in, for example::
FileOp(source_dir=os.path.dirname(__file__) + '/templates')
"""
self.simulate = simulate
self.verbose = verbose
self.interactive = interactive
if template_vars is None:
template_vars = {}
self.template_vars = template_vars
self.source_dir = source_dir
self.use_pkg_resources = isinstance(source_dir, tuple)
def copy_file(self, template, dest, filename=None, add_py=True, package=True,
template_renderer=None):
"""
Copy a file from the source location to somewhere in the
destination.
template
The filename underneath self.source_dir to copy/process
dest
The destination directory in the project relative to where
this command is being run
filename
What to name the file in the target project, use the same name
as the template if not provided
add_py
Add a .py extension to all files copied
package
Whether or not this file is part of a Python package, and any
directories created should contain a __init__.py file as well.
template_renderer
An optional template renderer
"""
if not filename:
filename = template.split('/')[0]
if filename.endswith('_tmpl'):
filename = filename[:-5]
base_package, cdir = self.find_dir(dest, package)
self.template_vars['base_package'] = base_package
content = self.load_content(base_package, cdir, filename, template,
template_renderer=template_renderer)
if add_py:
# @@: Why is it a default to add a .py extension?
filename = '%s.py' % filename
dest = os.path.join(cdir, filename)
self.ensure_file(dest, content, package)
def copy_dir(self, template_dir, dest, destname=None, package=True):
"""
Copy a directory recursively, processing any files within it
that need to be processed (end in _tmpl).
template_dir
Directory under self.source_dir to copy/process
dest
Destination directory into which this directory will be copied
to.
destname
Use this name instead of the original template_dir name for
creating the directory
package
This directory will be a Python package and needs to have a
__init__.py file.
"""
# @@: This should actually be implemented
raise NotImplementedError
def load_content(self, base_package, base, name, template,
template_renderer=None):
blank = os.path.join(base, name + '.py')
read_content = True
if not os.path.exists(blank):
if self.use_pkg_resources:
fullpath = '/'.join([self.source_dir[1], template])
content = pkg_resources.resource_string(
self.source_dir[0], fullpath)
read_content = False
blank = fullpath
else:
blank = os.path.join(self.source_dir,
template)
if read_content:
f = open(blank, 'r')
content = f.read()
f.close()
if blank.endswith('_tmpl'):
content = copydir.substitute_content(
content, self.template_vars, filename=blank,
template_renderer=template_renderer)
return content
def find_dir(self, dirname, package=False):
egg_info = pluginlib.find_egg_info_dir(os.getcwd())
# @@: Should give error about egg_info when top_level.txt missing
f = open(os.path.join(egg_info, 'top_level.txt'))
packages = [l.strip() for l in f.readlines()
if l.strip() and not l.strip().startswith('#')]
f.close()
if not len(packages):
raise BadCommand("No top level dir found for %s" % dirname)
# @@: This doesn't support deeper servlet directories,
# or packages not kept at the top level.
base = os.path.dirname(egg_info)
possible = []
for pkg in packages:
d = os.path.join(base, pkg, dirname)
if os.path.exists(d):
possible.append((pkg, d))
if not possible:
self.ensure_dir(os.path.join(base, packages[0], dirname),
package=package)
return self.find_dir(dirname)
if len(possible) > 1:
raise BadCommand(
"Multiple %s dirs found (%s)" % (dirname, possible))
return possible[0]
def parse_path_name_args(self, name):
"""
Given the name, assume that the first argument is a path/filename
combination. Return the name and dir of this. If the name ends with
'.py' that will be erased.
Examples:
comments -> comments, ''
admin/comments -> comments, 'admin'
h/ab/fred -> fred, 'h/ab'
"""
if name.endswith('.py'):
# Erase extensions
name = name[:-3]
if '.' in name:
# Turn into directory name:
name = name.replace('.', os.path.sep)
if '/' != os.path.sep:
name = name.replace('/', os.path.sep)
parts = name.split(os.path.sep)
name = parts[-1]
if not parts[:-1]:
dir = ''
elif len(parts[:-1]) == 1:
dir = parts[0]
else:
dir = os.path.join(*parts[:-1])
return name, dir
def ensure_dir(self, dir, svn_add=True, package=False):
"""
Ensure that the directory exists, creating it if necessary.
Respects verbosity and simulation.
Adds directory to subversion if ``.svn/`` directory exists in
parent, and directory was created.
package
If package is True, any directories created will contain a
__init__.py file.
"""
dir = dir.rstrip(os.sep)
if not dir:
# we either reached the parent-most directory, or we got
# a relative directory
# @@: Should we make sure we resolve relative directories
# first? Though presumably the current directory always
# exists.
return
if not os.path.exists(dir):
self.ensure_dir(os.path.dirname(dir), svn_add=svn_add, package=package)
if self.verbose:
print 'Creating %s' % self.shorten(dir)
if not self.simulate:
os.mkdir(dir)
if (svn_add and
os.path.exists(os.path.join(os.path.dirname(dir), '.svn'))):
self.svn_command('add', dir)
if package:
initfile = os.path.join(dir, '__init__.py')
f = open(initfile, 'wb')
f.write("#\n")
f.close()
print 'Creating %s' % self.shorten(initfile)
if (svn_add and
os.path.exists(os.path.join(os.path.dirname(dir), '.svn'))):
self.svn_command('add', initfile)
else:
if self.verbose > 1:
print "Directory already exists: %s" % self.shorten(dir)
def ensure_file(self, filename, content, svn_add=True, package=False):
"""
Ensure a file named ``filename`` exists with the given
content. If ``--interactive`` has been enabled, this will ask
the user what to do if a file exists with different content.
"""
global difflib
self.ensure_dir(os.path.dirname(filename), svn_add=svn_add, package=package)
if not os.path.exists(filename):
if self.verbose:
print 'Creating %s' % filename
if not self.simulate:
f = open(filename, 'wb')
f.write(content)
f.close()
if svn_add and os.path.exists(os.path.join(os.path.dirname(filename), '.svn')):
self.svn_command('add', filename)
return
f = open(filename, 'rb')
old_content = f.read()
f.close()
if content == old_content:
if self.verbose > 1:
print 'File %s matches expected content' % filename
return
if self.interactive:
print 'Warning: file %s does not match expected content' % filename
if difflib is None:
import difflib
diff = difflib.context_diff(
content.splitlines(),
old_content.splitlines(),
'expected ' + filename,
filename)
print '\n'.join(diff)
if self.interactive:
while 1:
s = raw_input(
'Overwrite file with new content? [y/N] ').strip().lower()
if not s:
s = 'n'
if s.startswith('y'):
break
if s.startswith('n'):
return
print 'Unknown response; Y or N please'
else:
return
if self.verbose:
print 'Overwriting %s with new content' % filename
if not self.simulate:
f = open(filename, 'wb')
f.write(content)
f.close()
def shorten(self, fn, *paths):
"""
Return a shorted form of the filename (relative to the current
directory), typically for displaying in messages. If
``*paths`` are present, then use os.path.join to create the
full filename before shortening.
"""
if paths:
fn = os.path.join(fn, *paths)
if fn.startswith(os.getcwd()):
return fn[len(os.getcwd()):].lstrip(os.path.sep)
else:
return fn
_svn_failed = False
def svn_command(self, *args, **kw):
"""
Run an svn command, but don't raise an exception if it fails.
"""
try:
return self.run_command('svn', *args, **kw)
except OSError, e:
if not self._svn_failed:
print 'Unable to run svn command (%s); proceeding anyway' % e
self._svn_failed = True
def run_command(self, cmd, *args, **kw):
"""
Runs the command, respecting verbosity and simulation.
Returns stdout, or None if simulating.
"""
cwd = popdefault(kw, 'cwd', os.getcwd())
capture_stderr = popdefault(kw, 'capture_stderr', False)
expect_returncode = popdefault(kw, 'expect_returncode', False)
assert not kw, ("Arguments not expected: %s" % kw)
if capture_stderr:
stderr_pipe = subprocess.STDOUT
else:
stderr_pipe = subprocess.PIPE
try:
proc = subprocess.Popen([cmd] + list(args),
cwd=cwd,
stderr=stderr_pipe,
stdout=subprocess.PIPE)
except OSError, e:
if e.errno != 2:
# File not found
raise
raise OSError(
"The expected executable %s was not found (%s)"
% (cmd, e))
if self.verbose:
print 'Running %s %s' % (cmd, ' '.join(args))
if self.simulate:
return None
stdout, stderr = proc.communicate()
if proc.returncode and not expect_returncode:
if not self.verbose:
print 'Running %s %s' % (cmd, ' '.join(args))
print 'Error (exit code: %s)' % proc.returncode
if stderr:
print stderr
raise OSError("Error executing command %s" % cmd)
if self.verbose > 2:
if stderr:
print 'Command error output:'
print stderr
if stdout:
print 'Command output:'
print stdout
return stdout
def popdefault(dict, name, default=None):
if name not in dict:
return default
else:
v = dict[name]
del dict[name]
return v
|