~eloaders/i-nex/I-Nex

« back to all changes in this revision

Viewing changes to pastebinit

  • Committer: eloaders
  • Date: 2016-11-12 18:08:29 UTC
  • Revision ID: git-v1:5ce85679ea5e86f8c5d04684e824c5ab508c8dbe
Fixes #8 Reconstruction of code

Remove changelogs
Create new changelog.md
Add new file to format git log as markdown
Fixes for i-nex-lspci - now i-nex at run copy need files to user.home dir
Remove pastebinit from code

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python3
2
 
# -*- coding: utf-8 -*-
3
 
 
4
 
# Author: Stéphane Graber <stgraber@ubuntu.com>
5
 
# Written by Stéphane Graber <stgraber@stgraber.org>
6
 
#            Daniel Bartlett <dan@f-box.org>
7
 
# Last modification : Mon Jan  6 18:46:46 EST 2014
8
 
 
9
 
# This program is free software; you can redistribute it and/or modify
10
 
# it under the terms of the GNU General Public License as published by
11
 
# the Free Software Foundation; either version 2 of the License, or
12
 
# (at your option) any later version.
13
 
#
14
 
# This program is distributed in the hope that it will be useful,
15
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
# GNU General Public License for more details.
18
 
#
19
 
# You should have received a copy of the GNU General Public License
20
 
# along with this program; if not, write to the Free Software
21
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
 
 
23
 
from __future__ import print_function
24
 
 
25
 
import sys
26
 
if sys.version[0] == "2":
27
 
    from ConfigParser import NoOptionError
28
 
    from ConfigParser import SafeConfigParser as ConfigParser
29
 
    from urllib import urlencode
30
 
    from urllib import FancyURLopener
31
 
else:
32
 
    from configparser import ConfigParser, NoOptionError
33
 
    from urllib.parse import urlencode
34
 
    from urllib.request import FancyURLopener
35
 
 
36
 
# Set the default pastebin
37
 
defaultPB = "pastebin.com"
38
 
 
39
 
# Now try to override it with a distributor pastebin
40
 
try:
41
 
    import platform
42
 
    release = platform.linux_distribution()[0].lower()
43
 
    if release == 'debian':
44
 
        defaultPB = "paste.debian.net"
45
 
    elif release == 'fedora':
46
 
        defaultPB = "fpaste.org"
47
 
    elif release == 'ubuntu':
48
 
        defaultPB = "paste.ubuntu.com"
49
 
except ImportError:
50
 
    pass
51
 
 
52
 
try:
53
 
    import getopt
54
 
    import gettext
55
 
    import os
56
 
    import re
57
 
    import socket
58
 
    import xml.dom.minidom
59
 
 
60
 
    try:
61
 
        import json
62
 
    except ImportError:
63
 
        json = None
64
 
 
65
 
    _ = gettext.gettext
66
 
    gettext.textdomain("pastebinit")
67
 
 
68
 
    # Timeout after 5s
69
 
    socket.setdefaulttimeout(15)
70
 
 
71
 
    # Version number to show in the usage
72
 
    version = "1.5"
73
 
    configfile = os.path.expanduser("~/.pastebinit.xml")
74
 
 
75
 
    # Custom urlopener to handle 401's
76
 
    class pasteURLopener(FancyURLopener):
77
 
        version = "Pastebinit v%s" % version
78
 
 
79
 
        def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
80
 
            return None
81
 
 
82
 
    def preloadPastebins():
83
 
        # Check several places for config files:
84
 
        #  - global config in /etc/pastebin.d
85
 
        #  - for source checkout, config in the checkout
86
 
        #  - user's overrides in ~/.pastebin.d
87
 
        # Files found later override files found earlier.
88
 
        pastebind = {}
89
 
        for confdir in ['/usr/share/pastebin.d', '/etc/pastebin.d',
90
 
                        '/usr/local/etc/pastebin.d',
91
 
                        os.path.expanduser('~/.pastebin.d'),
92
 
                        os.path.join(
93
 
                            os.path.dirname(
94
 
                                os.path.realpath(__file__)), 'pastebin.d')]:
95
 
            try:
96
 
                confdirlist = os.listdir(confdir)
97
 
            except OSError:
98
 
                continue
99
 
 
100
 
            for fileitem in confdirlist:
101
 
                if fileitem.startswith('.') or not fileitem.endswith('.conf'):
102
 
                    continue
103
 
 
104
 
                filename = os.path.join(confdir, fileitem)
105
 
                instance = ConfigParser()
106
 
                try:
107
 
                    instance.read(filename)
108
 
                except UnicodeError:
109
 
                    continue
110
 
 
111
 
                if not instance.has_section('pastebin'):
112
 
                    print(_('%s: no section [pastebin]') % filename,
113
 
                          file=sys.stderr)
