summaryrefslogtreecommitdiff
path: root/src/third_party/variant-1.3.0/support/wandbox.py
blob: 01c95e6dae0330fd28b5e2b0e54f868b824d970f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/bin/env python

# MPark.Variant
#
# This script uploads a directory to Wandbox (http://melpon.org/wandbox),
# which is an online compiler environment, and prints a permalink to the
# uploaded code. We use this to provide a "Try it online" version of the
# library to make the barrier to entry as low as possible.
#
# This script was adapted from the script proposed in
# https://github.com/melpon/wandbox/issues/153.
#
# To know how to use this script: ./wandbox.py --help
#
# Copyright Louis Dionne 2015
#
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#
# Copyright Michael Park, 2017
#
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

import argparse
import fnmatch
import json
import os
import re
import urllib2

# Post the given JSON data to Wandbox's API, and return the result
# as a JSON object.
def upload(options):
    request = urllib2.Request('http://melpon.org/wandbox/api/compile.json')
    request.add_header('Content-Type', 'application/json')
    response = urllib2.urlopen(request, json.dumps(options))
    return json.loads(response.read())

# Returns a list of the '.hpp' headers in the given directory and in
# subdirectories.
#
# The path must be absolute, and the returned paths are all absolute too.
def headers(path):
    return [
        os.path.join(dir, file)
            for (dir, _, files) in os.walk(path)
                for file in fnmatch.filter(files, "*.hpp")
    ]

def main():
    parser = argparse.ArgumentParser(description=
        """Upload a directory to Wandbox (http://melpon.org/wandbox).

           On success, the program prints a permalink to the uploaded
           directory on Wandbox and returns 0. On error, it prints the
           response from the Wandbox API and returns 1.

           Note that the comments are stripped from all the headers in the
           uploaded directory.
        """
    )
    parser.add_argument('directory', type=str, help=
        """A directory to upload to Wandbox.

           The path may be either absolute or relative to the current directory.
           However, the names of the files uploaded to Wandbox will all be
           relative to this directory. This way, one can easily specify the
           directory to be '/some/project/include', and the uploaded files
           will be uploaded as-if they were rooted at '/some/project/include'
        """)
    parser.add_argument('main', type=str, help=
        """The main source file.

           The path may be either absolute or relative to the current directory.
        """
    )
    args = parser.parse_args()
    directory = os.path.abspath(args.directory)
    if not os.path.exists(directory):
        raise Exception("'%s' is not a valid directory" % args.directory)

    cpp = os.path.abspath(args.main)
    if not os.path.exists(cpp):
        raise Exception("'%s' is not a valid file name" % args.main)

    response = upload({
        'code': open(cpp).read().strip(),
        'codes': [{
            'file': os.path.relpath(header, directory).replace('\\', '/'),
            'code': open(header).read().strip()
        } for header in headers(directory)],
        'options': 'warning,optimize,c++14',
        'compiler': 'clang-4.0.0',
        'save': True,
        'compiler-option-raw': '-I.'
    })

    if response['status'] == '0':
        print response['url']
        return 0
    else:
        print response
        return 1

exit(main())