Skip to content

Commit

Permalink
Fixed issue 730: Subversion 1.4 breaks mkzopeinstance.py
Browse files Browse the repository at this point in the history
Merged revision 70397 from the trunk
  • Loading branch information
Dmitry Vasiliev committed Nov 9, 2006
1 parent d61ac62 commit ff211c9
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 0 deletions.
120 changes: 120 additions & 0 deletions tests/test_zopeversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Zope Version Tests
$Id$
"""
import os
import shutil
import tempfile
import unittest

from zope.interface.verify import verifyObject
from zope.app.applicationcontrol.interfaces import IZopeVersion
from zope.app.applicationcontrol.zopeversion import ZopeVersion


class MockZopeVersion(ZopeVersion):

def setSVNInfoOutput(self, lines):
self.__lines = lines

def _getSVNInfoOutput(self):
return self.__lines

class Test(unittest.TestCase):

def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix="test-zopeversion-")
self.zopeVersion = MockZopeVersion(self.tmpdir)

def tearDown(self):
shutil.rmtree(self.tmpdir)

def prepare(self, version, fields):
if version:
f = open(os.path.join(self.tmpdir, "version.txt"), "w")
try:
f.write(version)
if not version.endswith("\n"):
f.write("\n")
finally:
f.close()
if fields:
os.mkdir(os.path.join(self.tmpdir, ".svn"))
self.zopeVersion.setSVNInfoOutput(fields)

def test_IVerify(self):
verifyObject(IZopeVersion, self.zopeVersion)

def test_ZopeVersion(self):
self.prepare(None, None)
self.assertEqual(self.zopeVersion.getZopeVersion(),
"Development/Unknown")

def test_ZopeVersion_svntrunk(self):
self.prepare(None, [
"URL: svn+ssh://svn.zope.org/repos/main/Zope3/trunk/src/zope",
"Revision: 10000"
])
self.assertEqual(self.zopeVersion.getZopeVersion(),
"Development/Revision: 10000")

def test_ZopeVersion_svnbranch(self):
self.prepare(None, [
"URL: svn+ssh://svn.zope.org/repos/main/Zope3/branches/Zope3-1.0/src/zope",
"Revision: 10000"
])
self.assertEqual(self.zopeVersion.getZopeVersion(),
"Development/Revision: 10000/Branch: Zope3-1.0")

def test_ZopeVersion_svntag(self):
self.prepare(None, [
"URL: svn+ssh://svn.zope.org/repos/main/Zope3/tags/Zope3-1.0/src/zope",
"Revision: 10000"
])
self.assertEqual(self.zopeVersion.getZopeVersion(),
"Development/Revision: 10000/Tag: Zope3-1.0")

def test_ZopeVersion_svn_unknown(self):
self.prepare(None, ["Nope: "])
self.assertEqual(self.zopeVersion.getZopeVersion(),
"Development/Unknown")

def test_ZopeVersion_release(self):
self.prepare("Zope 3 1.0.0", None)
self.assertEqual(self.zopeVersion.getZopeVersion(),
"Zope 3 1.0.0")

def test_ZopeVersion_release_empty(self):
self.prepare(" ", None)
self.assertEqual(self.zopeVersion.getZopeVersion(),
"Development/Unknown")

def test_ZopeVersion_release_svntrunk(self):
# demonstrate that the version.txt data is discarded if
# there's revision-control metadata:
self.prepare("Zope 3 1.0.0", [
"URL: svn+ssh://svn.zope.org/repos/main/Zope3/trunk/src/zope",
"Revision: 10000"
])
self.assertEqual(self.zopeVersion.getZopeVersion(),
"Development/Revision: 10000")


def test_suite():
return unittest.makeSuite(Test)

if __name__ == '__main__':
unittest.main(defaultTest="test_suite")
102 changes: 102 additions & 0 deletions zopeversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Utility to retrieve the Zope version.
$Id$
"""
__docformat__ = 'restructuredtext'

import os
import re
import subprocess

import zope.app
from zope.app.applicationcontrol.interfaces import IZopeVersion
from zope.interface import implements


class ZopeVersion(object):

implements(IZopeVersion)

__tags = re.compile(r'/(tags|branches)/([^/]+)/')

def __init__(self, path=None):
if path is None:
path = os.path.dirname(os.path.abspath(zope.app.__file__))
self.path = path
self.result = None

def getZopeVersion(self):
"""See zope.app.applicationcontrol.interfaces.IZopeVersion"""
if self.result is not None:
return self.result

self.result = "Development/Unknown"

# is this a SVN checkout?
svndir = os.path.join(self.path, ".svn")
if os.path.isdir(svndir):
self.__setSVNVersion(svndir)
else:
# try to get official Zope release information
versionfile = os.path.join(self.path, "version.txt")
if os.path.isfile(versionfile):
f = file(versionfile)
try:
self.result = f.readline().strip() or self.result
finally:
f.close()
return self.result

def _getSVNInfoOutput(self):
try:
proc = subprocess.Popen('svn info "%s"' % self.path,
shell=True, stdout=subprocess.PIPE)
except OSError:
pass
else:
if proc.wait() == 0:
return proc.stdout
return None

def __setSVNVersion(self, svndir):
output = self._getSVNInfoOutput()
if not output:
return

info = {}
for line in output:
parts = line.rstrip().split(": ", 1)
if len(parts) == 2:
key, value = parts
info[key] = value

revision = info.get("Revision", "")
url = info.get("URL", "")

if revision and url:
match = self.__tags.search(url)
if match is None:
tag = ""
else:
type, value = match.groups()
if type == "tags":
tag = "/Tag: %s" % value
elif type == "branches":
tag = "/Branch: %s" % value
self.result = ("Development/Revision: %s%s"
% (revision, tag))

ZopeVersionUtility = ZopeVersion()

0 comments on commit ff211c9

Please sign in to comment.