114
 
                    continue
115
 
 
116
 
                if not instance.has_option('pastebin', 'basename'):
117
 
                    print(_("%s: no 'basename' in [pastebin]") % filename,
118
 
                          file=sys.stderr)
119
 
                    continue
120
 
 
121
 
                pastebind[instance.get('pastebin', 'basename')] = instance
122
 
        return pastebind
123
 
 
124
 
    # Return the parameters depending of the pastebin used
125
 
    def getParameters(website, pastebind, content, user, jabberid, version,
126
 
                      format, permatag, title, username,
127
 
                      password, private):
128
 
        "Return the parameters array for the selected pastebin"
129
 
        params = {}
130
 
        for paste_name, paste_config in pastebind.items():
131
 
            basename = paste_config.get('pastebin', 'basename')
132
 
            try:
133
 
                https = paste_config.get('pastebin', 'https')
134
 
            except:
135
 
                https = False
136
 
 
137
 
            if basename == website or paste_name == website:
138
 
                if https:
139
 
                    website = "https://%s" % basename
140
 
                else:
141
 
                    website = "http://%s" % basename
142
 
 
143
 
            if re.search(paste_config.get('pastebin', 'regexp'), website):
144
 
                if paste_config.has_option('pastebin', 'sizelimit'):
145
 
                    params['sizelimit'] = paste_config.get('pastebin',
146
 
                                                           'sizelimit')
147
 
 
148
 
                for param in paste_config.options('format'):
149
 
                    paramname = paste_config.get('format', param)
150
 
                    if param == 'user':
151
 
                        params[paramname] = user
152
 
                    elif param == 'content':
153
 
                        params[paramname] = content
154
 
                    elif param == 'title':
155
 
                        params[paramname] = title
156
 
                    elif param == 'version':
157
 
                        params[paramname] = version
158
 
                    elif param == 'format':
159
 
                        try:
160
 
                            params[paramname] = paste_config.get('defaults',
161
 
                                                                 'format')
162
 
                        except NoOptionError:
163
 
                            params[paramname] = format
164
 
                    elif param == 'permatag':
165
 
                        params[paramname] = permatag
166
 
                    elif param == 'private':
167
 
                        params[paramname] = private
168
 
                    elif param == 'username':
169
 
                        params[paramname] = username
170
 
                    elif param == 'password':
171
 
                        params[paramname] = password
172
 
                    elif param == 'jabberid':
173
 
                        params[paramname] = jabberid
174
 
                    else:
175
 
                        params[paramname] = paste_config.get('defaults', param)
176
 
        if params:
177
 
            return website, params
178
 
        else:
179
 
            print(_("Unknown website, please post a bugreport to request "
180
 
                    "this pastebin to be added (%s)") % website,
181
 
                  file=sys.stderr)
182
 
            sys.exit(1)
183
 
 
184
 
    # XML Handling methods
185
 
    def getText(nodelist):
186
 
        rc = ""
187
 
        for node in nodelist:
188
 
            if node.nodeType == node.TEXT_NODE:
189
 
                rc = rc + node.data
190
 
        return rc
191
 
 
192
 
    def getNodes(nodes, title):
193
 
        return nodes.getElementsByTagName(title)
194
 
 
195
 
    def getFirstNode(nodes, title):
196
 
        return getNodes(nodes, title)[0]
197
 
 
198
 
    def getFirstNodeText(nodes, title):
199
 
        return getText(getFirstNode(nodes, title).childNodes)
200
 
 
201
 
    # Display usage instructions
202
 
    def Usage(fd=sys.stdout):
203
 
        print("pastebinit v" + version, file=fd)
204
 
        print(_("Reads on stdin for input or takes a list of filenames "
205
 
                "as parameters"), file=fd)
206
 
        print(_("\t-E also print content to standard output"), file=fd)
207
 
        print(_("Optional arguments (not supported by all pastebins):"),
208
 
              file=fd)
209
 
        print(_("\t-a <author:default is '%s'>") % user, file=fd)
210
 
        print(_("\t-b <pastebin url:default is '%s'>") % website, file=fd)
211
 
        print(_("\t-f <format of paste:default is '%s' (or from pastebin config)>") % format, file=fd)  # noqa
212
 
        print(_("\t-h This help screen"), file=fd)
213
 
        print(_("\t-i <input file>"), file=fd)
214
 
        print(_("\t-l List all supported pastebins"), file=fd)
215
 
        print(_("\t-j <jabberid for notifications:default is '%s'>") %
216
 
              jabberid, file=fd)
217
 
        print(_("\t-m <permatag for all versions of a post:default is blank>"),
218
 
              file=fd)
219
 
        print(_("\t-t <title of paste:default is blank>"), file=fd)
220
 
        print(_("\t-P Private. Makes your paste hidden if possible"), file=fd)
