get_main_build_size.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. import configparser
  3. import os
  4. import ydb
  5. dir = os.path.dirname(__file__)
  6. config = configparser.ConfigParser()
  7. config_file_path = f"{dir}/../config/ydb_qa_db.ini"
  8. config.read(config_file_path)
  9. build_preset = os.environ.get("build_preset")
  10. branch = os.environ.get("branch_to_compare")
  11. DATABASE_ENDPOINT = config["QA_DB"]["DATABASE_ENDPOINT"]
  12. DATABASE_PATH = config["QA_DB"]["DATABASE_PATH"]
  13. def get_build_size(time_of_current_commit):
  14. if "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS" not in os.environ:
  15. print(
  16. "Error: Env variable CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS is missing, skipping"
  17. )
  18. return 0
  19. else:
  20. # Do not set up 'real' variable from gh workflows because it interfere with ydb tests
  21. # So, set up it locally
  22. os.environ["YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"] = os.environ[
  23. "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"
  24. ]
  25. sql = f"""
  26. --!syntax_v1
  27. select git_commit_time,github_sha,size_bytes,size_stripped_bytes,build_preset from binary_size
  28. where
  29. github_workflow like "Postcommit%" and
  30. github_ref_name="{branch}" and
  31. build_preset="{build_preset}" and
  32. git_commit_time <= DateTime::FromSeconds({time_of_current_commit})
  33. order by git_commit_time desc
  34. limit 1;
  35. """
  36. with ydb.Driver(
  37. endpoint=DATABASE_ENDPOINT,
  38. database=DATABASE_PATH,
  39. credentials=ydb.credentials_from_env_variables(),
  40. ) as driver:
  41. driver.wait(timeout=10, fail_fast=True)
  42. session = ydb.retry_operation_sync(
  43. lambda: driver.table_client.session().create()
  44. )
  45. with session.transaction() as transaction:
  46. result = transaction.execute(sql, commit_tx=True)
  47. if result[0].rows:
  48. for row in result[0].rows:
  49. main_data = {}
  50. for field in row:
  51. main_data[field] = (
  52. row[field]
  53. if type(row[field]) != bytes
  54. else row[field].decode("utf-8")
  55. )
  56. else:
  57. print(
  58. f"Error: Cant get binary size in db with params: github_workflow like 'Postcommit%', github_ref_name='{branch}', build_preset='{build_preset}, git_commit_time <= DateTime::FromSeconds({time_of_current_commit})'"
  59. )
  60. return 0
  61. return {
  62. "github_sha": main_data["github_sha"],
  63. "git_commit_time": str(main_data["git_commit_time"]),
  64. "size_bytes": str(main_data["size_bytes"]),
  65. "size_stripped_bytes": str(main_data["size_stripped_bytes"]),
  66. }
  67. if __name__ == "__main__":
  68. get_build_size()