Giter Site home page Giter Site logo

plist-yaml-plist's Introduction

Convert plist <=> yaml and json => plist

This utility is designed to convert Apple plist files to yaml, or yaml files to plist. I/O is from regular files.

It can also convert json files to plist.

Installation

Prerequisites

The python ruamel.yaml module is required, which is not installed by default on Macs. You can install it with pip, which you may also need to install first. A few other things need to be updated for ruamel to install:

python -m ensurepip --user
python -m pip install -U pip setuptools wheel ruamel.yaml<0.18.0 --user

If you do not pre-install ruamel.yaml, setup.py will do it for you.

Local git repo install

git clone [email protected]:grahampugh/plist-yaml-plist && cd plist-yaml-plist
python -m pip install .

Remote git repo install

A github ssh key is required to run pip install -r requirements.txt.

requirements.txt:

-e git+ssh://[email protected]/grahampugh/plist-yaml-plist#egg=plistyamlplist

Usage

A single command can be used to convert from plist to yaml or from yaml to plist. This depends on the file suffices being predictable:

plistyamlplist -h
Usage: ./plistyamlplist.py <input-file> [<output-file>]

You can supply the input-file as a glob (*.yaml or *.json) to convert an entire directory or subset of yaml or json files. This currently only work for converting from yaml to plist. Note that you have to escape the glob, i.e. write as plistyamlplist /path/to/\*.yaml. Or, just supply a folder. The folder must be _YAML or YAML or a subfolder of one of these. Otherwise, each file can be used individually:

./plist_yaml.py -h
Usage: plist-yaml.py <input-file> [<output-file>]

./yaml_plist.py -h
Usage: yaml-plist.py <input-file> [<output-file>]

Notes:

  1. With plistyamlplist.py, if you do not specify an output-file value, the script determines if the input-file is a plist, yaml or json file. If a plist file, the input-file name will be appended with .yaml for the output file. If a yaml or json file, the output file name will be the input-file name with .yaml or json removed.
  2. With plist_yaml.py, if you do not specify an output-file value, the input-file name will be appended with .yaml for the output file.
  3. With yaml_plist.py, if you do not specify an output-file value, and the input-file name ends with .yaml, the output file name will be the input-file name with .yaml removed.
  4. With plist_yaml.py, you may have to first convert a binary plist to text format using plutil.

Examples

To convert a plist file to yaml:

plutil -convert xml1 ~/Library/Preferences/com.something.plist
./plistyamlplist.py ~/Library/Preferences/com.something.plist ~/Downloads/com.something.yaml
./plistyamlplist.py ~/Library/Preferences/com.something.plist
# this will output to `~/Library/Preferences/com.something.plist.yaml'

To convert a yaml file to a plist file:

./plistyamlplist.py ~/Downloads/com.something.yaml ~/Downloads/com.something.plist
$ ./plistyamlplist.py ~/Downloads/com.something.plist.yaml
# this will output to `~/Downloads/com.something.plist'

YAML/JSON folder

If you have a folder named YAML/_YAML, or JSON/_JSON, in your path, and you do not supply a destination, the script will determine if a corresponding folder exists in the path without YAML/JSON. For example, consider the following file:

/Users/myuser/gitrepo/YAML/product/com.something.plist.yaml

If the folder /Users/myuser/gitrepo/product exists, the converted file will be created/overwritten at:

/Users/myuser/gitrepo/product/com.something.plist

If the above folder does not exist, you will be prompted to create it.

If there is no YAML/JSON folder in the path, the converted file will be placed in the same folder.

Special handling of AutoPkg recipes

If you convert an AutoPkg recipe from plist to yaml, the following formatting is carried out:

  • The different process dictionaries are ordered by Processor, Comment, Arguments (python3 only).
  • The Input dictionary is ordered such that NAME is always at the top (python3 only).
  • The items are ordered thus: Comment, Description, Identifier, ParentRecipe, MinimumVersion, Input, Process (python3 only).
  • Blank lines are added for human readability. Specifically these are added above Input and Process dictionaries, and between each Processor dictionary.

You can also carry out reformatting of existing yaml recipes using the yaml_tidy.py script, or using plistyamlplist as in the following examples:

  • Convert an AutoPkg recipe to yaml format:

    plistyamlplist /path/to/SomeRecipe.recipe

  • Reformat a single yaml-based recipe:

    plistyamlplist /path/to/SomeRecipe.recipe.yaml --tidy

  • Reformat a an entire folder structure containing yaml-based recipes:

    plistyamlplist /path/to/YAML/ --tidy
    # this will process all .recipe.yaml files in the folders within /path/to/YAML
    
    plistyamlplist /path/to/\_YAML/ --tidy
    # this will process all .recipe.yaml files in the folders within /path/to/_YAML
    
    plistyamlplist /path/to/YAML/subfolder/ --tidy
    # this will process all .recipe.yaml files in the folders within /path/to/_YAML/subfolder

