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

« back to all changes in this revision

Viewing changes to setup.py

  • Committer: mattgiuca
  • Date: 2008-01-20 21:44:25 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:252
setup.py: Added action "updatejails" which wipes all student jails, replacing
them with fresh hardlinks from jails template (preserving the students' home
directories).

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
import compileall
69
69
import getopt
70
70
 
 
71
# Import modules from the website is tricky since they're in the www
 
72
# directory.
 
73
sys.path.append(os.path.join(os.getcwd(), 'www'))
 
74
import conf
 
75
import common.makeuser
 
76
 
71
77
# Operating system files to copy over into the jail.
72
78
# These will be copied from the given place on the OS file system into the
73
79
# same place within the jail.
157
163
        return 1
158
164
 
159
165
    # Disallow run as root unless installing
160
 
    if operation != 'install' and os.geteuid() == 0:
 
166
    if (operation != 'install' and operation != 'updatejails'
 
167
        and os.geteuid() == 0):
161
168
        print >>sys.stderr, "I do not want to run this stage as root."
162
169
        print >>sys.stderr, "Please run as a normal user."
163
170
        return 1
169
176
            'build' : build,
170
177
            'listmake' : listmake,
171
178
            'install' : install,
 
179
            'updatejails' : updatejails,
172
180
        }[operation]
173
181
    except KeyError:
174
182
        print >>sys.stderr, (
252
260
 
253
261
--nojail    Do not copy the jail.
254
262
--dry | -n  Print out the actions but don't do anything."""
 
263
    elif operation == 'updatejails':
 
264
        print """sudo python setup.py updatejails [--dry|-n]
 
265
(Requires root)
 
266
Copy jail/ to each subdirectory in jails directory.
 
267
 
 
268
--dry | -n  Print out the actions but don't do anything."""
255
269
    else:
256
270
        print >>sys.stderr, (
257
271
            """Invalid operation '%s'. Try python setup.py help."""
551
565
 
552
566
    return 0
553
567
 
 
568
def updatejails(args):
 
569
    # Get "dry" variable from command line
 
570
    (opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
 
571
    opts = dict(opts)
 
572
    dry = '-n' in opts or '--dry' in opts
 
573
 
 
574
    if dry:
 
575
        print "Dry run (no actions will be executed\n"
 
576
 
 
577
    if not dry and os.geteuid() != 0:
 
578
        print >>sys.stderr, "Must be root to run install"
 
579
        print >>sys.stderr, "(I need to chown some files)."
 
580
        return 1
 
581
 
 
582
    # Update the template jail directory in case it hasn't been installed
 
583
    # recently.
 
584
    action_copytree('jail', os.path.join(jail_base, 'template'), dry)
 
585
 
 
586
    # Re-link all the files in all students jails.
 
587
    for dir in os.listdir(jail_base):
 
588
        if dir == 'template': continue
 
589
        # First back up the student's home directory
 
590
        temp_home = os.tmpnam()
 
591
        action_rename(os.path.join(jail_base, dir, 'home'), temp_home, dry)
 
592
        # Delete the student's jail and relink the jail files
 
593
        action_linktree(os.path.join(jail_base, 'template'),
 
594
            os.path.join(jail_base, dir), dry)
 
595
        # Restore the student's home directory
 
596
        action_rename(temp_home, os.path.join(jail_base, dir, 'home'), dry)
 
597
        # Set up the user's home directory just in case they don't have a
 
598
        # directory for this yet
 
599
        action_mkdir(os.path.join(jail_base, dir, 'home', dir), dry)
 
600
 
 
601
    return 0
 
602
 
554
603
# The actions call Python os functions but print actions and handle dryness.
555
604
# May still throw os exceptions if errors occur.
556
605
 
580
629
    if ret != 0:
581
630
        raise RunError(prog, ret)
582
631
 
 
632
def action_rename(src, dst, dry):
 
633
    """Calls rename. Deletes the target if it already exists."""
 
634
    if os.access(dst, os.F_OK):
 
635
        print "rm -r", dst
 
636
        if not dry:
 
637
            shutil.rmtree(dst, True)
 
638
    print "mv ", src, dst
 
639
    if dry: return
 
640
    try:
 
641
        os.rename(src, dst)
 
642
    except OSError, (err, msg):
 
643
        if err != errno.EEXIST:
 
644
            raise
 
645
 
583
646
def action_mkdir(path, dry):
584
647
    """Calls mkdir. Silently ignored if the directory already exists.
585
648
    Creates all parent directories as necessary."""
605
668
    if dry: return
606
669
    shutil.copytree(src, dst, True)
607
670
 
 
671
def action_linktree(src, dst, dry):
 
672
    """Hard-links an entire directory tree. Same as copytree but the created
 
673
    files are hard-links not actual copies. Removes the existing destination.
 
674
    """
 
675
    if os.access(dst, os.F_OK):
 
676
        print "rm -r", dst
 
677
        if not dry:
 
678
            shutil.rmtree(dst, True)
 
679
    print "<cp with hardlinks> -r", src, dst
 
680
    if dry: return
 
681
    common.makeuser.linktree(src, dst)
 
682
 
608
683
def action_copylist(srclist, dst, dry):
609
684
    """Copies all files in a list to a new location. The files in the list
610
685
    are read relative to the current directory, and their destinations are the