def annotate_snapcraft(project: "Project", data: Dict[str, Any]) -> Dict[str, Any]: manifest = OrderedDict() # type: Dict[str, Any] manifest["snapcraft-version"] = snapcraft._get_version() manifest["snapcraft-started-at"] = project._get_start_time().isoformat( ) + "Z" release = os_release.OsRelease() with contextlib.suppress(errors.OsReleaseIdError): manifest["snapcraft-os-release-id"] = release.id() with contextlib.suppress(errors.OsReleaseVersionIdError): manifest["snapcraft-os-release-version-id"] = release.version_id() for k, v in data.items(): manifest[k] = v image_info = os.environ.get("SNAPCRAFT_IMAGE_INFO") if image_info: try: image_info_dict = json.loads(image_info) except json.decoder.JSONDecodeError as exception: raise errors.InvalidContainerImageInfoError( image_info) from exception manifest["image-info"] = image_info_dict global_state = GlobalState.load( filepath=project._get_global_state_file_path()) manifest["build-packages"] = sorted(global_state.get_build_packages()) manifest["build-snaps"] = sorted(global_state.get_build_snaps()) for part in data["parts"]: state_dir = os.path.join(project.parts_dir, part, "state") pull_state = get_state(state_dir, steps.PULL) manifest["parts"][part]["build-packages"] = sorted( pull_state.assets.get("build-packages", [])) manifest["parts"][part]["stage-packages"] = sorted( pull_state.assets.get("stage-packages", [])) source_details = pull_state.assets.get("source-details", {}) if source_details: manifest["parts"][part].update(source_details) build_state = get_state(state_dir, steps.BUILD) manifest["parts"][part].update(build_state.assets) # Assemble all primed stage packages into a single list... primed_stage_packages: Set[str] = set() for part in data["parts"]: state_dir = os.path.join(project.parts_dir, part, "state") prime_state = get_state(state_dir, steps.PRIME) primed_stage_packages |= prime_state.primed_stage_packages manifest["primed-stage-packages"] = sorted(primed_stage_packages) return manifest
def test_load_with_missing(self): with open("state", "w") as state_file: print("!GlobalState", file=state_file) print("assets: ", file=state_file) if self.build_packages: print(" build-packages: {}".format(self.build_packages), file=state_file) if self.build_snaps: print(" build-snaps: {}".format(self.build_snaps), file=state_file) global_state = GlobalState.load(filepath="state") self.assertThat(global_state.get_build_packages(), Equals(self.build_packages)) self.assertThat(global_state.get_build_snaps(), Equals(self.build_snaps))
def test_load(self): with open("state", "w") as state_file: print( dedent("""\ !GlobalState assets: build-packages: {} build-snaps: {} """).format(self.build_packages, self.build_snaps), file=state_file, ) global_state = GlobalState.load(filepath="state") self.assertThat(global_state.get_build_packages(), Equals(self.build_packages)) self.assertThat(global_state.get_build_snaps(), Equals(self.build_snaps))
def test_load_with_missing(self, tmp_work_path, build_packages, build_snaps, required_grade): with open("state", "w") as state_file: print("!GlobalState", file=state_file) print("assets: ", file=state_file) if build_packages: print(" build-packages: {}".format(build_packages), file=state_file) if build_snaps: print(" build-snaps: {}".format(build_snaps), file=state_file) if required_grade: print(" required-grade: {}".format(required_grade), file=state_file) global_state = GlobalState.load(filepath="state") assert global_state.get_build_packages() == build_packages assert global_state.get_build_snaps() == build_snaps assert global_state.get_required_grade() == required_grade
def test_save_load_and_append(self, tmp_work_path, build_packages, build_snaps, required_grade): global_state = GlobalState() global_state.append_build_packages(build_packages) global_state.append_build_snaps(build_snaps) global_state.save(filepath="state") assert global_state.get_build_packages() == build_packages assert global_state.get_build_snaps() == build_snaps new_packages = ["new-pkg1", "new-pkg2"] new_snaps = ["new-snap1", "new-snap2"] global_state = GlobalState.load(filepath="state") global_state.append_build_packages(new_packages) global_state.append_build_snaps(new_snaps) assert global_state.get_build_packages( ) == build_packages + new_packages assert global_state.get_build_snaps() == build_snaps + new_snaps
def annotate_snapcraft(data, parts_dir: str, global_state_path: str): manifest = OrderedDict() # type: Dict[str, Any] manifest["snapcraft-version"] = snapcraft._get_version() release = os_release.OsRelease() with contextlib.suppress(errors.OsReleaseIdError): manifest["snapcraft-os-release-id"] = release.id() with contextlib.suppress(errors.OsReleaseVersionIdError): manifest["snapcraft-os-release-version-id"] = release.version_id() for k, v in data.items(): manifest[k] = v image_info = os.environ.get("SNAPCRAFT_IMAGE_INFO") if image_info: try: image_info_dict = json.loads(image_info) except json.decoder.JSONDecodeError as exception: raise errors.InvalidContainerImageInfoError(image_info) from exception manifest["image-info"] = image_info_dict global_state = GlobalState.load(filepath=global_state_path) manifest["build-packages"] = global_state.get_build_packages() manifest["build-snaps"] = global_state.get_build_snaps() for part in data["parts"]: state_dir = os.path.join(parts_dir, part, "state") pull_state = get_state(state_dir, steps.PULL) manifest["parts"][part]["build-packages"] = pull_state.assets.get( "build-packages", [] ) manifest["parts"][part]["stage-packages"] = pull_state.assets.get( "stage-packages", [] ) source_details = pull_state.assets.get("source-details", {}) if source_details: manifest["parts"][part].update(source_details) build_state = get_state(state_dir, steps.BUILD) manifest["parts"][part].update(build_state.assets) return manifest
def test_save_load_and_append(self): global_state = GlobalState() global_state.append_build_packages(self.build_packages) global_state.append_build_snaps(self.build_snaps) global_state.save(filepath="state") self.assertThat(global_state.get_build_packages(), Equals(self.build_packages)) self.assertThat(global_state.get_build_snaps(), Equals(self.build_snaps)) new_packages = ["new-pkg1", "new-pkg2"] new_snaps = ["new-snap1", "new-snap2"] global_state = GlobalState.load(filepath="state") global_state.append_build_packages(new_packages) global_state.append_build_snaps(new_snaps) self.assertThat( global_state.get_build_packages(), Equals(self.build_packages + new_packages), ) self.assertThat( global_state.get_build_snaps(), Equals(self.build_snaps + new_snaps) )
def test_load(self, tmp_work_path, build_packages, build_snaps, required_grade): if required_grade: required_grade = required_grade else: required_grade = "null" with open("state", "w") as state_file: print( dedent("""\ !GlobalState assets: build-packages: {} build-snaps: {} required-grade: {} """).format(build_packages, build_snaps, required_grade), file=state_file, ) global_state = GlobalState.load(filepath="state") assert global_state.get_build_packages() == build_packages assert global_state.get_build_snaps() == build_snaps
def test_load(self): if self.required_grade: required_grade = self.required_grade else: required_grade = "null" with open("state", "w") as state_file: print( dedent( """\ !GlobalState assets: build-packages: {} build-snaps: {} required-grade: {} """ ).format(self.build_packages, self.build_snaps, required_grade), file=state_file, ) global_state = GlobalState.load(filepath="state") self.assertThat(global_state.get_build_packages(), Equals(self.build_packages)) self.assertThat(global_state.get_build_snaps(), Equals(self.build_snaps))