vss.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # vss.py -- Source Control using Microsoft VSS.
  2. # Provides routines for checking files out of VSS.
  3. #
  4. # Uses an INI file very similar to how VB integrates with VSS - even
  5. # as far as using the same name.
  6. # The file must be named "Mssccprj.scc", and be in the format of
  7. # an INI file. This file may be in a parent directory, in which
  8. # case the project name will be built from what is specified in the
  9. # ini file, plus the path from the INI file to the file itself.
  10. #
  11. # The INI file should have a [Python] section, and a
  12. # Project=Project Name
  13. # and optionally
  14. # Database=??
  15. import win32ui, win32api, win32con, os, string, sys
  16. import traceback
  17. g_iniName = "Mssccprj.scc" # Use the same INI name as VB!
  18. g_sourceSafe = None
  19. def FindVssProjectInfo(fullfname):
  20. """Looks up the file system for an INI file describing the project.
  21. Looking up the tree is for ni style packages.
  22. Returns (projectName, pathToFileName) where pathToFileName contains
  23. the path from the ini file to the actual file.
  24. """
  25. path, fnameonly = os.path.split(fullfname)
  26. origPath = path
  27. project = ""
  28. retPaths = [fnameonly]
  29. while not project:
  30. iniName = os.path.join(path, g_iniName)
  31. database = win32api.GetProfileVal("Python","Database", "", iniName)
  32. project = win32api.GetProfileVal("Python","Project", "", iniName)
  33. if project:
  34. break;
  35. # No valid INI file in this directory - look up a level.
  36. path, addpath = os.path.split(path)
  37. if not addpath: # Root?
  38. break
  39. retPaths.insert(0, addpath)
  40. if not project:
  41. win32ui.MessageBox("%s\r\n\r\nThis directory is not configured for Python/VSS" % origPath)
  42. return
  43. return project, "/".join(retPaths), database
  44. def CheckoutFile(fileName):
  45. global g_sourceSafe
  46. import pythoncom
  47. ok = 0
  48. # Assumes the fileName has a complete path,
  49. # and that the INI file can be found in that path
  50. # (or a parent path if a ni style package)
  51. try:
  52. import win32com.client, win32com.client.gencache
  53. mod = win32com.client.gencache.EnsureModule('{783CD4E0-9D54-11CF-B8EE-00608CC9A71F}', 0, 5, 0)
  54. if mod is None:
  55. win32ui.MessageBox("VSS does not appear to be installed. The TypeInfo can not be created")
  56. return ok
  57. rc = FindVssProjectInfo(fileName)
  58. if rc is None:
  59. return
  60. project, vssFname, database = rc
  61. if g_sourceSafe is None:
  62. g_sourceSafe=win32com.client.Dispatch("SourceSafe")
  63. # SS seems a bit wierd. It defaults the arguments as empty strings, but
  64. # then complains when they are used - so we pass "Missing"
  65. if not database:
  66. database = pythoncom.Missing
  67. g_sourceSafe.Open(database, pythoncom.Missing, pythoncom.Missing)
  68. item = g_sourceSafe.VSSItem("$/%s/%s" % (project, vssFname))
  69. item.Checkout(None, fileName)
  70. ok = 1
  71. except pythoncom.com_error, exc:
  72. win32ui.MessageBox(exc.strerror, "Error checking out file")
  73. except:
  74. typ, val, tb = sys.exc_info()
  75. traceback.print_exc()
  76. win32ui.MessageBox("%s - %s" % (str(typ), str(val)),"Error checking out file")
  77. tb = None # Cleanup a cycle
  78. return ok