| 1 |
""" |
| 2 |
Helpers for pulling data from CDs, DVDs and similar media. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import fcntl |
| 12 |
import os |
| 13 |
import shutil |
| 14 |
import subprocess |
| 15 |
import sys |
| 16 |
import time |
| 17 |
|
| 18 |
from gavo import base |
| 19 |
|
| 20 |
CTL_CD_EJECT = 0x5309 |
| 21 |
CTL_CD_QCHANGE = 0x5325 |
| 22 |
|
| 23 |
|
| 24 |
class CDHandler(object): |
| 25 |
"""A wrapper for mounting and ejecting CDs. |
| 26 |
|
| 27 |
This assumes that you let users mount CDs, e.g. via a line like |
| 28 |
|
| 29 |
/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto 0 0 |
| 30 |
|
| 31 |
in /etc/fstab. In this example, devPath would be /dev/scd0 and |
| 32 |
mountPath /media/cdrom0. |
| 33 |
""" |
| 34 |
def __init__(self, devPath, mountPath): |
| 35 |
self.devPath, self.mountPath = devPath, mountPath |
| 36 |
|
| 37 |
def ejectMedium(self): |
| 38 |
"""ejects the current CD. |
| 39 |
""" |
| 40 |
cd = os.open(self.devPath, os.O_RDONLY|os.O_NONBLOCK) |
| 41 |
try: |
| 42 |
fcntl.ioctl(cd, CTL_CD_EJECT) |
| 43 |
finally: |
| 44 |
if cd>=0: |
| 45 |
os.close(cd) |
| 46 |
|
| 47 |
def mediumChanged(self): |
| 48 |
"""returns True if there's a new medium available. |
| 49 |
""" |
| 50 |
cd = os.open(self.devPath, os.O_RDONLY|os.O_NONBLOCK) |
| 51 |
try: |
| 52 |
if cd<0: |
| 53 |
return False |
| 54 |
else: |
| 55 |
return fcntl.ioctl(cd, CTL_CD_QCHANGE, 0)==0 |
| 56 |
finally: |
| 57 |
if cd>=0: |
| 58 |
os.close(cd) |
| 59 |
|
| 60 |
def mount(self): |
| 61 |
subprocess.check_call(["mount", self.mountPath]) |
| 62 |
|
| 63 |
def unmount(self): |
| 64 |
subprocess.check_call(["umount", self.devPath]) |
| 65 |
|
| 66 |
def isMounted(self): |
| 67 |
f = open("/proc/mounts") |
| 68 |
stuff = f.read() |
| 69 |
f.close() |
| 70 |
return self.mountPath in stuff |
| 71 |
|
| 72 |
|
| 73 |
def _getCurMax(destBase): |
| 74 |
"""returns the highest index of ??\\d+-formed names in destBase. |
| 75 |
""" |
| 76 |
def brutalInt(val): |
| 77 |
try: |
| 78 |
return int(val) |
| 79 |
except ValueError: |
| 80 |
return 0 |
| 81 |
try: |
| 82 |
return max([brutalInt(n[2:]) for n in os.listdir(destBase)]+[0]) |
| 83 |
except IOError: |
| 84 |
return 0 |
| 85 |
|
| 86 |
|
| 87 |
def changeCD(cdHandler): |
| 88 |
if cdHandler.isMounted(): |
| 89 |
cdHandler.unmount() |
| 90 |
cdHandler.ejectMedium() |
| 91 |
sys.stdout.write("Waiting for Medium") |
| 92 |
sys.stdout.flush() |
| 93 |
while not cdHandler.mediumChanged(): |
| 94 |
time.sleep(1) |
| 95 |
sys.stdout.write(".") |
| 96 |
sys.stdout.flush() |
| 97 |
|
| 98 |
|
| 99 |
def readCDs(destBase, cdHandler): |
| 100 |
"""runs a crude UI to read CDs in batch. |
| 101 |
|
| 102 |
The idea is that you get a copy-in script by writing something like |
| 103 |
|
| 104 |
import os |
| 105 |
from gavo import api |
| 106 |
|
| 107 |
rd = api.getRD("foo/bar") |
| 108 |
cd = CDHandler("/dev/cdrom", "mnt/cd") |
| 109 |
copying.readCDs(os.path.join(rd.resdir, "raw"), cd) |
| 110 |
""" |
| 111 |
if not os.path.exists(destBase): |
| 112 |
os.mkdir(destBase) |
| 113 |
srcCount = 0 |
| 114 |
else: |
| 115 |
srcCount = _getCurMax(destBase)+1 |
| 116 |
|
| 117 |
while 1: |
| 118 |
changeCD(cdHandler) |
| 119 |
dest = os.path.join(destBase, "cd%03d"%srcCount) |
| 120 |
print("Now writing to", dest) |
| 121 |
try: |
| 122 |
cdHandler.mount() |
| 123 |
shutil.copytree(cdHandler.mountPath, dest) |
| 124 |
except (subprocess.CalledProcessError, shutil.Error, KeyboardInterrupt): |
| 125 |
base.ui.notifyError("Copying failed or interrupted." |
| 126 |
" Removing destination %s. Try again.")%dest |
| 127 |
if os.path.exists(dest): |
| 128 |
shutil.rmtree(dest) |
| 129 |
sys.exit(1) |
| 130 |
srcCount += 1 |