221
 
        print(_("\t-u <username> -p <password>"), file=fd)
222
 
        print(_("\t-v Print the version number"), file=fd)
223
 
        print(_("\t--verbose Verbose output to stderr"), file=fd)
224
 
 
225
 
    # Set defaults
226
 
    website = defaultPB
227
 
    user = os.environ.get('USER', os.environ.get('LOGNAME'))
228
 
    jabberid = ""
229
 
    title = ""
230
 
    permatag = ""
231
 
    format = "text"
232
 
    username = ""
233
 
    password = ""
234
 
    filenames = []
235
 
    content = ""
236
 
    private = 0
237
 
    verbose = False
238
 
    echo = False
239
 
 
240
 
    # Example configuration file string
241
 
    configexample = """\
242
 
<pastebinit>
243
 
<pastebin>paste.debian.net</pastebin>
244
 
<author>A pastebinit user</author>
245
 
<jabberid>nobody@nowhere.org</jabberid>
246
 
<format>text</format>
247
 
</pastebinit>
248
 
"""
249
 
 
250
 
    # Open configuration file if it exists
251
 
    try:
252
 
        f = open(configfile)
253
 
        configtext = f.read()
254
 
        f.close()
255
 
        gotconfigxml = True
256
 
    except KeyboardInterrupt:
257
 
        print(_("KeyboardInterrupt caught."), file=sys.stderr)
258
 
        sys.exit(1)
259
 
    except:
260
 
        gotconfigxml = False
261
 
 
262
 
    # Parse configuration file
263
 
    if gotconfigxml:
264
 
        try:
265
 
            configxml = xml.dom.minidom.parseString(configtext)
266
 
            for variable, key in (('pastebin', 'website'), ('author', 'user'),
267
 
                                  ('format', 'format'),
268
 
                                  ('jabberid', 'jabberid')):
269
 
                try:
270
 
                    value = getFirstNodeText(configxml, variable)
271
 
                    vars()[key] = value
272
 
                except:
273
 
                    pass
274
 
        except KeyboardInterrupt:
275
 
            print(_("KeyboardInterrupt caught."), file=sys.stderr)
276
 
            sys.exit(1)
277
 
        except:
278
 
            print(_("Error parsing configuration file!"), file=sys.stderr)
279
 
            print(_("Please ensure that your configuration file looks "
280
 
                    "similar to the following:"), file=sys.stderr)
281
 
            print(configexample, file=sys.stderr)
282
 
            sys.exit(1)
283
 
 
284
 
    # Get options
285
 
    try:
286
 
        optlist, arglist = getopt.getopt(sys.argv[1:],
287
 
                                         'EPhvli:f:b:a:j:t:m:u:p:',
288
 
                                         ('verbose',))
289
 
    except KeyboardInterrupt:
290
 
        print(_("KeyboardInterrupt caught."), file=sys.stderr)
291
 
        sys.exit(1)
292
 
    except getopt.GetoptError as e:
293
 
        print(_("Invalid arguments: %s!" % e)+"\n", file=sys.stderr)
294
 
        Usage(sys.stderr)
295
 
        sys.exit(1)
296
 
 
297
 
    # Get the config
298
 
    pastebind = preloadPastebins()
299
 
 
300
 
    # Iterate through options
301
 
    for opt in optlist:
302
 
        if opt[0] == "-h":
303
 
            Usage()
304
 
            sys.exit(0)
305
 
        if opt[0] == "-i":
306
 
            filenames.append(opt[1])
307
 
        elif opt[0] == "-E":
308
 
            echo = True
309
 
        elif opt[0] == "-f":
310
 
            format = opt[1]
311
 
        elif opt[0] == "-b":
312
 
            website = opt[1]
313
 
        elif opt[0] == "-a":
314
 
            user = opt[1]
315
 
        elif opt[0] == "-j":
316
 
            jabberid = opt[1]
317
 
        elif opt[0] == "-l":
318
 
            print(_("Supported pastebins:"))
319
 
            for pastebin in sorted(pastebind):
320
 
                print("- %s" % pastebin)
321
 
            sys.exit(0)
322
 
        elif opt[0] == "-t":
323
 
            title = opt[1]
324
 
        elif opt[0] == "-m":
325
 
            permatag = opt[1]
326
 
        elif opt[0] == "-P":
327
 
            private = 1
328
 
        elif opt[0] == "-u":
329
 
            username = opt[1]
330
 
        elif opt[0] == "-p":
331
 
            password = opt[1]
332
 
        elif opt[0] == "-v":
333
 
            print("pastebinit v" + version)
334
 
            sys.exit(0)
335
 
        elif opt[0] == "--verbose":
336
 
            verbose = True
337
 
 
338
 
    filenames += arglist
339
 
 
340
 
    if not filenames:
