Esempio n. 1
0
    def _load_info(self) -> Dict[str, Any]:
        filepath = os.path.join(self.provider_project_dir, "project-info.yaml")
        if not os.path.exists(filepath):
            return dict()

        with open(filepath) as info_file:
            return yaml_utils.load(info_file)
Esempio n. 2
0
def get_state(state_dir: str, step: steps.Step):
    state = None
    state_file = get_step_state_file(state_dir, step)
    if os.path.isfile(state_file):
        with open(state_file, "r") as f:
            state = yaml_utils.load(f)

    return state
Esempio n. 3
0
    def test_scriptlet_after_repull(self):
        self.run_command(["prime"])

        with open(os.path.join("prime", "meta", "snap.yaml")) as f:
            y = yaml_utils.load(f)

        self.assertThat(y["grade"], Equals("devel"))
        self.assertThat(y["version"], Equals("v1.0"))

        # modifying source file (src/version.txt) will trigger re-pull
        open(os.path.join("src", "version.txt"), "w").write("v2.0")
        self.run_command(["prime"])

        with open(os.path.join("prime", "meta", "snap.yaml")) as f:
            z = yaml_utils.load(f)

        self.assertThat(z["grade"], Equals("devel"))
        self.assertThat(z["version"], Equals("v2.0"))
Esempio n. 4
0
    def test_yaml_conversion(self, init_spy):
        state_string = yaml_utils.dump(self.state)

        # Verify that the dumped tag was correct
        self.assertThat(state_string.splitlines()[0], Equals("!PrimeState"))

        # Now verify the conversion
        state_from_yaml = yaml_utils.load(state_string)
        self.assertThat(state_from_yaml, Equals(self.state))

        # Verify that init was not called
        init_spy.assert_not_called()
Esempio n. 5
0
def _edit_validation_sets(validation_sets: str) -> Dict[str, Any]:
    """Spawn an editor to modify the validation-sets."""
    editor_cmd = os.getenv("EDITOR", "vi")

    with tempfile.NamedTemporaryFile() as ft:
        ft.close()
        with open(ft.name, "w") as fw:
            print(validation_sets, file=fw)
        subprocess.run([editor_cmd, ft.name], check=True)
        with open(ft.name, "r") as fr:
            edited_validation_sets = yaml_utils.load(fr)

    return edited_validation_sets
Esempio n. 6
0
def edit_validation_sets(account_id: str, set_name: str, sequence: int,
                         key_name: str):
    """Edit the list of validations for <set-name>.

    Refer to https://snapcraft.io/docs/validation-sets for further information
    on Validation Sets.
    """
    store_client = StoreClientCLI()

    asserted_validation_sets = store_client.get_validation_sets(
        name=set_name, sequence=str(sequence))

    try:
        # assertions should only have one item since a specific
        # sequence was requested.
        revision = asserted_validation_sets.assertions[0].revision
        snaps = yaml_utils.dump({
            "snaps": [
                s.marshal()
                for s in asserted_validation_sets.assertions[0].snaps
            ]
        })
    except IndexError:
        # If there is no assertion for a given sequence, the store API
        # will return an empty list.
        revision = "0"
        snaps = _VALIDATIONS_SETS_SNAPS_TEMPLATE

    unverified_validation_sets = _VALIDATION_SETS_TEMPLATE.format(
        account_id=account_id,
        set_name=set_name,
        sequence=sequence,
        revision=revision,
        snaps=snaps,
    )

    edited_validation_sets = _edit_validation_sets(unverified_validation_sets)
    if edited_validation_sets == yaml_utils.load(unverified_validation_sets):
        echo.warning("No changes made.")
    else:
        build_assertion = store_client.post_validation_sets_build_assertion(
            validation_sets=edited_validation_sets)
        signed_validation_sets = _sign_assertion(build_assertion.marshal(),
                                                 key_name=key_name)
        store_client.post_validation_sets(
            signed_validation_sets=signed_validation_sets)
Esempio n. 7
0
 def _get_snap_deb_arch(self, snap_filename):
     with tempfile.TemporaryDirectory() as temp_dir:
         unsquashfs_path = file_utils.get_snap_tool_path("unsquashfs")
         output = subprocess.check_output([
             unsquashfs_path,
             "-d",
             os.path.join(temp_dir, "squashfs-root"),
             snap_filename,
             "-e",
             # cygwin unsquashfs uses unix paths.
             Path("meta", "snap.yaml").as_posix(),
         ])
         logger.debug(output)
         with open(
                 os.path.join(temp_dir, "squashfs-root", "meta",
                              "snap.yaml")) as yaml_file:
             snap_yaml = yaml_utils.load(yaml_file)
     # XXX: add multiarch support later
     try:
         return snap_yaml["architectures"][0]
     except KeyError:
         return "all"
Esempio n. 8
0
def _load_registry(registry_filepath: Optional[str]) -> Dict[str, List[Any]]:
    if registry_filepath is None or not os.path.exists(registry_filepath):
        return dict()

    with open(registry_filepath) as registry_file:
        return yaml_utils.load(registry_file)
Esempio n. 9
0
 def from_file(cls, snap_yaml_path: str) -> "Snap":
     with open(snap_yaml_path, "r") as f:
         snap_dict = yaml_utils.load(f)
         return cls.from_dict(snap_dict=snap_dict)
Esempio n. 10
0
 def load(cls: Type["GlobalState"], *, filepath: str) -> "GlobalState":
     with open(filepath) as state_file:
         return yaml_utils.load(state_file)
Esempio n. 11
0
 def load(self):
     if os.path.exists(self._path):
         with open(self._path) as info_file:
             self.clear()
             self.update(yaml_utils.load(info_file))