├── .gitignore ├── LICENSE ├── README.md ├── code ├── munkitap └── munkitaplib │ ├── FoundationPlist │ ├── FoundationPlist.py │ └── __init__.py │ ├── __init__.py │ ├── brew_utils.py │ ├── munki_utils.py │ ├── munkitap_preferences.py │ └── utils.py └── package ├── build-info.plist ├── launchd └── com.github.munkitap.plist └── munkitap_build.sh /.gitignore: -------------------------------------------------------------------------------- 1 | #.gitignore file for jacobfgrant/munki-tap 2 | 3 | 4 | # .DS_Store files 5 | .DS_Store 6 | 7 | # Archives 8 | *.tar 9 | *.zip 10 | 11 | # Munkipkg directories 12 | package/build/ 13 | package/payload/ 14 | 15 | # Packages 16 | *.pkg 17 | 18 | # Python 19 | *.pyc 20 | 21 | # VS Code 22 | .vscode/ 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Munki-Tap 2 | 3 | Munkitap is a tool for automatically building updated, stand-alone pkg installers from formulae in [Homebrew](https://github.com/Homebrew/brew) using Timothy Sutton's [brew-pkg](https://github.com/timsutton/brew-pkg) tool. 4 | 5 | It can also automatically add these packages to a mounted munki repo. 6 | 7 | 8 | ## Installation 9 | 10 | The easiest way to install munkitap is to use the .pkg installers under [Releases](https://github.com/jacobfgrant/munki-tap/releases). You can also using git to clone this repository and either run munkitap directly or use the provided script to build your own pkg. 11 | 12 | 13 | ## Usage 14 | 15 | To use munkitap, use the `add` or `remove` commands to add/remove Homebrew formulae from the list of formulae "on tap." You can build the latest versions of these packages using the `munkitap pour` command. 16 | 17 | More information can be found by running `munkitap --help`. 18 | 19 | 20 | ### Munkitap 21 | ``` 22 | usage: munkitap [-h] [-q | -v] {add,remove,pour,set,list} ... 23 | 24 | Automates building pkg files from brew formulae. 25 | 26 | optional arguments: 27 | -h, --help show this help message and exit 28 | -q, --quiet Run munkitap silently. (Mutally exclusive with 29 | --verbose.) 30 | -v, --verbose Run munkitap with more verbose output. (Mutally 31 | exclusive with --quiet.) 32 | 33 | Munkitap commands: 34 | (command line options for munkitap) 35 | 36 | {add,remove,pour,set,list} 37 | additional help 38 | add Add brew formula to munkitap 39 | remove Remove brew formula from munkitap 40 | pour Pour munkitap formulas (build packages) 41 | set Set munkitap preferences 42 | list Show all formulae in munkitap 43 | ``` 44 | 45 | ##### Munkitap Add 46 | ``` 47 | usage: munkitap add [-h] [-i] formula 48 | 49 | positional arguments: 50 | formula Brew formula to add to munkitap 51 | 52 | optional arguments: 53 | -h, --help show this help message and exit 54 | -i, --install Tells brew to install/upgrade the given formula 55 | ``` 56 | 57 | 58 | ##### Munkitap Remove 59 | 60 | ``` 61 | usage: munkitap remove [-h] [-u] formula 62 | 63 | positional arguments: 64 | formula Brew formula to remove from munkitap 65 | 66 | optional arguments: 67 | -h, --help show this help message and exit 68 | -u, --uninstall Tells brew to uninstall the given formula 69 | ``` 70 | 71 | 72 | ##### Munkitap Pour 73 | 74 | ``` 75 | usage: munkitap pour [-h] [-f] [-id] [-m] 76 | 77 | optional arguments: 78 | -h, --help show this help message and exit 79 | -f, --formula Pour a specific formula 80 | -id, --identifier 81 | Use the specified identifer 82 | -m, --skip-munki Skip importing packages into munki 83 | ``` 84 | 85 | 86 | ##### Munkitap Set 87 | 88 | ``` 89 | usage: munkitap set [-h] [-c] [-id] [-mc] [-mr] [-ms] 90 | 91 | optional arguments: 92 | -h, --help show this help message and exit 93 | -c, --cache Set the munkitap pkg cache location 94 | -id, --identifier 95 | Set the pkg identifier 96 | -mc, --munki-catalog 97 | Set the munki default catalog 98 | -mr, --munki-repo 99 | Set the munki repo path 100 | -ms, --munki-subdir 101 | Set the munki subdirectory for munkitap 102 | ``` 103 | 104 | 105 | ##### Munkitap List 106 | 107 | ``` 108 | usage: munkitap list [-h] [-f] [-i] 109 | 110 | optional arguments: 111 | -h, --help show this help message and exit 112 | -f, --formula Show a specific formula 113 | -i, --info Show formula information 114 | ``` 115 | -------------------------------------------------------------------------------- /code/munkitap: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | Munkitap 5 | 6 | munkitap 7 | 8 | Tool for automating building macOS packages from the latest Homebrew 9 | formulae using Tim Sutton's brew-pkg tool and importing them into munki. 10 | 11 | Requires Homebrew and brew-pkg: 12 | 13 | https://github.com/Homebrew/brew 14 | https://github.com/timsutton/brew-pkg 15 | 16 | 17 | Created by Jacob F. Grant 18 | 19 | Written: 06/9/18 20 | Updated: 10/07/18 21 | """ 22 | 23 | import argparse 24 | import json 25 | import os 26 | import subprocess 27 | 28 | from glob import glob 29 | 30 | from munkitaplib import * 31 | 32 | 33 | BUNDLE_ID = 'com.github.munkitap' 34 | 35 | 36 | # Munkitap functions 37 | 38 | def add_formula(log, formula): 39 | """Add a formula to munkitap""" 40 | on_tap = get_pref('ON_TAP') 41 | if not on_tap: 42 | on_tap = set() 43 | else: 44 | on_tap = set(on_tap) 45 | if formula in on_tap: 46 | log.print_message(formula + " already in munkitap") 47 | else: 48 | on_tap.add(formula) 49 | set_pref('ON_TAP', list(on_tap)) 50 | log.print_message(formula + " added to munkitap") 51 | 52 | 53 | def remove_formula(log, formula): 54 | """Remove a formula from munkitap""" 55 | on_tap = set(get_pref('ON_TAP')) or set() 56 | if formula not in on_tap: 57 | log.print_message(formula + " not in munkitap") 58 | else: 59 | # Remove from formula list 60 | on_tap.remove(formula) 61 | set_pref('ON_TAP', list(on_tap)) 62 | log.print_message(formula + " removed from munkitap") 63 | # Remove from formula info array 64 | formula_info = dict(get_formula_info()) 65 | try: 66 | del formula_info[formula] 67 | log.print_message(formula + " info removed from munkitap") 68 | except KeyError: 69 | pass 70 | set_pref('FORMULA_INFO', formula_info) 71 | 72 | 73 | def update_formula_info(formula): 74 | """Update formula info""" 75 | formula_info = dict(get_formula_info()) 76 | brew_formula_info = subprocess.check_output([get_brew(), 'info', '--json=v1', formula]) 77 | brew_formula_info = json.loads(brew_formula_info)[0] 78 | if formula not in formula_info.keys(): 79 | formula_info[formula] = {} 80 | else: 81 | formula_info[formula] = dict(formula_info[formula]) 82 | formula_info[formula]['description'] = brew_formula_info['desc'] 83 | formula_info[formula]['url'] = brew_formula_info['homepage'] 84 | formula_info[formula]['version'] = brew_formula_info['versions']['stable'] 85 | if 'pkg' not in formula_info[formula].keys(): 86 | formula_info[formula]['pkg'] = '' 87 | set_pref('FORMULA_INFO', formula_info) 88 | 89 | 90 | def update_formula_info_pkg(formula, pkg_path): 91 | """Update pkg key in formula info""" 92 | formula_info = dict(get_formula_info()) 93 | if formula not in formula_info.keys(): 94 | update_formula_info(formula) 95 | formula_info = dict(get_formula_info()) 96 | formula_info[formula] = dict(formula_info[formula]) 97 | formula_info[formula]['pkg'] = pkg_path 98 | set_pref('FORMULA_INFO', formula_info) 99 | 100 | 101 | def pour_formula(log, formula, identifier=None, force_pour=False): 102 | """Pour a formula (build a pkg)""" 103 | formula_info = get_pref('FORMULA_INFO')[formula] 104 | build_path = os.path.join(get_cache(), formula) 105 | if not force_pour: 106 | for pkg in glob( 107 | os.path.join( 108 | build_path, 109 | (formula + '-' + formula_info['version'] + '*.pkg') 110 | ) 111 | ): 112 | if os.path.isfile(pkg): 113 | log.print_message( 114 | (pkg.split('/')[-1] + " already exists"), 115 | (pkg + " already exists (Use the --force command if you wish to rebuild this package)") 116 | ) 117 | return None, None 118 | create_dir(build_path) 119 | log.print_message( 120 | "Building " + (formula + '-' + formula_info['version'] + '.pkg'), 121 | "Building " + (formula + '-' + formula_info['version'] + '.pkg') + " at " + build_path + "/" 122 | ) 123 | if not identifier: 124 | identifier = get_id() 125 | out, err = subprocess.Popen( 126 | [ 127 | get_brew(), 128 | 'pkg', 129 | '--with-dep', 130 | '--identifier-prefix', 131 | identifier, 132 | formula, 133 | ], 134 | stdout=subprocess.PIPE, 135 | stderr=subprocess.PIPE, 136 | cwd=build_path 137 | ).communicate() 138 | return out, err 139 | 140 | 141 | # Parser functions 142 | 143 | def munkitap_add_subparser(subparsers): 144 | """Munkitap 'add' subparser""" 145 | add_command_subparser = subparsers.add_parser( 146 | "add", 147 | help="Add brew formula to munkitap" 148 | ) 149 | add_command_subparser.add_argument( 150 | "formula", 151 | action='store', 152 | type=str, 153 | help="Brew formula to add to munkitap", 154 | ) 155 | add_command_subparser.add_argument( 156 | "-i", 157 | "--install", 158 | action="store_true", 159 | default=False, 160 | help="Tells brew to install/upgrade the given formula" 161 | ) 162 | 163 | 164 | def munkitap_remove_subparser(subparsers): 165 | """Munkitap 'remove' subparser""" 166 | remove_command_subparser = subparsers.add_parser( 167 | "remove", 168 | help="Remove brew formula from munkitap" 169 | ) 170 | remove_command_subparser.add_argument( 171 | "formula", 172 | action='store', 173 | type=str, 174 | help="Brew formula to remove from munkitap", 175 | ) 176 | remove_command_subparser.add_argument( 177 | "-u", 178 | "--uninstall", 179 | action="store_true", 180 | default=False, 181 | help="Tells brew to uninstall the given formula" 182 | ) 183 | 184 | 185 | def munkitap_pour_subparser(subparsers): 186 | """Munkitap 'pour' subparser""" 187 | pour_command_subparser = subparsers.add_parser( 188 | "pour", 189 | help="Pour munkitap formulas (build packages)" 190 | ) 191 | pour_command_subparser.add_argument( 192 | "-f", 193 | "--formula", 194 | action='store', 195 | type=str, 196 | help="Pour a specific formula", 197 | metavar='\b' 198 | ) 199 | pour_command_subparser.add_argument( 200 | "-id", 201 | "--identifier", 202 | action='store', 203 | type=str, 204 | help="Use the specified identifer", 205 | dest='identifier', 206 | metavar='\b' 207 | ) 208 | pour_command_subparser.add_argument( 209 | "-m", 210 | "--skip-munki", 211 | action="store_true", 212 | default=False, 213 | help="Skip importing packages into munki" 214 | ) 215 | 216 | 217 | def munkitap_set_subparser(subparsers): 218 | """Munkitap 'set' subparser""" 219 | set_command_subparser = subparsers.add_parser( 220 | "set", 221 | help="Set munkitap preferences" 222 | ) 223 | set_command_subparser.add_argument( 224 | "-c", 225 | "--cache", 226 | action='store', 227 | type=str, 228 | help="Set the munkitap pkg cache location", 229 | dest='cache', 230 | metavar='\b' 231 | ) 232 | set_command_subparser.add_argument( 233 | "-id", 234 | "--identifier", 235 | action='store', 236 | type=str, 237 | help="Set the pkg identifier", 238 | dest='identifier', 239 | metavar='\b' 240 | ) 241 | set_command_subparser.add_argument( 242 | "-mc", 243 | "--munki-catalog", 244 | action='store', 245 | type=str, 246 | help="Set the munki default catalog", 247 | dest='munki_catalog', 248 | metavar='\b' 249 | ) 250 | set_command_subparser.add_argument( 251 | "-mr", 252 | "--munki-repo", 253 | action='store', 254 | type=str, 255 | help="Set the munki repo path", 256 | dest='munki_repo', 257 | metavar='\b' 258 | ) 259 | set_command_subparser.add_argument( 260 | "-ms", 261 | "--munki-subdir", 262 | action='store', 263 | type=str, 264 | help="Set the munki subdirectory for munkitap", 265 | dest='munki_subdir', 266 | metavar='\b' 267 | ) 268 | 269 | 270 | def munkitap_list_subparser(subparsers): 271 | """Munkitap 'list' subparser""" 272 | list_command_subparser = subparsers.add_parser( 273 | "list", 274 | help="Show all formulae in munkitap" 275 | ) 276 | list_command_subparser.add_argument( 277 | "-f", 278 | "--formula", 279 | action='store', 280 | type=str, 281 | help="Show a specific formula", 282 | metavar='\b' 283 | ) 284 | list_command_subparser.add_argument( 285 | "-i", 286 | "--info", 287 | action="store_true", 288 | default=False, 289 | help="Show formula information", 290 | ) 291 | 292 | 293 | 294 | ## Main function 295 | 296 | def main(): 297 | """Main function""" 298 | # Parse script arguments 299 | main_parser = argparse.ArgumentParser( 300 | description='Automates building pkg files from brew formulae.' 301 | ) 302 | verbosity = main_parser.add_mutually_exclusive_group() 303 | verbosity.add_argument( 304 | '-q', 305 | '--quiet', 306 | action="store_true", 307 | default=False, 308 | help="Run munkitap silently. (Mutally exclusive with --verbose.)", 309 | dest='quiet' 310 | ) 311 | verbosity.add_argument( 312 | '-v', 313 | '--verbose', 314 | action="store_true", 315 | default=False, 316 | help="Run munkitap with more verbose output. (Mutally exclusive with --quiet.)", 317 | dest='verbose' 318 | ) 319 | subparsers = main_parser.add_subparsers( 320 | title='Munkitap commands', 321 | description='(command line options for munkitap)', 322 | help='additional help', 323 | dest='command' 324 | ) 325 | munkitap_add_subparser(subparsers) 326 | munkitap_remove_subparser(subparsers) 327 | munkitap_pour_subparser(subparsers) 328 | munkitap_set_subparser(subparsers) 329 | munkitap_list_subparser(subparsers) 330 | 331 | args = main_parser.parse_args() 332 | munkitap_command = args.command 333 | try: 334 | formula = args.formula 335 | except AttributeError: 336 | pass 337 | 338 | # Create Munkitap Log 339 | munkitap_log = MunkitapLog( 340 | args.quiet, 341 | args.verbose, 342 | os.path.join(get_cache(), 'munkitap.log') 343 | ) 344 | 345 | # Check if sudo/root 346 | if os.geteuid() == 0: 347 | print "Do not run munkitap as root" 348 | exit() 349 | 350 | # Check if homebrew installed 351 | if not brew_installed(munkitap_log): 352 | munkitap_log.log_to_file() 353 | return 354 | 355 | # Add 356 | if munkitap_command == 'add': 357 | if is_brew_formula(munkitap_log, formula): 358 | add_formula(munkitap_log, formula) 359 | update_formula_info(formula) 360 | if args.install: 361 | install_latest_brew_formula(munkitap_log, formula) 362 | munkitap_log.log_to_file() 363 | return 364 | 365 | # Remove 366 | if munkitap_command == 'remove': 367 | remove_formula(munkitap_log, formula) 368 | if args.uninstall: 369 | uninstall_brew_formula(munkitap_log, formula) 370 | munkitap_log.log_to_file() 371 | return 372 | 373 | # Pour 374 | if munkitap_command == 'pour': 375 | if not args.formula and not get_on_tap(): 376 | munkitap_log.print_message("Nothing on tap to pour") 377 | munkitap_log.log_to_file() 378 | return 379 | # Install brew-pkg 380 | install_brew_tap('timsutton/formulae') 381 | install_latest_brew_formula(munkitap_log, 'brew-pkg') 382 | if args.formula: 383 | is_brew_formula(munkitap_log, formula) 384 | if args.formula in get_on_tap(): 385 | install_latest_brew_formula(munkitap_log, formula) 386 | update_formula_info(formula) 387 | out, err = pour_formula( 388 | munkitap_log, 389 | formula, 390 | identifier=args.identifier 391 | ) 392 | if out and not err: 393 | pkg_path = out.split('\n')[-2].split(' ')[-1] 394 | pkg_path = os.path.join( 395 | get_cache(), 396 | formula, 397 | pkg_path 398 | ) 399 | update_formula_info_pkg(formula, pkg_path) 400 | 401 | else: 402 | munkitap_log.print_message(args.formula + " not in munkitap") 403 | munkitap_log.log_to_file() 404 | return 405 | else: 406 | for on_tap_formula in get_on_tap(): 407 | install_latest_brew_formula(munkitap_log, on_tap_formula) 408 | update_formula_info(on_tap_formula) 409 | out, err = pour_formula( 410 | munkitap_log, 411 | on_tap_formula, 412 | identifier=args.identifier 413 | ) 414 | if out and not err: 415 | pkg_path = out.split('\n')[-2].split(' ')[-1] 416 | pkg_path = os.path.join( 417 | get_cache(), 418 | on_tap_formula, 419 | pkg_path 420 | ) 421 | update_formula_info_pkg(on_tap_formula, pkg_path) 422 | 423 | 424 | # Add to munki 425 | if args.skip_munki: 426 | munkitap_log.print_message("Skipping adding to munki") 427 | munkitap_log.log_to_file() 428 | return 429 | if not munkitools_installed(munkitap_log): 430 | munkitap_log.log_to_file() 431 | return 432 | munki_repo = get_munki_repo() 433 | if not os.path.isdir(munki_repo): 434 | munkitap_log.print_message("Munki repo not found") 435 | munkitap_log.log_to_file() 436 | return 437 | 438 | munki_subdir = get_munki_subdir() 439 | munkitap_catalog = get_munki_catalog() 440 | 441 | if not os.path.isfile(os.path.join( 442 | munki_repo, 443 | 'catalogs', 444 | munkitap_catalog 445 | )): 446 | munkitap_log.print_message(munkitap_catalog + " catalog not found in munki") 447 | munkitap_log.log_to_file() 448 | return 449 | 450 | catalog_info = get_all_catalog_info(munki_repo) 451 | already_in_munki = generate_already_in_munki(catalog_info) 452 | formula_info = get_formula_info() 453 | need_makecatalogs = False 454 | 455 | if args.formula: 456 | info = formula_info[args.formula] 457 | pkginfo = get_pkginfo( 458 | munkitap_log, 459 | info['pkg'], 460 | (args.formula[0].upper() + args.formula[1:]), 461 | info['description'], 462 | info['url'], 463 | munkitap_catalog 464 | ) 465 | if not already_in_munki(pkginfo): 466 | need_makecatalogs = True 467 | add_pkg_to_munki( 468 | munkitap_log, 469 | info['pkg'], 470 | pkginfo, 471 | get_munki_repo(), 472 | get_munki_subdir() 473 | ) 474 | else: 475 | munkitap_log.print_message(info['pkg'] + " already in munki") 476 | else: 477 | for on_tap_formula in get_on_tap(): 478 | info = formula_info[on_tap_formula] 479 | pkginfo = get_pkginfo( 480 | munkitap_log, 481 | info['pkg'], 482 | (on_tap_formula[0].upper() + on_tap_formula[1:]), 483 | info['description'], 484 | info['url'], 485 | munkitap_catalog 486 | ) 487 | if not already_in_munki(pkginfo): 488 | need_makecatalogs = True 489 | add_pkg_to_munki( 490 | munkitap_log, 491 | info['pkg'], 492 | pkginfo, 493 | get_munki_repo(), 494 | get_munki_subdir() 495 | ) 496 | else: 497 | munkitap_log.print_message(info['pkg'].split('/')[-1] + " already in munki") 498 | if need_makecatalogs: 499 | run_makecatalogs(munkitap_log, munki_repo) 500 | munkitap_log.log_to_file() 501 | return 502 | 503 | # Set 504 | if munkitap_command == 'set': 505 | if args.cache: 506 | set_cache(args.cache) 507 | munkitap_log.print_message("Setting cache to " + str(args.cache)) 508 | if args.identifier: 509 | set_id(args.identifier) 510 | munkitap_log.print_message("Setting identifier to " + str(args.identifier)) 511 | if args.munki_catalog: 512 | set_munki_catalog(args.munki_catalog) 513 | munkitap_log.print_message("Setting munki catalog to " + str(args.munki_catalog)) 514 | if args.munki_repo: 515 | set_munki_repo(args.munki_repo) 516 | munkitap_log.print_message("Setting munki repo to " + str(args.munki_repo)) 517 | if args.munki_subdir: 518 | set_munki_subdir(args.munki_subdir) 519 | munkitap_log.print_message("Setting munki subdirectory to " + str(args.munki_subdir)) 520 | munkitap_log.log_to_file() 521 | return 522 | 523 | # List 524 | if munkitap_command == 'list': 525 | on_tap_formulae = sorted(set(get_on_tap())) 526 | formula_info = get_formula_info() 527 | if args.formula: 528 | if args.formula in on_tap_formulae: 529 | print args.formula + " is on tap" 530 | if args.info: 531 | for key in sorted(formula_info[formula].keys()): 532 | print " " + key + " = " + formula_info[formula][key] 533 | print 534 | else: 535 | print args.formula + " not on tap" 536 | print "Please add it to munkitap" 537 | else: 538 | for formula in on_tap_formulae: 539 | print formula 540 | if args.info: 541 | for key in sorted(formula_info[formula].keys()): 542 | print " " + key + " = " + formula_info[formula][key] 543 | print 544 | munkitap_log.log_to_file() 545 | return 546 | 547 | 548 | if __name__ == "__main__": 549 | main() 550 | -------------------------------------------------------------------------------- /code/munkitaplib/FoundationPlist/FoundationPlist.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | # 3 | # Copyright 2009-2018 Greg Neagle. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | """FoundationPlist.py -- a tool to generate and parse OS X .plist files. 17 | 18 | This is intended as a drop-in replacement for Python's included plistlib, 19 | with a few caveats: 20 | - readPlist() and writePlist() operate only on a filepath, 21 | not a file object. 22 | - there is no support for the deprecated functions: 23 | readPlistFromResource() 24 | writePlistToResource() 25 | - there is no support for the deprecated Plist class. 26 | 27 | The Property List (.plist) file format is a simple XML pickle supporting 28 | basic object types, like dictionaries, lists, numbers and strings. 29 | Usually the top level object is a dictionary. 30 | 31 | To write out a plist file, use the writePlist(rootObject, filepath) 32 | function. 'rootObject' is the top level object, 'filepath' is a 33 | filename. 34 | 35 | To parse a plist from a file, use the readPlist(filepath) function, 36 | with a file name. It returns the top level object (again, usually a 37 | dictionary). 38 | 39 | To work with plist data in strings, you can use readPlistFromString() 40 | and writePlistToString(). 41 | """ 42 | 43 | # PyLint cannot properly find names inside Cocoa libraries, so issues bogus 44 | # No name 'Foo' in module 'Bar' warnings. Disable them. 45 | # pylint: disable=E0611 46 | from Foundation import NSData 47 | from Foundation import NSPropertyListSerialization 48 | from Foundation import NSPropertyListMutableContainers 49 | from Foundation import NSPropertyListXMLFormat_v1_0 50 | # pylint: enable=E0611 51 | 52 | # Disable PyLint complaining about 'invalid' camelCase names 53 | # pylint: disable=C0103 54 | 55 | 56 | class FoundationPlistException(Exception): 57 | """Basic exception for plist errors""" 58 | pass 59 | 60 | class NSPropertyListSerializationException(FoundationPlistException): 61 | """Read/parse error for plists""" 62 | pass 63 | 64 | class NSPropertyListWriteException(FoundationPlistException): 65 | """Write error for plists""" 66 | pass 67 | 68 | def readPlist(filepath): 69 | """ 70 | Read a .plist file from filepath. Return the unpacked root object 71 | (which is usually a dictionary). 72 | """ 73 | plistData = NSData.dataWithContentsOfFile_(filepath) 74 | dataObject, dummy_plistFormat, error = ( 75 | NSPropertyListSerialization. 76 | propertyListFromData_mutabilityOption_format_errorDescription_( 77 | plistData, NSPropertyListMutableContainers, None, None)) 78 | if dataObject is None: 79 | if error: 80 | error = error.encode('ascii', 'ignore') 81 | else: 82 | error = "Unknown error" 83 | errmsg = "%s in file %s" % (error, filepath) 84 | raise NSPropertyListSerializationException(errmsg) 85 | else: 86 | return dataObject 87 | 88 | 89 | def readPlistFromString(data): 90 | '''Read a plist data from a string. Return the root object.''' 91 | try: 92 | plistData = buffer(data) 93 | except TypeError, err: 94 | raise NSPropertyListSerializationException(err) 95 | dataObject, dummy_plistFormat, error = ( 96 | NSPropertyListSerialization. 97 | propertyListFromData_mutabilityOption_format_errorDescription_( 98 | plistData, NSPropertyListMutableContainers, None, None)) 99 | if dataObject is None: 100 | if error: 101 | error = error.encode('ascii', 'ignore') 102 | else: 103 | error = "Unknown error" 104 | raise NSPropertyListSerializationException(error) 105 | else: 106 | return dataObject 107 | 108 | 109 | def writePlist(dataObject, filepath): 110 | ''' 111 | Write 'rootObject' as a plist to filepath. 112 | ''' 113 | plistData, error = ( 114 | NSPropertyListSerialization. 115 | dataFromPropertyList_format_errorDescription_( 116 | dataObject, NSPropertyListXMLFormat_v1_0, None)) 117 | if plistData is None: 118 | if error: 119 | error = error.encode('ascii', 'ignore') 120 | else: 121 | error = "Unknown error" 122 | raise NSPropertyListSerializationException(error) 123 | else: 124 | if plistData.writeToFile_atomically_(filepath, True): 125 | return 126 | else: 127 | raise NSPropertyListWriteException( 128 | "Failed to write plist data to %s" % filepath) 129 | 130 | 131 | def writePlistToString(rootObject): 132 | '''Return 'rootObject' as a plist-formatted string.''' 133 | plistData, error = ( 134 | NSPropertyListSerialization. 135 | dataFromPropertyList_format_errorDescription_( 136 | rootObject, NSPropertyListXMLFormat_v1_0, None)) 137 | if plistData is None: 138 | if error: 139 | error = error.encode('ascii', 'ignore') 140 | else: 141 | error = "Unknown error" 142 | raise NSPropertyListSerializationException(error) 143 | else: 144 | return str(plistData) 145 | 146 | 147 | if __name__ == '__main__': 148 | print 'This is a support tool original built for Munki.' 149 | -------------------------------------------------------------------------------- /code/munkitaplib/FoundationPlist/__init__.py: -------------------------------------------------------------------------------- 1 | # We're effectively using this package as module 2 | try: 3 | from FoundationPlist import * 4 | except: 5 | print "WARNING: using 'from plistlib import *' instead of 'from FoundationPlist import *' in " + __name__ 6 | from plistlib import * -------------------------------------------------------------------------------- /code/munkitaplib/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | # Munkitap Library 4 | # 5 | # munkitaplib 6 | # 7 | # 8 | # Created by Jacob F. Grant 9 | # 10 | # Written: 06/24/18 11 | # Updated: 07/03/18 12 | # 13 | 14 | """ 15 | munkitaplib 16 | 17 | Library of tools and functions for Munkitap. 18 | """ 19 | 20 | from brew_utils import * 21 | from munki_utils import * 22 | from munkitap_preferences import * 23 | from utils import * 24 | -------------------------------------------------------------------------------- /code/munkitaplib/brew_utils.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | Munkitap Brew Utils 5 | 6 | brew_utils.py 7 | 8 | Functions for working with Homebrew. 9 | 10 | 11 | Created by Jacob F. Grant 12 | 13 | Written: 06/24/18 14 | Updated: 10/07/18 15 | """ 16 | 17 | import json 18 | import os 19 | import subprocess 20 | 21 | from munkitap_preferences import get_brew 22 | from utils import * 23 | 24 | 25 | # Homebrew Functions 26 | 27 | def brew_installed(log): 28 | """Return if brew installed""" 29 | if not get_brew(): 30 | log.print_message("ERROR: brew not found. Please install Homebrew.") 31 | return False 32 | else: 33 | return True 34 | 35 | 36 | def is_brew_formula(log, formula, formula_list=None): 37 | """Check if formula in homebrew""" 38 | if not formula_list: 39 | formula_list = subprocess.check_output([get_brew(), 'search']) 40 | formula_list = formula_list.split('\n')[:-1] 41 | if formula in formula_list: 42 | log.print_message(formula + " in homebrew") 43 | return True 44 | else: 45 | log.print_message(formula + " not in homebrew") 46 | return False 47 | 48 | 49 | def update_brew(): 50 | """Update brew""" 51 | out, err = subprocess.Popen( 52 | [ 53 | get_brew(), 54 | 'update', 55 | ], 56 | stdout=subprocess.PIPE, 57 | stderr=subprocess.PIPE 58 | ).communicate() 59 | return out, err 60 | 61 | 62 | def install_brew_tap(tap): 63 | """Installs a homebrew tap""" 64 | out, err = subprocess.Popen( 65 | [ 66 | get_brew(), 67 | 'tap', 68 | tap, 69 | ], 70 | stdout=subprocess.PIPE, 71 | stderr=subprocess.PIPE 72 | ).communicate() 73 | return out, err 74 | 75 | 76 | def install_latest_brew_formula(log, formula): 77 | """Install or upgrade a brew formula""" 78 | # Check formula info for install/up-to-date status 79 | out, err = subprocess.Popen( 80 | [ 81 | get_brew(), 82 | 'info', 83 | '--json=v1', 84 | formula 85 | ], 86 | stdout=subprocess.PIPE, 87 | stderr=subprocess.PIPE 88 | ).communicate() 89 | # Format JSON output and test for errors 90 | try: 91 | output = json.loads(out)[0] 92 | except TypeError: 93 | log.print_message("Error: " + formula + " not found") 94 | return out, err 95 | # Install if no versions installed 96 | if not output['installed']: 97 | brew_command = 'install' 98 | log.print_message("Installing " + formula) 99 | # Upgrade if outdated 100 | elif output['outdated']: 101 | brew_command = 'upgrade' 102 | log.print_message("Upgrading " + formula) 103 | # Return if installed and up-to-date 104 | else: 105 | log.print_message(formula + " already installed and up-to-date") 106 | return out, err 107 | # Brew install/upgrade 108 | out, err = subprocess.Popen( 109 | [ 110 | get_brew(), 111 | brew_command, 112 | formula, 113 | ], 114 | stdout=subprocess.PIPE, 115 | stderr=subprocess.PIPE 116 | ).communicate() 117 | return out, err 118 | 119 | 120 | def uninstall_brew_formula(log, formula): 121 | """Uninstall a brew formula""" 122 | log.print_message("Uninstalling " + formula) 123 | out, err = subprocess.Popen( 124 | [ 125 | get_brew(), 126 | 'uninstall', 127 | formula, 128 | ], 129 | stdout=subprocess.PIPE, 130 | stderr=subprocess.PIPE 131 | ).communicate() 132 | return out, err 133 | 134 | 135 | if __name__ == "__main__": 136 | pass 137 | -------------------------------------------------------------------------------- /code/munkitaplib/munki_utils.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | Munkitap Munki Utils 5 | 6 | munki_utils.py 7 | 8 | Functions for importing packages into Munki. 9 | 10 | 11 | Created by Jacob F. Grant 12 | 13 | Written: 06/29/18 14 | Updated: 10/07/18 15 | """ 16 | 17 | import os 18 | import shutil 19 | import subprocess 20 | 21 | from FoundationPlist import * 22 | 23 | from utils import * 24 | 25 | 26 | # Functions 27 | 28 | def munkitools_installed(log): 29 | """Return if required munki installed""" 30 | if not os.path.isfile('/usr/local/munki/managedsoftwareupdate'): 31 | log.print_message( 32 | "Munki tools not installed", 33 | "Munki tools not installed (managedsoftwareupdate not found)" 34 | ) 35 | return False 36 | if not os.path.isfile('/usr/local/munki/makepkginfo'): 37 | log.print_message( 38 | "Munki tools not installed", 39 | "Munki tools not installed (makepkginfo not found)" 40 | ) 41 | return False 42 | if not os.path.isfile('/usr/local/munki/makecatalogs'): 43 | log.print_message( 44 | "Munki tools not installed", 45 | "Munki tools not installed (makecatalogs not found)" 46 | ) 47 | return False 48 | return True 49 | 50 | 51 | def get_all_catalog_info(repo_path): 52 | """Return dictionary of all catalog info""" 53 | all_items_path = os.path.join( 54 | repo_path, 55 | 'catalogs', 56 | 'all' 57 | ) 58 | if not os.path.exists(all_items_path): 59 | catalog_info = [] 60 | else: 61 | catalog_info = readPlist(all_items_path) 62 | return catalog_info 63 | 64 | 65 | def generate_already_in_munki(catalog_info): 66 | """Return lambda function for checking if pkginfo exists in munki""" 67 | item_hashes = [] 68 | for item in catalog_info: 69 | try: 70 | item_hashes.append(item['installer_item_hash']) 71 | except KeyError: 72 | pass 73 | return lambda pkginfo : pkginfo['installer_item_hash'] in item_hashes 74 | 75 | 76 | def get_pkginfo(log, pkg, name, desc, url, catalog): 77 | """Return packageinfo file for a pkg""" 78 | log.print_message( 79 | "Generating pkginfo for " + pkg.split('/')[-1], 80 | "Generating pkginfo for " + pkg 81 | ) 82 | description = desc + '.\n\n' + url 83 | out, err = subprocess.Popen( 84 | [ 85 | '/usr/local/munki/makepkginfo', 86 | pkg, 87 | '--displayname', 88 | name, 89 | '--description', 90 | description, 91 | '--catalog', 92 | catalog, 93 | '--category', 94 | 'Munkitap', 95 | '--developer', 96 | 'Homebrew' 97 | ], 98 | stdout=subprocess.PIPE, 99 | stderr=subprocess.PIPE 100 | ).communicate() 101 | if not err: 102 | return readPlistFromString(out) 103 | else: 104 | return None 105 | 106 | 107 | def add_pkg_to_munki(log, pkg, pkginfo, repo_path, repo_subdirectory): 108 | """Adds a pkg to munki""" 109 | pkginfo_dest_path = os.path.join(repo_path, 'pkgsinfo', repo_subdirectory) 110 | pkg_dest_path = os.path.join(repo_path, 'pkgs', repo_subdirectory) 111 | create_dir(pkginfo_dest_path) 112 | create_dir(pkg_dest_path) 113 | name = "%s-%s" % ( 114 | pkginfo["name"], 115 | pkginfo["version"].strip() 116 | ) 117 | pkginfo_path = os.path.join(pkginfo_dest_path, name) 118 | pkg_path = os.path.join(pkg_dest_path, (name + '.pkg')) 119 | index = 0 120 | while os.path.exists(pkg_path) or os.path.exists(pkginfo_path): 121 | index += 1 122 | name = "%s-%s__%s" % ( 123 | pkginfo['name'], 124 | pkginfo['version'], 125 | index 126 | ) 127 | pkginfo_path = os.path.join(pkginfo_dest_path, name) 128 | pkg_path = os.path.join(pkg_dest_path, (name + '.pkg')) 129 | log.print_message( 130 | "Copying pkginfo for " + name + " to munki", 131 | "Copying pkginfo for " + name + " to " + pkginfo_path 132 | ) 133 | log.print_message( 134 | "Copying " + name + ".pkg to munki", 135 | "Copying " + name + ".pkg to " + pkg_path 136 | ) 137 | 138 | # Set installer_item_location 139 | pkginfo = dict(pkginfo) 140 | pkginfo['installer_item_location'] = os.path.join(repo_subdirectory, (name + '.pkg')) 141 | writePlist(pkginfo, pkginfo_path) 142 | shutil.copyfile(pkg, pkg_path) 143 | return pkginfo_path, pkg_path 144 | 145 | 146 | def run_makecatalogs(log, repo_path): 147 | """Run munki makecatalogs""" 148 | log.print_message("Running makecatalogs") 149 | out, err = subprocess.Popen( 150 | [ 151 | '/usr/local/munki/makecatalogs', 152 | repo_path 153 | ], 154 | stdout=subprocess.PIPE, 155 | stderr=subprocess.PIPE 156 | ).communicate() 157 | return out, err 158 | 159 | 160 | if __name__ == "__main__": 161 | pass 162 | -------------------------------------------------------------------------------- /code/munkitaplib/munkitap_preferences.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | Munkitap Preferences 5 | 6 | munkitap_preferences.py 7 | 8 | Functions for getting and setting Munkitap preferences. 9 | 10 | 11 | Created by Jacob F. Grant 12 | 13 | Written: 06/24/18 14 | Updated: 10/07/18 15 | """ 16 | 17 | import os 18 | import subprocess 19 | 20 | # Disable Pylint warnings 21 | # pylint: disable=E0611 22 | from CoreFoundation import CFPreferencesAppSynchronize, \ 23 | CFPreferencesCopyAppValue, \ 24 | CFPreferencesSetAppValue 25 | # pylint: enable=E0611 26 | 27 | from utils import * 28 | 29 | 30 | BUNDLE_ID = 'com.github.munkitap' 31 | 32 | 33 | ### Exceptions 34 | 35 | class PreferenceError(Exception): 36 | """Preference exception""" 37 | pass 38 | 39 | 40 | ### Low-level prefs API 41 | 42 | def get_pref(key, domain=BUNDLE_ID): 43 | """Get preference value""" 44 | return CFPreferencesCopyAppValue(key, domain) 45 | 46 | 47 | def set_pref(key, value, domain=BUNDLE_ID): 48 | """Set preference value""" 49 | try: 50 | CFPreferencesSetAppValue(key, value, domain) 51 | if not CFPreferencesAppSynchronize(domain): 52 | raise PreferenceError( 53 | "Could not synchronize %s preference: %s" % key) 54 | except Exception, err: 55 | raise PreferenceError( 56 | "Could not set %s preference: %s" % (key, err)) 57 | 58 | 59 | ### High-level prefs APIs 60 | 61 | # Get functions 62 | 63 | def get_brew(): 64 | """Get brew path""" 65 | brew_path = get_pref('BREW_PATH') 66 | if not brew_path or not os.path.isfile(brew_path): 67 | try: 68 | brew_path = subprocess.check_output(['which', 'brew']).split('\n')[0] 69 | set_pref('BREW_PATH', brew_path) 70 | except subprocess.CalledProcessError: 71 | pass 72 | return brew_path 73 | 74 | 75 | def get_cache(): 76 | """Get munkitap cache""" 77 | cache = get_pref('CACHE') 78 | if not cache: 79 | cache = os.path.join(os.getenv("HOME"), 'Library/Munkitap/') 80 | set_pref('CACHE', cache) 81 | create_dir(cache) 82 | return cache 83 | 84 | 85 | def get_formula_info(): 86 | """Get formula info""" 87 | formula_info = get_pref('FORMULA_INFO') 88 | if not formula_info: 89 | formula_info = {} 90 | set_pref('FORMULA_INFO', formula_info) 91 | return formula_info 92 | 93 | 94 | def get_id(): 95 | """Get package identifier""" 96 | identifier = get_pref('IDENTIFIER') 97 | if not identifier: 98 | identifier = 'com.munkitap' 99 | set_pref('IDENTIFIER', identifier) 100 | return identifier 101 | 102 | 103 | def get_munki_catalog(): 104 | """Get Munki catalog""" 105 | munki_catalog = get_pref('MUNKI_CATALOG') 106 | if not munki_catalog: 107 | munki_catalog = 'testing' 108 | set_pref('MUNKI_CATALOG', munki_catalog) 109 | return munki_catalog 110 | 111 | 112 | def get_munki_repo(): 113 | """Get Munki repo path""" 114 | munki_repo = get_pref('MUNKI_REPO') 115 | if not munki_repo: 116 | munki_repo = '' 117 | set_pref('MUNKI_REPO', munki_repo) 118 | return munki_repo 119 | 120 | 121 | def get_munki_subdir(): 122 | """Get Munki subdirectory for munkitap""" 123 | munki_subdir = get_pref('MUNKI_SUBDIR') 124 | if not munki_subdir: 125 | munki_subdir = 'munkitap' 126 | set_pref('MUNKI_SUBDIR', munki_subdir) 127 | return munki_subdir 128 | 129 | 130 | def get_on_tap(): 131 | """Get formulas 'on tap'""" 132 | on_tap = get_pref('ON_TAP') 133 | if not on_tap: 134 | on_tap = [] 135 | set_pref('ON_TAP', on_tap) 136 | return on_tap 137 | 138 | 139 | # Set functions 140 | 141 | def set_cache(cache): 142 | """Set cache location""" 143 | cache = os.path.expanduser(cache) 144 | if os.path.isfile(cache): 145 | print "ERROR: " + cache + " already exists as a file" 146 | return 147 | set_pref('CACHE', cache) 148 | 149 | 150 | def set_id(identifier): 151 | """Set identifier""" 152 | set_pref('IDENTIFIER', identifier) 153 | 154 | 155 | def set_munki_catalog(munki_catalog): 156 | """Set Munki catalog""" 157 | set_pref('MUNKI_CATALOG', munki_catalog) 158 | 159 | 160 | def set_munki_repo(munki_repo): 161 | """Set Munki repo path""" 162 | set_pref('MUNKI_REPO', munki_repo) 163 | 164 | 165 | def set_munki_subdir(munki_subdir): 166 | """Set Munki subdirectory for munkitap""" 167 | set_pref('MUNKI_SUBDIR', munki_subdir) 168 | 169 | 170 | # Testing Preferences 171 | 172 | def initialize_preferences(): 173 | """Create preferences""" 174 | # Brew 175 | print get_brew() 176 | # Cache 177 | print get_cache() 178 | # Formula Info 179 | print get_formula_info() 180 | # Munki 181 | get_munki_repo() 182 | # On Tap 183 | print get_on_tap() 184 | 185 | 186 | if __name__ == "__main__": 187 | initialize_preferences() 188 | -------------------------------------------------------------------------------- /code/munkitaplib/utils.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | Munkitap Utils 5 | 6 | utils.py 7 | 8 | Basic functions and utilities for Munkitap. 9 | 10 | 11 | Created by Jacob F. Grant 12 | 13 | Written: 07/02/18 14 | Updated: 10/07/18 15 | """ 16 | 17 | from datetime import datetime 18 | import os 19 | 20 | 21 | # Functions 22 | 23 | def create_dir(path): 24 | """Create a directory with error handling""" 25 | try: 26 | os.makedirs(path) 27 | except OSError: 28 | if not os.path.isdir(path): 29 | raise 30 | 31 | 32 | # Classes 33 | 34 | class MunkitapLog(object): 35 | 36 | def __init__(self, quiet, verbose, logfile=None): 37 | self.quiet = quiet 38 | self.verbose = verbose 39 | self.logfile = logfile 40 | self._log_message("\nBeginning munkitap run at " + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) 41 | 42 | 43 | def _log_message(self, message): 44 | if self.logfile: 45 | with open(self.logfile, 'a+') as logfile: 46 | logfile.write("%s\n" % message) 47 | 48 | 49 | def print_message(self, message, verbose_message=None): 50 | if self.verbose and verbose_message: 51 | self._log_message(verbose_message) 52 | print verbose_message 53 | else: 54 | self._log_message(message) 55 | if not self.quiet: 56 | print message 57 | 58 | 59 | def log_to_file(self): 60 | self._log_message("Concluding munkitap run at " + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) 61 | 62 | 63 | if __name__ == "__main__": 64 | pass 65 | -------------------------------------------------------------------------------- /package/build-info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | distribution_style 6 | 7 | identifier 8 | com.github.munkitap.pkg 9 | install_location 10 | / 11 | name 12 | munkitap-${version}.pkg 13 | ownership 14 | recommended 15 | postinstall_action 16 | none 17 | suppress_bundle_relocation 18 | 19 | version 20 | 0.3.0 21 | 22 | 23 | -------------------------------------------------------------------------------- /package/launchd/com.github.munkitap.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Label 6 | com.github.munkitap 7 | ProgramArguments 8 | 9 | /usr/local/bin/munkitap 10 | -q 11 | pour 12 | 13 | RunAtLoad 14 | 15 | StartInterval 16 | 3600 17 | 18 | 19 | -------------------------------------------------------------------------------- /package/munkitap_build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Munkitap Pkg Build Script 4 | # 5 | # munkitap_build.sh 6 | # 7 | # 8 | # Created by Jacob F. Grant 9 | # 10 | # Written: 06/28/18 11 | # Updated: 10/07/18 12 | # 13 | 14 | 15 | # Check for requirements 16 | MUNKIPKG=$(which munkipkg) 17 | 18 | if [ ! -e "$MUNKIPKG" ] 19 | then 20 | echo 'munkipkg not found' 21 | exit 1 22 | fi 23 | 24 | if [ ! -f "build-info.plist" ] 25 | then 26 | echo 'build-info.plist not found' 27 | exit 2 28 | fi 29 | 30 | if [ ! -f "../code/munkitap" ] || [ ! -d "../code/munkitaplib" ] 31 | then 32 | echo 'munkitap files not found' 33 | exit 3 34 | fi 35 | 36 | # Create payload directory 37 | if [ -d "./payload" ] 38 | then 39 | rm -r ./payload 40 | fi 41 | mkdir -p ./payload/Library/Munkitap/ 42 | mkdir -p ./payload/Library/LaunchAgents/ 43 | mkdir -p ./payload/usr/local/bin/ 44 | 45 | # Copy munkitap files 46 | cp ../code/munkitap ./payload/Library/Munkitap/ 47 | cp -r ../code/munkitaplib ./payload/Library/Munkitap/ 48 | cp ./launchd/com.github.munkitap.plist ./payload/Library/LaunchAgents/ 49 | 50 | # Create symbolic link in /usr/local/bin/ 51 | ln -s /Library/Munkitap/munkitap ./payload/usr/local/bin/munkitap 52 | 53 | # Build package 54 | "$MUNKIPKG" . 55 | --------------------------------------------------------------------------------