Bug 1473441 [wpt PR 11792] - [css-properties-values-api] Require PropertyDescriptor...
[gecko.git] / tools / mach_commands.py
blobb9d5cc3d85e68306cae77f7bd6e6ae409eb60a45
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, # You can obtain one at http://mozilla.org/MPL/2.0/.
5 from __future__ import absolute_import, unicode_literals
7 import sys
9 from mach.decorators import (
10 CommandArgument,
11 CommandProvider,
12 Command,
15 from mozbuild.base import MachCommandBase, MozbuildObject
18 @CommandProvider
19 class SearchProvider(object):
20 @Command('dxr', category='misc',
21 description='Search for something in DXR.')
22 @CommandArgument('term', nargs='+', help='Term(s) to search for.')
23 def dxr(self, term):
24 import webbrowser
25 term = ' '.join(term)
26 uri = 'http://dxr.mozilla.org/mozilla-central/search?q=%s&redirect=true' % term
27 webbrowser.open_new_tab(uri)
29 @Command('mdn', category='misc',
30 description='Search for something on MDN.')
31 @CommandArgument('term', nargs='+', help='Term(s) to search for.')
32 def mdn(self, term):
33 import webbrowser
34 term = ' '.join(term)
35 uri = 'https://developer.mozilla.org/search?q=%s' % term
36 webbrowser.open_new_tab(uri)
38 @Command('google', category='misc',
39 description='Search for something on Google.')
40 @CommandArgument('term', nargs='+', help='Term(s) to search for.')
41 def google(self, term):
42 import webbrowser
43 term = ' '.join(term)
44 uri = 'https://www.google.com/search?q=%s' % term
45 webbrowser.open_new_tab(uri)
47 @Command('search', category='misc',
48 description='Search for something on the Internets. '
49 'This will open 3 new browser tabs and search for the term on Google, '
50 'MDN, and DXR.')
51 @CommandArgument('term', nargs='+', help='Term(s) to search for.')
52 def search(self, term):
53 self.google(term)
54 self.mdn(term)
55 self.dxr(term)
58 @CommandProvider
59 class UUIDProvider(object):
60 @Command('uuid', category='misc',
61 description='Generate a uuid.')
62 @CommandArgument('--format', '-f', choices=['idl', 'cpp', 'c++'],
63 help='Output format for the generated uuid.')
64 def uuid(self, format=None):
65 import uuid
66 u = uuid.uuid4()
67 if format in [None, 'idl']:
68 print(u)
69 if format is None:
70 print('')
71 if format in [None, 'cpp', 'c++']:
72 u = u.hex
73 print('{ 0x%s, 0x%s, 0x%s, \\' % (u[0:8], u[8:12], u[12:16]))
74 pairs = tuple(map(lambda n: u[n:n+2], range(16, 32, 2)))
75 print((' { ' + '0x%s, ' * 7 + '0x%s } }') % pairs)
78 @CommandProvider
79 class PastebinProvider(object):
80 @Command('pastebin', category='misc',
81 description='Command line interface to pastebin.mozilla.org.')
82 @CommandArgument('--language', default=None,
83 help='Language to use for syntax highlighting')
84 @CommandArgument('--poster', default='',
85 help='Specify your name for use with pastebin.mozilla.org')
86 @CommandArgument('--duration', default='day',
87 choices=['d', 'day', 'm', 'month', 'f', 'forever'],
88 help='Keep for specified duration (default: %(default)s)')
89 @CommandArgument('file', nargs='?', default=None,
90 help='Specify the file to upload to pastebin.mozilla.org')
91 def pastebin(self, language, poster, duration, file):
92 import urllib
93 import urllib2
95 URL = 'https://pastebin.mozilla.org/'
97 FILE_TYPES = [{'value': 'text', 'name': 'None', 'extension': 'txt'},
98 {'value': 'bash', 'name': 'Bash', 'extension': 'sh'},
99 {'value': 'c', 'name': 'C', 'extension': 'c'},
100 {'value': 'cpp', 'name': 'C++', 'extension': 'cpp'},
101 {'value': 'html4strict', 'name': 'HTML', 'extension': 'html'},
102 {'value': 'javascript', 'name': 'Javascript', 'extension': 'js'},
103 {'value': 'javascript', 'name': 'Javascript', 'extension': 'jsm'},
104 {'value': 'lua', 'name': 'Lua', 'extension': 'lua'},
105 {'value': 'perl', 'name': 'Perl', 'extension': 'pl'},
106 {'value': 'php', 'name': 'PHP', 'extension': 'php'},
107 {'value': 'python', 'name': 'Python', 'extension': 'py'},
108 {'value': 'ruby', 'name': 'Ruby', 'extension': 'rb'},
109 {'value': 'css', 'name': 'CSS', 'extension': 'css'},
110 {'value': 'diff', 'name': 'Diff', 'extension': 'diff'},
111 {'value': 'ini', 'name': 'INI file', 'extension': 'ini'},
112 {'value': 'java', 'name': 'Java', 'extension': 'java'},
113 {'value': 'xml', 'name': 'XML', 'extension': 'xml'},
114 {'value': 'xml', 'name': 'XML', 'extension': 'xul'}]
116 lang = ''
118 if file:
119 try:
120 with open(file, 'r') as f:
121 content = f.read()
122 # TODO: Use mime-types instead of extensions; suprocess('file <f_name>')
123 # Guess File-type based on file extension
124 extension = file.split('.')[-1]
125 for l in FILE_TYPES:
126 if extension == l['extension']:
127 print('Identified file as %s' % l['name'])
128 lang = l['value']
129 except IOError:
130 print('ERROR. No such file')
131 return 1
132 else:
133 content = sys.stdin.read()
134 duration = duration[0]
136 if language:
137 lang = language
139 params = [
140 ('parent_pid', ''),
141 ('format', lang),
142 ('code2', content),
143 ('poster', poster),
144 ('expiry', duration),
145 ('paste', 'Send')]
147 data = urllib.urlencode(params)
148 print('Uploading ...')
149 try:
150 req = urllib2.Request(URL, data)
151 response = urllib2.urlopen(req)
152 http_response_code = response.getcode()
153 if http_response_code == 200:
154 pasteurl = response.geturl()
155 if pasteurl == URL:
156 if "Query failure: Data too long for column" in response.readline():
157 print('ERROR. Request too large. Limit is 64KB.')
158 else:
159 print('ERROR. Unknown error')
160 else:
161 print(pasteurl)
162 else:
163 print('Could not upload the file, '
164 'HTTP Response Code %s' % (http_response_code))
165 except urllib2.URLError:
166 print('ERROR. Could not connect to pastebin.mozilla.org.')
167 return 1
168 return 0
171 def mozregression_import():
172 # Lazy loading of mozregression.
173 # Note that only the mach_interface module should be used from this file.
174 try:
175 import mozregression.mach_interface
176 except ImportError:
177 return None
178 return mozregression.mach_interface
181 def mozregression_create_parser():
182 # Create the mozregression command line parser.
183 # if mozregression is not installed, or not up to date, it will
184 # first be installed.
185 cmd = MozbuildObject.from_environment()
186 cmd._activate_virtualenv()
187 mozregression = mozregression_import()
188 if not mozregression:
189 # mozregression is not here at all, install it
190 cmd.virtualenv_manager.install_pip_package('mozregression')
191 print("mozregression was installed. please re-run your"
192 " command. If you keep getting this message please "
193 " manually run: 'pip install -U mozregression'.")
194 else:
195 # check if there is a new release available
196 release = mozregression.new_release_on_pypi()
197 if release:
198 print(release)
199 # there is one, so install it. Note that install_pip_package
200 # does not work here, so just run pip directly.
201 cmd.virtualenv_manager._run_pip([
202 'install',
203 'mozregression==%s' % release
205 print("mozregression was updated to version %s. please"
206 " re-run your command." % release)
207 else:
208 # mozregression is up to date, return the parser.
209 return mozregression.parser()
210 # exit if we updated or installed mozregression because
211 # we may have already imported mozregression and running it
212 # as this may cause issues.
213 sys.exit(0)
216 @CommandProvider
217 class MozregressionCommand(MachCommandBase):
218 @Command('mozregression',
219 category='misc',
220 description=("Regression range finder for nightly"
221 " and inbound builds."),
222 parser=mozregression_create_parser)
223 def run(self, **options):
224 self._activate_virtualenv()
225 mozregression = mozregression_import()
226 mozregression.run(options)