build.py
  1  #!/usr/bin/env python3
  2  
  3  # The MIT License (MIT)
  4  #
  5  # Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
  6  #               2018, 2019 Michael Schroeder
  7  #
  8  # Permission is hereby granted, free of charge, to any person obtaining a copy
  9  # of this software and associated documentation files (the "Software"), to deal
 10  # in the Software without restriction, including without limitation the rights
 11  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  # copies of the Software, and to permit persons to whom the Software is
 13  # furnished to do so, subject to the following conditions:
 14  #
 15  # The above copyright notice and this permission notice shall be included in
 16  # all copies or substantial portions of the Software.
 17  #
 18  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  # THE SOFTWARE.
 25  
 26  import os
 27  import os.path
 28  import pathlib
 29  import semver
 30  import shutil
 31  import sys
 32  import subprocess
 33  import tempfile
 34  
 35  IGNORE_PY = ["setup.py", "conf.py", "__init__.py"]
 36  GLOB_PATTERNS = ["*.py", "font5x8.bin"]
 37  
 38  def version_string(path=None, *, valid_semver=False):
 39      version = None
 40      tag = subprocess.run('git describe --tags --exact-match', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=path)
 41      if tag.returncode == 0:
 42          version = tag.stdout.strip().decode("utf-8", "strict")
 43      else:
 44          describe = subprocess.run("git describe --tags --always", shell=True, stdout=subprocess.PIPE, cwd=path)
 45          describe = describe.stdout.strip().decode("utf-8", "strict").rsplit("-", maxsplit=2)
 46          if len(describe) == 3:
 47              tag, additional_commits, commitish = describe
 48              commitish = commitish[1:]
 49          else:
 50              tag = "0.0.0"
 51              commit_count = subprocess.run("git rev-list --count HEAD", shell=True, stdout=subprocess.PIPE, cwd=path)
 52              additional_commits = commit_count.stdout.strip().decode("utf-8", "strict")
 53              commitish = describe[0]
 54          if valid_semver:
 55              version_info = semver.parse_version_info(tag)
 56              if not version_info.prerelease:
 57                  version = semver.bump_patch(tag) + "-alpha.0.plus." + additional_commits + "+" + commitish
 58              else:
 59                  version = tag + ".plus." + additional_commits + "+" + commitish
 60          else:
 61              version = commitish
 62      return version
 63  
 64  def mpy_cross(mpy_cross_filename, circuitpython_tag, quiet=False):
 65      if os.path.isfile(mpy_cross_filename):
 66          return
 67      if not quiet:
 68          title = "Building mpy-cross for circuitpython " + circuitpython_tag
 69          print()
 70          print(title)
 71          print("=" * len(title))
 72  
 73      os.makedirs("build_deps/", exist_ok=True)
 74      if not os.path.isdir("build_deps/circuitpython"):
 75          clone = subprocess.run("git clone https://github.com/adafruit/circuitpython.git build_deps/circuitpython", shell=True)
 76          if clone.returncode != 0:
 77              sys.exit(clone.returncode)
 78  
 79      current_dir = os.getcwd()
 80      os.chdir("build_deps/circuitpython")
 81      make = subprocess.run("git fetch && git checkout {TAG} && git submodule update".format(TAG=circuitpython_tag), shell=True)
 82      os.chdir("tools")
 83      make = subprocess.run("git submodule update --init .", shell=True)
 84      os.chdir("../mpy-cross")
 85      make = subprocess.run("make clean && make", shell=True)
 86      os.chdir(current_dir)
 87  
 88      shutil.copy("build_deps/circuitpython/mpy-cross/mpy-cross", mpy_cross_filename)
 89  
 90      if make.returncode != 0:
 91          sys.exit(make.returncode)
 92  
 93  def _munge_to_temp(original_path, temp_file, library_version):
 94      with open(original_path, "rb") as original_file:
 95          for line in original_file:
 96              if original_path.endswith(".bin"):
 97                  # this is solely for adafruit_framebuf/examples/font5x8.bin
 98                  temp_file.write(line)
 99              else:
