Как создать файл version.py, который обновляется каждый раз, когда любой файл отправляется через gitPython

Программы на Python
Anonymous
Как создать файл version.py, который обновляется каждый раз, когда любой файл отправляется через git

Сообщение Anonymous »

Я надеюсь, что git создаст файл version.py, который выглядит следующим образом (где версия увеличивается при каждом коммите):

Код: Выделить всё

# This file is created by the pre-push script
class Version:
comment = "git commit comment"
hash = "some git hash"
version = "0.8.9"
Я пробовал это:

Код: Выделить всё

#!/usr/bin/env /usr/bin/python
import os
import subprocess
import re
import sys

commit_msg_file = sys.argv[1]

with open(commit_msg_file, 'r') as file:
commit_msg = file.read().strip()

version_file = os.path.abspath('version.py')
hashed_code = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip().decode('utf-8')

if os.path.exists(version_file):
print(f'Reading previous {version_file}')

with open(version_file, 'r') as f:
content = f.read()
major, minor, patch = map(int, re.search(r'version = "(\d+)\.(\d+)\.(\d+)"', content).groups())

patch += 1
else:
print(f'Creating new {version_file}')
major, minor, patch = 0, 0, 1

print(f'Writing contents of {version_file} with "{commit_msg}"')

with open(version_file, 'w') as f:
f.write(f'''# This file is created by the pre-push script
class Version:
comment = "{commit_msg}"
hash = "{hashed_code}"
version = "{major}.{minor}.{patch}"

if __name__ == "__main__":
print(Version.version)
''')
f.close()

print(f'adding {version_file}')
subprocess.call(['git', 'add', version_file])
# subprocess.call(['git', 'commit', '-m',  comment])
print(f'adding {version_file}')
subprocess.call(['git', 'add', version_file])
# subprocess.call(['git', 'commit', '-m', comment])
Я пробовал это в предварительной отправке и подготовке-фиксации-msg, но безрезультатно... Есть ли лучший способ или способ исправить то, что у меня есть?
спасибо заранее

Подробнее здесь: https://stackoverflow.com/questions/785 ... pushed-via

Вернуться в «Python»