Skip to content

Commit

Permalink
Extract release version number from tag, if applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbez1 committed Mar 15, 2022
1 parent 916854c commit 1f7bf8a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 28 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/electronics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,45 @@ jobs:

- name: Export JLCPCB fabrication files [view_base]
run: |
./electronics/scripts/export_jlcpcb.py electronics/view_base/view_base.kicad_pcb
./electronics/scripts/export_jlcpcb.py --release-prefix releases/view_base/ electronics/view_base/view_base.kicad_pcb
cp -r electronics/build/view_base-jlc electronics/build/outputs
- name: Export PCB overview PDF [view_base]
run: |
./electronics/scripts/generate_pdf.py electronics/view_base/view_base.kicad_pcb
./electronics/scripts/generate_pdf.py --release-prefix releases/view_base/ electronics/view_base/view_base.kicad_pcb
cp electronics/build/view_base-pcb-packet.pdf electronics/build/outputs
env:
PYTHONUNBUFFERED: 1

- name: Render PCB 3D [view_base]
run: |
./electronics/scripts/export_3d.py electronics/view_base/view_base.kicad_pcb --suffix front --width 1024 --height 1024 --skip-virtual --color-soldermask 1 1 1 --color-silk 0.1 0.1 0.1 transform z+ z+ rx+ rx+ rz- ry- ry- mu
./electronics/scripts/export_3d.py electronics/view_base/view_base.kicad_pcb --suffix front --width 1024 --height 1024 --skip-virtual --color-soldermask 1 1 1 --color-silk 0.1 0.1 0.1 --release-prefix releases/view_base/ transform z+ z+ rx+ rx+ rz- ry- ry- mu
cp electronics/build/view_base-front-3d.png electronics/build/outputs
./scripts/annotate_image.sh electronics/build/outputs/view_base-front-3d.png
./electronics/scripts/export_3d.py electronics/view_base/view_base.kicad_pcb --suffix back --width 1024 --height 1024 --skip-virtual --color-soldermask 1 1 1 --color-silk 0.1 0.1 0.1 transform z+ z+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ rx+ rx+ rz- ry+ ry+ mu
./electronics/scripts/export_3d.py electronics/view_base/view_base.kicad_pcb --suffix back --width 1024 --height 1024 --skip-virtual --color-soldermask 1 1 1 --color-silk 0.1 0.1 0.1 --release-prefix releases/view_base/ transform z+ z+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ rx+ rx+ rz- ry+ ry+ mu
cp electronics/build/view_base-back-3d.png electronics/build/outputs
./scripts/annotate_image.sh electronics/build/outputs/view_base-back-3d.png
env:
PYTHONUNBUFFERED: 1

- name: Export JLCPCB fabrication files [view_screen]
run: |
./electronics/scripts/export_jlcpcb.py electronics/view_screen/view_screen.kicad_pcb
./electronics/scripts/export_jlcpcb.py --release-prefix releases/view_screen/ electronics/view_screen/view_screen.kicad_pcb
cp -r electronics/build/view_screen-jlc electronics/build/outputs
- name: Export PCB overview PDF [view_screen]
run: |
./electronics/scripts/generate_pdf.py electronics/view_screen/view_screen.kicad_pcb
./electronics/scripts/generate_pdf.py --release-prefix releases/view_screen/ electronics/view_screen/view_screen.kicad_pcb
cp electronics/build/view_screen-pcb-packet.pdf electronics/build/outputs
env:
PYTHONUNBUFFERED: 1

- name: Render PCB 3D [view_screen]
run: |
./electronics/scripts/export_3d.py electronics/view_screen/view_screen.kicad_pcb --suffix front --width 1024 --height 1024 --skip-virtual transform z+ z+ rx+ rx+ rz- ry- ry- mu
./electronics/scripts/export_3d.py electronics/view_screen/view_screen.kicad_pcb --suffix front --width 1024 --height 1024 --skip-virtual --release-prefix releases/view_screen/ transform z+ z+ rx+ rx+ rz- ry- ry- mu
cp electronics/build/view_screen-front-3d.png electronics/build/outputs
./scripts/annotate_image.sh electronics/build/outputs/view_screen-front-3d.png
./electronics/scripts/export_3d.py electronics/view_screen/view_screen.kicad_pcb --suffix back --width 1024 --height 1024 --skip-virtual transform z+ z+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ rx+ rx+ rz- ry+ ry+ mu
./electronics/scripts/export_3d.py electronics/view_screen/view_screen.kicad_pcb --suffix back --width 1024 --height 1024 --skip-virtual --release-prefix releases/view_screen/ transform z+ z+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ rx+ rx+ rz- ry+ ry+ mu
cp electronics/build/view_screen-back-3d.png electronics/build/outputs
./scripts/annotate_image.sh electronics/build/outputs/view_screen-back-3d.png
env:
Expand Down
7 changes: 4 additions & 3 deletions electronics/scripts/export_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _pcbnew_export_3d(output_file, width, height, transforms):
time.sleep(2)


