Skip to content

Commit

Permalink
Reimplement schematic export in python
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbez1 committed Apr 9, 2016
1 parent 97fff41 commit ef968ee
Show file tree
Hide file tree
Showing 14 changed files with 380 additions and 1 deletion.
20 changes: 19 additions & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ excepted below, all files are licensed under Apache v2.

Exceptions to the Apache v2 license:
- electronics/bom/* (GPL v3, see electronics/bom/LICENSE.txt)
--------------------

----

splitflap - Copyright 2016 Scott Bezek

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

----

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down
Empty file added __init__.py
Empty file.
Empty file added electronics/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions electronics/scripts/export_bom.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#!/bin/bash

# Copyright 2016 Scott Bezek
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

# Kill background jobs when the script terminates
Expand Down
163 changes: 163 additions & 0 deletions electronics/scripts/export_schematic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/usr/bin/env python

# Copyright 2016 Scott Bezek
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import subprocess
import sys
import tempfile
import time

from contextlib import contextmanager

electronics_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
repo_root = os.path.dirname(electronics_root)
sys.path.append(repo_root)

from thirdparty.xvfbwrapper.xvfbwrapper import Xvfb
from util import file_util, rev_info

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

class PopenContext(subprocess.Popen):
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
if self.stdout:
self.stdout.close()
if self.stderr:
self.stderr.close()
if self.stdin:
self.stdin.close()
if type:
self.terminate()
# Wait for the process to terminate, to avoid zombies.
self.wait()

def xdotool(command):
return subprocess.check_output(['xdotool'] + command)

def wait_for_window(name, window_regex, timeout=10):
DELAY = 0.5
logger.info('Waiting for %s window...', name)
for i in range(int(timeout/DELAY)):
try:
xdotool(['search', '--name', window_regex])
logger.info('Found %s window', name)
return
except subprocess.CalledProcessError:
pass
time.sleep(DELAY)
raise RuntimeError('Timed out waiting for %s window' % name)

@contextmanager
def recorded_xvfb(video_filename, **xvfb_args):
with Xvfb(**xvfb_args):
with PopenContext([
'recordmydesktop',
'--no-sound',
'--no-frame',
'--on-the-fly-encoding',
'-o', video_filename], close_fds=True) as screencast_proc:
yield
screencast_proc.terminate()

def _get_versioned_contents(filename):
with open(filename, 'rb') as schematic:
original_contents = schematic.read()
return original_contents, original_contents \
.replace('Date ""', 'Date "%s"' % rev_info.current_date()) \
.replace('Rev ""', 'Rev "%s"' % rev_info.git_short_rev())

@contextmanager
def versioned_schematic(filename):
original_contents, versioned_contents = _get_versioned_contents(filename)
with open(filename, 'wb') as temp_schematic:
logger.debug('Writing to %s', filename)
temp_schematic.write(versioned_contents)
try:
yield
finally:
with open(filename, 'wb') as temp_schematic:
logger.debug('Restoring %s', filename)
temp_schematic.write(original_contents)

def eeschema_plot_schematic(output_directory):
wait_for_window('eeschema', '\[')

logger.info('Focus main eeschema window')
xdotool(['search', '--name', '\[', 'windowfocus'])

logger.info('Open File->Plot->Plot')
xdotool(['key', 'alt+f'])
xdotool(['key', 'p'])
xdotool(['key', 'p'])

wait_for_window('plot', 'Plot')
xdotool(['search', '--name', 'Plot', 'windowfocus'])

logger.info('Enter build output directory')
xdotool(['type', output_directory])

logger.info('Select PDF plot format')
xdotool([
'key',
'Tab',
'Tab',
'Tab',
'Tab',
'Tab',
'Up',
'Up',
'Up',
'space',
])

logger.info('Plot')
xdotool(['key', 'Return'])

logger.info('Wait before shutdown')
time.sleep(2)

def export_schematic():
schematic_file = os.path.join(electronics_root, 'splitflap.sch')
output_dir = os.path.join(electronics_root, 'build')
file_util.mkdir_p(output_dir)

screencast_output_file = os.path.join(output_dir, 'export_schematic_screencast.ogv')
schematic_output_pdf_file = os.path.join(output_dir, 'splitflap.pdf')
schematic_output_png_file = os.path.join(output_dir, 'splitflap.png')

with versioned_schematic(schematic_file):
with recorded_xvfb(screencast_output_file, width=800, height=600, colordepth=24):
with PopenContext(['eeschema', schematic_file], close_fds=True) as eeschema_proc:
eeschema_plot_schematic(output_dir)
eeschema_proc.terminate()

logger.info('Rasterize')
subprocess.check_call([
'convert',
'-density', '96',
schematic_output_pdf_file,
'-background', 'white',
'-alpha', 'remove',
schematic_output_png_file,
])

if __name__ == '__main__':
export_schematic()

14 changes: 14 additions & 0 deletions electronics/scripts/export_schematic.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#!/bin/bash

# Copyright 2016 Scott Bezek
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

# Kill background jobs when the script terminates
Expand Down
25 changes: 25 additions & 0 deletions thirdparty/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This project uses the following open source software:

----

xvfbwrapper - Copyright (c) 2012-015 Corey Goldberg


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Empty file added thirdparty/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions thirdparty/xvfbwrapper/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
xvfbwrapper - Copyright (c) 2012-015 Corey Goldberg


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Empty file.
95 changes: 95 additions & 0 deletions thirdparty/xvfbwrapper/xvfbwrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env python
#
# * Corey Goldberg, 2012, 2013, 2015, 2016
#
# * inspired by: PyVirtualDisplay


"""wrapper for running display inside X virtual framebuffer (Xvfb)"""


import os
import fnmatch
import subprocess
import tempfile
import time


class Xvfb:

def __init__(self, width=800, height=680, colordepth=24, **kwargs):
self.width = width
self.height = height
self.colordepth = colordepth

if not self.xvfb_exists():
msg = 'Can not find Xvfb. Please install it and try again.'
raise EnvironmentError(msg)

self.extra_xvfb_args = ['-screen', '0', '{}x{}x{}'.format(
self.width, self.height, self.colordepth)]

for key, value in kwargs.items():
self.extra_xvfb_args += ['-{}'.format(key), value]

if 'DISPLAY' in os.environ:
self.orig_display = os.environ['DISPLAY'].split(':')[1]
else:
self.orig_display = None

self.proc = None

def __enter__(self):
self.start()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()

def start(self):
self.new_display = self._get_next_unused_display()
display_var = ':{}'.format(self.new_display)
self.xvfb_cmd = ['Xvfb', display_var] + self.extra_xvfb_args
with open(os.devnull, 'w') as fnull:
self.proc = subprocess.Popen(self.xvfb_cmd,
stdout=fnull,
stderr=fnull,
preexec_fn=os.setpgrp, #NOTE(sbezek): added this to avoid Xvfb handling SIGINT before python
close_fds=True)
time.sleep(0.1) # give Xvfb time to start
ret_code = self.proc.poll()
if ret_code is None:
self._set_display_var(self.new_display)
else:
raise RuntimeError('Xvfb did not start')

def stop(self):
if self.orig_display is None:
del os.environ['DISPLAY']
else:
self._set_display_var(self.orig_display)
if self.proc is not None:
try:
self.proc.terminate()
self.proc.wait()
except OSError:
pass
self.proc = None

def _get_next_unused_display(self):
tmpdir = tempfile.gettempdir()
pattern = '.X*-lock'
lockfile_names = fnmatch.filter(os.listdir(tmpdir), pattern)
existing_displays = [int(name.split('X')[1].split('-')[0])
for name in lockfile_names]
highest_display = max(existing_displays) if existing_displays else 0
return highest_display + 1

def _set_display_var(self, display):
os.environ['DISPLAY'] = ':{}'.format(display)

def xvfb_exists(self):
"""Check that Xvfb is available on PATH and is executable."""
paths = os.environ['PATH'].split(os.pathsep)
return any(os.access(os.path.join(path, 'Xvfb'), os.X_OK)
for path in paths)
Empty file added util/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions util/file_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python

import errno
import os

def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise

Loading

0 comments on commit ef968ee

Please sign in to comment.