Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 import argparse
00018 import fnmatch
00019 import json
00020 import os
00021 import re
00022 import urllib2
00023
00024
00025
00026
00027
00028 def strip_comments(text):
00029 def replacer(match):
00030 s = match.group(0)
00031 if s.startswith('/'):
00032 return " "
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
00043
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
00052
00053
00054
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
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())