2010-04-04 03:57:33 +04:00
# customised version of 'waf dist' for Samba tools
# uses git ls-files to get file lists
2015-10-27 22:46:46 +03:00
import os , sys , tarfile
2018-01-31 12:48:43 +03:00
from waflib import Utils , Scripting , Logs , Options
from waflib . Configure import conf
2019-11-04 07:07:44 +03:00
from samba_utils import get_string
2017-04-13 19:45:50 +03:00
from waflib import Context
2010-04-04 03:57:33 +04:00
2010-04-04 05:00:42 +04:00
dist_dirs = None
2012-06-14 21:07:23 +04:00
dist_files = None
2010-05-31 05:59:50 +04:00
dist_blacklist = " "
2017-04-13 19:45:50 +03:00
dist_archive = None
class Dist ( Context . Context ) :
# TODO remove
cmd = ' dist '
fun = ' dist '
def execute ( self ) :
Context . g_module . dist ( )
class DistCheck ( Scripting . DistCheck ) :
fun = ' distcheck '
cmd = ' distcheck '
def execute ( self ) :
Options . options . distcheck_args = ' '
if Context . g_module . distcheck is Scripting . distcheck :
# default
Context . g_module . distcheck ( self )
else :
Context . g_module . distcheck ( )
Context . g_module . dist ( )
self . check ( )
def get_arch_name ( self ) :
global dist_archive
return dist_archive
def make_distcheck_cmd ( self , tmpdir ) :
waf = os . path . abspath ( sys . argv [ 0 ] )
return [ sys . executable , waf , ' configure ' , ' build ' , ' install ' , ' uninstall ' , ' --destdir= ' + tmpdir ]
2010-04-04 05:00:42 +04:00
2010-04-12 05:09:44 +04:00
def add_symlink ( tar , fname , abspath , basedir ) :
''' handle symlinks to directories that may move during packaging '''
if not os . path . islink ( abspath ) :
return False
tinfo = tar . gettarinfo ( name = abspath , arcname = fname )
tgt = os . readlink ( abspath )
if dist_dirs :
# we need to find the target relative to the main directory
# this is here to cope with symlinks into the buildtools
# directory from within the standalone libraries in Samba. For example,
# a symlink to ../../builtools/scripts/autogen-waf.sh needs
# to be rewritten as a symlink to buildtools/scripts/autogen-waf.sh
# when the tarball for talloc is built
# the filename without the appname-version
rel_fname = ' / ' . join ( fname . split ( ' / ' ) [ 1 : ] )
# join this with the symlink target
tgt_full = os . path . join ( os . path . dirname ( rel_fname ) , tgt )
# join with the base directory
tgt_base = os . path . normpath ( os . path . join ( basedir , tgt_full ) )
# see if this is inside one of our dist_dirs
for dir in dist_dirs . split ( ) :
if dir . find ( ' : ' ) != - 1 :
destdir = dir . split ( ' : ' ) [ 1 ]
dir = dir . split ( ' : ' ) [ 0 ]
else :
destdir = ' . '
if dir == basedir :
# internal links don't get rewritten
continue
if dir == tgt_base [ 0 : len ( dir ) ] and tgt_base [ len ( dir ) ] == ' / ' :
new_tgt = destdir + tgt_base [ len ( dir ) : ]
tinfo . linkname = new_tgt
break
tinfo . uid = 0
tinfo . gid = 0
tinfo . uname = ' root '
tinfo . gname = ' root '
tar . addfile ( tinfo )
return True
def add_tarfile ( tar , fname , abspath , basedir ) :
2010-04-04 03:57:33 +04:00
''' add a file to the tarball '''
2010-04-12 05:09:44 +04:00
if add_symlink ( tar , fname , abspath , basedir ) :
return
2010-04-09 15:00:49 +04:00
try :
tinfo = tar . gettarinfo ( name = abspath , arcname = fname )
except OSError :
Logs . error ( ' Unable to find file %s - missing from git checkout? ' % abspath )
sys . exit ( 1 )
2010-04-04 03:57:33 +04:00
tinfo . uid = 0
tinfo . gid = 0
tinfo . uname = ' root '
tinfo . gname = ' root '
2018-11-24 18:27:21 +03:00
fh = open ( abspath , " rb " )
2010-04-04 03:57:33 +04:00
tar . addfile ( tinfo , fileobj = fh )
fh . close ( )
2010-11-30 01:47:00 +03:00
def vcs_dir_contents ( path ) :
""" Return the versioned files under a path.
: return : List of paths relative to path
"""
repo = path
while repo != " / " :
2021-08-11 14:26:41 +03:00
if os . path . exists ( os . path . join ( repo , " .git " ) ) :
2010-11-30 01:47:00 +03:00
ls_files_cmd = [ ' git ' , ' ls-files ' , ' --full-name ' ,
2019-11-04 07:07:44 +03:00
os . path . relpath ( path , repo ) ]
2010-11-30 01:47:00 +03:00
cwd = None
env = dict ( os . environ )
env [ " GIT_DIR " ] = os . path . join ( repo , " .git " )
break
repo = os . path . dirname ( repo )
if repo == " / " :
raise Exception ( " unsupported or no vcs for %s " % path )
2019-02-06 18:27:41 +03:00
return get_string ( Utils . cmd_output ( ls_files_cmd , cwd = cwd , env = env ) ) . split ( ' \n ' )
2010-11-30 01:47:00 +03:00
2012-09-27 20:30:47 +04:00
def dist ( appname = ' ' , version = ' ' ) :
2012-09-12 19:01:40 +04:00
def add_files_to_tarball ( tar , srcdir , srcsubdir , dstdir , dstsubdir , blacklist , files ) :
2012-09-27 20:30:47 +04:00
if blacklist is None :
2012-09-12 19:01:40 +04:00
blacklist = [ ]
for f in files :
abspath = os . path . join ( srcdir , f )
if srcsubdir != ' . ' :
f = f [ len ( srcsubdir ) + 1 : ]
# Remove files in the blacklist
if f in blacklist :
continue
blacklisted = False
# Remove directories in the blacklist
for d in blacklist :
if f . startswith ( d ) :
blacklisted = True
if blacklisted :
continue
2017-08-06 07:56:17 +03:00
if os . path . isdir ( abspath ) and not os . path . islink ( abspath ) :
2012-09-12 19:01:40 +04:00
continue
if dstsubdir != ' . ' :
f = dstsubdir + ' / ' + f
fname = dstdir + ' / ' + f
add_tarfile ( tar , fname , abspath , srcsubdir )
2014-06-20 13:52:11 +04:00
def list_directory_files ( path ) :
2014-05-25 05:31:44 +04:00
curdir = os . getcwd ( )
os . chdir ( srcdir )
2012-09-12 19:06:23 +04:00
out_files = [ ]
2014-06-20 13:52:11 +04:00
for root , dirs , files in os . walk ( path ) :
2012-09-12 19:06:23 +04:00
for f in files :
out_files . append ( os . path . join ( root , f ) )
2014-05-25 05:31:44 +04:00
os . chdir ( curdir )
2012-09-12 19:06:23 +04:00
return out_files
2010-04-08 16:10:22 +04:00
if not isinstance ( appname , str ) or not appname :
2010-04-05 03:58:23 +04:00
# this copes with a mismatch in the calling arguments for dist()
2018-01-31 12:48:43 +03:00
appname = Context . g_module . APPNAME
version = Context . g_module . VERSION
2010-04-05 03:58:23 +04:00
if not version :
2018-01-31 12:48:43 +03:00
version = Context . g_module . VERSION
2010-04-04 03:57:33 +04:00
2018-06-27 14:33:36 +03:00
srcdir = os . path . normpath (
os . path . join ( os . path . dirname ( Context . g_module . root_path ) ,
Context . g_module . top ) )
2010-04-04 03:57:33 +04:00
2010-04-04 05:00:42 +04:00
if not dist_dirs :
2010-04-09 15:12:02 +04:00
Logs . error ( ' You must use samba_dist.DIST_DIRS() to set which directories to package ' )
2010-04-04 03:57:33 +04:00
sys . exit ( 1 )
dist_base = ' %s - %s ' % ( appname , version )
2010-05-28 14:24:47 +04:00
if Options . options . SIGN_RELEASE :
dist_name = ' %s .tar ' % ( dist_base )
tar = tarfile . open ( dist_name , ' w ' )
else :
dist_name = ' %s .tar.gz ' % ( dist_base )
tar = tarfile . open ( dist_name , ' w:gz ' )
2010-05-28 12:50:25 +04:00
blacklist = dist_blacklist . split ( )
2010-04-04 03:57:33 +04:00
2010-04-04 05:00:42 +04:00
for dir in dist_dirs . split ( ) :
2010-04-04 03:57:33 +04:00
if dir . find ( ' : ' ) != - 1 :
destdir = dir . split ( ' : ' ) [ 1 ]
dir = dir . split ( ' : ' ) [ 0 ]
else :
destdir = ' . '
absdir = os . path . join ( srcdir , dir )
2010-04-04 04:23:43 +04:00
try :
2010-11-30 01:47:00 +03:00
files = vcs_dir_contents ( absdir )
2018-02-14 01:19:49 +03:00
except Exception as e :
2010-11-30 01:47:00 +03:00
Logs . error ( ' unable to get contents of %s : %s ' % ( absdir , e ) )
2010-04-04 04:23:43 +04:00
sys . exit ( 1 )
2012-09-12 19:01:40 +04:00
add_files_to_tarball ( tar , srcdir , dir , dist_base , destdir , blacklist , files )
2010-04-04 03:57:33 +04:00
2012-06-14 21:07:23 +04:00
if dist_files :
for file in dist_files . split ( ) :
if file . find ( ' : ' ) != - 1 :
destfile = file . split ( ' : ' ) [ 1 ]
file = file . split ( ' : ' ) [ 0 ]
else :
destfile = file
absfile = os . path . join ( srcdir , file )
2017-08-08 13:50:25 +03:00
if os . path . isdir ( absfile ) and not os . path . islink ( absfile ) :
2012-09-12 19:06:23 +04:00
destdir = destfile
dir = file
files = list_directory_files ( dir )
add_files_to_tarball ( tar , srcdir , dir , dist_base , destdir , blacklist , files )
else :
fname = dist_base + ' / ' + destfile
add_tarfile ( tar , fname , absfile , destfile )
2012-06-14 21:07:23 +04:00
2010-04-04 03:57:33 +04:00
tar . close ( )
2010-05-28 14:24:47 +04:00
if Options . options . SIGN_RELEASE :
2010-12-12 23:57:37 +03:00
import gzip
2010-05-28 14:24:47 +04:00
try :
os . unlink ( dist_name + ' .asc ' )
except OSError :
pass
cmd = " gpg --detach-sign --armor " + dist_name
os . system ( cmd )
uncompressed_tar = open ( dist_name , ' rb ' )
compressed_tar = gzip . open ( dist_name + ' .gz ' , ' wb ' )
while 1 :
buffer = uncompressed_tar . read ( 1048576 )
if buffer :
compressed_tar . write ( buffer )
else :
break
uncompressed_tar . close ( )
compressed_tar . close ( )
os . unlink ( dist_name )
Logs . info ( ' Created %s .gz %s .asc ' % ( dist_name , dist_name ) )
dist_name = dist_name + ' .gz '
else :
Logs . info ( ' Created %s ' % dist_name )
2017-04-13 19:45:50 +03:00
# TODO use the ctx object instead
global dist_archive
dist_archive = dist_name
2010-04-05 03:58:23 +04:00
return dist_name
2010-04-04 03:57:33 +04:00
@conf
2010-04-04 05:00:42 +04:00
def DIST_DIRS ( dirs ) :
2010-04-04 03:57:33 +04:00
''' set the directories to package, relative to top srcdir '''
2010-04-04 05:00:42 +04:00
global dist_dirs
if not dist_dirs :
dist_dirs = dirs
2010-04-04 04:06:34 +04:00
2012-06-14 21:07:23 +04:00
@conf
2012-09-12 19:02:39 +04:00
def DIST_FILES ( files , extend = False ) :
2012-06-14 21:07:23 +04:00
''' set additional files for packaging, relative to top srcdir '''
global dist_files
if not dist_files :
dist_files = files
2012-09-12 19:02:39 +04:00
elif extend :
dist_files = dist_files + " " + files
2012-06-14 21:07:23 +04:00
2010-05-28 12:50:25 +04:00
@conf
def DIST_BLACKLIST ( blacklist ) :
2010-09-21 01:41:09 +04:00
''' set the files to exclude from packaging, relative to top srcdir '''
2010-05-28 12:50:25 +04:00
global dist_blacklist
if not dist_blacklist :
dist_blacklist = blacklist
2010-04-04 04:06:34 +04:00
Scripting . dist = dist