bundle_dashboard_v1.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright: © 2021 Netdata Inc.
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. import os
  6. import shutil
  7. import subprocess
  8. import sys
  9. from pathlib import Path
  10. os.chdir(Path(__file__).parent.absolute())
  11. BASEPATH = Path('v1')
  12. URLTEMPLATE = 'https://github.com/netdata/dashboard/releases/download/{0}/dashboard.tar.gz'
  13. CMAKETEMPLATE = '''
  14. install(FILES {0} DESTINATION ${{WEB_DEST}})
  15. install(FILES {1} DESTINATION ${{WEB_DEST}}/css)
  16. install(FILES {2} DESTINATION ${{WEB_DEST}}/fonts)
  17. install(FILES {3} DESTINATION ${{WEB_DEST}}/images)
  18. install(FILES {4} DESTINATION ${{WEB_DEST}}/lib)
  19. install(FILES {5} DESTINATION ${{WEB_DEST}}/static/css)
  20. install(FILES {6} DESTINATION ${{WEB_DEST}}/static/js)
  21. install(FILES {7} DESTINATION ${{WEB_DEST}}/static/media)
  22. install(FILES web/gui/v1/index.html DESTINATION ${WEB_DEST}/v1)
  23. '''
  24. def copy_dashboard(tag):
  25. '''Fetch and bundle the dashboard code.'''
  26. print('Preparing target directory')
  27. shutil.rmtree(BASEPATH)
  28. BASEPATH.mkdir()
  29. print('::group::Fetching dashboard release tarball')
  30. subprocess.check_call('curl -L -o dashboard.tar.gz ' + URLTEMPLATE.format(tag), shell=True)
  31. print('::endgroup::')
  32. print('::group::Extracting dashboard release tarball')
  33. subprocess.check_call('tar -xvzf dashboard.tar.gz -C ' + str(BASEPATH) + ' --strip-components=1', shell=True)
  34. print('::endgroup::')
  35. print('Copying README.md')
  36. BASEPATH.joinpath('README.md').symlink_to('../.dashboard-notice.md')
  37. print('Removing dashboard release tarball')
  38. BASEPATH.joinpath('..', 'dashboard.tar.gz').unlink()
  39. def genfilelist(path):
  40. '''Generate a list of files for the Makefile.'''
  41. files = [f for f in path.iterdir() if f.is_file() and f.name != 'README.md']
  42. files = [Path(*f.parts[1:]) for f in files]
  43. files.sort()
  44. return '\n'.join([("web/gui/v1/" + str(f)) for f in files])
  45. def write_cmakefile():
  46. '''Write out the cmake file for the dashboard code.'''
  47. print('Generating cmake file')
  48. output = CMAKETEMPLATE.format(
  49. genfilelist(BASEPATH),
  50. genfilelist(BASEPATH.joinpath('css')),
  51. genfilelist(BASEPATH.joinpath('fonts')),
  52. genfilelist(BASEPATH.joinpath('images')),
  53. genfilelist(BASEPATH.joinpath('lib')),
  54. genfilelist(BASEPATH.joinpath('static', 'css')),
  55. genfilelist(BASEPATH.joinpath('static', 'js')),
  56. genfilelist(BASEPATH.joinpath('static', 'media')),
  57. )
  58. BASEPATH.joinpath('dashboard_v1.cmake').write_text(output)
  59. def list_changed_files():
  60. '''Create a list of changed files, and set it in an environment variable.'''
  61. if 'GITHUB_ENV' in os.environ:
  62. print('Generating file list for commit.')
  63. subprocess.check_call('echo "COMMIT_FILES<<EOF" >> $GITHUB_ENV', shell=True)
  64. subprocess.check_call('git status --porcelain=v1 --no-renames --untracked-files=all | rev | cut -d \' \' -f 1 | rev >> $GITHUB_ENV', shell=True)
  65. subprocess.check_call('echo "EOF" >> $GITHUB_ENV', shell=True)
  66. copy_dashboard(sys.argv[1])
  67. write_cmakefile()
  68. list_changed_files()