mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-24 21:31:04 +00:00
Bug 902209 - Add mach pastebin; r=gps
This commit is contained in:
parent
4660924fcb
commit
fbcb415eeb
@ -194,3 +194,91 @@ class UUIDProvider(object):
|
||||
updates.add(interface)
|
||||
|
||||
updates.update()
|
||||
|
||||
@CommandProvider
|
||||
class PastebinProvider(object):
|
||||
@Command('pastebin', category='misc',
|
||||
description='Command line interface to pastebin.mozilla.org.')
|
||||
@CommandArgument('--language', default=None,
|
||||
help='Language to use for syntax highlighting')
|
||||
@CommandArgument('--poster', default=None,
|
||||
help='Specify your name for use with pastebin.mozilla.org')
|
||||
@CommandArgument('--duration', default='day',
|
||||
choices=['d', 'day', 'm', 'month', 'f', 'forever'],
|
||||
help='Keep for specified duration (default: %(default)s)')
|
||||
@CommandArgument('file', nargs='?', default=None,
|
||||
help='Specify the file to upload to pastebin.mozilla.org')
|
||||
|
||||
def pastebin(self, language, poster, duration, file):
|
||||
import sys
|
||||
import urllib
|
||||
import urllib2
|
||||
|
||||
URL = 'http://pastebin.mozilla.org/'
|
||||
|
||||
FILE_TYPES = [{'value': 'text', 'name': 'None', 'extension': 'txt'},
|
||||
{'value': 'bash', 'name': 'Bash', 'extension': 'sh'},
|
||||
{'value': 'c', 'name': 'C', 'extension': 'c'},
|
||||
{'value': 'cpp', 'name': 'C++', 'extension': 'cpp'},
|
||||
{'value': 'html4strict', 'name': 'HTML', 'extension': 'html'},
|
||||
{'value': 'javascript', 'name': 'Javascript', 'extension': 'js'},
|
||||
{'value': 'javascript', 'name': 'Javascript', 'extension': 'jsm'},
|
||||
{'value': 'lua', 'name': 'Lua', 'extension': 'lua'},
|
||||
{'value': 'perl', 'name': 'Perl', 'extension': 'pl'},
|
||||
{'value': 'php', 'name': 'PHP', 'extension': 'php'},
|
||||
{'value': 'python', 'name': 'Python', 'extension': 'py'},
|
||||
{'value': 'ruby', 'name': 'Ruby', 'extension': 'rb'},
|
||||
{'value': 'css', 'name': 'CSS', 'extension': 'css'},
|
||||
{'value': 'diff', 'name': 'Diff', 'extension': 'diff'},
|
||||
{'value': 'ini', 'name': 'INI file', 'extension': 'ini'},
|
||||
{'value': 'java', 'name': 'Java', 'extension': 'java'},
|
||||
{'value': 'xml', 'name': 'XML', 'extension': 'xml'},
|
||||
{'value': 'xml', 'name': 'XML', 'extension': 'xul'}]
|
||||
|
||||
lang = ''
|
||||
|
||||
if file:
|
||||
try:
|
||||
with open(file, 'r') as f:
|
||||
content = f.read()
|
||||
# TODO: Use mime-types instead of extensions; suprocess('file <f_name>')
|
||||
# Guess File-type based on file extension
|
||||
extension = file.split('.')[-1]
|
||||
for l in FILE_TYPES:
|
||||
if extension == l['extension']:
|
||||
print('Identified file as %s' % l['name'])
|
||||
lang = l['value']
|
||||
except IOError:
|
||||
print('ERROR. No such file')
|
||||
return 1
|
||||
else:
|
||||
content = sys.stdin.read()
|
||||
duration = duration[0]
|
||||
|
||||
if language:
|
||||
lang = language
|
||||
|
||||
|
||||
params = [
|
||||
('parent_pid', ''),
|
||||
('format', lang),
|
||||
('code2', content),
|
||||
('poster', poster),
|
||||
('expiry', duration),
|
||||
('paste', 'Send')]
|
||||
|
||||
data = urllib.urlencode(params)
|
||||
print('Uploading ...')
|
||||
try:
|
||||
req = urllib2.Request(URL, data)
|
||||
response = urllib2.urlopen(req)
|
||||
http_response_code = response.getcode()
|
||||
if http_response_code == 200:
|
||||
print(response.geturl())
|
||||
else:
|
||||
print('Could not upload the file, '
|
||||
'HTTP Response Code %s' %(http_response_code))
|
||||
except urllib2.URLError:
|
||||
print('ERROR. Could not connect to pastebin.mozilla.org.')
|
||||
return 1
|
||||
return 0
|
||||
|
Loading…
Reference in New Issue
Block a user