2010-11-12 19:18:30 +03:00
import os
2010-11-12 19:08:09 +03:00
import Utils
2010-12-08 03:25:28 +03:00
import samba_utils
2011-01-25 04:14:50 +03:00
import sys
2010-05-28 09:20:03 +04:00
2010-11-12 19:54:41 +03:00
def bzr_version_summary ( path ) :
try :
2011-01-25 04:14:50 +03:00
import bzrlib
2010-11-12 19:54:41 +03:00
except ImportError :
return ( " BZR-UNKNOWN " , { } )
2010-11-12 19:26:06 +03:00
2011-01-25 04:14:50 +03:00
import bzrlib . ui
bzrlib . ui . ui_factory = bzrlib . ui . make_ui_for_terminal (
sys . stdin , sys . stdout , sys . stderr )
from bzrlib import branch , osutils , workingtree
2010-11-12 19:54:41 +03:00
from bzrlib . plugin import load_plugins
load_plugins ( )
b = branch . Branch . open ( path )
( revno , revid ) = b . last_revision_info ( )
rev = b . repository . get_revision ( revid )
2010-11-12 19:26:06 +03:00
fields = {
2010-11-12 19:54:41 +03:00
" BZR_REVISION_ID " : revid ,
2010-11-12 20:06:43 +03:00
" BZR_REVNO " : revno ,
2010-11-12 19:54:41 +03:00
" COMMIT_DATE " : osutils . format_date_with_offset_in_original_timezone ( rev . timestamp ,
rev . timezone or 0 ) ,
" COMMIT_TIME " : int ( rev . timestamp ) ,
" BZR_BRANCH " : rev . properties . get ( " branch-nick " , " " ) ,
2010-11-12 19:26:06 +03:00
}
2010-11-12 19:54:41 +03:00
# If possible, retrieve the git sha
try :
from bzrlib . plugins . git . object_store import get_object_store
except ImportError :
# No git plugin
ret = " BZR- %d " % revno
else :
store = get_object_store ( b . repository )
2011-07-22 14:44:59 +04:00
store . lock_read ( )
try :
full_rev = store . _lookup_revision_sha1 ( revid )
finally :
store . unlock ( )
2010-11-12 19:54:41 +03:00
fields [ " GIT_COMMIT_ABBREV " ] = full_rev [ : 7 ]
fields [ " GIT_COMMIT_FULLREV " ] = full_rev
ret = " GIT- " + fields [ " GIT_COMMIT_ABBREV " ]
2010-11-12 20:19:00 +03:00
if workingtree . WorkingTree . open ( path ) . has_changes ( ) :
2010-11-12 20:20:02 +03:00
fields [ " COMMIT_IS_CLEAN " ] = 0
2010-11-12 19:26:06 +03:00
ret + = " + "
2010-11-12 20:19:00 +03:00
else :
2010-11-12 20:20:02 +03:00
fields [ " COMMIT_IS_CLEAN " ] = 1
2010-11-12 19:26:06 +03:00
return ( ret , fields )
2010-11-16 04:05:14 +03:00
def git_version_summary ( path , env = None ) :
2010-11-12 19:13:37 +03:00
# Get version from GIT
2011-02-23 09:10:10 +03:00
if not ' GIT ' in env and os . path . exists ( " /usr/bin/git " ) :
# this is useful when doing make dist without configuring
env . GIT = " /usr/bin/git "
2010-11-16 04:05:14 +03:00
if not ' GIT ' in env :
2010-11-12 19:13:37 +03:00
return ( " GIT-UNKNOWN " , { } )
2010-12-03 05:10:27 +03:00
environ = dict ( os . environ )
environ [ " GIT_DIR " ] = ' %s /.git ' % path
environ [ " GIT_WORK_TREE " ] = path
git = Utils . cmd_output ( env . GIT + ' show --pretty=format: " %h% n %c t % n % H % n %c d " --stat HEAD ' , silent = True , env = environ )
2010-11-12 19:13:37 +03:00
lines = git . splitlines ( )
2010-11-16 04:05:14 +03:00
if not lines or len ( lines ) < 4 :
return ( " GIT-UNKNOWN " , { } )
2010-11-12 19:13:37 +03:00
fields = {
" GIT_COMMIT_ABBREV " : lines [ 0 ] ,
" GIT_COMMIT_FULLREV " : lines [ 2 ] ,
2010-11-12 20:06:43 +03:00
" COMMIT_TIME " : int ( lines [ 1 ] ) ,
2010-11-12 19:54:41 +03:00
" COMMIT_DATE " : lines [ 3 ] ,
2010-11-12 19:13:37 +03:00
}
ret = " GIT- " + fields [ " GIT_COMMIT_ABBREV " ]
2010-11-16 04:05:14 +03:00
if env . GIT_LOCAL_CHANGES :
2011-02-23 09:10:10 +03:00
clean = Utils . cmd_output ( ' %s diff HEAD | wc -l ' % env . GIT , silent = True ) . strip ( )
2010-11-16 04:05:14 +03:00
if clean == " 0 " :
fields [ " COMMIT_IS_CLEAN " ] = 1
else :
fields [ " COMMIT_IS_CLEAN " ] = 0
ret + = " + "
2010-11-12 19:13:37 +03:00
return ( ret , fields )
2010-11-12 19:54:41 +03:00
class SambaVersion ( object ) :
2010-11-12 19:08:09 +03:00
2011-06-17 09:21:39 +04:00
def __init__ ( self , version_dict , path , env = None , is_install = True ) :
2010-05-28 09:20:03 +04:00
''' Determine the version number of samba
See VERSION for the format . Entries on that file are
also accepted as dictionary entries here
'''
self . MAJOR = None
self . MINOR = None
self . RELEASE = None
self . REVISION = None
self . TP_RELEASE = None
self . ALPHA_RELEASE = None
self . PRE_RELEASE = None
self . RC_RELEASE = None
2010-11-12 19:08:09 +03:00
self . IS_SNAPSHOT = True
2010-05-28 09:20:03 +04:00
self . RELEASE_NICKNAME = None
self . VENDOR_SUFFIX = None
self . VENDOR_PATCH = None
for a , b in version_dict . iteritems ( ) :
if a . startswith ( " SAMBA_VERSION_ " ) :
setattr ( self , a [ 14 : ] , b )
else :
setattr ( self , a , b )
2010-09-21 00:08:29 +04:00
if self . IS_GIT_SNAPSHOT == " yes " :
2010-11-12 19:08:09 +03:00
self . IS_SNAPSHOT = True
2010-09-21 00:08:29 +04:00
elif self . IS_GIT_SNAPSHOT == " no " :
2010-11-12 19:08:09 +03:00
self . IS_SNAPSHOT = False
else :
raise Exception ( " Unknown value for IS_GIT_SNAPSHOT: %s " % self . IS_GIT_SNAPSHOT )
2010-05-28 09:20:03 +04:00
##
## start with "3.0.22"
##
self . MAJOR = int ( self . MAJOR )
self . MINOR = int ( self . MINOR )
self . RELEASE = int ( self . RELEASE )
SAMBA_VERSION_STRING = ( " %u . %u . %u " % ( self . MAJOR , self . MINOR , self . RELEASE ) )
##
## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
## We do not do pre or rc version on patch/letter releases
##
if self . REVISION is not None :
SAMBA_VERSION_STRING + = self . REVISION
if self . TP_RELEASE is not None :
self . TP_RELEASE = int ( self . TP_RELEASE )
2010-11-12 19:08:09 +03:00
SAMBA_VERSION_STRING + = " tp %u " % self . TP_RELEASE
2010-05-28 09:20:03 +04:00
if self . ALPHA_RELEASE is not None :
self . ALPHA_RELEASE = int ( self . ALPHA_RELEASE )
SAMBA_VERSION_STRING + = ( " alpha %u " % self . ALPHA_RELEASE )
if self . PRE_RELEASE is not None :
self . PRE_RELEASE = int ( self . PRE_RELEASE )
SAMBA_VERSION_STRING + = ( " pre %u " % self . PRE_RELEASE )
if self . RC_RELEASE is not None :
self . RC_RELEASE = int ( self . RC_RELEASE )
SAMBA_VERSION_STRING + = ( " rc %u " % self . RC_RELEASE )
2010-11-12 19:08:09 +03:00
if self . IS_SNAPSHOT :
2011-06-17 09:21:39 +04:00
if not is_install :
suffix = " DEVELOPERBUILD "
self . vcs_fields = { }
elif os . path . exists ( os . path . join ( path , " .git " ) ) :
2010-11-16 04:05:14 +03:00
suffix , self . vcs_fields = git_version_summary ( path , env = env )
2010-11-12 19:54:41 +03:00
elif os . path . exists ( os . path . join ( path , " .bzr " ) ) :
suffix , self . vcs_fields = bzr_version_summary ( path )
2010-11-12 19:18:30 +03:00
else :
suffix = " UNKNOWN "
self . vcs_fields = { }
2010-11-12 19:13:37 +03:00
SAMBA_VERSION_STRING + = " - " + suffix
else :
self . vcs_fields = { }
2010-09-18 20:45:41 +04:00
2010-11-12 19:08:09 +03:00
self . OFFICIAL_STRING = SAMBA_VERSION_STRING
2010-05-28 09:20:03 +04:00
if self . VENDOR_SUFFIX is not None :
SAMBA_VERSION_STRING + = ( " - " + self . VENDOR_SUFFIX )
self . VENDOR_SUFFIX = self . VENDOR_SUFFIX
if self . VENDOR_PATCH is not None :
SAMBA_VERSION_STRING + = ( " - " + self . VENDOR_PATCH )
self . VENDOR_PATCH = self . VENDOR_PATCH
self . STRING = SAMBA_VERSION_STRING
if self . RELEASE_NICKNAME is not None :
2010-12-24 02:14:04 +03:00
self . STRING_WITH_NICKNAME = " %s ( %s ) " % ( self . STRING , self . RELEASE_NICKNAME )
2010-05-28 09:20:03 +04:00
else :
self . STRING_WITH_NICKNAME = self . STRING
2010-11-12 19:08:09 +03:00
2010-05-28 09:20:03 +04:00
def __str__ ( self ) :
string = " /* Autogenerated by waf */ \n "
string + = " #define SAMBA_VERSION_MAJOR %u \n " % self . MAJOR
string + = " #define SAMBA_VERSION_MINOR %u \n " % self . MINOR
string + = " #define SAMBA_VERSION_RELEASE %u \n " % self . RELEASE
if self . REVISION is not None :
string + = " #define SAMBA_VERSION_REVISION %u \n " % self . REVISION
if self . TP_RELEASE is not None :
string + = " #define SAMBA_VERSION_TP_RELEASE %u \n " % self . TP_RELEASE
if self . ALPHA_RELEASE is not None :
string + = " #define SAMBA_VERSION_ALPHA_RELEASE %u \n " % self . ALPHA_RELEASE
if self . PRE_RELEASE is not None :
string + = " #define SAMBA_VERSION_PRE_RELEASE %u \n " % self . PRE_RELEASE
if self . RC_RELEASE is not None :
string + = " #define SAMBA_VERSION_RC_RELEASE %u \n " % self . RC_RELEASE
2010-11-12 20:06:43 +03:00
for name in sorted ( self . vcs_fields . keys ( ) ) :
string + = " #define SAMBA_VERSION_ %s " % name
value = self . vcs_fields [ name ]
if isinstance ( value , basestring ) :
string + = " \" %s \" " % value
elif type ( value ) is int :
string + = " %d " % value
else :
raise Exception ( " Unknown type for %s : %r " % ( name , value ) )
string + = " \n "
2010-05-28 09:20:03 +04:00
string + = " #define SAMBA_VERSION_OFFICIAL_STRING \" " + self . OFFICIAL_STRING + " \" \n "
if self . VENDOR_SUFFIX is not None :
string + = " #define SAMBA_VERSION_VENDOR_SUFFIX " + self . VENDOR_SUFFIX + " \n "
if self . VENDOR_PATCH is not None :
string + = " #define SAMBA_VERSION_VENDOR_PATCH " + self . VENDOR_PATCH + " \n "
if self . RELEASE_NICKNAME is not None :
string + = " #define SAMBA_VERSION_RELEASE_NICKNAME " + self . RELEASE_NICKNAME + " \n "
# We need to put this #ifdef in to the headers so that vendors can override the version with a function
string + = '''
#ifdef SAMBA_VERSION_VENDOR_FUNCTION
# define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
#else /* SAMBA_VERSION_VENDOR_FUNCTION */
# define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
#endif
'''
string + = " /* Version for mkrelease.sh: \n SAMBA_VERSION_STRING= " + self . STRING_WITH_NICKNAME + " \n */ \n "
return string
2011-06-17 09:21:39 +04:00
def samba_version_file ( version_file , path , env = None , is_install = True ) :
2010-11-12 19:54:41 +03:00
''' Parse the version information from a VERSION file '''
f = open ( version_file , ' r ' )
version_dict = { }
for line in f :
line = line . strip ( )
if line == ' ' :
continue
if line . startswith ( " # " ) :
continue
try :
split_line = line . split ( " = " )
if split_line [ 1 ] != " " :
value = split_line [ 1 ] . strip ( ' " ' )
version_dict [ split_line [ 0 ] ] = value
except :
print ( " Failed to parse line %s from %s " % ( line , version_file ) )
raise
2011-06-17 09:21:39 +04:00
return SambaVersion ( version_dict , path , env = env , is_install = is_install )
2010-12-08 03:25:28 +03:00
2011-06-17 09:21:39 +04:00
def load_version ( env = None , is_install = True ) :
2010-12-08 03:25:28 +03:00
''' load samba versions either from ./VERSION or git
return a version object for detailed breakdown '''
if not env :
env = samba_utils . LOAD_ENVIRONMENT ( )
2011-06-17 09:21:39 +04:00
version = samba_version_file ( " ./VERSION " , " . " , env , is_install = is_install )
2010-12-08 03:25:28 +03:00
Utils . g_module . VERSION = version . STRING
return version