summaryrefslogtreecommitdiff
path: root/scripts/builder
authorGiulio Cesare Solaroli <giulio.cesare@clipperz.com>2012-02-11 23:34:25 (UTC)
committer Giulio Cesare Solaroli <giulio.cesare@clipperz.com>2012-02-11 23:34:25 (UTC)
commit185949cb778dd13a566781bbfa8ef7ed344cc705 (patch) (side-by-side diff)
tree8fc4ff8cf1efead7610e4141041dd440eb9febbd /scripts/builder
parent17efce23d9f205f56f5ebbe8e4f4ca581ea47bcb (diff)
downloadclipperz-185949cb778dd13a566781bbfa8ef7ed344cc705.zip
clipperz-185949cb778dd13a566781bbfa8ef7ed344cc705.tar.gz
clipperz-185949cb778dd13a566781bbfa8ef7ed344cc705.tar.bz2
Improved build scripts
Signed-off-by: Giulio Cesare Solaroli <giulio.cesare@clipperz.com>
Diffstat (limited to 'scripts/builder') (more/less context) (ignore whitespace changes)
-rw-r--r--scripts/builder/backendBuilder.py101
-rw-r--r--scripts/builder/backends/phpBuilder.py (renamed from scripts/builder/phpBuilder.py)7
-rw-r--r--scripts/builder/backends/pythonBuilder.py27
-rw-r--r--scripts/builder/backends/scriptLanguageBuilder.py20
-rw-r--r--scripts/builder/frontendBuilder.py141
-rwxr-xr-xscripts/builder/main.py85
-rw-r--r--scripts/builder/pythonBuilder.py14
-rw-r--r--scripts/builder/repository.py75
8 files changed, 304 insertions, 166 deletions
diff --git a/scripts/builder/backendBuilder.py b/scripts/builder/backendBuilder.py
index 16dbe2f..5ecdda0 100644
--- a/scripts/builder/backendBuilder.py
+++ b/scripts/builder/backendBuilder.py
@@ -5,6 +5,10 @@ import sys, os, json
import shutil
-import main
import hashlib
-class BackendBuilder:
+import main
+
+#===================================================================
+
+
+class BackendBuilder(object):
@@ -15,2 +19,4 @@ class BackendBuilder:
self.settings = settings
+
+ # --------------------------------------------------------------------------
@@ -18,2 +24,3 @@ class BackendBuilder:
raise NotImplementedError()
+
@@ -21,30 +28,35 @@ class BackendBuilder:
raise NotImplementedError()
+
def compileCode (self):
- pass
+ raise NotImplementedError()
+
- def copyCompiledCodeToTargetDir (self):
- src = self.sourceFolder()
- dst = self.targetFolder()
- main.createFolder(os.path.dirname(dst))
- shutil.copytree(src, dst)
+ def createPackage (self):
+ raise NotImplementedError()
+
+ # --------------------------------------------------------------------------
def sourceFolder (self):
- return main.projectBaseDir() + '/backend/' + self.relativePath() + '/src'
-
+ return os.path.join(main.projectBaseDir() , 'backend', self.relativePath(), 'src')
- def targetFolder (self):
- return self.projectTargetDir + self.relativePath()
-
- def createTargetFolder (self):
- main.createFolder(self.targetFolder())
-
-# def copyFrontendResources (self, frontend):
-# print "copying resources for frontend: " + frontend
-# print "SETTINGS: " + str(self.settings)
-
+ def tempFolder (self):
+ return os.path.join(self.projectTargetDir, '.tmp', self.relativePath())
+
+
+ def frontEndTempFolder (self):
+ return self.tempFolder()
+
- def writeToTargetFolder (self, filename, content):
- file = open(self.targetFolder() + '/' + filename, 'w')
+ def developmentTargetFolder (self):
+ return os.path.join(self.projectTargetDir, 'development', self.relativePath())
+
+ def targetFolder (self):
+ return os.path.join(self.projectTargetDir, self.relativePath())
+
+ # --------------------------------------------------------------------------
+
+ def writeToFolder (self, folder, filename, content):
+ file = open(os.path.join(folder, filename), 'w')
file.write(content.encode('utf-8'))
@@ -53,5 +65,5 @@ class BackendBuilder:
- def configureIndexContent (self, indexContent):
+ def configureIndexContent (self, indexContent, requestPathPrefix = ".."):
result = indexContent
- result = result.replace( '@request.path@', self.settings['request.path'] )
+ result = result.replace( '@request.path@', requestPathPrefix + '/' + self.settings['request.path'] )
result = result.replace( '@should.pay.toll@', self.settings['should.pay.toll'] )
@@ -70,2 +82,5 @@ class BackendBuilder:
+ def shouldCompileCode (self):
+ return ('debug' in self.versions) or ('install' in self.versions)
+
@@ -74,16 +89,30 @@ class BackendBuilder:
- self.compileCode()
- self.copyCompiledCodeToTargetDir()
+ if self.shouldCompileCode():
+ self.compileCode()
- for frontend in self.frontends:
- frontendPath = frontend.module + '/'
- if 'debug' in self.versions:
- frontend.copyResourcesToTargetFolder(self.targetFolder())
- #self.writeToTargetFolder(frontendPath + 'index_debug.html', self.configureIndexContent(frontend.assembleDebugVersion()))
- self.writeToTargetFolder(frontendPath + 'index_debug.html', self.configureIndexContent(frontend.assemble(assemblyMode='DEBUG', versionType='DEBUG')))
+ for frontend in self.frontends:
+ main.createFolder(os.path.join(self.frontEndTempFolder(), frontend.module))
+
+ if 'debug' in self.versions:
+ frontend.copyResourcesToFolder(self.frontEndTempFolder())
+
+ index = self.configureIndexContent(frontend.assemble(assemblyMode='DEBUG', versionType='DEBUG'))
+ self.writeToFolder(self.frontEndTempFolder(), os.path.join(frontend.module, 'index_debug.html'), index)
+
+ if 'install' in self.versions:
+ index = self.configureIndexContent(frontend.assemble())
+ self.writeToFolder(self.frontEndTempFolder(), os.path.join(frontend.module, 'index.html'), index)
+
+ self.logChecksums(index, "[" + self.name() + " - " + frontend.module + "] index.html checksum")
- if 'install' in self.versions:
- index = self.configureIndexContent(frontend.assemble())
- self.writeToTargetFolder(frontendPath + 'index.html', index)
- self.logChecksums(index, "[" + self.name() + " - " + frontend.module + "] index.html checksum")
+ self.createPackage()
+
+ if 'development' in self.versions:
+ for frontend in self.frontends:
+ main.createFolder(os.path.join(self.developmentTargetFolder(), frontend.module))
+
+ index = self.configureIndexContent(frontend.assemble(assemblyMode='DEVELOPMENT', versionType='DEBUG'), self.settings['development.settings']['url'])
+ self.writeToFolder(self.developmentTargetFolder(), os.path.join(frontend.module, 'index_development.html'), index)
+
+#===================================================================
diff --git a/scripts/builder/phpBuilder.py b/scripts/builder/backends/phpBuilder.py
index cb4661d..c928aa0 100644
--- a/scripts/builder/phpBuilder.py
+++ b/scripts/builder/backends/phpBuilder.py
@@ -3,5 +3,5 @@
-from backendBuilder import BackendBuilder
+from scriptLanguageBuilder import ScriptLanguageBuilder
-class PhpBuilder(BackendBuilder):
+class PhpBuilder(ScriptLanguageBuilder):
@@ -9,2 +9,3 @@ class PhpBuilder(BackendBuilder):
return "PHP builder"
+
@@ -12,3 +13 @@ class PhpBuilder(BackendBuilder):
return 'php'
-
-
diff --git a/scripts/builder/backends/pythonBuilder.py b/scripts/builder/backends/pythonBuilder.py
new file mode 100644
index 0000000..1e44209
--- a/dev/null
+++ b/scripts/builder/backends/pythonBuilder.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+# -*- coding: UTF-8 -*-
+
+from scriptLanguageBuilder import ScriptLanguageBuilder
+
+class PythonBuilder(ScriptLanguageBuilder):
+
+ def name(self):
+ return "Python builder"
+
+
+ def relativePath(self):
+ return 'python'
+
+
+ def compileCode (self):
+ src = self.sourceFolder()
+ dst = self.targetFolder()
+
+ shutil.copytree(src, dst)
+
+
+# def copyCompiledCodeToTargetDir (self):
+# src = self.sourceFolder()
+# dst = self.targetFolder()
+#
+# shutil.copytree(src, dst)
diff --git a/scripts/builder/backends/scriptLanguageBuilder.py b/scripts/builder/backends/scriptLanguageBuilder.py
new file mode 100644
index 0000000..7d5b31c
--- a/dev/null
+++ b/scripts/builder/backends/scriptLanguageBuilder.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+# -*- coding: UTF-8 -*-
+
+import shutil
+from backendBuilder import BackendBuilder
+
+class ScriptLanguageBuilder(BackendBuilder):
+
+ def compileCode (self):
+ src = self.sourceFolder()
+ dst = self.tempFolder()
+
+ shutil.copytree(src, dst)
+
+
+ def createPackage (self):
+ src = self.tempFolder()
+ dst = self.targetFolder()
+
+ shutil.copytree(src, dst)
diff --git a/scripts/builder/frontendBuilder.py b/scripts/builder/frontendBuilder.py
index 55054ee..dae837b 100644
--- a/scripts/builder/frontendBuilder.py
+++ b/scripts/builder/frontendBuilder.py
@@ -11,13 +11,7 @@ import urllib
-#from mercurial import ui, hg
-#from mercurial.node import hex
-from dulwich.repo import Repo
-
import main
+class FrontendBuilder(object):
-
-class FrontendBuilder:
-
- def __init__ (self, frontend, settings):
+ def __init__ (self, frontend, settings, repositoryVersion):
if '.' in frontend:
@@ -32,2 +26,4 @@ class FrontendBuilder:
self.projectDir = main.projectBaseDir()
+ # self.repository = repository.repositoryWithPath(self.projectDir)
+ self.repositoryVersion = repositoryVersion
self.processedFiles = {}
@@ -35,36 +31,2 @@ class FrontendBuilder:
- def mercurialRepositoryVersion (self):
- repo = hg.repository(ui.ui(), self.projectDir)
- context = repo['tip']
- result = str(context)
-
- return result
-
-
- def gitRepositoryVersion (self):
- repo = Repo(self.projectDir)
- #if repo.is_dirty():
- # print "WARNING: build run with dirty repository"
- result = repo.refs['HEAD']
-
- return result
-
-
-
- def repositoryVersion (self):
- cacheKey = 'repositoryVersion'
- if not self.processedFiles.has_key(cacheKey):
- #result = self.mercurialRepositoryVersion()
- result = self.gitRepositoryVersion()
- self.processedFiles[cacheKey] = result
- else:
- result = self.processedFiles[cacheKey]
-
- return result
-
-
- #def relativePath (self):
- # return self.module
- #
-
def log (self, message):
@@ -73,4 +35,8 @@ class FrontendBuilder:
- def absolutePathForSourceFile (self, folder, basePath, file):
- return folder + '/frontend/' + self.module + '/' + basePath + '/' + file
+ def absolutePathForSources (self):
+ return os.path.join(self.projectDir, 'frontend', self.module)
+
+
+ def absolutePathForSourceFile (self, basePath, file):
+ return os.path.join(self.absolutePathForSources(), basePath, file)
@@ -78,3 +44,4 @@ class FrontendBuilder:
def absolutePathForTargetFile (self, folder, basePath, file):
- return folder + '/' + self.module + '/' + basePath + '/' + file
+ return os.path.join(folder, self.module, basePath, file)
+
@@ -94,3 +61,3 @@ class FrontendBuilder:
for file in self.filterFiles(self.settings[fileType]):
- src = self.absolutePathForSourceFile(sourceFolder, fileType, file)
+ src = self.absolutePathForSourceFile(fileType, file)
dst = self.absolutePathForTargetFile(destinationFolder, fileType, file)
@@ -100,3 +67,3 @@ class FrontendBuilder:
- def copyResourcesToTargetFolder (self, targetFolder):
+ def copyResourcesToFolder (self, targetFolder):
self.copyResources(self.projectDir, targetFolder, 'css')
@@ -110,3 +77,3 @@ class FrontendBuilder:
try:
- fileHandler = codecs.open(self.absolutePathForSourceFile(self.projectDir, basePath, file), 'r', 'utf-8')
+ fileHandler = codecs.open(self.absolutePathForSourceFile(basePath, file), 'r', 'utf-8')
except:
@@ -183,4 +150,4 @@ class FrontendBuilder:
- def compressJS_jsmin (self, js):
- self.log("compressing JS code")
+ def compressJS_jsmin (self, js, description):
+ self.log("compressing " + description + " code")
original = StringIO.StringIO(js)
@@ -198,3 +165,3 @@ class FrontendBuilder:
- def compressJS_closureCompiler (self, js):
+ def compressJS_closureCompiler (self, js, description):
# Googles Closure compiler
@@ -207,5 +174,5 @@ class FrontendBuilder:
- def compressJS (self, js):
- return self.compressJS_jsmin(js)
- #return self.compressJS_closureCompiler(js)
+ def compressJS (self, js, description):
+ return self.compressJS_jsmin(js, description)
+ #return self.compressJS_closureCompiler(js, description)
@@ -214,3 +181,3 @@ class FrontendBuilder:
- def packBookmarklet (self, bookmakeletCode):
+ def packBookmarklet (self, bookmakeletCode, version):
replacers = [
@@ -229,3 +196,3 @@ class FrontendBuilder:
]
- result = self.compressJS(bookmakeletCode)
+ result = self.compressJS(bookmakeletCode, version + " bookmarklet")
@@ -302,3 +269,3 @@ class FrontendBuilder:
if not self.processedFiles.has_key(cacheKey):
- result = 'bookmarklet="' + self.packBookmarklet(self.loadFilesContent('js', ['Bookmarklet.js'])) + '";bookmarklet_ie="' + self.packBookmarklet(self.loadFilesContent('js', ['Bookmarklet_IE.js'])) + '";'
+ result = 'bookmarklet="' + self.packBookmarklet(self.loadFilesContent('js', ['Bookmarklet.js']), "regular") + '";bookmarklet_ie="' + self.packBookmarklet(self.loadFilesContent('js', ['Bookmarklet_IE.js']), "IE") + '";'
self.processedFiles[cacheKey] = result
@@ -310,3 +277,3 @@ class FrontendBuilder:
- def replaceTemplatePlaceholders (self, assemblyMode, pageTitle, copyright, css, code, version, versionType):
+ def replaceTemplatePlaceholders (self, pageTitle, copyright, css, code, jsLoadMode, version, versionType):
result = self.template()
@@ -319,3 +286,3 @@ class FrontendBuilder:
result = result.replace('@application.version.type@', versionType, 1)
- result = result.replace('@js_' + assemblyMode + '@', code, 1)
+ result = result.replace('@js_' + jsLoadMode + '@', code, 1)
@@ -345,3 +312,3 @@ class FrontendBuilder:
#<link rel="stylesheet" type="text/css" href="./css/reset-min.css" />
- return '\n'.join(map(lambda file: '<link rel="stylesheet" type="text/css" href="./' + basePath + '/' + file + '" />', files))
+ return '\n'.join(map(lambda file: '<link rel="stylesheet" type="text/css" href="' + basePath + '/' + file + '" />', files))
@@ -354,3 +321,3 @@ class FrontendBuilder:
#<script type='text/javascript' src='./js/src/bookmarklet.js'></script>
- return '\n'.join(map(lambda file: '<script type="text/javascript" src="./' + basePath + '/' + file + '"></script>', files))
+ return '\n'.join(map(lambda file: '<script type="text/javascript" src="' + basePath + '/' + file + '"></script>', files))
@@ -361,6 +328,6 @@ class FrontendBuilder:
- def assembleVersion (self, assemblyMode, pageTitle, copyright, css, js, version, versionType):
+ def assembleVersion (self, pageTitle, copyright, css, js, jsLoadMode, version, versionType):
cacheKey = version + "-" + versionType
if not self.processedFiles.has_key(cacheKey):
- result = self.replaceTemplatePlaceholders(assemblyMode, pageTitle, copyright, css, js, version, versionType)
+ result = self.replaceTemplatePlaceholders(pageTitle, copyright, css, js, jsLoadMode, version, versionType)
self.processedFiles[cacheKey] = result
@@ -374,20 +341,44 @@ class FrontendBuilder:
def assemble (self, assemblyMode='INSTALL', versionType='LIVE'):
- pageTitle = "Clipperz - " + self.module
- if versionType != 'LIVE':
- pageTitle += " [" + versionType + " - " + assemblyMode +"]"
-
+
+ if versionType == 'LIVE':
+ pageTitle = "Clipperz - " + self.module
+ else:
+ pageTitle = "Clipperz - " + self.module + " [" + versionType + " - " + assemblyMode +"]"
+
if assemblyMode == 'INSTALL':
- css = self.cssTagForContent(self.compressCSS(self.loadFilesContent('css', self.settings['css'])))
- js = self.scriptTagForContent(self.bookmarklet() + '\n' + self.compressJS(self.loadFilesContent('js', self.settings['js'])))
+ copyright = self.assembleCopyrightHeader()
+ css = self.cssTagForContent(self.compressCSS(self.loadFilesContent('css', self.settings['css'])))
+ js = self.scriptTagForContent(
+ self.bookmarklet() +
+ '\n' +
+ self.compressJS(self.loadFilesContent('js', self.settings['js']), "application")
+ )
+ jsLoadMode = 'EMBEDDED'
+
+ elif assemblyMode == 'DEBUG':
+ copyright = self.assembleCopyrightHeader()
+ css = self.cssTagsForFiles('./css', self.filterFiles(self.settings['css']))
+ js = self.scriptTagForContent(self.bookmarklet()) + \
+ '\n' + \
+ self.scriptTagsForFiles('./js', self.filterFiles(self.settings['js']))
+ jsLoadMode = 'LINKED'
+
+ elif assemblyMode == 'DEVELOPMENT':
+ copyright = ""
+ css = self.cssTagsForFiles('file://' + str(os.path.join(self.absolutePathForSources(), 'css')), self.filterFiles(self.settings['css']))
+ js = self.scriptTagForContent(self.bookmarklet()) + \
+ '\n' + \
+ self.scriptTagsForFiles('file://' + str(os.path.join(self.absolutePathForSources(), 'js')), self.filterFiles(self.settings['js']))
+ jsLoadMode = 'LINKED'
+
else:
- css = self.cssTagsForFiles('css', self.filterFiles(self.settings['css']))
- js = self.scriptTagForContent(self.bookmarklet()) + '\n' + self.scriptTagsForFiles('js', self.filterFiles(self.settings['js']))
-
+ raise NotImplementedError()
+
return self.assembleVersion(
- assemblyMode = assemblyMode,
pageTitle = pageTitle,
- copyright = self.assembleCopyrightHeader(),
+ copyright = copyright,
css = css,
js = js,
- version = self.repositoryVersion(),
+ jsLoadMode = jsLoadMode,
+ version = self.repositoryVersion,
versionType = versionType
diff --git a/scripts/builder/main.py b/scripts/builder/main.py
index 94f738f..6fce65d 100755
--- a/scripts/builder/main.py
+++ b/scripts/builder/main.py
@@ -3,12 +3,13 @@
-import sys, os, json
+import sys
+import os
+import json
import shutil
import pprint
-import frontendBuilder
import codecs
import itertools
-
from collections import deque
-from phpBuilder import PhpBuilder
-from pythonBuilder import PythonBuilder
+
+import frontendBuilder
+import repository
@@ -36,3 +37,3 @@ def createFolder (path):
def loadSettings (component, module):
- print "MODULE: " + module
+ # print "MODULE: " + module
@@ -45,3 +46,4 @@ def loadSettings (component, module):
- settings = codecs.open(projectBaseDir() + '/' + component + '/' + module + '/properties/' + submodule + '.properties.json', 'r', 'utf-8')
+ #settings = codecs.open(projectBaseDir() + os.sep + component + os.sep + module + os.sep + 'properties' + os.sep + submodule + '.properties.json', 'r', 'utf-8')
+ settings = codecs.open(os.path.join(projectBaseDir(), component, module, 'properties', submodule + '.properties.json'), 'r', 'utf-8')
result = json.load(settings)
@@ -73,11 +75,9 @@ def assembleBackend (backend, frontends, versions):
- if backend == 'php':
- backendBuilder = PhpBuilder(projectTargetDir(), frontends, versions, settings)
- elif backend == 'python':
- backendBuilder = PythonBuilder(projectTargetDir(), frontends, versions, settings)
- #elif backend == 'java':
- # buildJavaBackend (frontends, versions, settings)
- else:
- raise Exception('unrecognized backend: ' + backend)
-
+ builderModuleName = backend + 'Builder'
+ builderClassName = backend.capitalize() + 'Builder'
+
+ builderModule = __import__(builderModuleName)
+ builderClass = getattr(builderModule, builderClassName)
+
+ backendBuilder = builderClass(projectTargetDir(), frontends, versions, settings)
backendBuilder.run()
@@ -86,7 +86,10 @@ def assembleBackend (backend, frontends, versions):
-def build (settings):
+def build (settings, repository):
frontends = []
+ if repository.areTherePendingChanges():
+ print "\nWARNING: repository has pending changes\n"
+
for frontend in settings['frontends']:
- frontends.append(frontendBuilder.FrontendBuilder(frontend, loadSettings('frontend', frontend)))
+ frontends.append(frontendBuilder.FrontendBuilder(frontend, loadSettings('frontend', frontend), repository.version()))
@@ -98,3 +101,3 @@ def build (settings):
def clean ():
- print "cleaning up …"
+ # print "cleaning up …"
if os.path.exists(projectTargetDir()):
@@ -109,9 +112,9 @@ def usage (message):
print
- print "build.py clean"
- print "build.py clean install"
- print "build.py install --ALL"
- print "build.py install debug --ALL"
- print "build.py clean install debug --ALL"
- print "build.ph install, debug --backends php java --frontends beta gamma"
- print "build.ph install, debug --backends php java --frontends beta gamma gamma.mobile"
+ # print "build clean"
+ # print "build clean install"
+ print "build install --ALL"
+ print "build install debug --ALL"
+ # print "build clean install debug --ALL"
+ print "build install debug --backends php python --frontends beta gamma"
+ print "build install debug development --backends php python --frontends beta gamma gamma.mobile"
exit(1)
@@ -120,2 +123,10 @@ def usage (message):
+def allFrontends ():
+ return ['beta', 'gamma', 'mobile']
+
+def allBackends ():
+ return ['php', 'python']
+
+#--------------------------------------------------------------------
+
def main ():
@@ -123,10 +134,9 @@ def main ():
parameters = list(itertools.islice(sys.argv, 1, None))
-
- shouldClean = len(filter(lambda x: x == 'clean', parameters)) > 0
- if (shouldClean):
- clean ()
-
- parameters = filter(lambda x: x != 'clean', parameters)
+
+ sys.path.append(os.path.join(scriptDir(), 'backends'))
+ currentRepository = repository.repositoryWithPath(projectBaseDir())
+
+ clean()
versions = list(itertools.takewhile(lambda x: not x.startswith('--'), parameters))
- settings['versions'] = versions; #['debug', 'install']
+ settings['versions'] = versions; #['debug', 'install', 'development']
parameters = deque(itertools.dropwhile(lambda x: not x.startswith('--'), parameters))
@@ -136,4 +146,4 @@ def main ():
if parameter == "--ALL":
- settings['frontends'] = ['beta', 'gamma', 'mobile']
- settings['backends'] = ['php', 'python', 'java']
+ settings['frontends'] = allFrontends()
+ settings['backends'] = allBackends()
else:
@@ -160,4 +170,5 @@ def main ():
- build (settings)
-
+ build(settings, currentRepository)
+ else:
+ usage("Suggestions on how to call the 'build' script:")
diff --git a/scripts/builder/pythonBuilder.py b/scripts/builder/pythonBuilder.py
deleted file mode 100644
index a84598d..0000000
--- a/scripts/builder/pythonBuilder.py
+++ b/dev/null
@@ -1,14 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: UTF-8 -*-
-
-from backendBuilder import BackendBuilder
-
-class PythonBuilder(BackendBuilder):
-
- def name(self):
- return "Python builder"
-
- def relativePath(self):
- return 'python'
-
-
diff --git a/scripts/builder/repository.py b/scripts/builder/repository.py
new file mode 100644
index 0000000..89db9a5
--- a/dev/null
+++ b/scripts/builder/repository.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+# -*- coding: UTF-8 -*-
+
+
+def repositoryWithPath (path):
+ try:
+ from dulwich.repo import Repo
+
+ repo = Repo(path)
+ result = GitRepository(repo, path)
+ except:
+ from mercurial import ui, hg
+
+ repo = hg.repository(ui.ui(), path)
+ result = HgRepository(repo, path)
+
+ return result
+
+
+#===================================================================
+
+
+class Repository(object):
+
+ def __init__ (self, repository, path):
+ self.repository = repository
+ self.path = path
+
+
+ def revision (self):
+ raise NotImplementedError()
+
+
+ def areTherePendingChanges (self):
+ raise NotImplementedError()
+
+
+ def version (self):
+ result = self.revision()
+ if self.areTherePendingChanges():
+ result = '>>> ' + result + ' <<<'
+
+ # print "VERSION: " + result
+ return result
+
+
+#===================================================================
+
+
+class GitRepository(Repository):
+
+ def revision (self):
+ return repository.refs['HEAD']
+
+
+ def areTherePendingChanges (self):
+ return repository.is_dirty()
+
+
+#===================================================================
+
+
+class HgRepository(Repository):
+ # http://mercurial.selenic.com/wiki/MercurialApi
+
+ def revision (self):
+ return 'hg:' + str(self.repository['tip'])
+
+
+ def areTherePendingChanges (self):
+ # TODO: FIXME: repository.status() does not report 'unknown(?)' files. :(
+ return not all(map(lambda fileList: len(fileList) == 0, self.repository.status()))
+
+
+#===================================================================