Skip to content

Instantly share code, notes, and snippets.

@projectgus
Last active August 29, 2015 13:55
Show Gist options
  • Save projectgus/8701232 to your computer and use it in GitHub Desktop.
Save projectgus/8701232 to your computer and use it in GitHub Desktop.
A command line program to upload a directory (or zip file) of gerbers to the excellent online gerber preview tool http://gerblook.org/
#!/usr/bin/env python3
"""
usage: gerblook_it.py [-h] [-m COLOUR] [-s COLOUR] [-c COLOUR] [-n] [GERBERS]
Upload a zip file (or directory of gerbers) to gerblook
Most basic use to upload a whole directory of gerber files is:
cd <gerber_directory>
gerblook_it.py .
positional arguments:
GERBERS Gerbers (zip file path or path to a directory with gerbers)
optional arguments:
-h, --help show this help message and exit
-m COLOUR, --soldermask COLOUR
Soldermask colour
-s COLOUR, --silkscreen COLOUR
Silkscreen colour
-c COLOUR, --copper COLOUR
Copper colour
-n, --no-launch Don't launch a web browser after uploading, just print
the URL
***
Main argument can be a directory containing gerber files (*.g?? or *.d?? or *.TXT extensions)
Requirements:
Python 3, BeautifulSoup, Requests
On Debian/Ubuntu:
sudo apt-get install python3-bs4 python3-requests
Copyright (C) 2014 Angus Gratton
Licensed under New BSD License as described in the text section LICENSE at the bottom. :)
"""
from bs4 import BeautifulSoup
from zipfile import ZipFile
import requests, sys, argparse, io, os, os.path, re, webbrowser
URL="http://gerblook.org/"
def is_gerbername(f):
EXTS = [ "g...?.?", "d...?", ".txt", "out", "oln" ]
return any(re.search(r".+\.%s$" % e, f, re.I) for e in EXTS) and not f.lower().endswith(".gpi")
parser = argparse.ArgumentParser(description='Upload a zip file (or directory of gerbers) to gerblook')
parser.add_argument('-m', '--soldermask', metavar='COLOUR', choices=["Blue","Yellow","Black","Green","White","Red"],
help='Soldermask colour', default="Blue")
parser.add_argument('-s', '--silkscreen', metavar='COLOUR', choices=["Black","White"],
help='Silkscreen colour', default="White")
parser.add_argument('-c', '--copper', metavar='COLOUR', choices=["Gold","Silver"],
help='Copper colour', default="Silver")
parser.add_argument('-n', '--no-launch',help="Don't launch a web browser after uploading, just print the URL", action="store_true")
parser.add_argument('gerbers', metavar='GERBERS', help="Gerbers (zip file or directory path", default=".", nargs="?")
args = parser.parse_args()
# Find zipfile of gerbers
if os.path.isfile(args.gerbers):
try:
zip_data = open(args.gerbers, "rb")
with ZipFile(zip_data, 'r') as zf:
if zf.testzip() is not None:
print("%s is not a valid zip file" % args.gerbers)
sys.exit(2)
except:
print("Error opening zip file %s" % args.gerbers)
sys.exit(2)
else:
if not os.path.isdir(args.gerbers):
print("%s does not point to a directory" % args.gerbers)
sys.exit(2)
gerbers = [ f for f in os.listdir(args.gerbers) if is_gerbername(f) ]
if len(gerbers) == 0:
print("%s does not contain any files with gerber extensions (*.g?? or *.d??)" % args.gerbers)
sys.exit(2)
zip_data = io.BytesIO()
with ZipFile(zip_data, 'w') as zf:
for f in gerbers:
print("Sending file %s" % f)
zf.write(os.path.join(args.gerbers,f), f)
zip_data.seek(0)
# Establish a gerblook session, get a session cookie & csrf token
r = requests.get(URL)
cookies = r.cookies
soup = BeautifulSoup(r.text)
csrf_token = soup.find(id="csrf_token")["value"]
post_data = {
"csrf_token" : csrf_token,
"soldermask_color" : args.soldermask,
"silkscreen_color" : args.silkscreen,
"copper_color" : args.copper,
"i_am_a_bot" : "true",
}
files = { "gerbers" : zip_data }
r = requests.post(URL, data=post_data, files=files, cookies=cookies)
if r.status_code != 200 or len(r.history) != 1 or r.history[0].status_code != 302:
print("Something went wrong, got back status %d requests %s" % (r.status_code, r.history))
soup = BeautifulSoup(r.text)
err = soup.find(attrs={"class":re.compile("alert")})
if err is not None:
print("Got error message: %s" % err.text)
sys.exit(1)
if not args.no_launch:
print("Opening URL %s" % r.url)
webbrowser.open(r.url)
else:
print("Gerblook URL: %s" % r.url)
"""
LICENSE
Copyright (C) 2014 Angus Gratton.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment