bundle_dashboard.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright: © 2021 Netdata Inc.
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. '''Bundle the dashboard code into the agent repo.
  6. This is designed to be run as part of a GHA workflow, but will work fine outside of one.'''
  7. import os
  8. import shutil
  9. import subprocess
  10. import sys
  11. from pathlib import Path
  12. os.chdir(Path(__file__).parent.absolute())
  13. BASEPATH = Path('dashboard')
  14. URLTEMPLATE = 'https://github.com/netdata/dashboard/releases/download/{0}/dashboard.tar.gz'
  15. MAKEFILETEMPLATE = '''
  16. # Auto-generated by generate-dashboard-makefile.py
  17. # Copyright: © 2021 Netdata Inc.
  18. # SPDX-License-Identifier: GPL-3.0-or-later
  19. MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
  20. dist_noinst_DATA = \\
  21. README.md
  22. dist_web_DATA = \\
  23. {0} \\
  24. $(NULL)
  25. webcssdir=$(webdir)/css
  26. dist_webcss_DATA = \\
  27. {1} \\
  28. $(NULL)
  29. webfontsdir=$(webdir)/fonts
  30. dist_webfonts_DATA = \\
  31. {2} \\
  32. $(NULL)
  33. webimagesdir=$(webdir)/images
  34. dist_webimages_DATA = \\
  35. {3} \\
  36. $(NULL)
  37. weblibdir=$(webdir)/lib
  38. dist_weblib_DATA = \\
  39. {4} \\
  40. $(NULL)
  41. webstaticcssdir=$(webdir)/static/css
  42. dist_webstaticcss_DATA = \\
  43. {5} \\
  44. $(NULL)
  45. webstaticjsdir=$(webdir)/static/js
  46. dist_webstaticjs_DATA = \\
  47. {6} \\
  48. $(NULL)
  49. webstaticmediadir=$(webdir)/static/media
  50. dist_webstaticmedia_DATA = \\
  51. {7} \\
  52. $(NULL)
  53. '''
  54. def copy_dashboard(tag):
  55. '''Fetch and bundle the dashboard code.'''
  56. print('Preparing target directory')
  57. shutil.rmtree(BASEPATH)
  58. BASEPATH.mkdir()
  59. print('::group::Fetching dashboard release tarball')
  60. subprocess.check_call('curl -L -o dashboard.tar.gz ' + URLTEMPLATE.format(tag), shell=True)
  61. print('::endgroup::')
  62. print('::group::Extracting dashboard release tarball')
  63. subprocess.check_call('tar -xvzf dashboard.tar.gz -C ' + str(BASEPATH) + ' --strip-components=1', shell=True)
  64. print('::endgroup::')
  65. print('Copying README.md')
  66. BASEPATH.joinpath('README.md').symlink_to('../.dashboard-notice.md')
  67. print('Removing dashboard release tarball')
  68. BASEPATH.joinpath('..', 'dashboard.tar.gz').unlink()
  69. def genfilelist(path):
  70. '''Generate a list of files for the Makefile.'''
  71. files = [f for f in path.iterdir() if f.is_file() and f.name != 'README.md']
  72. files = [Path(*f.parts[1:]) for f in files]
  73. files.sort()
  74. return ' \\\n '.join([str(f) for f in files])
  75. def write_makefile():
  76. '''Write out the makefile for the dashboard code.'''
  77. print('Generating Makefile')
  78. MAKEFILEDATA = MAKEFILETEMPLATE.format(
  79. genfilelist(BASEPATH),
  80. genfilelist(BASEPATH.joinpath('css')),
  81. genfilelist(BASEPATH.joinpath('fonts')),
  82. genfilelist(BASEPATH.joinpath('images')),
  83. genfilelist(BASEPATH.joinpath('lib')),
  84. genfilelist(BASEPATH.joinpath('static', 'css')),
  85. genfilelist(BASEPATH.joinpath('static', 'js')),
  86. genfilelist(BASEPATH.joinpath('static', 'media')),
  87. )
  88. BASEPATH.joinpath('Makefile.am').write_text(MAKEFILEDATA)
  89. def list_changed_files():
  90. '''Create a list of changed files, and set it in an environment variable.'''
  91. if 'GITHUB_ENV' in os.environ:
  92. print('Generating file list for commit.')
  93. subprocess.check_call('echo "COMMIT_FILES<<EOF" >> $GITHUB_ENV', shell=True)
  94. subprocess.check_call('git status --porcelain=v1 --no-renames --untracked-files=all | rev | cut -d \' \' -f 1 | rev >> $GITHUB_ENV', shell=True)
  95. subprocess.check_call('echo "EOF" >> $GITHUB_ENV', shell=True)
  96. copy_dashboard(sys.argv[1])
  97. write_makefile()
  98. list_changed_files()