#
# Copyright (c) 2004 Specifix, Inc.
# All rights reserved
#
import changeset
import os
import sys
import util
import versions

def doUpdate(repos, db, cfg, pkg, versionStr = None):
    cs = None
    if not os.path.exists(cfg.root):
        util.mkdirChain(cfg.root)
    
    if os.path.exists(pkg):
        # there is a file, try to read it as a changeset file
	if versionStr:
	    sys.stderr.write("Verison should not be specified when a SRS "
			     "change set is being installed.\n")
	    return 1

        try:
            cs = changeset.ChangeSetFromFile(pkg)
        except KeyError:
            # invalid changeset file
            pass
        else:
            if cs.isAbstract():
                newcs = db.rootChangeSet(cs, cfg.defaultbranch)
                if newcs:
                    cs = newcs

    if not cs:
        # so far no changeset (either the path didn't exist or we could not
        # read it
	if pkg and pkg[0] != ":":
	    pkg = cfg.packagenamespace + ":" + pkg

	if versionStr and versionStr[0] != "/":
	    versionStr = cfg.defaultbranch.asString() + "/" + versionStr

	if versionStr:
	    newVersion = versions.VersionFromString(versionStr)
	else:
	    newVersion = None

	list = []
	bail = 0
	for pkgName in repos.getPackageList(pkg):
	    if not newVersion:
		newVersion = repos.pkgLatestVersion(pkgName, cfg.defaultbranch)

	    if not repos.hasPackageVersion(pkgName, newVersion):
		sys.stderr.write("package %s does not contain version %s\n" %
				     (pkgName, newVersion.asString()))
		bail = 1
	    else:
		if db.hasPackage(pkgName):
		    currentVersion = db.pkgLatestVersion(pkgName, 
							 newVersion.branch())
		else:
		    currentVersion = None

		list.append((pkgName, currentVersion, newVersion))
	if bail:
	    return

        if not list:
            sys.stderr.write("repository does not contain a package called %s\n" % pkg)
            return

	cs = repos.createChangeSet(list)

    if cs.isAbstract():
	db.commitChangeSet(cfg.sourcepath, cs, eraseOld = 0)
    else:
	inverse = cs.invert(db)
	db.addRollback(inverse)
	db.commitChangeSet(cfg.sourcepath, cs, eraseOld = 1)

@@ -809,16 +809,95 @@
  newcs%0A%0A
+%09    list = %5B%5D%0A%09    list = map(lambda x: list.append(x), cs.getPackageList())%0A%0A
     if n
@@ -2011,16 +2011,257 @@
 (list)%0A%0A
+%09# permute the list into a list of just package names%0A%09list = map(lambda x: x%5B0%5D, list)%0A%0A    # create a change set between what is in the database and what is%0A    # on the disk%0A    localChanges = changeset.CreateAgainstLocal(cfg, db, list)%0A%0A
     if c

create a changeset against local before changing local; we'll use this later