| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 # Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. | 3 # Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions | 6 # modification, are permitted provided that the following conditions |
| 7 # are met: | 7 # are met: |
| 8 # | 8 # |
| 9 # 1. Redistributions of source code must retain the above | 9 # 1. Redistributions of source code must retain the above |
| 10 # copyright notice, this list of conditions and the following | 10 # copyright notice, this list of conditions and the following |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 # FIXME: Figure out what is triggering this and what to do about
it. | 62 # FIXME: Figure out what is triggering this and what to do about
it. |
| 63 _log.error("Trying to load %s, which is a directory", filename) | 63 _log.error("Trying to load %s, which is a directory", filename) |
| 64 doc = None | 64 doc = None |
| 65 | 65 |
| 66 if is_ref: | 66 if is_ref: |
| 67 self.ref_doc = doc | 67 self.ref_doc = doc |
| 68 else: | 68 else: |
| 69 self.test_doc = doc | 69 self.test_doc = doc |
| 70 | 70 |
| 71 def analyze_test(self, test_contents=None, ref_contents=None): | 71 def analyze_test(self, test_contents=None, ref_contents=None): |
| 72 """ Analyzes a file to determine if it's a test, what type of test, and
what reference or support files it requires. Returns all of the test info """ | 72 """Analyzes a file to determine if it's a test, what type of test, and w
hat reference or support files it requires. |
| 73 | 73 |
| 74 Returns: A dict which can have the properties: |
| 75 "test": test file name. |
| 76 "reference": related reference test file name if this is a reference
test. |
| 77 "reference_support_info": extra information about the related refere
nce test and any support files. |
| 78 "jstest": A boolean, whether this is a JS test. |
| 79 If the given contents are empty, then None is returned. |
| 80 """ |
| 74 test_info = None | 81 test_info = None |
| 75 | 82 |
| 76 if test_contents is None and self.test_doc is None: | 83 if test_contents is None and self.test_doc is None: |
| 77 return test_info | 84 return test_info |
| 78 | 85 |
| 79 if test_contents is not None: | 86 if test_contents is not None: |
| 80 self.test_doc = Parser(test_contents) | 87 self.test_doc = Parser(test_contents) |
| 81 | 88 |
| 82 if ref_contents is not None: | 89 if ref_contents is not None: |
| 83 self.ref_doc = Parser(ref_contents) | 90 self.ref_doc = Parser(ref_contents) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 95 except KeyError as e: | 102 except KeyError as e: |
| 96 # FIXME: Figure out what to do w/ invalid test files. | 103 # FIXME: Figure out what to do w/ invalid test files. |
| 97 _log.error('%s has a reference link but is missing the "href"',
self.filesystem) | 104 _log.error('%s has a reference link but is missing the "href"',
self.filesystem) |
| 98 return None | 105 return None |
| 99 | 106 |
| 100 if self.ref_doc is None: | 107 if self.ref_doc is None: |
| 101 self.load_file(ref_file, True) | 108 self.load_file(ref_file, True) |
| 102 | 109 |
| 103 test_info = {'test': self.filename, 'reference': ref_file} | 110 test_info = {'test': self.filename, 'reference': ref_file} |
| 104 | 111 |
| 105 # If the ref file does not live in the same directory as the test fi
le, check it for support files | 112 # If the ref file does not live in the same directory as the test fi
le, check it for support files. |
| 106 test_info['reference_support_info'] = {} | 113 test_info['reference_support_info'] = {} |
| 107 if self.filesystem.dirname(ref_file) != self.filesystem.dirname(self
.filename): | 114 if self.filesystem.dirname(ref_file) != self.filesystem.dirname(self
.filename): |
| 108 reference_support_files = self.support_files(self.ref_doc) | 115 reference_support_files = self.support_files(self.ref_doc) |
| 109 if len(reference_support_files) > 0: | 116 if len(reference_support_files) > 0: |
| 110 reference_relpath = self.filesystem.relpath(self.filesystem.
dirname( | 117 reference_relpath = self.filesystem.relpath(self.filesystem.
dirname( |
| 111 self.filename), self.filesystem.dirname(ref_file)) + sel
f.filesystem.sep | 118 self.filename), self.filesystem.dirname(ref_file)) + sel
f.filesystem.sep |
| 112 test_info['reference_support_info'] = {'reference_relpath':
reference_relpath, 'files': reference_support_files} | 119 test_info['reference_support_info'] = {'reference_relpath':
reference_relpath, 'files': reference_support_files} |
| 113 | 120 |
| 114 elif self.is_jstest(): | 121 elif self.is_jstest(): |
| 115 test_info = {'test': self.filename, 'jstest': True} | 122 test_info = {'test': self.filename, 'jstest': True} |
| 116 elif self.options['all'] is True and not('-ref' in self.filename) and no
t('reference' in self.filename): | 123 elif self.options['all'] is True and not('-ref' in self.filename) and no
t('reference' in self.filename): |
| 117 test_info = {'test': self.filename} | 124 test_info = {'test': self.filename} |
| 118 | 125 |
| 119 return test_info | 126 return test_info |
| 120 | 127 |
| 121 def reference_links_of_type(self, reftest_type): | 128 def reference_links_of_type(self, reftest_type): |
| 122 return self.test_doc.findAll(rel=reftest_type) | 129 return self.test_doc.findAll(rel=reftest_type) |
| 123 | 130 |
| 124 def is_jstest(self): | 131 def is_jstest(self): |
| 125 """Returns whether the file appears to be a jstest, by searching for usa
ge of W3C-style testharness paths.""" | 132 """Returns whether the file appears to be a jstest, by searching for usa
ge of W3C-style testharness paths.""" |
| 126 return bool(self.test_doc.find(src=re.compile('[\'\"/]?/resources/testha
rness'))) | 133 return bool(self.test_doc.find(src=re.compile('[\'\"/]?/resources/testha
rness'))) |
| 127 | 134 |
| 128 def support_files(self, doc): | 135 def support_files(self, doc): |
| 129 """ Searches the file for all paths specified in url()'s or src attribut
es.""" | 136 """Searches the file for all paths specified in url()s or src attributes
.""" |
| 130 support_files = [] | 137 support_files = [] |
| 131 | 138 |
| 132 if doc is None: | 139 if doc is None: |
| 133 return support_files | 140 return support_files |
| 134 | 141 |
| 135 elements_with_src_attributes = doc.findAll(src=re.compile('.*')) | 142 elements_with_src_attributes = doc.findAll(src=re.compile('.*')) |
| 136 elements_with_href_attributes = doc.findAll(href=re.compile('.*')) | 143 elements_with_href_attributes = doc.findAll(href=re.compile('.*')) |
| 137 | 144 |
| 138 url_pattern = re.compile('url\(.*\)') | 145 url_pattern = re.compile('url\(.*\)') |
| 139 urls = [] | 146 urls = [] |
| 140 for url in doc.findAll(text=url_pattern): | 147 for url in doc.findAll(text=url_pattern): |
| 141 url = re.search(url_pattern, url) | 148 url = re.search(url_pattern, url) |
| 142 url = re.sub('url\([\'\"]?', '', url.group(0)) | 149 url = re.sub('url\([\'\"]?', '', url.group(0)) |
| 143 url = re.sub('[\'\"]?\)', '', url) | 150 url = re.sub('[\'\"]?\)', '', url) |
| 144 urls.append(url) | 151 urls.append(url) |
| 145 | 152 |
| 146 src_paths = [src_tag['src'] for src_tag in elements_with_src_attributes] | 153 src_paths = [src_tag['src'] for src_tag in elements_with_src_attributes] |
| 147 href_paths = [href_tag['href'] for href_tag in elements_with_href_attrib
utes] | 154 href_paths = [href_tag['href'] for href_tag in elements_with_href_attrib
utes] |
| 148 | 155 |
| 149 paths = src_paths + href_paths + urls | 156 paths = src_paths + href_paths + urls |
| 150 for path in paths: | 157 for path in paths: |
| 151 if not(path.startswith('http:')) and not(path.startswith('mailto:'))
: | 158 if not(path.startswith('http:')) and not(path.startswith('mailto:'))
: |
| 152 uri_scheme_pattern = re.compile(r"[A-Za-z][A-Za-z+.-]*:") | 159 uri_scheme_pattern = re.compile(r"[A-Za-z][A-Za-z+.-]*:") |
| 153 if not uri_scheme_pattern.match(path): | 160 if not uri_scheme_pattern.match(path): |
| 154 support_files.append(path) | 161 support_files.append(path) |
| 155 | 162 |
| 156 return support_files | 163 return support_files |
| OLD | NEW |