|
| 1 | + |
| 2 | +import os |
| 3 | +from os import path |
| 4 | +import shutil |
| 5 | +import pathlib |
| 6 | +import json |
| 7 | +import sys |
| 8 | +import fnmatch |
| 9 | + |
| 10 | +src_path = path.join(path.dirname(__file__),'..') |
| 11 | +version_path = path.join(path.dirname(__file__),'_version.py') |
| 12 | +metadata_template = path.join(path.dirname(__file__),'metadata_template.json') |
| 13 | +resources_path = path.join(path.dirname(__file__),'resources') |
| 14 | +build_path = path.join('build') |
| 15 | + |
| 16 | +# https://stackoverflow.com/a/6257321 |
| 17 | +def findReplace(directory, find, replace, filePattern): |
| 18 | + for path, dirs, files in os.walk(os.path.abspath(directory)): |
| 19 | + for filename in fnmatch.filter(files, filePattern): |
| 20 | + filepath = os.path.join(path, filename) |
| 21 | + with open(filepath) as f: |
| 22 | + s = f.read() |
| 23 | + s = s.replace(find, replace) |
| 24 | + with open(filepath, "w") as f: |
| 25 | + f.write(s) |
| 26 | + |
| 27 | +# ====================================================== |
| 28 | + |
| 29 | +# Delete build and recreate |
| 30 | +try: |
| 31 | + shutil.rmtree(build_path) |
| 32 | +except FileNotFoundError: |
| 33 | + pass |
| 34 | +os.mkdir(build_path) |
| 35 | +os.mkdir(path.join(build_path,'content')) |
| 36 | +os.chdir(build_path) |
| 37 | + |
| 38 | +# Copy symbols, footprints and 3dmodels |
| 39 | +shutil.copytree(path.join(src_path,'symbols'), path.join('content')) |
| 40 | +shutil.copytree(path.join(src_path,'footprints'), path.join('content')) |
| 41 | +shutil.copytree(path.join(src_path,'3dmodels'), path.join('content')) |
| 42 | + |
| 43 | +# Replace every occurrence of "(model "${SPARKFUN_KICAD_LIBRARY}/3dmodels/3d-library.3dshapes/" |
| 44 | +# with "(model "${KICAD9_3RD_PARTY}/3dmodels/com_github_sparkfun_sparkfun-kicad-libraries/3d-library.3dshapes/" |
| 45 | +findReplace(path.join('content'), '(model "${SPARKFUN_KICAD_LIBRARY}/3dmodels/3d-library.3dshapes/', |
| 46 | + '(model "${KICAD9_3RD_PARTY}/3dmodels/com_github_sparkfun_SparkFun-KiCad-Libraries/3d-library.3dshapes/', '*.*') |
| 47 | + |
| 48 | +# This is the with-PCM_ version |
| 49 | +# Replace every occurrence of "(symbol "SparkFun-" |
| 50 | +# with "(symbol "PCM_SparkFun-" |
| 51 | +findReplace(path.join('content'), "(symbol \"SparkFun-", |
| 52 | + "(symbol \"PCM_SparkFun-", "*.*") |
| 53 | + |
| 54 | +# Copy icon |
| 55 | +shutil.copytree(resources_path, path.join('content','resources')) |
| 56 | + |
| 57 | +# Copy metadata |
| 58 | +metadata = path.join('content','metadata.json') |
| 59 | +shutil.copy(metadata_template, metadata) |
| 60 | + |
| 61 | +# Load up json script |
| 62 | +with open(metadata) as f: |
| 63 | + md = json.load(f) |
| 64 | + |
| 65 | +# Get version from _version.py |
| 66 | +# https://stackoverflow.com/a/7071358 |
| 67 | +import re |
| 68 | +verstrline = open(version_path, "rt").read() |
| 69 | +VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]" |
| 70 | +mo = re.search(VSRE, verstrline, re.M) |
| 71 | +if mo: |
| 72 | + verstr = mo.group(1) |
| 73 | +else: |
| 74 | + verstr = "9.0.0" |
| 75 | + |
| 76 | +# Update download URL |
| 77 | +md['versions'][0].update({ |
| 78 | + 'version': verstr, |
| 79 | + 'download_url': 'https://github.com/sparkfun/SparkFun-KiCad-Libraries/releases/download/v{0}/SparkFun-KiCad-Libraries-{0}-with-pcm.zip'.format(verstr) |
| 80 | +}) |
| 81 | + |
| 82 | +# Update metadata.json |
| 83 | +with open(metadata, 'w') as of: |
| 84 | + json.dump(md, of, indent=2) |
| 85 | + |
| 86 | +# Zip all files |
| 87 | +zip_file = 'SparkFun-KiCad-Libraries-{0}-with-pcm.zip'.format(md['versions'][0]['version']) |
| 88 | +shutil.make_archive(pathlib.Path(zip_file).stem, 'zip', 'content') |
| 89 | + |
| 90 | +# Rename the content directory so we can upload it as an artifact - and avoid the double-zip |
| 91 | +shutil.move('content', 'SparkFun-KiCad-Libraries-{0}-with-pcm'.format(md['versions'][0]['version'])) |
| 92 | + |
| 93 | +# ====================================================== |
| 94 | + |
| 95 | +# Do it all again for the without-PCM_ version |
| 96 | +os.mkdir(path.join('content')) |
| 97 | + |
| 98 | +# Copy symbols, footprints and 3dmodels |
| 99 | +shutil.copytree(path.join(src_path,'symbols'), path.join('content')) |
| 100 | +shutil.copytree(path.join(src_path,'footprints'), path.join('content')) |
| 101 | +shutil.copytree(path.join(src_path,'3dmodels'), path.join('content')) |
| 102 | + |
| 103 | +# Replace every occurrence of "(model "${SPARKFUN_KICAD_LIBRARY}/3dmodels/3d-library.3dshapes/" |
| 104 | +# with "(model "${KICAD9_3RD_PARTY}/3dmodels/com_github_sparkfun_sparkfun-kicad-libraries/3d-library.3dshapes/" |
| 105 | +findReplace(path.join('content'), '(model "${SPARKFUN_KICAD_LIBRARY}/3dmodels/3d-library.3dshapes/', |
| 106 | + '(model "${KICAD9_3RD_PARTY}/3dmodels/com_github_sparkfun_SparkFun-KiCad-Libraries/3d-library.3dshapes/', '*.*') |
| 107 | + |
| 108 | +# Copy icon |
| 109 | +shutil.copytree(resources_path, path.join('content','resources')) |
| 110 | + |
| 111 | +# Copy metadata |
| 112 | +metadata = path.join('content','metadata.json') |
| 113 | +shutil.copy(metadata_template, metadata) |
| 114 | + |
| 115 | +# Load up json script |
| 116 | +with open(metadata) as f: |
| 117 | + md = json.load(f) |
| 118 | + |
| 119 | +# Get version from _version.py |
| 120 | +# https://stackoverflow.com/a/7071358 |
| 121 | +import re |
| 122 | +verstrline = open(version_path, "rt").read() |
| 123 | +VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]" |
| 124 | +mo = re.search(VSRE, verstrline, re.M) |
| 125 | +if mo: |
| 126 | + verstr = mo.group(1) |
| 127 | +else: |
| 128 | + verstr = "9.0.0" |
| 129 | + |
| 130 | +# Update download URL |
| 131 | +md['versions'][0].update({ |
| 132 | + 'version': verstr, |
| 133 | + 'download_url': 'https://github.com/sparkfun/SparkFun-KiCad-Libraries/releases/download/v{0}/SparkFun-KiCad-Libraries-{0}-without-pcm.zip'.format(verstr) |
| 134 | +}) |
| 135 | + |
| 136 | +# Update metadata.json |
| 137 | +with open(metadata, 'w') as of: |
| 138 | + json.dump(md, of, indent=2) |
| 139 | + |
| 140 | +# Zip all files |
| 141 | +zip_file = 'SparkFun-KiCad-Libraries-{0}-without-pcm.zip'.format(md['versions'][0]['version']) |
| 142 | +shutil.make_archive(pathlib.Path(zip_file).stem, 'zip', 'content') |
| 143 | + |
| 144 | +# Rename the content directory so we can upload it as an artifact - and avoid the double-zip |
| 145 | +shutil.move('content', 'SparkFun-KiCad-Libraries-{0}-without-pcm'.format(md['versions'][0]['version'])) |
0 commit comments