1#!/usr/bin/env python
2
3import io, os, re, sys, subprocess
4import hashlib
5
6from scriptCommon import catchPath
7from releaseCommon import Version
8
9print(catchPath)
10
11default_path = '../vcpkg/ports/catch/'
12
13def adjusted_path(path):
14    return os.path.join(catchPath, path)
15
16def get_hash(path):
17    BUFF_SIZE = 65536
18    sha512 = hashlib.sha512()
19    # The newlines should be normalized into \n, which is what we want
20    # If reused use 'rb' with a file written with io.open(newline='\n')
21    with open(path, 'r') as f:
22        while True:
23            data = f.read(BUFF_SIZE)
24            if not data:
25                break
26            if sys.version_info[0] < 3:
27                sha512.update(data)
28            else:
29                sha512.update(data.encode('utf-8'))
30    return sha512.hexdigest()
31
32def update_control(path):
33    v = Version()
34    ver_string = v.getVersionString()
35
36    # Update control
37    lines = []
38    control_path = os.path.join(path, 'CONTROL')
39    with open(control_path, 'r') as f:
40        for line in f:
41            lines.append(line)
42    with open(control_path, 'w') as f:
43        for line in lines:
44            if 'Version: ' in line:
45                line = 'Version: {}\n'.format(v.getVersionString())
46            f.write(line)
47
48def update_portfile(path, header_hash, licence_hash):
49    print('Updating portfile')
50    v = Version()
51    ver_string = v.getVersionString()
52
53    # Update portfile
54    lines = []
55    portfile_path = os.path.join(path, 'portfile.cmake')
56    with open(portfile_path, 'r') as f:
57        for line in f:
58            lines.append(line)
59    with open(portfile_path, 'w') as f:
60        # There are three things we need to change/update
61        # 1) CATCH_VERSION cmake variable
62        # 2) Hash of header
63        # 3) Hash of licence
64        # We could assume licence never changes, but where is the fun in that?
65        for line in lines:
66            # Update the version
67            if 'set(CATCH_VERSION' in line:
68                line = 'set(CATCH_VERSION v{})\n'.format(v.getVersionString())
69
70            # Determine which file we are updating
71            if 'vcpkg_download_distfile' in line:
72                kind = line.split('(')[-1].strip()
73
74            # Update the hashes
75            if 'SHA512' in line and kind == 'HEADER':
76                line = '    SHA512 {}\n'.format(header_hash)
77            if 'SHA512' in line and kind == 'LICENSE':
78                line = '    SHA512 {}\n'.format(licence_hash)
79            f.write(line)
80
81
82def git_push(path_to_repo):
83    v = Version()
84    ver_string = v.getVersionString()
85
86    os.chdir(path_to_repo)
87
88    # Work with git
89    # Make sure we branch off master
90    subprocess.call('git checkout master', shell=True)
91
92    # Update repo to current master, so we don't work off old version of the portsfile
93    subprocess.call('git pull Microsoft master', shell=True)
94    subprocess.call('git push', shell=True)
95
96    # Create a new branch for the update
97    subprocess.call('git checkout -b catch-{}'.format(ver_string), shell=True)
98    # Add changed files (should be only our files)
99    subprocess.call('git add -u .', shell=True)
100    # Create a commit with these changes
101    subprocess.call('git commit -m "Update Catch to {}"'.format(ver_string), shell=True)
102    # Don't push, so author can review
103    print('Changes were commited to the vcpkg fork. Please check, push and open PR.')
104
105header_hash = get_hash(adjusted_path('single_include/catch.hpp'))
106licence_hash = get_hash(adjusted_path('LICENSE.txt'))
107update_control(adjusted_path(default_path))
108update_portfile(adjusted_path(default_path), header_hash, licence_hash)
109
110git_push(adjusted_path('../vcpkg'))
111