2019-02-18 02:31:28 +03:00
#!/usr/bin/env python3
# Copyright (C) Catalyst.Net Ltd 2019
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Manage dependencies and bootstrap environments for Samba .
CLI script to render bootstrap . sh / Dockerfile / Vagrantfile .
Author : Joe Guo < joeg @catalyst.net.nz >
"""
import io
import os
2019-03-20 02:26:48 +03:00
import hashlib
2019-02-18 02:31:28 +03:00
import logging
import argparse
from config import DISTS , VAGRANTFILE , OUT
2019-03-20 02:26:48 +03:00
HERE = os . path . abspath ( os . path . dirname ( __file__ ) )
SHA1SUM_FILE_PATH = os . path . join ( HERE , ' sha1sum.txt ' )
2020-09-11 00:36:29 +03:00
README_FILE_PATH = os . path . join ( HERE , ' README.md ' )
2019-03-20 02:26:48 +03:00
2019-02-18 02:31:28 +03:00
logging . basicConfig ( level = ' INFO ' )
log = logging . getLogger ( __file__ )
2019-03-20 02:26:48 +03:00
def get_files ( path ) :
""" Get all files recursively in path as a list """
filepaths = [ ]
for root , dirnames , filenames in os . walk ( path ) :
for filename in filenames :
filepath = os . path . join ( root , filename )
filepaths . append ( filepath )
return filepaths
def get_sha1sum ( debug = False ) :
""" Get sha1sum for dists + .gitlab-ci.yml """
filepaths = get_files ( HERE )
m = hashlib . sha1 ( )
i = 0
for filepath in sorted ( list ( filepaths ) ) :
_filepath = os . path . relpath ( filepath )
i + = 1
if filepath == SHA1SUM_FILE_PATH :
d = " skip "
if debug :
print ( " %s : %s : %s " % ( i , d , _filepath ) )
continue
if filepath == README_FILE_PATH :
d = " skip "
if debug :
print ( " %s : %s : %s " % ( i , d , _filepath ) )
continue
if filepath . endswith ( ' .pyc ' ) :
d = " skip "
if debug :
print ( " %s : %s : %s " % ( i , d , _filepath ) )
continue
with io . open ( filepath , mode = ' rb ' ) as _file :
_bytes = _file . read ( )
m1 = hashlib . sha1 ( )
m1 . update ( _bytes )
d = m1 . hexdigest ( )
if debug :
print ( " %s : %s : %s " % ( i , d , _filepath ) )
m . update ( _bytes )
return m . hexdigest ( )
2019-02-18 02:31:28 +03:00
def render ( dists ) :
""" Render files for all dists """
for dist , config in dists . items ( ) :
home = config [ ' home ' ]
os . makedirs ( home , exist_ok = True )
2019-03-07 09:00:37 +03:00
for key in [ ' bootstrap.sh ' , ' locale.sh ' , ' packages.yml ' , ' Dockerfile ' ] :
2019-02-18 02:31:28 +03:00
path = os . path . join ( home , key )
log . info ( ' %s : render " %s " to %s ' , dist , key , path )
with io . open ( path , mode = ' wt ' , encoding = ' utf8 ' ) as fp :
fp . write ( config [ key ] )
2019-03-07 09:00:37 +03:00
if path . endswith ( ' .sh ' ) :
os . chmod ( path , 0o755 )
2019-02-18 02:31:28 +03:00
2019-04-11 11:34:28 +03:00
key = ' Vagrantfile '
path = os . path . join ( OUT , key )
log . info ( ' %s : render " %s " to %s ' , dist , key , path )
with io . open ( path , mode = ' wt ' , encoding = ' utf8 ' ) as fp :
fp . write ( VAGRANTFILE )
2019-02-18 02:31:28 +03:00
2019-03-20 02:26:48 +03:00
# always calc sha1sum after render
sha1sum = get_sha1sum ( )
log . info ( ' write sha1sum to %s : %s ' , SHA1SUM_FILE_PATH , sha1sum )
with io . open ( SHA1SUM_FILE_PATH , mode = ' wt ' , encoding = ' utf8 ' ) as fp :
fp . write ( sha1sum + " \n " )
2019-02-18 02:31:28 +03:00
def main ( ) :
parser = argparse . ArgumentParser (
formatter_class = argparse . ArgumentDefaultsHelpFormatter ,
description = ( ' Render templates with samba dependencies '
' to bootstrap multiple distributions. ' ) )
parser . add_argument (
' -r ' , ' --render ' , action = ' store_true ' , help = ' Render templates ' )
2019-03-20 02:26:48 +03:00
parser . add_argument (
' -s ' , ' --sha1sum ' , action = ' store_true ' , help = ' Print sha1sum ' )
parser . add_argument (
' -d ' , ' --debug ' , action = ' store_true ' , help = ' Debug sha1sum ' )
2019-02-18 02:31:28 +03:00
args = parser . parse_args ( )
2019-03-20 02:26:48 +03:00
need_help = True
2019-02-18 02:31:28 +03:00
if args . render :
render ( DISTS )
2019-03-20 02:26:48 +03:00
need_help = False
if args . sha1sum :
# we will use the output to check sha1sum in ci
print ( get_sha1sum ( args . debug ) )
need_help = False
if need_help :
2019-02-18 02:31:28 +03:00
parser . print_help ( )
if __name__ == ' __main__ ' :
main ( )