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
9 from mach
.decorators
import (
15 from mozbuild
.base
import MachCommandBase
, MozbuildObject
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.')
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.')
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
):
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, '
51 @CommandArgument('term', nargs
='+', help='Term(s) to search for.')
52 def search(self
, term
):
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):
67 if format
in [None, 'idl']:
71 if format
in [None, 'cpp', 'c++']:
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
)
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):
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'}]
120 with
open(file, 'r') as f
:
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]
126 if extension
== l
['extension']:
127 print('Identified file as %s' % l
['name'])
130 print('ERROR. No such file')
133 content
= sys
.stdin
.read()
134 duration
= duration
[0]
144 ('expiry', duration
),
147 data
= urllib
.urlencode(params
)
148 print('Uploading ...')
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()
156 if "Query failure: Data too long for column" in response
.readline():
157 print('ERROR. Request too large. Limit is 64KB.')
159 print('ERROR. Unknown error')
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.')
171 def mozregression_import():
172 # Lazy loading of mozregression.
173 # Note that only the mach_interface module should be used from this file.
175 import mozregression
.mach_interface
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'.")
195 # check if there is a new release available
196 release
= mozregression
.new_release_on_pypi()
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
([
203 'mozregression==%s' % release
205 print("mozregression was updated to version %s. please"
206 " re-run your command." % release
)
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.
217 class MozregressionCommand(MachCommandBase
):
218 @Command('mozregression',
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
)