import errno
import os
import os.path
import re
import subprocess
import logging
import sys
VERSION_REGEX = re.compile(r'^(v\d+\.\d+) +[0-9]+-[0-9]+-[0-9]+$')
def mkdir_p(directory):
"""Make the directory, and all its ancestors as required. Any of the
directories are allowed to already exist."""
if directory == "":
return
try:
os.makedirs(directory)
except OSError as e:
if e.errno == errno.EEXIST and os.path.isdir(directory):
pass
else:
raise
def main():
FORMAT = '%(asctime)s %(message)s'
logging.basicConfig(format="[%(asctime)s][%(levelname)-8s] %(message)s", datefmt="%H:%M:%S")
if len(sys.argv) != 4:
logging.error("usage: {} <CHANGES-path> <tag> <output-file>".format(sys.argv[0]))
sys.exit(1)
changes_path = sys.argv[1]
start_tag = sys.argv[2]
output_file_path = sys.argv[3]
changelog = []
has_found_start = False
with open(changes_path, "r") as file:
for line in file.readlines():
m = VERSION_REGEX.match(line)
if m:
print(m.groups()[0])
print(start_tag)
if has_found_start:
break;
if start_tag == m.groups()[0]:
has_found_start = True
continue
if has_found_start:
changelog.append(line)
if not has_found_start:
logging.error("No tag matching {} found.".format(start_tag))
sys.exit(1)
content = "".join(changelog)
if os.path.isfile(output_file_path):
with open(output_file_path, 'r') as f:
if content == f.read():
sys.exit(0)
mkdir_p(os.path.dirname(output_file_path))
with open(output_file_path, 'w') as f:
f.write(content)
sys.exit(0)
if __name__ == '__main__':
main()