~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to setup/util.py

  • Committer: William Grant
  • Date: 2012-06-28 01:52:02 UTC
  • Revision ID: me@williamgrant.id.au-20120628015202-f6ru7o367gt6nvgz
Hah

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
__all__ = ['PYTHON_VERSION', 'copy_file_to_jail', 'RunError',
35
35
           'action_runprog', 'action_remove', 'action_rename', 'action_mkdir',
36
36
           'action_copytree', 'action_copylist', 'action_copyfile',
37
 
           'action_symlink', 'action_append', 'action_chown',
 
37
           'action_symlink', 'action_chown',
38
38
           'action_chown_setuid', 'action_chmod_x', 'action_make_private',
39
 
           'query_user', 'filter_mutate', 'get_svn_revision', 'InstallList',
40
 
           'wwwuid']
 
39
           'filter_mutate', 'get_svn_revision', 'InstallList',
 
40
           'make_install_path', 'wwwuid']
41
41
 
42
42
# Determine which Python version (2.4 or 2.5, for example) we are running,
43
43
# and use that as the filename to the Python directory.
195
195
    if not dry:
196
196
        os.symlink(src, dst)
197
197
 
198
 
def action_append(ivle_pth, ivle_www):
199
 
    file = open(ivle_pth, 'a+')
200
 
    file.write(ivle_www + '\n')
201
 
    file.close()
202
 
 
203
198
def action_chown_setuid(file, dry):
204
199
    """Chowns a file to root, and sets the setuid bit on the file.
205
200
    Calling this function requires the euid to be root.
235
230
    if not dry:
236
231
        os.chmod(file, stat.S_IRUSR | stat.S_IWUSR)
237
232
 
238
 
def query_user(default, prompt):
239
 
    """Prompts the user for a string, which is read from a line of stdin.
240
 
    Exits silently if EOF is encountered. Returns the string, with spaces
241
 
    removed from the beginning and end.
242
 
 
243
 
    Returns default if a 0-length line (after spaces removed) was read.
244
 
    """
245
 
    if default is None:
246
 
        # A default of None means the value will be computed specially, so we
247
 
        # can't really tell you what it is
248
 
        defaultstr = "computed"
249
 
    elif isinstance(default, basestring):
250
 
        defaultstr = '"%s"' % default
251
 
    else:
252
 
        defaultstr = repr(default)
253
 
    sys.stdout.write('%s\n    (default: %s)\n>' % (prompt, defaultstr))
254
 
    try:
255
 
        val = sys.stdin.readline()
256
 
    except KeyboardInterrupt:
257
 
        # Ctrl+C
258
 
        sys.stdout.write("\n")
259
 
        sys.exit(1)
260
 
    sys.stdout.write("\n")
261
 
    # If EOF, exit
262
 
    if val == '': sys.exit(1)
263
 
    # If empty line, return default
264
 
    val = val.strip()
265
 
    if val == '': return default
266
 
    return val
267
 
 
268
233
def filter_mutate(function, list):
269
234
    """Like built-in filter, but mutates the given list instead of returning a
270
235
    new one. Returns None."""
291
256
# Mime types which will automatically be placed in the list by InstallList.
292
257
installlist_mimetypes = ['text/x-python', 'text/html',
293
258
    'application/x-javascript', 'application/javascript',
294
 
    'text/css', 'image/png', 'image/gif', 'application/xml']
 
259
    'text/css', 'image/png', 'image/gif', 'application/xml', 'text/plain',
 
260
    'text/x-sh']
 
261
# Filenames which will automatically be placed in the list by InstallList.
 
262
whitelist_filenames = ['ivle-spec.conf']
295
263
 
296
264
def build_list_py_files(dir, no_top_level=False):
297
265
    """Builds a list of all py files found in a directory and its
305
273
        filter_mutate(lambda x: x[0] != '.', dirnames)
306
274
        # All *.py files are added to the list
307
275
        pylist += [os.path.join(dirpath, item) for item in filenames
308
 
            if mimetypes.guess_type(item)[0] in installlist_mimetypes]
 
276
            if mimetypes.guess_type(item)[0] in installlist_mimetypes or
 
277
               item in whitelist_filenames]
309
278
    if no_top_level:
310
279
        for i in range(0, len(pylist)):
311
280
            _, pylist[i] = pylist[i].split(os.sep, 1)
312
281
    return pylist
313
282
 
 
283
def make_install_path(rootdir, path):
 
284
    '''Combine an installation root directory and final install path.
 
285
 
 
286
    Normalises path, and joins it to the end of rootdir, removing the leading
 
287
    / to make it relative if required.
 
288
    '''
 
289
    normpath = os.path.normpath(path)
 
290
    if normpath.startswith(os.sep):
 
291
        normpath = normpath[1:]
 
292
    return os.path.join(rootdir, normpath)
 
293
 
314
294
class InstallList(object):
315
 
    # We build two separate lists, by walking www and console
316
 
    list_www = property(lambda self: build_list_py_files('www'))
317
 
 
318
295
    list_ivle_lib = property(lambda self: build_list_py_files('ivle'))
319
296
 
320
297
    list_subjects = property(lambda self: build_list_py_files('subjects',
327
304
        "services/python-console",
328
305
        "services/fileservice",
329
306
        "services/serveservice",
330
 
        "services/interpretservice",
331
307
        "services/usrmgt-server",
332
308
        "services/diffservice",
333
309
        "services/svnlogservice",
 
310
        "services/usrmgt-server", # XXX: Should be in bin/
334
311
    ]
335
312
 
336
313
    list_user_binaries = [
 
314
        "bin/ivle-addexercise",
 
315
        "bin/ivle-adduser",
 
316
        "bin/ivle-buildjail",
 
317
        "bin/ivle-cloneworksheets",
 
318
        "bin/ivle-config",
 
319
        "bin/ivle-createdatadirs",
337
320
        "bin/ivle-enrol",
338
321
        "bin/ivle-enrolallusers",
 
322
        "bin/ivle-fetchsubmissions",
339
323
        "bin/ivle-listusers",
340
 
        "bin/ivle-makeuser",
341
 
        "bin/ivle-marks",
 
324
        "bin/ivle-loadsampledata",
342
325
        "bin/ivle-mountallusers",
 
326
        "bin/ivle-refreshfilesystem",
343
327
        "bin/ivle-remakeuser",
344
328
        "bin/ivle-showenrolment",
345
329
    ]