100                  line = line.decode("utf-8").strip("\n")
101                  if line.startswith("__version__"):
102                      line = line.replace("0.0.0-auto.0", library_version)
103                  temp_file.write(line.encode("utf-8") + b"\r\n")
104      temp_file.flush()
105  
106  def library(library_path, output_directory, package_folder_prefix,
107              mpy_cross=None, example_bundle=False):
108      py_files = []
109      package_files = []
110      example_files = []
111      total_size = 512
112  
113      lib_path = pathlib.Path(library_path)
114      parent_idx = len(lib_path.parts)
115      glob_search = []
116      for pattern in GLOB_PATTERNS:
117          glob_search.extend(list(lib_path.rglob(pattern)))
118  
119      for file in glob_search:
120          if file.parts[parent_idx] == "examples":
121              example_files.append(file)
122          else:
123              if not example_bundle:
124                  is_package = False
125                  for prefix in package_folder_prefix:
126                      if file.parts[parent_idx].startswith(prefix):
127                          is_package = True
128  
129                  if is_package:
130                      package_files.append(file)
131                  else:
132                      if file.name in IGNORE_PY:
133                          #print("Ignoring:", file.resolve())
134                          continue
135                      if file.parent == lib_path:
136                          py_files.append(file)
137  
138      if len(py_files) > 1:
139          raise ValueError("Multiple top level py files not allowed. Please put "
140                           "them in a package or combine them into a single file.")
141  
142      for fn in example_files:
143          base_dir = os.path.join(output_directory.replace("/lib", "/"),
144                                  fn.relative_to(library_path).parent)
145          if not os.path.isdir(base_dir):
146              os.makedirs(base_dir)
147              total_size += 512
148  
149      for fn in package_files:
150          base_dir = os.path.join(output_directory,
151                                  fn.relative_to(library_path).parent)
152          if not os.path.isdir(base_dir):
153              os.makedirs(base_dir)
154              total_size += 512
155  
156      new_extension = ".py"
157      if mpy_cross:
158          new_extension = ".mpy"
159  
160      try:
161          library_version = version_string(library_path, valid_semver=True)
162      except ValueError as e:
163          print(library_path + " has version that doesn't follow SemVer (semver.org)")
164          print(e)
165          library_version = version_string(library_path)
166  
167      for filename in py_files:
168          full_path = os.path.join(library_path, filename)
169          output_file = os.path.join(
170              output_directory,
171              filename.relative_to(library_path).with_suffix(new_extension)
172          )
173          with tempfile.NamedTemporaryFile() as temp_file:
174              _munge_to_temp(full_path, temp_file, library_version)
175  
176              if mpy_cross:
177                  mpy_success = subprocess.call([
178                      mpy_cross,
179                      "-o", output_file,
180                      "-s", str(filename.relative_to(library_path)),
181                      temp_file.name
182                  ])
183                  if mpy_success != 0:
184                      raise RuntimeError("mpy-cross failed on", full_path)
185              else:
186                  shutil.copyfile(temp_file.name, output_file)
187  
188      for filename in package_files:
189          full_path = os.path.join(library_path, filename)
190          with tempfile.NamedTemporaryFile() as temp_file:
191              _munge_to_temp(full_path, temp_file, library_version)
192              if not mpy_cross or os.stat(full_path).st_size == 0:
193                  output_file = os.path.join(output_directory,
194                                             filename.relative_to(library_path))
195                  shutil.copyfile(temp_file.name, output_file)
196              else:
197                  output_file = os.path.join(
198                      output_directory,
199                      filename.relative_to(library_path).with_suffix(new_extension)
200                  )
201  
202                  mpy_success = subprocess.call([
203                      mpy_cross,
204                      "-o", output_file,
205                      "-s", str(filename.relative_to(library_path)),
206                      temp_file.name
207                  ])
208                  if mpy_success != 0:
209                      raise RuntimeError("mpy-cross failed on", full_path)
210  
211      for filename in example_files:
212          full_path = os.path.join(library_path, filename)
213          output_file = os.path.join(output_directory.replace("/lib", "/"),
214                                     filename.relative_to(library_path))
215          with tempfile.NamedTemporaryFile() as temp_file:
216              _munge_to_temp(full_path, temp_file, library_version)
217              shutil.copyfile(temp_file.name, output_file)