bh@144: #! /usr/bin/python2.4
bh@233: # Copyright (C) 2007, 2008, 2009 by Intevation GmbH
bh@144: # Authors:
bh@144: # Bernhard Herzog <bh@intevation.de>
bh@144: #
bh@144: # This program is free software under the GPL (>=v2)
bh@144: # Read the file COPYING coming with the software for details.
bh@144: 
bh@144: """Script to help update a tree packager installation to the current
bh@144: version.  The script updates the information stored in the filesystem
bh@144: for the individual revisions in the following ways:
bh@144: 
bh@144:  - Rename the build log from build.log to log/build_log.txt
bh@144: 
bh@144:  - Rename the build log from build_log.txt to log/build_log.txt
bh@233: 
bh@233:  - Rename the directory name of the revision from <revision>-1 to <revision>-0
bh@233: 
bh@233:    The second number is now the revision of the packaging rules if
bh@233:    they're managed by SVN and always 0 for manual management of the
bh@233:    rules.
bh@144: """
bh@144: 
bh@144: import os
bh@144: 
bh@144: import treepkgcmd
bh@144: from treepkg.options import create_parser
bh@144: from treepkg.packager import create_package_track, PackagerGroup
bh@144: from treepkg.readconfig import read_config
bh@144: 
bh@144: def rename_file(old_name, new_name, dry_run):
bh@144:     if os.path.exists(old_name):
bh@144:         new_dir = os.path.dirname(new_name)
bh@144:         if not os.path.isdir(new_dir):
bh@144:             print "mkdir %s" % (new_dir,)
bh@144:             if not dry_run:
bh@144:                 os.mkdir(new_dir)
bh@144:         print "mv %s %s" % (old_name, new_name)
bh@144:         if not dry_run:
bh@144:             os.rename(old_name, new_name)
bh@144: 
bh@233: def update_rules_revision_in_directory_names(track, dry_run):
bh@233:     revisions = track.get_revisions()
bh@233:     rules_revisions = set(revision.rules_revision for revision in revisions)
bh@233:     if rules_revisions == set([1]):
bh@233:         for revision in revisions:
bh@233:             rename_file(revision.base_dir,
bh@233:                         revision.base_dir[:-2] + "-0",
bh@233:                         dry_run)
bh@233: 
bh@144: def update_treepkg(config_file, dry_run):
bh@144:     treepkg_opts, packager_opts = read_config(config_file)
bh@144:     for opts in packager_opts:
bh@144:         opts["handle_dependencies"] = False
bh@144:     group = PackagerGroup([create_package_track(**opts)
bh@144:                            for opts in packager_opts],
bh@144:                           **treepkg_opts)
bh@144:     for track in group.get_package_tracks():
bh@144:         for revision in track.get_revisions():
bh@144:             # Originally, the build logs were called build.log and were
bh@144:             # in the base directory of a revision
bh@144:             rename_file(os.path.join(revision.base_dir, "build.log"),
bh@144:                         revision.build_log, dry_run)
bh@144:             # for a while, the build logs were called build_log.txt but
bh@144:             # still were in the base directory
bh@144:             rename_file(os.path.join(revision.base_dir, "build_log.txt"),
bh@144:                         revision.build_log, dry_run)
bh@233:         # Revision directories used to end with "-1".  Now that number
bh@233:         # is the revision of the packaging rules which default to 0 for
bh@233:         # the traditional manual rules management without a version
bh@233:         # control system.  Rename those directories if necessary
bh@233:         update_rules_revision_in_directory_names(track, dry_run)
bh@144: 
bh@144: def parse_commandline():
bh@144:     parser = create_parser()
bh@144:     parser.set_defaults(dry_run=False)
bh@144:     parser.add_option("-n", "--dry-run", action="store_true",
bh@144:                       help="Do not actually change anything")
bh@144:     return parser.parse_args()
bh@144: 
bh@144: 
bh@144: def main():
bh@144:     options, args = parse_commandline()
bh@144:     update_treepkg(options.config_file, options.dry_run)
bh@144: 
bh@144: main()