/
msclib.py
136 lines (117 loc) · 4.79 KB
/
msclib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# encoding: utf-8
#
# msclib.py
#
# Created by Greg Neagle on 12/10/13.
# Copyright 2010-2014 Greg Neagle.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''Some functions used a few places that don't (yet) have an obvious home'''
import os
import sys
import shutil
from zipfile import ZipFile, BadZipfile
from Foundation import *
from AppKit import *
import msclog
import munki
_html_dir = None
def updateCountMessage(count):
'''Return a localized message describing the count of updates to install'''
if count == 0:
return NSLocalizedString(u"No pending updates", u"No Updates message")
if count == 1:
return NSLocalizedString(u"1 pending update", u"One Update message")
else:
return (NSLocalizedString(u"%s pending updates", u"Multiple Updates message") % count)
def getInstallAllButtonTextForCount(count):
'''Return localized display text for action button in Updates view'''
if count == 0:
return NSLocalizedString(u"Check Again", u"Check Again button title")
elif count == 1:
return NSLocalizedString(u"Update", u"Update button title/action text")
else:
return NSLocalizedString(u"Update All", u"Update All button title")
def get_custom_resources():
'''copies custom resources into our html dir'''
if not _html_dir:
return
managed_install_dir = munki.pref('ManagedInstallDir')
source_path = os.path.join(managed_install_dir, 'client_resources/custom.zip')
if os.path.exists(source_path):
dest_path = os.path.join(_html_dir, 'custom')
if os.path.exists(dest_path):
try:
shutil.rmtree(dest_path, ignore_errors=True)
except (OSError, IOError), err:
msclog.debug_log('Error clearing %s: %s' % (dest_path, err))
if not os.path.exists(dest_path):
try:
os.mkdir(dest_path)
except (OSError, IOError), err:
msclog.debug_log('Error creating %s: %s' % (dest_path, err))
try:
archive = ZipFile(source_path)
except BadZipfile:
# ignore it
return
archive_files = archive.namelist()
# sanity checking in case the archive is not built correctly
files_to_extract = [filename for filename in archive_files
if filename.startswith('resources/')
or filename.startswith('templates/')]
if not files_to_extract:
msclog.debug_log('Invalid client resources archive.')
for filename in files_to_extract:
try:
if filename.endswith('/'):
# it's a directory. The extract method in Python 2.6 handles this wrong
# do we'll do it ourselves
os.makedirs(os.path.join(dest_path, filename))
else:
archive.extract(filename, dest_path)
except (OSError, IOError), err:
msclog.debug_log('Error expanding %s from archive: %s' % (filename, err))
def html_dir():
'''sets up our local html cache directory'''
global _html_dir
if _html_dir:
return _html_dir
bundle_id = NSBundle.mainBundle().bundleIdentifier()
cache_dir_urls = NSFileManager.defaultManager().URLsForDirectory_inDomains_(
NSCachesDirectory, NSUserDomainMask)
if cache_dir_urls:
cache_dir = cache_dir_urls[0].path()
else:
cache_dir = u'/private/tmp'
our_cache_dir = os.path.join(cache_dir, bundle_id)
if not os.path.exists(our_cache_dir):
os.makedirs(our_cache_dir)
_html_dir = os.path.join(our_cache_dir, 'html')
if os.path.exists(_html_dir):
# empty it
shutil.rmtree(_html_dir)
os.mkdir(_html_dir)
# symlink our static files dir
resourcesPath = NSBundle.mainBundle().resourcePath()
source_path = os.path.join(resourcesPath, 'WebResources')
link_path = os.path.join(_html_dir, 'static')
os.symlink(source_path, link_path)
# symlink the Managed Installs icons dir
managed_install_dir = munki.pref('ManagedInstallDir')
source_path = os.path.join(managed_install_dir, 'icons')
link_path = os.path.join(_html_dir, 'icons')
os.symlink(source_path, link_path)
# unzip any custom client resources
get_custom_resources()
return _html_dir