~david.edmundson/usb-creator/kde-frontend

« back to all changes in this revision

Viewing changes to install.py

  • Committer: Evan Dandrea
  • Date: 2008-09-02 19:03:27 UTC
  • Revision ID: evan.dandrea@canonical.com-20080902190327-6bru0o17r3ht0w4i
Imported project.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
import getopt
 
3
import os
 
4
import stat
 
5
import sys
 
6
import shutil
 
7
 
 
8
def main(source, target):
 
9
    # TODO: debate pulling the total_size bit in from ubiquity for more
 
10
    # accurate progress.
 
11
    # TODO: Fedora's program shells out to cp and uses another thread that
 
12
    # checks the free space on the drive and updates the UI accordingly.
 
13
    # Is this worth replicating?
 
14
 
 
15
    # Some of the code in this function was copied from Ubiquity's
 
16
    # scripts/install.py
 
17
 
 
18
    if not os.path.exists(source) or not os.path.exists(target):
 
19
        sys.exit(1)
 
20
    walklen = 0
 
21
    percent = 0.0
 
22
    for dirpath, dirnames, filenames in os.walk(source):
 
23
        for name in dirnames + filenames:
 
24
            walklen += 1
 
25
    inc = 100.0 / walklen
 
26
    for dirpath, dirnames, filenames in os.walk(source):
 
27
        sp = dirpath[len(source) + 1:]
 
28
        for name in dirnames + filenames:
 
29
            relpath = os.path.join(sp, name)
 
30
            sourcepath = os.path.join(source, relpath)
 
31
            targetpath = os.path.join(target, relpath)
 
32
            st = os.lstat(sourcepath)
 
33
            mode = stat.S_IMODE(st.st_mode)
 
34
            if stat.S_ISLNK(st.st_mode):
 
35
                if os.path.lexists(targetpath):
 
36
                    os.unlink(targetpath)
 
37
                linkto = os.readlink(sourcepath)
 
38
                #os.symlink(linkto, targetpath)
 
39
                # FIXME: Handle this somehow?
 
40
                #print 'trying to symlink %s' % linkto
 
41
                pass
 
42
            elif stat.S_ISDIR(st.st_mode):
 
43
                if not os.path.isdir(targetpath):
 
44
                    os.mkdir(targetpath, mode)
 
45
            elif stat.S_ISCHR(st.st_mode):
 
46
                os.mknod(targetpath, stat.S_IFCHR | mode, st.st_rdev)
 
47
            elif stat.S_ISBLK(st.st_mode):
 
48
                os.mknod(targetpath, stat.S_IFBLK | mode, st.st_rdev)
 
49
            elif stat.S_ISFIFO(st.st_mode):
 
50
                os.mknod(targetpath, stat.S_IFIFO | mode)
 
51
            elif stat.S_ISSOCK(st.st_mode):
 
52
                os.mknod(targetpath, stat.S_IFSOCK | mode)
 
53
            elif stat.S_ISREG(st.st_mode):
 
54
                if os.path.exists(targetpath):
 
55
                    os.unlink(targetpath)
 
56
                try:
 
57
                    sourcefh = open(sourcepath, 'rb')
 
58
                    targetfh = open(targetpath, 'wb')
 
59
                    # TODO: md5 check.  Copy error handling.
 
60
                    shutil.copyfileobj(sourcefh, targetfh)
 
61
                finally:
 
62
                    sourcefh.close()
 
63
                    targetfh.close()
 
64
                #print '%s -> %s' % (sourcepath, targetpath)
 
65
            percent = percent + inc
 
66
            # TODO: self.frontend.progress(percent)
 
67
            print '%d' % percent
 
68
            #self.frontend.progress(percent)
 
69
 
 
70
if __name__ == '__main__':
 
71
    source = ''
 
72
    target = ''
 
73
    try:
 
74
        opts, args = getopt.getopt(sys.argv[1:], 's:t:')
 
75
    except getopt.GetoptError:
 
76
        sys.exit(1)
 
77
    for opt, arg in opts:
 
78
        if opt == '-s':
 
79
            source = arg
 
80
        elif opt == '-t':
 
81
            target = arg
 
82
    #print '%s %s' % (source, target)
 
83
    main(source, target)