def export_3d(filename, suffix, width, height, transforms, raytrace, virtual, color_soldermask, color_silk, color_board, color_copper):
def export_3d(filename, suffix, width, height, transforms, raytrace, virtual, color_soldermask, color_silk, color_board, color_copper, release_prefix):
pcb_file = os.path.abspath(filename)
output_dir = os.path.join(electronics_root, 'build')
file_util.mkdir_p(output_dir)
Expand Down Expand Up @@ -167,7 +167,7 @@ def apply_color(name, values):
apply_color('CopperColor', color_copper)

with patch_config(os.path.expanduser('~/.config/kicad/pcbnew'), settings):
with versioned_file(pcb_file):
with versioned_file(pcb_file, release_prefix):
with recorded_xvfb(screencast_output_file, width=width, height=height, colordepth=24):
with PopenContext(['pcbnew', pcb_file], close_fds=True) as pcbnew_proc:
_pcbnew_export_3d(output_file, width, height, transforms)
Expand All @@ -186,6 +186,7 @@ def apply_color(name, values):
parser.add_argument('--color-silk', type=float, nargs=3, help='Silkscreen color as 3 floats from 0-1', default=[1, 1, 1])
parser.add_argument('--color-board', type=float, nargs=3, help='PCB substrate color as 3 floats from 0-1', default=[0.764705882, 0.729411765, 0.607843137])
parser.add_argument('--color-copper', type=float, nargs=3, help='Copper color as 3 floats from 0-1', default=[0.7, 0.7, 0.7])
parser.add_argument('--release-prefix', type=str, required=True, help='Tag prefix to check if this is a tagged/versioned release. E.g. "releases/" for tags like "releases/v1.0"')

# Use subparsers to for an optional nargs="*" choices argument (workaround for https://bugs.python.org/issue9625)
subparsers = parser.add_subparsers(dest='which')
Expand All @@ -196,4 +197,4 @@ def apply_color(name, values):

transforms = args.transform if args.which == 'transform' else []

export_3d(args.pcb, args.suffix, args.width, args.height, transforms, not args.skip_raytrace, not args.skip_virtual, args.color_soldermask, args.color_silk, args.color_board, args.color_copper)
export_3d(args.pcb, args.suffix, args.width, args.height, transforms, not args.skip_raytrace, not args.skip_virtual, args.color_soldermask, args.color_silk, args.color_board, args.color_copper, args.release_prefix)
7 changes: 4 additions & 3 deletions electronics/scripts/export_jlcpcb.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
logger = logging.getLogger(__name__)


def export_jlcpcb(pcb, schematic, alt_fields):
def export_jlcpcb(pcb, schematic, alt_fields, release_prefix):
pcb_file = os.path.abspath(pcb)

output_dir = os.path.join(electronics_root, 'build', os.path.splitext(os.path.basename(pcb_file))[0] + '-jlc')
file_util.mkdir_p(output_dir)

