Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/burntools.py
blob: 9acd3bda2e8a18877d1a20bd45ae31719856f6b8 (plain)
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
# Copyright (c) 2010, Nathaniel Theis

import pexpect, re

#Define some handy things (regexes, strings, etc)

trackmatcher = re.compile(r"Track\s(?P<tracknum>\d+):\s(?P<tracktype>[A-Za-z]+)"
            + r"\W+(?P<tracksize>\d+\s[MmKkGg]B)\s*(?P<tracktime>\(\d+"
            + r":\d+\.\d+\))?")

totalmatcher = re.compile(r"Total size:\s*(?P<totsize>\d+\s[MmKkGg]B)\s+"
            + r"(?P<tottime>\(\d+:\d+\.\d+\))\s=\s(?P<totsectors>\d+)\ssectors")

writestartmatcher = re.compile(r"0\sseconds.\sOperation\sstarts.")

trackstartmatcher = re.compile(r"Starting\snew\strack\sat\ssector:\s+"
                               +r"(?P<startsector>\d+)")

trackprogressmatcher = re.compile(r"\rTrack\s(?P<tracknum>\d+):\s+("
                                  + r"?P<sofar>\d+)\sof\s+(?P<total>\d+)\s"
                                  + r"(?P<unit>[MmKkGg]B)\swritten"
                                  + r"(\s\(fifo\s+(?P<fifofill>\d+)+%\)\s+)?"
                                  + r"(\[buf\s+(?P<buffill>\d+)%\]\s+)?"
                                  + r"((?P<speed>\d+\.\d+)x)?")

trackdonematcher = re.compile(r"Track\s(?P<tracknum>\d+):\sTotal\sbytes"
                              + r"\sread/written:\s(?P<bytesread>\d+)/"
                              + r"(?P<byteswritten>\d+)\s\((?P<sectors>\d+)\s"
                              + r"sectors\)\.")

fixatestartmatcher = re.compile(r"Fixating...")

fixatedonematcher = re.compile(r"Fixating\stime:\s+(?P<time>\d+\.\d+)s")
                                  
carriagereturn = chr(13)



class WodimWrapper():
    '''WodimWrapper is a class to burn CDs with Wodim'''
    def _get_track_data(self):
        trackinfo = {}
        totalline = ""
        
        while True:
            whichmatch = self.wodim_process.expect([trackmatcher, totalmatcher])
            matchdata = self.wodim_process.match.groupdict()
            if whichmatch == 1:
                #Total info line
                totalline = matchdata
                break
            else:
                #Track info line
                trackinfo[matchdata["tracknum"]] = matchdata

        return (trackinfo, totalline)

    def _wait_until_write_start(self):
        match = self.wodim_process.expect(writestartmatcher)

    def _do_burn(self):
        self.wodim_process.expect(trackstartmatcher)
        match = self.wodim_process.match.groupdict()
        
        startsector = int(match['startsector'])
        
        self.trackstart_cb(startsector)

        while True:
            linetype = self.wodim_process.expect([trackprogressmatcher,
                                       trackdonematcher])

            match = self.wodim_process.match.groupdict()

            if linetype == 0:
                self.trackprogress_cb(match)
            elif linetype == 1:
                self.trackdone_cb(match)
                break

        ##FIXME: Only supports 1-track CD burning
        ##I need example output for a multi-track burn

        ##fixate CD

        self.wodim_process.expect(fixatestartmatcher)
        self.fixatestart_cb()
        
        self.wodim_process.expect(fixatedonematcher)
        self.fixatedone_cb(self.wodim_process.match.groupdict())

        ##now just wait for Wodim to finish!

        if self.wodim_process.exitstatus is None: 
            self.wodim_process.wait()

        self.wodim_process.close()

    def __init__(self, args, trackstart_cb, trackprogress_cb,
                 trackdone_cb, fixatestart_cb, fixatedone_cb, 
                 burncomplete_cb, trackdataready_cb,
                 command_path="wodim"):
        '''Initialize stuff, mainly set variables'''
        #Yeah, that's a lot of callbacks
        
        
        self.trackstart_cb = trackstart_cb
        self.trackprogress_cb = trackprogress_cb
        self.trackdone_cb = trackdone_cb
        self.fixatestart_cb = fixatestart_cb
        self.fixatedone_cb = fixatedone_cb
        self.burncomplete_cb = burncomplete_cb
        self.trackdataready_cb = trackdataready_cb
        
        self.wodim_command = command_path + " -v "
        #Some info is only shown with -v
        
        self.wodim_command += " ".join(args)

    def run(self):
        '''Do the burn!'''
        self.wodim_process = pexpect.spawn(self.wodim_command)

        trackdata = self._get_track_data()
        self.trackdataready_cb(trackdata)
        
        self._wait_until_write_start()

        self._do_burn()

        self.burncomplete_cb()

        return self.wodim_process.exitstatus
        
        
        
if __name__ == "__main__":
    #run a few tests for development purposes
    #i should write unit tests later
    
    def printer(*args):
        for arg in args: print arg
        
    ww = WodimWrapper([], printer, printer, printer,
                      printer, printer, printer, printer,
                      command_path="./wodim")
    ww.run()