-
Notifications
You must be signed in to change notification settings - Fork 4
/
models.py
218 lines (195 loc) · 8.34 KB
/
models.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
pkgsinfo/models.py
"""
from django.db import models
import os
import logging
import plistlib
import functools
from collections import defaultdict
from distutils.version import LooseVersion
from multiprocessing.pool import ThreadPool
from xml.parsers.expat import ExpatError
from django.conf import settings
from process.utils import record_status
from catalogs.models import Catalog
from api.models import MunkiRepo, \
FileReadError, FileWriteError, FileDeleteError
PKGSINFO_STATUS_TAG = 'pkgsinfo_list_process'
LOGGER = logging.getLogger('munkiwebadmin')
def pkg_ref_count(pkginfo_path, catalog_items):
'''Returns the number of pkginfo items containing a reference to
the installer_item_location in pkginfo_path and the relative path to
the installer_item_location'''
try:
plistdata = MunkiRepo.read('pkgsinfo', pkginfo_path)
except FileReadError:
return 0, ''
pkg_path = plistdata.get('installer_item_location')
if not pkg_path:
return 0, ''
matching_count = 0
if catalog_items:
matches = [item for item in catalog_items
if item.get('installer_item_location') == pkg_path]
matching_count = len(matches)
return matching_count, pkg_path
def process_file(pkginfo_path):
'''Worker function called by our multiprocessing pool. Reads a pkginfo
file and returns a tuple of name, version, catalogs, and relative path'''
try:
pkginfo = MunkiRepo.read('pkgsinfo', pkginfo_path)
except (ExpatError, IOError):
return ()
return (pkginfo.get('name', 'NO_NAME'),
pkginfo.get('display_name', 'NO_DISPLAY_NAME'),
pkginfo.get('version', 'NO_VERSION'),
pkginfo.get('catalogs', []),
pkginfo_path)
def any_files_in_list_newer_than(files, filepath):
'''Returns true if any file in the list of files
is newer that filepath'''
# todo: needs to be fixed
'''try:
mtime = os.stat(filepath).st_mtime
except OSError:
return True
for fname in files:
fullpath = os.path.join(PKGSINFO_PATH, fname)
if os.stat(fullpath).st_mtime > mtime:
return True'''
return False
def record(message=None, percent_done=None):
'''Record a status message for a long-running process'''
record_status(
PKGSINFO_STATUS_TAG, message=message, percent_done=percent_done)
class PkginfoFile(models.Model):
'''Placeholder so we get permissions entries in the admin database'''
pass
class Pkginfo(MunkiRepo):
'''Models pkginfo items'''
@classmethod
def data(cls):
'''Returns a structure with itemnames, versions, and filepaths'''
def cmp(a, b):
try:
return (a > b) - (a < b)
except TypeError:
return 1
def compare_versions(a, b):
"""Internal comparison function for use in sorting"""
return cmp(LooseVersion(b[0]), LooseVersion(a[0]))
record(message='Starting scan of pkgsinfo data')
files = cls.list('pkgsinfo')
record(message='Processing %s files' % len(files))
all_items = cls.read('catalogs', 'all')
use_slower_approach = False
if len(all_items) != len(files):
LOGGER.debug('number of files differ from all catalog')
use_slower_approach = True
#elif any_files_in_list_newer_than(files, all_catalog):
# LOGGER.debug('files newer than all catalog')
# use_slower_approach = True
pkginfo_dict = defaultdict(list)
record(message='Assembling pkgsinfo data')
if use_slower_approach:
LOGGER.debug("using slower approach")
# read the individual pkgsinfo files but use four threads
# to speed things up a bit since we wait a lot for I/O
pool = ThreadPool(processes=4)
tuples = pool.map(process_file, files)
for name, display_name, version, catalogs, pathname in tuples:
pkginfo_dict[name].append((version, catalogs, pathname, display_name))
else:
LOGGER.debug("using faster approach")
# use the data in the all catalog; one file read instead
# of hundreds or thousands
for index, item in enumerate(all_items):
name = item.get('name', 'NO_NAME')
version = item.get('version', 'NO_VERSION')
catalogs = item.get('catalogs', [])
pathname = files[index]
pkginfo_dict[name].append((version, catalogs, pathname))
for key in pkginfo_dict.keys():
pkginfo_dict[key].sort(key=functools.cmp_to_key(compare_versions))
LOGGER.debug('Sorted pkgsinfo dict')
# now convert to a list of lists
pkginfo_list = []
for key, value in pkginfo_dict.items():
pkginfo_list.append([key, value])
LOGGER.debug('Converted to tuple')
record(message='Completed assembly of pkgsinfo data')
return pkginfo_list
@classmethod
def mass_delete(cls, pathname_list, user, delete_pkgs=False):
'''Deletes pkginfo files from a list and optionally deletes the
associated installer items (pkgs)'''
errors = []
catalog_items = []
if delete_pkgs:
# cache the catalog_items once; we don't want to incur the
# overhead of reading the 'all' catalog multiple times
catalog_items = Catalog.detail('all')
for pathname in pathname_list:
delete_this_pkg = False
if delete_pkgs:
ref_count, pkg_path = pkg_ref_count(pathname, catalog_items)
if ref_count == 1:
# OK to delete the pkg if there is only one pkginfo
# file that refers to it
delete_this_pkg = True
try:
cls.delete('pkgsinfo', pathname, user)
except FileDeleteError as err:
errors.append('Error %s when removing %s' % (err, pathname))
else:
if delete_this_pkg:
try:
MunkiRepo.delete('pkgs', pkg_path)
except FileDeleteError as err:
errors.append('Error %s when removing %s'
% (err, pkg_path))
if errors:
raise FileDeleteError(errors)
@classmethod
def mass_edit_catalogs(
cls, pathname_list, catalogs_to_add, catalogs_to_remove, user):
'''For all pkginfo items in the list, add and remove catalogs'''
errors = []
# normalize the catalog lists -- no duplicates; eliminate
# any items that are in both lists
normalized_catalogs_to_add = (
set(catalogs_to_add) - set(catalogs_to_remove))
normalized_catalogs_to_remove = (
set(catalogs_to_remove)- set(catalogs_to_add))
catalogs_to_add = list(normalized_catalogs_to_add)
catalogs_to_remove = list(normalized_catalogs_to_remove)
for pathname in pathname_list:
try:
plist = cls.read('pkgsinfo', pathname)
except FileReadError:
errors.append('Could not read %s' % pathname)
continue
if not 'catalogs' in plist:
plist['catalogs'] = []
# what will be added?
new_catalogs = [item for item in catalogs_to_add
if item not in plist['catalogs']]
# what will be removed?
removed_catalogs = [item for item in catalogs_to_remove
if item in plist['catalogs']]
if new_catalogs or removed_catalogs:
# add the new ones
plist['catalogs'].extend(new_catalogs)
# remove catalogs to remove
plist['catalogs'] = [item for item in plist['catalogs']
if item not in catalogs_to_remove]
data = plistlib.writePlistToString(plist)
try:
cls.write(data, 'pkgsinfo', pathname, user)
except FileWriteError as err:
LOGGER.error('Update failed for %s: %s', pathname, err)
errors.append('Error %s when updating %s' % (err, pathname))
continue
if errors:
raise FileWriteError(errors)