~xubuntu-dev/ubiquity/lp1437180_feh

« back to all changes in this revision

Viewing changes to scripts/install.py

  • Committer: Colin Watson
  • Date: 2012-04-30 23:38:41 UTC
  • mfrom: (5402 trunk)
  • mto: This revision was merged to the branch mainline in revision 5403.
  • Revision ID: cjwatson@canonical.com-20120430233841-xb0qsk46lnhski7m
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import subprocess
29
29
import time
30
30
import syslog
 
31
import signal
 
32
 
31
33
import debconf
32
 
import warnings
33
 
warnings.filterwarnings("ignore", "apt API not stable yet", FutureWarning)
34
34
import apt_pkg
35
35
from apt.cache import Cache
36
 
import signal
37
36
 
38
37
sys.path.insert(0, '/usr/lib/ubiquity')
39
38
 
79
78
            self.db.progress('INFO', 'ubiquity/install/blacklist')
80
79
            self.generate_blacklist()
81
80
 
82
 
        apt_pkg.InitConfig()
83
 
        apt_pkg.Config.set("Dir", self.target)
84
 
        apt_pkg.Config.set("Dir::State::status",
 
81
        apt_pkg.init_config()
 
82
        apt_pkg.config.set("Dir", self.target)
 
83
        apt_pkg.config.set("Dir::State::status",
85
84
                           os.path.join(self.target, 'var/lib/dpkg/status'))
86
 
        apt_pkg.Config.set("APT::GPGV::TrustedKeyring",
 
85
        apt_pkg.config.set("APT::GPGV::TrustedKeyring",
87
86
                           os.path.join(self.target, 'etc/apt/trusted.gpg'))
88
 
        apt_pkg.Config.set("Acquire::gpgv::Options::",
 
87
        apt_pkg.config.set("Acquire::gpgv::Options::",
89
88
                           "--ignore-time-conflict")
90
 
        apt_pkg.Config.set("DPkg::Options::", "--root=%s" % self.target)
 
89
        apt_pkg.config.set("DPkg::Options::", "--root=%s" % self.target)
91
90
        # We don't want apt-listchanges or dpkg-preconfigure, so just clear
92
91
        # out the list of pre-installation hooks.
93
 
        apt_pkg.Config.clear("DPkg::Pre-Install-Pkgs")
94
 
        apt_pkg.InitSystem()
 
92
        apt_pkg.config.clear("DPkg::Pre-Install-Pkgs")
 
93
        apt_pkg.init_system()
95
94
 
96
95
    def run(self):
97
96
        """Run the install stage: copy everything to the target system, then
155
154
                    sys.exit(3)
156
155
                else:
157
156
                    raise
158
 
        self.db.progress('INFO', 'ubiquity/install/waiting')
159
157
 
160
158
        if self.source == '/var/lib/ubiquity/source':
161
159
            self.umount_source()
339
337
        self.db.progress('INFO', 'ubiquity/install/copying')
340
338
 
341
339
        fs_size = os.path.join(self.casper_path, 'filesystem.size')
342
 
        assert os.path.exists(fs_size), "Missing filesystem.size."
343
 
        with open(fs_size) as total_size_fp:
344
 
            total_size = int(total_size_fp.readline())
 
340
        if os.path.exists(fs_size):
 
341
            with open(fs_size) as total_size_fp:
 
342
                total_size = int(total_size_fp.readline())
 
343
        else:
 
344
            # Fallback in case an Ubuntu derivative forgets to put
 
345
            # /casper/filesystem.size on the CD, or to account for things
 
346
            # like CD->USB transformation tools that don't copy this file.
 
347
            # This is slower than just reading the size from a file, but
 
348
            # better than crashing.
 
349
            #
 
350
            # Obviously doing os.walk() twice is inefficient, but I'd rather
 
351
            # not suck the list into ubiquity's memory, and I'm guessing
 
352
            # that the kernel's dentry cache will avoid most of the slowness
 
353
            # anyway.
 
354
            total_size = 0
 
355
            for dirpath, dirnames, filenames in os.walk(self.source):
 
356
                for name in dirnames + filenames:
 
357
                    fqpath = os.path.join(dirpath, name)
 
358
                    total_size += os.lstat(fqpath).st_size
345
359
 
346
360
        # Progress bar handling:
347
361
        # We sample progress every half-second (assuming time.time() gives
386
400
            for name in dirnames + filenames:
387
401
                relpath = os.path.join(sp, name)
388
402
                # /etc/fstab was legitimately created by partman, and
389
 
                # shouldn't be copied again.
390
 
                if relpath == "etc/fstab":
 
403
                # shouldn't be copied again.  Similarly, /etc/crypttab may
 
404
                # have been legitimately created by the user-setup plugin.
 
405
                if relpath in ("etc/fstab", "etc/crypttab"):
391
406
                    continue
392
407
                sourcepath = os.path.join(self.source, relpath)
393
408
                targetpath = os.path.join(self.target, relpath)
394
409
                st = os.lstat(sourcepath)
 
410
 
 
411
                # Is the path blacklisted?
 
412
                if (not stat.S_ISDIR(st.st_mode) and
 
413
                    '/%s' % relpath in self.blacklist):
 
414
                    if debug:
 
415
                        syslog.syslog('Not copying %s' % relpath)
 
416
                    continue
 
417
 
 
418
                # Remove the target if necessary and if we can.
 
419
                install_misc.remove_target(
 
420
                    self.source, self.target, relpath, st)
 
421
 
 
422
                # Now actually copy source to target.
395
423
                mode = stat.S_IMODE(st.st_mode)
396
424
                if stat.S_ISLNK(st.st_mode):
397
 
                    try:
398
 
                        os.unlink(targetpath)
399
 
                    except OSError as e:
400
 
                        if e.errno == errno.ENOENT:
401
 
                            pass
402
 
                        elif e.errno == errno.EISDIR:
403
 
                            os.rmdir(targetpath)
404
 
                        else:
405
 
                            raise
406
425
                    linkto = os.readlink(sourcepath)
407
426
                    os.symlink(linkto, targetpath)
408
427
                elif stat.S_ISDIR(st.st_mode):
409
428
                    if not os.path.isdir(targetpath):
410
 
                        os.mkdir(targetpath, mode)
 
429
                        try:
 
430
                            os.mkdir(targetpath, mode)
 
431
                        except OSError, e:
 
432
                            # there is a small window where update-apt-cache
 
433
                            # can race with us since it creates
 
434
                            # "/target/var/cache/apt/...". Hence, ignore
 
435
                            # failure if the directory does now exist where
 
436
                            # brief moments before it didn't.
 
437
                            if e.errno != errno.EEXIST:
 
438
                                raise
411
439
                elif stat.S_ISCHR(st.st_mode):
412
440
                    os.mknod(targetpath, stat.S_IFCHR | mode, st.st_rdev)
413
441
                elif stat.S_ISBLK(st.st_mode):
417
445
                elif stat.S_ISSOCK(st.st_mode):
418
446
                    os.mknod(targetpath, stat.S_IFSOCK | mode)
419
447
                elif stat.S_ISREG(st.st_mode):
420
 
                    if '/%s' % relpath in self.blacklist:
421
 
                        if debug:
422
 
                            syslog.syslog('Not copying %s' % relpath)
423
 
                        continue
424
 
                    osextras.unlink_force(targetpath)
425
448
                    install_misc.copy_file(self.db, sourcepath, targetpath, md5_check)
426
449
 
 
450
                # Copy metadata.
427
451
                copied_size += st.st_size
428
452
                os.lchown(targetpath, st.st_uid, st.st_gid)
429
453
                if not stat.S_ISLNK(st.st_mode):
432
456
                    directory_times.append((targetpath, st.st_atime, st.st_mtime))
433
457
                # os.utime() sets timestamp of target, not link
434
458
                elif not stat.S_ISLNK(st.st_mode):
435
 
                    os.utime(targetpath, (st.st_atime, st.st_mtime))
 
459
                    try:
 
460
                        os.utime(targetpath, (st.st_atime, st.st_mtime))
 
461
                    except Exception:
 
462
                        # We can live with timestamps being wrong.
 
463
                        pass
436
464
 
437
465
                if int((copied_size * 90) / total_size) != copy_progress:
438
466
                    copy_progress = int((copied_size * 90) / total_size)
462
490
            (directory, atime, mtime) = dirtime
463
491
            try:
464
492
                os.utime(directory, (atime, mtime))
465
 
            except OSError:
 
493
            except Exception:
466
494
                # I have no idea why I've been getting lots of bug reports
467
495
                # about this failing, but I really don't care. Ignore it.
468
496
                pass
488
516
            os.lchown(target_kernel, 0, 0)
489
517
            os.chmod(target_kernel, 0644)
490
518
            st = os.lstat(kernel)
491
 
            os.utime(target_kernel, (st.st_atime, st.st_mtime))
 
519
            try:
 
520
                os.utime(target_kernel, (st.st_atime, st.st_mtime))
 
521
            except Exception:
 
522
                # We can live with timestamps being wrong.
 
523
                pass
492
524
 
493
525
        os.umask(old_umask)
494
526