sanitize_version.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Tiny Python module to sanitize a Git version into something that can be used in a URL
  2. #
  3. # (this is used in multiple places: conf_common.py and in tools/ci/docs_deploy
  4. #
  5. # Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. import os
  19. def sanitize_version(original_version):
  20. """ Given a version (probably output from 'git describe --always' or similar), return
  21. a URL-safe sanitized version. (this is used as 'release' config variable when building
  22. the docs.)
  23. Will override the original version with the Gitlab CI CI_COMMIT_REF_NAME environment variable if
  24. this is present.
  25. Also follows the RTD-ism that master branch is named 'latest'
  26. """
  27. try:
  28. version = os.environ['CI_COMMIT_REF_NAME']
  29. except KeyError:
  30. version = original_version
  31. if version == 'master':
  32. return 'latest'
  33. version = version.replace('/', '-')
  34. return version