import helper 
import package
from local import update
import versions

def ChangeSetCommand(repos, cfg, pkgName, outFileName, oldVersionStr, \
	      newVersionStr):
    newVersion = versions.VersionFromString(newVersionStr, cfg.defaultbranch)

    if (oldVersionStr):
	oldVersion = versions.VersionFromString(oldVersionStr, 
					        cfg.defaultbranch)
    else:
	oldVersion = None

    list = [(pkgName, oldVersion, newVersion, (not oldVersion))]

    cs = repos.createChangeSet(list)
    cs.writeToFile(outFileName)

def LocalChangeSetCommand(db, cfg, pkgName, outFileName):
    try:
	pkgList = helper.findPackage(db, cfg.installbranch, pkgName, None)
    except helper.PackageNotFound, e:
	log.error(e)
	return

    list = []
    dupFilter = {}
    i = 0
    for outerPackage in pkgList:
	for pkg in package.walkPackageSet(db, outerPackage):
	    ver = pkg.getVersion()
	    origPkg = db.getPackageVersion(pkg.getName(), ver, pristine = True)
	    ver = ver.fork(versions.LocalBranch(), sameVerRel = 1)
	    list.append((pkg, origPkg, ver))
	    
    result = update.buildLocalChanges(db, list, root = cfg.root)
    if not result: return
    cs = result[0]

    for outerPackage in pkgList:
	cs.addPrimaryPackage(outerPackage.getName(), 
	  outerPackage.getVersion().fork(
		versions.LocalBranch(), sameVerRel = 1))

    hasChanges = False
    for (changed, fsPkg) in result[1]:
	if changed:
	    hasChanges = True
	    break

    if not changed:
	log.error("there have been no local changes")
    else:
	cs.writeToFile(outFileName)

@@ -1,57 +1,130 @@
-import helper %0Aimport package%0Afrom local import updat
+#%0A# Copyright (c) 2004 Specifix, Inc.%0A# All rights reserved%0A#%0A%0Afrom local import update%0Aimport helper%0Aimport log%0Aimport packag
 e%0Aim
@@ -805,37 +805,8 @@
  %5B%5D%0A
-    dupFilter = %7B%7D%0A    i = 0%0A
     
@@ -1367,31 +1367,8 @@
 ))%0A%0A
-    hasChanges = False%0A
     
@@ -1419,31 +1419,8 @@
 ed:%0A
-%09    hasChanges = True%0A
 %09   

clean up cscmd a little, remove unused variables, import log