send_to_wandbox.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 # This script uploads a directory to Wandbox (http://melpon.org/wandbox),
00004 # which is an online compiler environment, and prints a permalink to the
00005 # uploaded code. We use this to provide a "Try it online" version of the
00006 # library to make the barrier to entry as low as possible.
00007 #
00008 # This script was adapted from the script proposed in
00009 # https://github.com/melpon/wandbox/issues/153.
00010 #
00011 # To know how to use this script: ./wandbox.py --help
00012 #
00013 # Copyright Louis Dionne 2015
00014 # Distributed under the Boost Software License, Version 1.0.
00015 # (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
00016 
00017 import argparse
00018 import fnmatch
00019 import json
00020 import os
00021 import re
00022 import urllib2
00023 
00024 
00025 # Strips C and C++ comments from the given string.
00026 #
00027 # Copied from http://stackoverflow.com/a/241506/627587.
00028 def strip_comments(text):
00029     def replacer(match):
00030         s = match.group(0)
00031         if s.startswith('/'):
00032             return " " # note: a space and not an empty string
00033         else:
00034             return s
00035     pattern = re.compile(
00036         r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
00037         re.DOTALL | re.MULTILINE
00038     )
00039     return re.sub(pattern, replacer, text)
00040 
00041 
00042 # Post the given JSON data to Wandbox's API, and return the result
00043 # as a JSON object.
00044 def upload(options):
00045     request = urllib2.Request('http://melpon.org/wandbox/api/compile.json')
00046     request.add_header('Content-Type', 'application/json')
00047     response = urllib2.urlopen(request, json.dumps(options))
00048     return json.loads(response.read())
00049 
00050 
00051 # Returns a list of the '.hpp' headers in the given directory and in
00052 # subdirectories.
00053 #
00054 # The path must be absolute, and the returned paths are all absolute too.
00055 def headers(path):
00056     return [
00057         os.path.join(dir, file)
00058             for (dir, _, files) in os.walk(path)
00059                 for file in fnmatch.filter(files, "*.hpp")
00060     ]
00061 
00062 
00063 def main():
00064     parser = argparse.ArgumentParser(description=
00065         """Upload a directory to Wandbox (http://melpon.org/wandbox).
00066 
00067            On success, the program prints a permalink to the uploaded
00068            directory on Wandbox and returns 0. On error, it prints the
00069            response from the Wandbox API and returns 1.
00070 
00071            Note that the comments are stripped from all the headers in the
00072            uploaded directory.
00073         """
00074     )
00075     parser.add_argument('directory', type=str, help=
00076         """A directory to upload to Wandbox.
00077 
00078            The path may be either absolute or relative to the current directory.
00079            However, the names of the files uploaded to Wandbox will all be
00080            relative to this directory. This way, one can easily specify the
00081            directory to be '/some/project/include', and the uploaded files
00082            will be uploaded as-if they were rooted at '/some/project/include'
00083         """)
00084     parser.add_argument('main', type=str, help=
00085         """The main source file.
00086 
00087            The path may be either absolute or relative to the current directory.
00088         """
00089     )
00090     args = parser.parse_args()
00091     directory = os.path.abspath(args.directory)
00092     if not os.path.exists(directory):
00093         raise Exception("'%s' is not a valid directory" % args.directory)
00094 
00095     cpp = os.path.abspath(args.main)
00096     if not os.path.exists(cpp):
00097         raise Exception("'%s' is not a valid file name" % args.main)
00098 
00099     response = upload({
00100         'code': open(cpp).read(),
00101         'codes': [{
00102             'file': os.path.relpath(header, directory),
00103             #'code': strip_comments(open(header).read())
00104             'code': open(header).read()
00105         } for header in headers(directory)],
00106         'options': 'boost-nothing,c++11',
00107         'compiler': 'gcc-4.9.2',
00108         'save': True,
00109         'compiler-option-raw': '-I.'
00110     })
00111 
00112     if 'status' in response and response['status'] == '0':
00113         print response['url']
00114         return 0
00115     else:
00116         print response
00117         return 1
00118 
00119 
00120 exit(main())


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06