Credits

Elements of these scripts come from:

plist-yaml-plist's People

Contributors

grahampugh avatar homebysix avatar w0de avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

plist-yaml-plist's Issues

Indent array values

At the moment, strings within an array have the same indent level:

Current behavior:

- Processor: CodeSignatureVerifier
  Arguments:
    expected_authority_names:
    - 'Developer ID Installer: Yamaha Corporation (5LE7A8CF65)'
    - Developer ID Certification Authority
    - Apple Root CA
    input_path: '%RECIPE_CACHE_DIR%/%NAME%.pkg'

Expected behavior:

- Processor: CodeSignatureVerifier
  Arguments:
    expected_authority_names:
      - 'Developer ID Installer: Yamaha Corporation (5LE7A8CF65)'
      - Developer ID Certification Authority
      - Apple Root CA
    input_path: '%RECIPE_CACHE_DIR%/%NAME%.pkg'

Input keys sorted in reverse alphabetical order

When converting an AutoPkg plist recipe to YAML, the Input keys are sorted in reverse alphabetical order. For example, when converting FirefoxSignedPkg-latest.jamf.recipe to YAML, the input keys appear as follows:

  SITE: IAML
  POLICY_TEMPLATE: Policy-install-latest-site-group.xml
  POLICY_NAME: '%SITE% %NAME% Latest'
  POLICY_CATEGORY: '%CATEGORY%'
  GROUP_NAME: IAML Both Labs
  CATEGORY: Browsers

Python 3 plistlib error

I changed the first line of the script to call python3 and when I run it this is what I get.

Traceback (most recent call last):
  File "/usr/local/bin/plistyamlplist_lib/plist_yaml.py", line 20, in <module>
	from plistlib import Data  # Python 3
ImportError: cannot import name 'Data' from 'plistlib' (/usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/plistlib.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/plistyamlplist", line 21, in <module>
	from plistyamlplist_lib.plist_yaml import plist_yaml
  File "/usr/local/bin/plistyamlplist_lib/plist_yaml.py", line 23, in <module>
	from plistlib import Data  # Python 2
ImportError: cannot import name 'Data' from 'plistlib' (/usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/plistlib.py)

Issue with Python 3.11

sw_vers
ProductName: macOS
ProductVersion: 14.2
BuildVersion: 23C5030f
Python 3.11.3 (v3.11.3:f3909b8bc8, Apr 4 2023, 20:12:10) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin

plist-yaml-plist version 0.6.3
Processing plist file...
Traceback (most recent call last):
File "/usr/local/bin/plistyamlplist", line 319, in
main()
File "/usr/local/bin/plistyamlplist", line 311, in main
plist_yaml(in_path, out_path)
File "/usr/local/bin/plistyamlplist_lib/plist_yaml.py", line 104, in plist_yaml
output = convert(normalized)
^^^^^^^^^^^^^^^^^^^
File "/usr/local/bin/plistyamlplist_lib/plist_yaml.py", line 91, in convert
return dump(xml, width=float("inf"), default_flow_style=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/homeuser/Library/Python/3.11/lib/python/site-packages/ruamel/yaml/main.py", line 1237, in dump
error_deprecation('dump', 'dump', arg="typ='unsafe', pure=True")
File "/Users/homeuser/Library/Python/3.11/lib/python/site-packages/ruamel/yaml/main.py", line 1025, in error_deprecation
raise AttributeError(s, name=None)
AttributeError:
"dump()" has been removed, use

yaml = YAML(typ='unsafe', pure=True)
yaml.dump(...)

instead of file "/usr/local/bin/plistyamlplist_lib/plist_yaml.py", line 91

return dump(xml, width=float("inf"), default_flow_style=False)

RecipeOverrides % plistyamlplist -h
plist-yaml-plist version 0.6.3
Traceback (most recent call last):
File "/usr/local/bin/plistyamlplist", line 319, in
main()
File "/usr/local/bin/plistyamlplist", line 303, in main
if check_if_plist(in_path):
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/bin/plistyamlplist", line 62, in check_if_plist
with open(in_path) as fp:
^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '-h'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.