/usr/lib/python2.7/dist-packages/cogent/util/update_version.py is in python-cogent 1.9-9.
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 | #!/usr/bin/env python
"""Support for updating version strings in the PyCogent source tree
All .py, .pyx, and .c files descending from cogent/ will be updated
All .py files descending from tests/ will be updated
All .pyx and .h files descending from include/ will be updated
docs/conf.py will be updated
setup.py will be updated
"""
from optparse import make_option, OptionParser
from sys import argv
from os import path
import os
__author__ = "Daniel McDonald"
__copyright__ = "Copyright 2007-2016, The Cogent Project"
__credits__ = ["Daniel McDonald"]
__license__ = "GPL"
__version__ = "1.9"
__maintainer__ = "Daniel McDonald"
__email__ = "mcdonadt@colorado.edu"
__status__ = "Development"
options = [make_option('--pycogent_dir',dest='pycogent_dir',type='string',
default=''),
make_option('--new_version',dest='version',type='string',
default=''),
make_option('--is_release',dest='is_release',\
action='store_true', default=False),
make_option('--verbose',dest='verbose',action='store_true',
default=False),
make_option('--mock_run',dest='mockrun',action='store_true',
default=False),
make_option('--new_version_short',dest='version_short',type='string',
default=None)]
class VersionUpdater(object):
"""Handles version update of files contained within the PyCogent tree"""
def __init__(self, PyCogentDirectory=None, Version=None, \
IsRelease=False, Verbose=False, MockRun=False, VersionShort=None):
self.PyCogentDirectory = PyCogentDirectory
self.Version = Version
self.VersionShort = VersionShort
self.VersionTuple = tuple(self.Version.split('.'))
self.IsRelease = IsRelease
self.Verbose = Verbose
self.MockRun = MockRun
self.CodesDirectory = path.join(self.PyCogentDirectory, 'cogent')
self.TestsDirectory = path.join(self.PyCogentDirectory, 'tests')
self.DocDirectory = path.join(self.PyCogentDirectory, 'doc')
self.IncludesDirectory = path.join(self.PyCogentDirectory, 'include')
if not os.access(path.join(self.CodesDirectory, '__init__.py'),os.R_OK):
raise IOError, "Could not locate cogent/__init__.py"
if not os.access(path.join(self.TestsDirectory, '__init__.py'),os.R_OK):
raise IOError, "Could not locate tests/__init__.py"
if not os.access(path.join(self.DocDirectory, 'conf.py'), os.R_OK):
raise IOError, "Could not locate doc/conf.py"
def _get_base_files(self):
"""Support method, provides relative locations for files in base dir"""
setup_file = path.join(self.PyCogentDirectory, 'setup.py')
#reqs_file = path.join(self.PyCogentDirectory, 'cogent-requirements.txt')
#return [(setup_file, 'Python'), (reqs_file, 'Properties')]
return [(setup_file, 'Python')]
def _get_test_files(self):
"""Support method, provides relative locations for test files"""
for dirpath, dirnames, filenames in os.walk(self.TestsDirectory):
for f in filenames:
if f.endswith('.py'):
yield (path.join(dirpath, f), 'Python')
def _get_code_files(self):
"""Support method, provides relative locations for code files
Yields file name and file type
"""
for dirpath, dirnames, filenames in os.walk(self.CodesDirectory):
for f in filenames:
rel_name = path.join(dirpath, f)
if f.endswith('.py'):
yield (rel_name, 'Python')
elif f.endswith('.pyx'):
yield (rel_name, 'PyRex')
elif f.endswith('.c'):
yield (rel_name, 'C')
else:
pass
def _get_doc_files(self):
"""Support method, provides relative locations for test files
Only yields conf.py currently
"""
return [(path.join(self.DocDirectory, 'conf.py'), 'Python')]
def _get_include_files(self):
"""Support method, provides relative locations for include files
Yields file name and file type
"""
for dirpath, dirnames, filenames in os.walk(self.IncludesDirectory):
for f in filenames:
rel_name = path.join(dirpath, f)
if f.endswith('.pyx'):
yield (rel_name, 'PyRex')
elif f.endswith('.h'):
yield (rel_name, 'Header')
else:
pass
def _update_python_file(self, lines, filename):
"""Updates the __version__ string of a Python file"""
found_version_line = False
for lineno, line in enumerate(lines):
if line.startswith('__version__'):
found_version_line = True
break
if found_version_line:
if self.Verbose:
print 'Version string found on line %d' % lineno
lines[lineno] = '__version__ = "%s"\n' % self.Version
else:
print "No version string found in %s" % filename
return (lines, found_version_line)
def _update_properties_file(self, lines, filename):
"""Updates version information in specific properties files
Expects the properties file to be in "key=value" lines
"""
found_version_line = False
if filename.endswith('cogent-requirements.txt'):
for lineno, line in enumerate(lines):
if 'packages/source/c/cogent' in line:
found_version_line = True
break
if found_version_line:
if self.Verbose:
print 'Version string found on line %d' % lineno
http_base = lines[lineno].rsplit('/',1)[0]
lines[lineno] = '%s/PyCogent-%s.tgz\n' % (http_base, self.Version)
else:
print "No version string found in %s" % filename
return (lines, found_version_line)
def _update_doc_conf_file(self, lines, filename):
"""Updates doc/conf.py file"""
versionline = None
releaseline = None
for lineno, line in enumerate(lines):
if line.startswith('version'):
versionline = lineno
if line.startswith('release'):
releaseline = lineno
if versionline is not None and releaseline is not None:
break
if versionline is None:
print "No version string found in doc/conf.py"
else:
if self.Verbose:
print 'Version string found on line %d' % versionline
lines[versionline] = 'version = "%s"\n' % self.VersionShort
if releaseline is None:
print "No release string found in doc/conf.py"
else:
if self.Verbose:
print 'Release string found on line %d' % releaseline
lines[releaseline] = 'release = "%s"\n' % self.Version
return (lines, versionline and releaseline)
def _update_pyrex_file(self, lines, filename):
"""Updates __version__ within a pyx file"""
found_version_line = False
for lineno, line in enumerate(lines):
if line.startswith('__version__'):
found_version_line = True
break
if found_version_line:
if self.Verbose:
print 'Version string found on line %d' % lineno
lines[lineno] = '__version__ = "%s"\n' % str(self.VersionTuple)
else:
print "No version string found in %s" % filename
return (lines, found_version_line)
def _update_header_file(self, lines, filename):
"""Updates a C header file"""
found_version_line = False
for lineno, line in enumerate(lines):
if line.startswith('#define PYCOGENT_VERSION'):
found_version_line = True
break
if found_version_line:
if self.Verbose:
print 'Version string found on line %d' % lineno
lines[lineno] = '#define PYCOGENT_VERSION "%s"\n' \
% self.Version
else:
print "No version string found in %s" % filename
return (lines, found_version_line)
def _update_c_file(self, lines, filename):
"""Updates a C file"""
# same as C header...
return self._update_header_file(lines, filename)
def _file_writer(self, lines, filename):
"""Handles writing out to the file system"""
if self.MockRun:
return
if self.Verbose:
print "Writing file %s" % filename
updated_file = open(filename, 'w')
updated_file.write(''.join(lines))
updated_file.close()
def updateBaseFiles(self):
"""Updates version strings in files in base PyCogent directory"""
for filename, filetype in self._get_base_files():
lines = open(filename).readlines()
if self.Verbose:
print 'Reading %s' % filename
if filetype is 'Python':
lines, write_out = self._update_python_file(lines, filename)
elif filetype is 'Properties':
lines, write_out = self._update_properties_file(lines,filename)
else:
raise TypeError, "Unknown base file type %s" % filetype
if write_out:
self._file_writer(lines, filename)
def updateDocFiles(self):
"""Updates version strings in documentation files
So far we only update conf.py
"""
for filename, filetype in self._get_doc_files():
lines = open(filename).readlines()
if self.Verbose:
print 'Reading %s' % filename
if filename.endswith('conf.py'):
lines, write_out = self._update_doc_conf_file(lines, filename)
else:
raise TypeError, "Unknown doc file type: %s" % filetype
if write_out:
self._file_writer(lines, filename)
def updateIncludeFiles(self):
"""Updates version strings in include files"""
for filename, filetype in self._get_include_files():
lines = open(filename).readlines()
found_version_line = False
if self.Verbose:
print 'Reading %s' % filename
if filetype is 'PyRex':
lines, write_out = self._update_pyrex_file(lines, filename)
elif filetype is 'Header':
lines, write_out = self._update_header_file(lines, filename)
else:
raise TypeError, "Unknown include file type %s" % filetype
if write_out:
self._file_writer(lines, filename)
def updateTestFiles(self):
"""Updates version strings in test files"""
for filename, filetype in self._get_test_files():
lines = open(filename).readlines()
found_version_line = False
if self.Verbose:
print 'Reading %s' % filename
if filetype is 'Python':
lines, write_out = self._update_python_file(lines, filename)
else:
raise TypeError, "Unknown test file type %s" % filetype
if write_out:
self._file_writer(lines, filename)
def updateCodeFiles(self):
"""Updates version strings in code files"""
# if this annoying slow, could probably drop to bash or soemthing
# for a search/replace
for filename, filetype in self._get_code_files():
lines = open(filename).readlines()
found_version_line = False
if self.Verbose:
print 'Reading %s' % filename
if filetype is 'Python':
lines, write_out = self._update_python_file(lines, filename)
elif filetype is 'PyRex':
lines, write_out = self._update_pyrex_file(lines, filename)
elif filetype is 'C':
lines, write_out = self._update_c_file(lines, filename)
else:
raise TypeError, "Unknown code file type %s" % filetype
if write_out:
self._file_writer(lines, filename)
def main(arg_list=argv):
parser = OptionParser(option_list=options)
opts, args = parser.parse_args(args=arg_list)
updater = VersionUpdater(PyCogentDirectory=opts.pycogent_dir,
Version=opts.version,
VersionShort=opts.version_short,
IsRelease=opts.is_release,
Verbose=opts.verbose,
MockRun=opts.mockrun)
updater.updateCodeFiles()
updater.updateTestFiles()
updater.updateDocFiles()
updater.updateIncludeFiles()
updater.updateBaseFiles()
if __name__ == '__main__':
main(argv)
|