Move many functions shared by the various cli tools to munkilib/cliutils.py

This commit is contained in:
Greg Neagle
2017-03-13 08:03:23 -07:00
parent 553eb503e9
commit e042a2c8d5
5 changed files with 275 additions and 374 deletions
+18 -99
View File
@@ -22,21 +22,18 @@ Created by Greg Neagle on 2010-09-29.
Assists with importing installer items into the munki repo
"""
import ctypes
from ctypes.util import find_library
import os
import readline
import subprocess
import sys
import tempfile
import time
import thread
import urllib
import urlparse
from optparse import OptionParser, BadOptionError, AmbiguousOptionError
from munkilib.cliutils import ConfigurationSaveError
from munkilib.cliutils import configure as _configure
from munkilib.cliutils import pref, path2url
from munkilib.cliutils import raw_input_with_default
from munkilib import iconutils
from munkilib import info
from munkilib import display
@@ -48,60 +45,6 @@ from munkilib import pkgutils
from munkilib import FoundationPlist
# PyLint cannot properly find names inside Cocoa libraries, so issues bogus
# No name 'Foo' in module 'Bar' warnings. Disable them.
# pylint: disable=E0611
from Foundation import CFPreferencesAppSynchronize
from Foundation import CFPreferencesCopyAppValue
from Foundation import CFPreferencesSetAppValue
# pylint: enable=E0611
if 'libedit' in readline.__doc__:
# readline module was compiled against libedit
LIBEDIT = ctypes.cdll.LoadLibrary(find_library('libedit'))
else:
LIBEDIT = None
def raw_input_with_default(prompt, default_text):
'''Get input from user with a prompt and a suggested default value'''
# 10.6's libedit doesn't have the rl_set_prompt function, so we fall back
# to the previous behavior
if osutils.getOsVersion() == '10.6':
if default_text:
prompt = '%s [%s]: ' % (prompt.rstrip(': '), default_text)
return (unicode(raw_input(prompt), encoding=sys.stdin.encoding) or
unicode(default_text))
else:
# no default value, just call raw_input
return unicode(raw_input(prompt), encoding=sys.stdin.encoding)
# A nasty, nasty hack to get around Python readline limitations under
# OS X. Gives us editable default text for munkiimport choices'''
def insert_default_text(prompt, text):
'''Helper function'''
time.sleep(0.01)
LIBEDIT.rl_set_prompt(prompt)
readline.insert_text(text)
LIBEDIT.rl_forced_update_display()
readline.clear_history()
if not default_text:
return unicode(raw_input(prompt), encoding=sys.stdin.encoding)
elif LIBEDIT:
# readline module was compiled against libedit
thread.start_new_thread(insert_default_text, (prompt, default_text))
return unicode(raw_input(), encoding=sys.stdin.encoding)
else:
readline.set_startup_hook(lambda: readline.insert_text(default_text))
try:
return unicode(raw_input(prompt), encoding=sys.stdin.encoding)
finally:
readline.set_startup_hook()
class PassThroughOptionParser(OptionParser):
"""
An unknown option pass-through implementation of OptionParser.
@@ -728,45 +671,21 @@ def cleanup_and_exit(exitcode):
exit(exitcode or result)
BUNDLE_ID = 'com.googlecode.munki.munkiimport'
def pref(prefname):
"""Return a preference. Since this uses CFPreferencesCopyAppValue,
Preferences can be defined several places. Precedence is:
- MCX/Configuration Profile
- ~/Library/Preferences/ByHost/com.googlecode.munki.munkiimport.XX.plist
- ~/Library/Preferences/com.googlecode.munki.munkiimport.plist
- /Library/Preferences/com.googlecode.munki.munkiimport.plist
"""
return CFPreferencesCopyAppValue(prefname, BUNDLE_ID)
def configure():
"""Configures munkiimport for use"""
_prefs = {}
for (key, prompt) in [
#('repo_path', 'Path to munki repo (example: /Volumes/repo)'),
('repo_url',
'Repo URL (example: afp://munki.example.com/repo)'),
('pkginfo_extension', 'pkginfo extension (Example: .plist)'),
('editor',
'pkginfo editor (examples: /usr/bin/vi or TextMate.app; '
'leave empty to not open an editor after import)'),
('default_catalog', 'Default catalog to use (example: testing)'),
('plugin', 'Repo access plugin (defaults to FileRepo)')]:
_prefs[key] = raw_input_with_default('%15s: ' % prompt, pref(key))
for key, value in _prefs.items():
try:
CFPreferencesSetAppValue(key, value, BUNDLE_ID)
except BaseException:
print >> sys.stderr, 'Could not save configuration!'
finally:
CFPreferencesAppSynchronize(BUNDLE_ID)
def path2url(path):
'''Converts a path to a file: url'''
return urlparse.urljoin('file:', urllib.pathname2url(path))
prompt_list = [
#('repo_path', 'Path to munki repo (example: /Volumes/repo)'),
('repo_url', 'Repo URL (example: afp://munki.example.com/repo)'),
('pkginfo_extension', 'pkginfo extension (Example: .plist)'),
('editor', 'pkginfo editor (examples: /usr/bin/vi or TextMate.app; '
'leave empty to not open an editor after import)'),
('default_catalog', 'Default catalog to use (example: testing)'),
('plugin', 'Repo access plugin (defaults to FileRepo)')
]
try:
_configure(prompt_list)
except ConfigurationSaveError:
pass
def main():