Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #! /usr/bin/env python | |
| 2 | |
| 3 """ | |
| 4 Script for updating AFL. Also updates AFL version in README.chromium. | |
| 5 | |
| 6 """ | |
| 7 | |
| 8 import argparse | |
| 9 import cStringIO | |
| 10 import datetime | |
| 11 import os | |
| 12 import re | |
| 13 import sys | |
| 14 import tarfile | |
| 15 import urllib2 | |
| 16 | |
| 17 | |
| 18 VERSION_REGEX = r'(?P<version>([0-9]*[.])?[0-9]+b)' | |
| 19 PATH_REGEX = r'(afl-)' + VERSION_REGEX | |
| 20 | |
| 21 | |
| 22 class ChromiumReadme(object): | |
| 23 """Class that handles reading from and updating the README.chromium""" | |
| 24 | |
| 25 README_FILE_PATH = 'third_party/afl/README.chromium' | |
| 26 README_VERSION_REGEX = r'Version: ' + VERSION_REGEX | |
| 27 | |
| 28 def __init__(self): | |
| 29 """ | |
| 30 Inits the ChromiumReadme. | |
| 31 """ | |
| 32 with open(self.README_FILE_PATH) as readme_file_handle: | |
| 33 self.readme_contents = readme_file_handle.read() | |
| 34 | |
| 35 def get_current_version(self): | |
| 36 """ | |
| 37 Get the current version of AFL according to the README.chromium | |
| 38 """ | |
| 39 match = re.search(self.README_VERSION_REGEX, self.readme_contents) | |
| 40 if not match: | |
| 41 raise Exception('Could not determine current AFL version') | |
| 42 return match.groupdict()['version'] | |
| 43 | |
| 44 def update(self, new_version): | |
| 45 """ | |
| 46 Update the readme to reflect the new version that has been downloaded. | |
| 47 """ | |
| 48 new_readme = self.readme_contents | |
| 49 subsitutions = [(VERSION_REGEX, new_version), # Update the version. | |
| 50 (r'Date: .*', | |
| 51 'Date: ' + datetime.date.today().strftime("%B %d, %Y")), | |
| 52 # Update the Local Modifications. | |
| 53 (PATH_REGEX + r'/', 'afl-' + new_version + '/')] | |
| 54 | |
| 55 for regex, replacement in subsitutions: | |
| 56 new_readme = re.subn(regex, replacement, new_readme, 1)[0] | |
| 57 | |
| 58 self.readme_contents = new_readme | |
| 59 with open(self.README_FILE_PATH, 'w+') as readme_file_handle: | |
| 60 readme_file_handle.write(self.readme_contents) | |
| 61 | |
| 62 | |
| 63 class AflTarball(object): | |
| 64 """ | |
| 65 Class that handles the afl-latest.tgz tarball. | |
| 66 """ | |
| 67 # Regexes that match files that we don't want to extract. | |
| 68 # Note that you should add these removals to "Local Modifications" in | |
| 69 # the README.chromium. | |
| 70 UNWANTED_FILE_REGEX = '|'.join([ | |
| 71 r'(.*\.elf)', # presubmit complains these aren't marked executable. | |
| 72 r'(others/elf)', # We don't need this if we have no elfs. | |
| 73 r'(.*afl-llvm-pass\.so\.cc)', # checkdeps complains about #includes. | |
| 74 # checkdeps complains about #includes. | |
| 75 r'(.*argv.*)' | |
| 76 | |
| 77 ]) | |
| 78 | |
| 79 AFL_SRC_DIR = 'third_party/afl/src' | |
| 80 | |
| 81 def __init__(self, version): | |
| 82 """ | |
| 83 Init this AFL tarball. | |
| 84 """ | |
| 85 release_name = 'afl-{0}'.format(version) | |
| 86 filename = '{0}.tgz'.format(release_name) | |
| 87 self.url = "http://lcamtuf.coredump.cx/afl/releases/{0}".format(filename) | |
|
Oliver Chang
2016/08/10 22:56:29
no https?
Jonathan Metzman
2016/08/10 22:59:43
Nope.
Lcamtuf doesn't have it on his site.
Shoul
Oliver Chang
2016/08/10 23:22:14
comment sgtm if there's no other option.
| |
| 88 self.tarball = None | |
| 89 self.real_version = version if version != 'latest' else None | |
| 90 | |
| 91 def download(self): | |
| 92 """Download the tarball version from | |
| 93 http://lcamtuf.coredump.cx/afl/releases/ | |
| 94 """ | |
| 95 tarball_contents = urllib2.urlopen(self.url).read() | |
| 96 tarball_file = cStringIO.StringIO(tarball_contents) | |
| 97 self.tarball = tarfile.open(fileobj=tarball_file, mode="r:gz") | |
| 98 if self.real_version is None: | |
| 99 regex_match = re.search(VERSION_REGEX, self.tarball.members[0].path) | |
| 100 self.real_version = regex_match.groupdict()['version'] | |
| 101 | |
| 102 def extract(self): | |
| 103 """ | |
| 104 Extract the files and folders from the tarball we have downloaded while | |
| 105 skipping unwanted ones. | |
| 106 """ | |
| 107 | |
| 108 for member in self.tarball.getmembers(): | |
| 109 member.path = re.sub(PATH_REGEX, self.AFL_SRC_DIR, member.path) | |
| 110 if re.match(self.UNWANTED_FILE_REGEX, member.path): | |
| 111 print 'skipping unwanted file: {0}'.format(member.path) | |
| 112 continue | |
| 113 self.tarball.extract(member) | |
| 114 | |
| 115 | |
| 116 def version_to_float(version): | |
| 117 """ | |
| 118 Convert version string to float. | |
| 119 """ | |
| 120 if version.endswith('b'): | |
| 121 return float(version[:-1]) | |
| 122 | |
| 123 return float(version) | |
| 124 | |
| 125 | |
| 126 def update_afl(new_version): | |
| 127 """ | |
| 128 Update this version of AFL to newer version, new_version. | |
| 129 """ | |
| 130 readme = ChromiumReadme() | |
| 131 old_version = readme.get_current_version() | |
| 132 if new_version != 'latest': | |
| 133 new_float = version_to_float(new_version) | |
| 134 assert version_to_float(old_version) < new_float, ( | |
| 135 'Trying to update from version {0} to {1}'.format(old_version, | |
| 136 new_version)) | |
| 137 | |
| 138 # Extract the tarball. | |
| 139 tarball = AflTarball(new_version) | |
| 140 tarball.download() | |
| 141 current_less_than_new = version_to_float(old_version) < version_to_float( | |
| 142 tarball.real_version) | |
| 143 assert current_less_than_new, ( | |
| 144 'Trying to update from version {0} to {1}'.format(old_version, | |
| 145 tarball.real_version)) | |
| 146 | |
| 147 tarball.extract() | |
| 148 | |
| 149 readme.update(tarball.real_version) | |
| 150 | |
| 151 | |
| 152 def main(): | |
| 153 """ | |
| 154 Update AFL if possible. | |
| 155 """ | |
| 156 parser = argparse.ArgumentParser('Update AFL.') | |
| 157 parser.add_argument('version', metavar='version', default='latest', nargs='?', | |
| 158 help='(optional) Version to update AFL to.') | |
| 159 | |
| 160 args = parser.parse_args() | |
| 161 version = args.version | |
| 162 if version != 'latest' and not version.endswith('b'): | |
| 163 version += 'b' | |
| 164 | |
| 165 in_correct_directory = (os.path.basename(os.getcwd()) == 'src' and | |
| 166 os.path.exists('third_party')) | |
| 167 | |
| 168 assert in_correct_directory, ( | |
| 169 '{0} must be run from the repo\'s root'.format(sys.argv[0])) | |
| 170 | |
| 171 update_afl(version) | |
| 172 print ("Run git diff third_party/afl/src/docs/ChangeLog to see changes to AFL" | |
| 173 " since the last roll") | |
| 174 | |
| 175 | |
| 176 if __name__ == '__main__': | |
| 177 main() | |
| OLD | NEW |