| 1 | # -*- mode: python; coding: utf-8 -*- |
|---|
| 2 | # :Progetto: vcpx -- Frontend capabilities |
|---|
| 3 | # :Creato: dom 04 lug 2004 00:40:54 CEST |
|---|
| 4 | # :Autore: Lele Gaifax <lele@nautilus.homeip.net> |
|---|
| 5 | # :Licenza: GNU General Public License |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | """ |
|---|
| 9 | Implement the frontend functionalities. |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | __docformat__ = 'reStructuredText' |
|---|
| 13 | |
|---|
| 14 | __version__ = '0.9.34' |
|---|
| 15 | |
|---|
| 16 | from logging import getLogger |
|---|
| 17 | from optparse import OptionParser, OptionGroup, Option |
|---|
| 18 | from vcpx import TailorBug, TailorException |
|---|
| 19 | from vcpx.config import Config, ConfigurationError |
|---|
| 20 | from vcpx.project import Project |
|---|
| 21 | from vcpx.source import GetUpstreamChangesetsFailure |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class Tailorizer(Project): |
|---|
| 25 | """ |
|---|
| 26 | A Tailorizer has two main capabilities: its able to bootstrap a |
|---|
| 27 | new Project, or brought it in sync with its current upstream |
|---|
| 28 | revision. |
|---|
| 29 | """ |
|---|
| 30 | |
|---|
| 31 | def _applyable(self, changeset): |
|---|
| 32 | """ |
|---|
| 33 | Print the changeset being applied. |
|---|
| 34 | """ |
|---|
| 35 | |
|---|
| 36 | if self.verbose: |
|---|
| 37 | self.log.info('Changeset "%s"', changeset.revision) |
|---|
| 38 | if changeset.log: |
|---|
| 39 | self.log.info("Log message: %s", changeset.log) |
|---|
| 40 | self.log.debug("Going to apply changeset:\n%s", str(changeset)) |
|---|
| 41 | return True |
|---|
| 42 | |
|---|
| 43 | def _applied(self, changeset): |
|---|
| 44 | """ |
|---|
| 45 | Separate changesets with an empty line. |
|---|
| 46 | """ |
|---|
| 47 | |
|---|
| 48 | if self.verbose: |
|---|
| 49 | self.log.info('-*'*30) |
|---|
| 50 | |
|---|
| 51 | def bootstrap(self): |
|---|
| 52 | """ |
|---|
| 53 | Bootstrap a new tailorized module. |
|---|
| 54 | |
|---|
| 55 | First of all prepare the target system working directory such |
|---|
| 56 | that it can host the upstream source tree. This is backend |
|---|
| 57 | specific. |
|---|
| 58 | |
|---|
| 59 | Then extract a copy of the upstream repository and import its |
|---|
| 60 | content into the target repository. |
|---|
| 61 | """ |
|---|
| 62 | |
|---|
| 63 | self.log.info('Bootstrapping "%s" in "%s"', self.name, self.rootdir) |
|---|
| 64 | |
|---|
| 65 | dwd = self.workingDir() |
|---|
| 66 | try: |
|---|
| 67 | dwd.prepareWorkingDirectory(self.source) |
|---|
| 68 | except: |
|---|
| 69 | self.log.critical('Cannot prepare working directory!', exc_info=True) |
|---|
| 70 | raise |
|---|
| 71 | |
|---|
| 72 | revision = self.config.get(self.name, 'start-revision', 'INITIAL') |
|---|
| 73 | try: |
|---|
| 74 | actual = dwd.checkoutUpstreamRevision(revision) |
|---|
| 75 | except: |
|---|
| 76 | self.log.critical("Checkout of %s failed!", self.name) |
|---|
| 77 | raise |
|---|
| 78 | |
|---|
| 79 | try: |
|---|
| 80 | dwd.importFirstRevision(self.source, actual, 'INITIAL'==revision) |
|---|
| 81 | except: |
|---|
| 82 | self.log.critical('Could not import checked out tree in "%s"!', |
|---|
| 83 | self.rootdir, exc_info=True) |
|---|
| 84 | raise |
|---|
| 85 | |
|---|
| 86 | self.log.info("Bootstrap completed") |
|---|
| 87 | |
|---|
| 88 | def update(self): |
|---|
| 89 | """ |
|---|
| 90 | Update an existing tailorized project. |
|---|
| 91 | """ |
|---|
| 92 | |
|---|
| 93 | self.log.info('Updating "%s" in "%s"', self.name, self.rootdir) |
|---|
| 94 | |
|---|
| 95 | dwd = self.workingDir() |
|---|
| 96 | try: |
|---|
| 97 | pendings = dwd.getPendingChangesets() |
|---|
| 98 | except KeyboardInterrupt: |
|---|
| 99 | self.log.warning('Leaving "%s" unchanged, stopped by user', |
|---|
| 100 | self.name) |
|---|
| 101 | raise |
|---|
| 102 | except: |
|---|
| 103 | self.log.fatal('Unable to get changes for "%s"', self.name) |
|---|
| 104 | raise |
|---|
| 105 | |
|---|
| 106 | if pendings.pending(): |
|---|
| 107 | self.log.info("Applying pending upstream changesets") |
|---|
| 108 | |
|---|
| 109 | try: |
|---|
| 110 | last, conflicts = dwd.applyPendingChangesets( |
|---|
| 111 | applyable=self._applyable, applied=self._applied) |
|---|
| 112 | except KeyboardInterrupt: |
|---|
| 113 | self.log.warning('Leaving "%s" incomplete, stopped by user', |
|---|
| 114 | self.name) |
|---|
| 115 | raise |
|---|
| 116 | except: |
|---|
| 117 | self.log.fatal('Upstream change application failed') |
|---|
| 118 | raise |
|---|
| 119 | |
|---|
| 120 | if last: |
|---|
| 121 | self.log.info('Update completed, now at revision "%s"', |
|---|
| 122 | last.revision) |
|---|
| 123 | else: |
|---|
| 124 | self.log.info("Update completed with no upstream changes") |
|---|
| 125 | |
|---|
| 126 | def __call__(self): |
|---|
| 127 | from shwrap import ExternalCommand |
|---|
| 128 | from target import SynchronizableTargetWorkingDir |
|---|
| 129 | from changes import Changeset |
|---|
| 130 | |
|---|
| 131 | def pconfig(option, raw=False): |
|---|
| 132 | return self.config.get(self.name, option, raw=raw) |
|---|
| 133 | |
|---|
| 134 | ExternalCommand.DEBUG = pconfig('debug') |
|---|
| 135 | |
|---|
| 136 | pname_format = pconfig('patch-name-format', raw=True) |
|---|
| 137 | if pname_format is not None: |
|---|
| 138 | SynchronizableTargetWorkingDir.PATCH_NAME_FORMAT = pname_format.strip() |
|---|
| 139 | SynchronizableTargetWorkingDir.REMOVE_FIRST_LOG_LINE = pconfig('remove-first-log-line') |
|---|
| 140 | Changeset.REFILL_MESSAGE = pconfig('refill-changelogs') |
|---|
| 141 | |
|---|
| 142 | try: |
|---|
| 143 | if not self.exists(): |
|---|
| 144 | self.bootstrap() |
|---|
| 145 | if pconfig('start-revision') == 'HEAD': |
|---|
| 146 | return |
|---|
| 147 | self.update() |
|---|
| 148 | except (UnicodeDecodeError, UnicodeEncodeError), exc: |
|---|
| 149 | raise ConfigurationError('%s: it seems that the encoding ' |
|---|
| 150 | 'used by either the source ("%s") or the ' |
|---|
| 151 | 'target ("%s") repository ' |
|---|
| 152 | 'cannot properly represent at least one ' |
|---|
| 153 | 'of the characters in the upstream ' |
|---|
| 154 | 'changelog. You need to use a wider ' |
|---|
| 155 | 'character set, using "encoding" option, ' |
|---|
| 156 | 'or even "encoding-errors-policy".' |
|---|
| 157 | % (exc, self.source.encoding, |
|---|
| 158 | self.target.encoding)) |
|---|
| 159 | except TailorBug, e: |
|---|
| 160 | self.log.fatal("Unexpected internal error, please report", exc_info=e) |
|---|
| 161 | except TailorException: |
|---|
| 162 | raise |
|---|
| 163 | except Exception, e: |
|---|
| 164 | self.log.fatal("Something unexpected!", exc_info=e) |
|---|
| 165 | |
|---|
| 166 | class RecogOption(Option): |
|---|
| 167 | """ |
|---|
| 168 | Make it possible to recognize an option explicitly given on the |
|---|
| 169 | command line from those simply coming out for their default value. |
|---|
| 170 | """ |
|---|
| 171 | |
|---|
| 172 | def process (self, opt, value, values, parser): |
|---|
| 173 | setattr(values, '__seen_' + self.dest, True) |
|---|
| 174 | return Option.process(self, opt, value, values, parser) |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | GENERAL_OPTIONS = [ |
|---|
| 178 | RecogOption("-D", "--debug", dest="debug", |
|---|
| 179 | action="store_true", default=False, |
|---|
| 180 | help="Print each executed command. This also keeps " |
|---|
| 181 | "temporary files with the upstream logs, that are " |
|---|
| 182 | "otherwise removed after use."), |
|---|
| 183 | RecogOption("-v", "--verbose", dest="verbose", |
|---|
| 184 | action="store_true", default=False, |
|---|
| 185 | help="Be verbose, echoing the changelog of each applied " |
|---|
| 186 | "changeset to stdout."), |
|---|
| 187 | RecogOption("-c", "--configfile", metavar="CONFNAME", |
|---|
| 188 | help="Centralized storage of projects info. With this " |
|---|
| 189 | "option and no other arguments tailor will update " |
|---|
| 190 | "every project found in the config file."), |
|---|
| 191 | RecogOption("--encoding", metavar="CHARSET", default=None, |
|---|
| 192 | help="Force the output encoding to given CHARSET, rather " |
|---|
| 193 | "then using the user's default settings specified " |
|---|
| 194 | "in the environment."), |
|---|
| 195 | ] |
|---|
| 196 | |
|---|
| 197 | UPDATE_OPTIONS = [ |
|---|
| 198 | RecogOption("-F", "--patch-name-format", metavar="FORMAT", |
|---|
| 199 | help="Specify the prototype that will be used " |
|---|
| 200 | "to compute the patch name. The prototype may contain " |
|---|
| 201 | "%(keyword)s such as 'author', 'date', " |
|---|
| 202 | "'revision', 'firstlogline', 'remaininglog'. It " |
|---|
| 203 | "defaults to 'Tailorized \"%(revision)s\"'; " |
|---|
| 204 | "setting it to the empty string means that tailor will " |
|---|
| 205 | "simply use the original changelog."), |
|---|
| 206 | RecogOption("-1", "--remove-first-log-line", action="store_true", |
|---|
| 207 | default=False, |
|---|
| 208 | help="Remove the first line of the upstream changelog. This " |
|---|
| 209 | "is intended to pair with --patch-name-format, " |
|---|
| 210 | "when using its 'firstlogline' variable to build the " |
|---|
| 211 | "name of the patch."), |
|---|
| 212 | RecogOption("-N", "--refill-changelogs", action="store_true", |
|---|
| 213 | default=False, |
|---|
| 214 | help="Refill every changelog, useful when upstream logs " |
|---|
| 215 | "are not uniform."), |
|---|
| 216 | ] |
|---|
| 217 | |
|---|
| 218 | BOOTSTRAP_OPTIONS = [ |
|---|
| 219 | RecogOption("-s", "--source-kind", dest="source_kind", metavar="VC-KIND", |
|---|
| 220 | help="Select the backend for the upstream source " |
|---|
| 221 | "version control VC-KIND. Default is 'cvs'.", |
|---|
| 222 | default="cvs"), |
|---|
| 223 | RecogOption("-t", "--target-kind", dest="target_kind", metavar="VC-KIND", |
|---|
| 224 | help="Select VC-KIND as backend for the shadow repository, " |
|---|
| 225 | "with 'darcs' as default.", |
|---|
| 226 | default="darcs"), |
|---|
| 227 | RecogOption("-R", "--repository", "--source-repository", |
|---|
| 228 | dest="source_repository", metavar="REPOS", |
|---|
| 229 | help="Specify the upstream repository, from where bootstrap " |
|---|
| 230 | "will checkout the module. REPOS syntax depends on " |
|---|
| 231 | "the source version control kind."), |
|---|
| 232 | RecogOption("-m", "--module", "--source-module", dest="source_module", |
|---|
| 233 | metavar="MODULE", |
|---|
| 234 | help="Specify the module to checkout at bootstrap time. " |
|---|
| 235 | "This has different meanings under the various upstream " |
|---|
| 236 | "systems: with CVS it indicates the module, while under " |
|---|
| 237 | "SVN it's the prefix of the tree you want and must begin " |
|---|
| 238 | "with a slash. Since it's used in the description of the " |
|---|
| 239 | "target repository, you may want to give it a value with " |
|---|
| 240 | "darcs too, even though it is otherwise ignored."), |
|---|
| 241 | RecogOption("-r", "--revision", "--start-revision", dest="start_revision", |
|---|
| 242 | metavar="REV", |
|---|
| 243 | help="Specify the revision bootstrap should checkout. REV " |
|---|
| 244 | "must be a valid 'name' for a revision in the upstream " |
|---|
| 245 | "version control kind. For CVS it may be either a branch " |
|---|
| 246 | "name, a timestamp or both separated by a space, and " |
|---|
| 247 | "timestamp may be 'INITIAL' to denote the beginning of " |
|---|
| 248 | "time for the given branch. Under Darcs, INITIAL is a " |
|---|
| 249 | "shortcut for the name of the first patch in the upstream " |
|---|
| 250 | "repository, otherwise it is interpreted as the name of " |
|---|
| 251 | "a tag. Under Subversion, 'INITIAL' is the first patch " |
|---|
| 252 | "that touches given repos/module, otherwise it must be " |
|---|
| 253 | "an integer revision number. " |
|---|
| 254 | "'HEAD' means the latest version in all backends.", |
|---|
| 255 | default="INITIAL"), |
|---|
| 256 | RecogOption("-T", "--target-repository", |
|---|
| 257 | dest="target_repository", metavar="REPOS", default=None, |
|---|
| 258 | help="Specify the target repository, the one that will " |
|---|
| 259 | "receive the patches coming from the source one."), |
|---|
| 260 | RecogOption("-M", "--target-module", dest="target_module", |
|---|
| 261 | metavar="MODULE", |
|---|
| 262 | help="Specify the module on the target repository that will " |
|---|
| 263 | "actually contain the upstream source tree."), |
|---|
| 264 | RecogOption("--subdir", metavar="DIR", |
|---|
| 265 | help="Force the subdirectory where the checkout will happen, " |
|---|
| 266 | "by default it's the tail part of the module name."), |
|---|
| 267 | ] |
|---|
| 268 | |
|---|
| 269 | VC_SPECIFIC_OPTIONS = [ |
|---|
| 270 | RecogOption("--use-propset", action="store_true", default=False, |
|---|
| 271 | dest="use_propset", |
|---|
| 272 | help="Use 'svn propset' to set the real date and author of " |
|---|
| 273 | "each commit, instead of appending these information to " |
|---|
| 274 | "the changelog. This requires some tweaks on the SVN " |
|---|
| 275 | "repository to enable revision propchanges."), |
|---|
| 276 | RecogOption("--ignore-arch-ids", action="store_true", default=False, |
|---|
| 277 | dest="ignore_ids", |
|---|
| 278 | help="Ignore .arch-ids directories when using a tla source."), |
|---|
| 279 | ] |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | class ExistingProjectError(TailorException): |
|---|
| 283 | "Project seems already tailored" |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | class ProjectNotTailored(TailorException): |
|---|
| 287 | "Not a tailored project" |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | def main(): |
|---|
| 291 | """ |
|---|
| 292 | Script entry point. |
|---|
| 293 | |
|---|
| 294 | Parse the command line options and arguments, and for each |
|---|
| 295 | specified working copy directory (the current working directory by |
|---|
| 296 | default) execute the tailorization steps. |
|---|
| 297 | """ |
|---|
| 298 | |
|---|
| 299 | import sys |
|---|
| 300 | from os import getcwd |
|---|
| 301 | |
|---|
| 302 | usage = "usage: \n\ |
|---|
| 303 | 1. %prog [options] [project ...]\n\ |
|---|
| 304 | 2. %prog test [--help] [...]" |
|---|
| 305 | parser = OptionParser(usage=usage, |
|---|
| 306 | version=__version__, |
|---|
| 307 | option_list=GENERAL_OPTIONS) |
|---|
| 308 | |
|---|
| 309 | bsoptions = OptionGroup(parser, "Bootstrap options") |
|---|
| 310 | bsoptions.add_options(BOOTSTRAP_OPTIONS) |
|---|
| 311 | |
|---|
| 312 | upoptions = OptionGroup(parser, "Update options") |
|---|
| 313 | upoptions.add_options(UPDATE_OPTIONS) |
|---|
| 314 | |
|---|
| 315 | vcoptions = OptionGroup(parser, "VC specific options") |
|---|
| 316 | vcoptions.add_options(VC_SPECIFIC_OPTIONS) |
|---|
| 317 | |
|---|
| 318 | parser.add_option_group(bsoptions) |
|---|
| 319 | parser.add_option_group(upoptions) |
|---|
| 320 | parser.add_option_group(vcoptions) |
|---|
| 321 | |
|---|
| 322 | options, args = parser.parse_args() |
|---|
| 323 | |
|---|
| 324 | defaults = {} |
|---|
| 325 | for k,v in options.__dict__.items(): |
|---|
| 326 | if k.startswith('__'): |
|---|
| 327 | continue |
|---|
| 328 | if k <> 'configfile' and hasattr(options, '__seen_' + k): |
|---|
| 329 | defaults[k.replace('_', '-')] = str(v) |
|---|
| 330 | |
|---|
| 331 | if options.configfile or (len(sys.argv)==2 and len(args)==1): |
|---|
| 332 | # Either we have a --configfile, or there are no options |
|---|
| 333 | # and a single argument (to support shebang style scripts) |
|---|
| 334 | |
|---|
| 335 | if not options.configfile: |
|---|
| 336 | options.configfile = sys.argv[1] |
|---|
| 337 | args = None |
|---|
| 338 | |
|---|
| 339 | config = Config(open(options.configfile), defaults) |
|---|
| 340 | |
|---|
| 341 | if not args: |
|---|
| 342 | args = config.projects() |
|---|
| 343 | |
|---|
| 344 | for projname in args: |
|---|
| 345 | tailorizer = Tailorizer(projname, config) |
|---|
| 346 | try: |
|---|
| 347 | tailorizer() |
|---|
| 348 | except GetUpstreamChangesetsFailure: |
|---|
| 349 | # Do not stop on this kind of error, but keep going |
|---|
| 350 | pass |
|---|
| 351 | else: |
|---|
| 352 | for omit in ['source-kind', 'target-kind', |
|---|
| 353 | 'source-module', 'target-module', |
|---|
| 354 | 'source-repository', 'target-repository', |
|---|
| 355 | 'start-revision', 'subdir']: |
|---|
| 356 | if omit in defaults: |
|---|
| 357 | del defaults[omit] |
|---|
| 358 | |
|---|
| 359 | config = Config(None, defaults) |
|---|
| 360 | |
|---|
| 361 | config.add_section('project') |
|---|
| 362 | source = options.source_kind + ':source' |
|---|
| 363 | config.set('project', 'source', source) |
|---|
| 364 | target = options.target_kind + ':target' |
|---|
| 365 | config.set('project', 'target', target) |
|---|
| 366 | config.set('project', 'root-directory', getcwd()) |
|---|
| 367 | config.set('project', 'subdir', options.subdir or '.') |
|---|
| 368 | config.set('project', 'state-file', 'tailor.state') |
|---|
| 369 | config.set('project', 'start-revision', options.start_revision) |
|---|
| 370 | |
|---|
| 371 | config.add_section(source) |
|---|
| 372 | if options.source_repository: |
|---|
| 373 | config.set(source, 'repository', options.source_repository) |
|---|
| 374 | else: |
|---|
| 375 | logger = getLogger('tailor') |
|---|
| 376 | logger.warning("By any chance you forgot either the --source-repository or the --configfile option...") |
|---|
| 377 | |
|---|
| 378 | if options.source_module: |
|---|
| 379 | config.set(source, 'module', options.source_module) |
|---|
| 380 | |
|---|
| 381 | config.add_section(target) |
|---|
| 382 | if options.target_repository: |
|---|
| 383 | config.set(target, 'repository', options.target_repository) |
|---|
| 384 | if options.target_module: |
|---|
| 385 | config.set(target, 'module', options.target_module) |
|---|
| 386 | |
|---|
| 387 | if options.verbose: |
|---|
| 388 | sys.stderr.write("You should put the following configuration " |
|---|
| 389 | "in some file, adjust it as needed\n" |
|---|
| 390 | "and use --configfile option with that " |
|---|
| 391 | "file as argument:\n") |
|---|
| 392 | config.write(sys.stdout) |
|---|
| 393 | |
|---|
| 394 | if options.debug: |
|---|
| 395 | tailorizer = Tailorizer('project', config) |
|---|
| 396 | tailorizer() |
|---|
| 397 | elif not options.verbose: |
|---|
| 398 | sys.stderr.write("Operation not performed, try --verbose\n") |
|---|