vssutil.py :  » Windows » pyExcelerator » pywin32-214 » win32 » scripts » VersionStamp » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » win32 » scripts » VersionStamp » vssutil.py
import win32con, string, traceback
import win32com.client, win32com.client.gencache
import pythoncom
import time
import os

constants = win32com.client.constants

win32com.client.gencache.EnsureModule('{783CD4E0-9D54-11CF-B8EE-00608CC9A71F}', 0, 5, 0)

error = "vssutil error"

def GetSS():
  ss=win32com.client.Dispatch("SourceSafe")
  # SS seems a bit wierd.  It defaults the arguments as empty strings, but
  # then complains when they are used - so we pass "Missing"
  ss.Open(pythoncom.Missing, pythoncom.Missing, pythoncom.Missing)
  return ss

def test(projectName):
  ss=GetSS()
  project = ss.VSSItem(projectName)

  for item in project.GetVersions(constants.VSSFLAG_RECURSYES):
    print item.VSSItem.Name, item.VersionNumber, item.Action
    

#  item=i.Versions[0].VSSItem
#  for h in i.Versions:
#    print `h.Comment`, h.Action, h.VSSItem.Name
  

def SubstituteInString(inString, evalEnv):
  substChar = "$"
  fields = string.split(inString, substChar)
  newFields = []
  for i in range(len(fields)):
    didSubst = 0
    strVal = fields[i]
    if i%2!=0:
      try:
        strVal = eval(strVal,evalEnv[0], evalEnv[1])
        newFields.append(strVal)
        didSubst = 1
      except:
        traceback.print_exc()
        print "Could not substitute", strVal
    if not didSubst:
      newFields.append(strVal)
  return string.join(map(str, newFields), "")

def SubstituteInFile(inName, outName, evalEnv):
  inFile = open(inName, "r")
  try:
    outFile = open(outName, "w")
    try:
      while 1:
        line = inFile.read()
        if not line: break
        outFile.write(SubstituteInString(line, evalEnv))
    finally:
      outFile.close()
  finally:
    inFile.close()

def VssLog(project, linePrefix = "", noLabels = 5, maxItems=150):
  lines = []
  num = 0
  labelNum = 0
  for i in project.GetVersions(constants.VSSFLAG_RECURSYES):
    num = num + 1
    if num > maxItems : break
    commentDesc = itemDesc = ""
    if i.Action[:5]=="Added":
      continue
    if len(i.Label):
      labelNum = labelNum + 1
      itemDesc = i.Action
    else:
      itemDesc = i.VSSItem.Name
      if str(itemDesc[-4:])==".dsp":
        continue
    if i.Comment:
      commentDesc ="\n%s\t%s" % (linePrefix, i.Comment)
    lines.append("%s%s\t%s%s" % (linePrefix, time.asctime(time.localtime(int(i.Date))), itemDesc, commentDesc))
    if labelNum > noLabels:
      break
  return string.join(lines,"\n")
  
def SubstituteVSSInFile(projectName, inName, outName):
  import win32api
  if win32api.GetFullPathName(inName)==win32api.GetFullPathName(outName):
    raise RuntimeError("The input and output filenames can not be the same")
  sourceSafe=GetSS()
  project = sourceSafe.VSSItem(projectName)
  # Find the last label
  label = None
  for version in project.Versions:
    if version.Label:
      break
  else:
    print "Couldnt find a label in the sourcesafe project!"
    return
  # Setup some local helpers for the conversion strings.
  vss_label = version.Label
  vss_date = time.asctime(time.localtime(int(version.Date)))
  now = time.asctime(time.localtime(time.time()))
  SubstituteInFile(inName, outName, (locals(),globals()))
  
      
def CountCheckouts(item):
  num = 0
  if item.Type==constants.VSSITEM_PROJECT:
    for sub in item.Items:
      num = num + CountCheckouts(sub)
  else:
    if item.IsCheckedOut:
      num = num + 1
  return num

def GetLastBuildNo(project):
  i = GetSS().VSSItem(project)
  # Find the last label
  lab = None
  for version in i.Versions:
    lab = str(version.Label)
    if lab: return lab
  return None

def MakeNewBuildNo(project, buildDesc = None, auto=0, bRebrand = 0):
  if buildDesc is None: buildDesc = "Created by Python"
  ss = GetSS()
  i = ss.VSSItem(project)
  num = CountCheckouts(i)
  if num > 0:
    msg = "This project has %d items checked out\r\n\r\nDo you still want to continue?" % num
    import win32ui
    if win32ui.MessageBox(msg, project, win32con.MB_YESNO) != win32con.IDYES:
      return


  oldBuild = buildNo = GetLastBuildNo(project)
  if buildNo is None:
    buildNo = "1"
    oldBuild = "<None>"
  else:
    try:
      buildNo = string.atoi(buildNo)
      if not bRebrand: buildNo = buildNo + 1
      buildNo = str(buildNo)
    except ValueError:
      raise error("The previous label could not be incremented: %s" % (oldBuild))

  if not auto:
    from pywin.mfc import dialog
    buildNo = dialog.GetSimpleInput("Enter new build number", buildNo, "%s - Prev: %s" % (project, oldBuild))
    if buildNo is None: return
  i.Label(buildNo, "Build %s: %s" % (buildNo,buildDesc))
  if auto:
    print "Branded project %s with label %s" % (project, buildNo)
  return buildNo

if __name__=='__main__':
#  UpdateWiseExeName("PyWiseTest.wse", "PyWiseTest-10.exe")

#  MakeVersion()
#  test(tp)
#  MakeNewBuildNo(tp)
  tp = "\\Python\\Python Win32 Extensions"
  SubstituteVSSInFile(tp, "d:\\src\\pythonex\\win32\\win32.txt", "d:\\temp\\win32.txt")
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.