with versioned_file(pcb_file):
with versioned_file(pcb_file, release_prefix):
command = [
'kikit',
'fab',
Expand All @@ -66,5 +66,6 @@ def export_jlcpcb(pcb, schematic, alt_fields):
parser.add_argument('pcb')
parser.add_argument('--assembly-schematic')
parser.add_argument('--alt-fields', nargs='+')
parser.add_argument('--release-prefix', type=str, required=True, help='Tag prefix to check if this is a tagged/versioned release. E.g. "releases/" for tags like "releases/v1.0"')
args = parser.parse_args()
export_jlcpcb(args.pcb, args.assembly_schematic, args.alt_fields)
export_jlcpcb(args.pcb, args.assembly_schematic, args.alt_fields, args.release_prefix)
14 changes: 10 additions & 4 deletions electronics/scripts/export_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,30 @@ def recorded_xvfb(video_filename, **xvfb_args):
screencast_proc.terminate()


def get_versioned_contents(filename):
def get_versioned_contents(filename, release_search_prefix):
with open(filename, 'r') as f:
original_contents = f.read()
date = rev_info.git_date()
date_long = rev_info.git_date(short=False)
rev = rev_info.git_short_rev()
logger.info('Replacing placeholders with %s and %s' % (date, rev))
release_version = 'v#.#'
if release_search_prefix:
tag_version = rev_info.git_release_version(release_search_prefix)
if tag_version:
release_version = tag_version
return original_contents, original_contents \
.replace('Date ""', 'Date "%s"' % date_long) \
.replace('DATE: YYYY-MM-DD TIME TZ', 'DATE: %s' % date_long) \
.replace('DATE: YYYY-MM-DD', 'DATE: %s' % date) \
.replace('Rev ""', 'Rev "%s"' % rev) \
.replace('COMMIT: deadbeef', 'COMMIT: %s' % rev)
.replace('COMMIT: deadbeef', 'COMMIT: %s' % rev) \
.replace('v#.#', release_version)


@contextmanager
def versioned_file(filename):
original_contents, versioned_contents = get_versioned_contents(filename)
def versioned_file(filename, release_search_prefix):
original_contents, versioned_contents = get_versioned_contents(filename, release_search_prefix)
with open(filename, 'w') as temp_schematic:
logger.debug('Writing to %s', filename)
temp_schematic.write(versioned_contents)
Expand Down
11 changes: 6 additions & 5 deletions electronics/scripts/generate_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@
logger = logging.getLogger(__name__)


def run(pcb_file):
def run(pcb_file, release_prefix):
output_directory = os.path.join(electronics_root, 'build')
temp_dir = os.path.join(output_directory, 'temp_pdfs')
shutil.rmtree(temp_dir, ignore_errors=True)
try:
os.makedirs(temp_dir)
plot_to_directory(pcb_file, output_directory, temp_dir)
plot_to_directory(pcb_file, output_directory, temp_dir, release_prefix)
finally:
shutil.rmtree(temp_dir, ignore_errors=True)


def plot_to_directory(pcb_file, output_directory, temp_dir):
def plot_to_directory(pcb_file, output_directory, temp_dir, release_prefix):
board_name = os.path.splitext(os.path.basename(pcb_file))[0]

with pcb_util.get_plotter(pcb_file, temp_dir) as plotter:
with pcb_util.get_plotter(pcb_file, temp_dir, release_prefix) as plotter:
plotter.plot_options.SetDrillMarksType(pcbnew.PCB_PLOT_PARAMS.NO_DRILL_SHAPE)
plotter.plot_options.SetExcludeEdgeLayer(False)

Expand Down Expand Up @@ -74,7 +74,8 @@ def plot_to_directory(pcb_file, output_directory, temp_dir):

if __name__ == '__main__':
parser = argparse.ArgumentParser('Generate a pdf of the PCB')
parser.add_argument('--release-prefix', type=str, required=True, help='Tag prefix to check if this is a tagged/versioned release. E.g. "releases/" for tags like "releases/v1.0"')
parser.add_argument('pcb_file')
args = parser.parse_args()
run(args.pcb_file)
run(args.pcb_file, args.release_prefix)

11 changes: 6 additions & 5 deletions electronics/scripts/pcb_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
logger = logging.getLogger(__name__)

@contextmanager
def versioned_board(filename):
_, versioned_contents = get_versioned_contents(filename)
def versioned_board(filename, release_search_prefix):
_, versioned_contents = get_versioned_contents(filename, release_search_prefix)
with tempfile.NamedTemporaryFile(suffix='.kicad_pcb', mode='w') as temp_pcb:
logger.debug('Writing to %s', temp_pcb.name)
temp_pcb.write(versioned_contents)
Expand All @@ -41,8 +41,8 @@ def versioned_board(filename):
yield board

@contextmanager
def get_plotter(pcb_filename, build_directory):
with versioned_board(pcb_filename) as board:
def get_plotter(pcb_filename, build_directory, release_prefix):
with versioned_board(pcb_filename, release_prefix) as board:
yield Plotter(board, build_directory)


Expand Down Expand Up @@ -104,9 +104,10 @@ def plot_drill(self):

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Test pcb util')
parser.add_argument('--release-prefix', type=str, required=True, help='Tag prefix to check if this is a tagged/versioned release. E.g. "releases/" for tags like "releases/v1.0"')
parser.add_argument('input_file', help='Input .kicad_pcb file')
args = parser.parse_args()
with versioned_board(args.input_file) as board:
with versioned_board(args.input_file, args.release_prefix) as board:
logger.info('Loaded %s', board.GetFileName())
for module in board.GetModules():
logger.info('Module %s: %s', module.GetReference(), module.GetValue())
16 changes: 16 additions & 0 deletions util/rev_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,19 @@ def git_date(short=True):
return iso
except Exception:
raise RuntimeError("Could not read git commit date. Make sure you have git installed and you're working with a git clone of the repository.")

def git_release_version(search_prefix):
try:
tags = subprocess.check_output([
'git',
'tag',
'--points-at',
'HEAD',
]).decode('utf-8').splitlines()
for tag in tags:
if tag.startswith(search_prefix):
return tag[len(search_prefix):]
return None
except Exception:
raise RuntimeError("Could not read git release tags. Make sure you have git installed and you're working with a git clone of the repository.")

0 comments on commit 1f7bf8a

Please sign in to comment.