341
 
        filenames.append("-")
342
 
 
343
 
    contents = []
344
 
    for filename in filenames:
345
 
        # If - is specified as a filename read from stdin
346
 
        # otherwise load the specified files.
347
 
        if filename == "-":
348
 
            content = sys.stdin.read()
349
 
        else:
350
 
            try:
351
 
                with open(filename, "rb") as fd:
352
 
                    content = fd.read()
353
 
            except KeyboardInterrupt:
354
 
                print(_("KeyboardInterrupt caught."), file=sys.stderr)
355
 
                sys.exit(1)
356
 
            except:
357
 
                print(_("Unable to read from: %s") % filename, file=sys.stderr)
358
 
                sys.exit(1)
359
 
 
360
 
        if not content:
361
 
            print(_("You are trying to send an empty document, exiting."),
362
 
                  file=sys.stderr)
363
 
            sys.exit(1)
364
 
 
365
 
        contents.append(content)
366
 
        if echo:
367
 
            print(content)
368
 
 
369
 
    for content in contents:
370
 
        # Get the parameter array
371
 
        website, params = getParameters(website, pastebind, content, user,
372
 
                                        jabberid, version, format,
373
 
                                        permatag, title, username, password,
374
 
                                        private)
375
 
 
376
 
        if not website.endswith("/"):
377
 
            website += "/"
378
 
 
379
 
        if "sizelimit" in params:
380
 
            if len(content) > int(params['sizelimit']):
381
 
                print(_("The content you are trying to send exceeds "
382
 
                        "the pastebin's size limit."), file=sys.stderr)
383
 
                sys.exit(1)
384
 
            else:
385
 
                del params['sizelimit']
386
 
 
387
 
        if "page" in params:
388
 
            # Use page, without leading slash: website has a trailing one.
389
 
            fetch_url = website + params['page'].lstrip("/")
390
 
            del params['page']
391
 
        else:
392
 
            fetch_url = website
393
 
        if "regexp" in params:
394
 
            reLink = params['regexp']
395
 
            del params["regexp"]
396
 
        else:
397
 
            reLink = False
398
 
        # Get target_url for replacement, only used with reLink.
399
 
        if "target_url" in params:
400
 
            relink_target_url = params["target_url"]
401
 
            del params["target_url"]
402
 
            if not reLink:
403
 
                print("Warning: using target_url without regexp.",
404
 
                      file=sys.stderr)
405
 
        elif reLink:
406
 
            relink_target_url = website
407
 
        if 'post_format' in params:
408
 
            post_format = params['post_format']
409
 
            del params['post_format']
410
 
        else:
411
 
            post_format = 'standard'
412
 
 
413
 
        url_opener = pasteURLopener()
414
 
 
415
 
        if post_format == 'json':
416
 
            if json:
417
 
                params = json.dumps(params)
418
 
                url_opener.addheader('Content-type', 'text/json')
419
 
            else:
420
 
                print(_("Could not find any json library."), file=sys.stderr)
421
 
                sys.exit(1)
422
 
        else:
423
 
            # Convert to a format usable with the HTML POST
424
 
            params = urlencode(params)
425
 
 
426
 
        # Send the informations and be redirected to the final page
427
 
        if verbose:
428
 
            print("POSTing to: %s\nParams: %s" % (
429
 
                fetch_url, str(params)), file=sys.stderr)
430
 
        try:
431
 
            page = url_opener.open(fetch_url, params)
432
 
        except Exception as e:
433
 
            print(_("Failed to contact the server: %s") % e, file=sys.stderr)
434
 
            sys.exit(1)
435
 
 
436
 
        try:
437
 
            # Check if we have to apply a regexp
438
 
            page_url = ""
439
 
            if reLink:
440
 
                if reLink == '(.*)':
441
 
                    page_url = page.read().decode('utf-8').strip()
442
 
                else:
443
 
                    # Print the result of the regexp
444
 
                    page_url = relink_target_url + re.split(
445
 
                        reLink,
446
 
                        page.read().decode('utf-8'))[1]
447
 
            else:
448
 
                # Get the final page and show the url
449
 
                if echo:
450
 
                    print("-" * len(page.url))
451
 
                page_url = page.url
452
 
 
453
 
            print(page_url.strip())
454
 
        except KeyboardInterrupt:
455
 
            print(_("KeyboardInterrupt caught."), file=sys.stderr)
456
 
            sys.exit(1)
457
 
        except:
458
 
            print(_("Unable to read or parse the result page, it could be a "
459
 
                    "server timeout or a change server side, "
460
 
                    "try with another pastebin."), file=sys.stderr)
461
 
            sys.exit(1)
462
 
 
463
 
except KeyboardInterrupt:
464
 
    print(_("KeyboardInterrupt caught."), file=sys.stderr)
465
 
    sys.exit(1)