Bug 902209 - Add mach pastebin; r=gps
authorAnhad Jai Singh <ffledgling>
Wed, 16 Oct 2013 10:13:00 -0700
changeset 164877 27a00d5fa55dd0d9d1f77ee6161e927575ae353d
parent 164876 287878a32dd22f0d6a26845a3ee1cddc012b961b
child 164878 df950d461d3ee0f74c02c87e3b361e8dfaa335a4
push idunknown
push userunknown
push dateunknown
reviewersgps
bugs902209
milestone27.0a1
Bug 902209 - Add mach pastebin; r=gps
tools/mach_commands.py
--- a/tools/mach_commands.py
+++ b/tools/mach_commands.py
@@ -189,8 +189,96 @@ class UUIDProvider(object):
             shutil.rmtree(tmpdir)
 
         updates = IDLUpdater(registry)
 
         for interface in interfaces:
             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