blob: 299e9f3b93d1d0f928eaea70bfa106f927437a1d [file] [log] [blame]
Martin Hořeňovskýac369b72017-04-11 17:25:04 +02001#!/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 # Two things we need to change/update
61 # 1) Link and hash of releaseCommon
62 # 2) Link and hash of licence
63 # We could assume licence never changes, but where is the fun in that?
64 first_hash = True
65 for line in lines:
66 # Check what we are updating
67 if 'vcpkg_download_distfile' in line:
68 kind = line.split('(')[-1].strip()
69 print(kind)
70
71 # Deal with URLS
72 if 'URLS' in line and kind == 'HEADER':
73 line = ' URLS "https://github.com/philsquared/Catch/releases/download/v{}/catch.hpp"\n'.format(v.getVersionString())
74 if 'URLS' in line and kind == 'LICENSE':
75 line = ' URLS "https://raw.githubusercontent.com/philsquared/Catch/v{}/LICENSE.txt"\n'.format(v.getVersionString())
76
77 # Deal with hashes
78 if 'SHA512' in line and kind == 'HEADER':
79 line = ' SHA512 {}\n'.format(header_hash)
80 if 'SHA512' in line and kind == 'LICENSE':
81 line = ' SHA512 {}\n'.format(licence_hash)
82 f.write(line)
83
84
85def git_push(path_to_repo):
86 v = Version()
87 ver_string = v.getVersionString()
88
89 # Move to the repo dir
90 old_path = os.getcwd()
91 os.chdir(path_to_repo)
92
93 # Work with git
94 # Make sure we branch off master
95 subprocess.call('git checkout master', shell=True)
96
97 # Update repo to current master, so we don't work off old version of the portsfile
98 subprocess.call('git pull Microsoft master', shell=True)
99
100 # Create a new branch for the update
101 subprocess.call('git checkout -b catch-{}'.format(ver_string), shell=True)
102 # Add changed files (should be only our files)
103 subprocess.call('git add -u .', shell=True)
104 # Create a commit with these changes
105 subprocess.call('git commit -m "Update Catch to {}"'.format(ver_string), shell=True)
106 # Don't push, so author can review
107 print('Changes were commited to the vcpkg fork. Please check, push and open PR.')
108
109header_hash = get_hash(adjusted_path('single_include/catch.hpp'))
110licence_hash = get_hash(adjusted_path('LICENSE.txt'))
111update_control(adjusted_path(default_path))
112update_portfile(adjusted_path(default_path), header_hash, licence_hash)
113
114git_push(adjusted_path('../vcpkg'))