| 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 fcntlscip-python python python-stdlib 3.11 fcntl/__init__: |
| 12 | import osscip-python python python-stdlib 3.11 os/__init__: |
| 13 | import shutilscip-python python python-stdlib 3.11 shutil/__init__: |
| 14 | import subprocessscip-python python python-stdlib 3.11 subprocess/__init__: |
| 15 | import sysscip-python python python-stdlib 3.11 sys/__init__: |
| 16 | import timescip-python python python-stdlib 3.11 time/__init__: |
| 17 | |
| 18 | from gavoscip-python python gavodachs 2.13 gavo/__init__: import basescip-python python gavodachs 2.13 `gavo.base`/__init__: |
| 19 | |
| 20 | CTL_CD_EJECTscip-python python gavodachs 2.13 `gavo.helpers.copying`/CTL_CD_EJECT. = 0x5309 |
| 21 | CTL_CD_QCHANGEscip-python python gavodachs 2.13 `gavo.helpers.copying`/CTL_CD_QCHANGE. = 0x5325 |
| 22 | |
| 23 | |
| 24 | class CDHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#(objectscip-python python python-stdlib 3.11 builtins/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__scip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#__init__().(selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#__init__().(self), devPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#__init__().(devPath), mountPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#__init__().(mountPath)): |
| 35 | selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#__init__().(self).devPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#devPath., selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#__init__().(self).mountPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#mountPath. = devPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#__init__().(devPath), mountPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#__init__().(mountPath) |
| 36 | |
| 37 | def ejectMediumscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#ejectMedium().(selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#ejectMedium().(self)): |
| 38 | """ejects the current CD. |
| 39 | """ |
| 40 | cd = osscip-python python python-stdlib 3.11 os/__init__:.openscip-python python python-stdlib 3.11 os/open().(selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#ejectMedium().(self).devPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#devPath., osscip-python python python-stdlib 3.11 os/__init__:.O_RDONLYscip-python python python-stdlib 3.11 os/O_RDONLY.O_RDONLY.|osscip-python python python-stdlib 3.11 os/__init__:.O_NONBLOCKscip-python python python-stdlib 3.11 os/O_NONBLOCK.O_NONBLOCK.) |
| 41 | try: |
| 42 | fcntlscip-python python python-stdlib 3.11 fcntl/__init__:.ioctlscip-python python python-stdlib 3.11 fcntl/ioctl().(cd, CTL_CD_EJECTscip-python python gavodachs 2.13 `gavo.helpers.copying`/CTL_CD_EJECT.) |
| 43 | finally: |
| 44 | if cd>=0: |
| 45 | osscip-python python python-stdlib 3.11 os/__init__:.closescip-python python python-stdlib 3.11 os/close().(cd) |
| 46 | |
| 47 | def mediumChangedscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#mediumChanged().(selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#mediumChanged().(self)): |
| 48 | """returns True if there's a new medium available. |
| 49 | """ |
| 50 | cd = osscip-python python python-stdlib 3.11 os/__init__:.openscip-python python python-stdlib 3.11 os/open().(selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#mediumChanged().(self).devPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#devPath., osscip-python python python-stdlib 3.11 os/__init__:.O_RDONLYscip-python python python-stdlib 3.11 os/O_RDONLY.O_RDONLY.|osscip-python python python-stdlib 3.11 os/__init__:.O_NONBLOCKscip-python python python-stdlib 3.11 os/O_NONBLOCK.O_NONBLOCK.) |
| 51 | try: |
| 52 | if cd<0: |
| 53 | return False |
| 54 | else: |
| 55 | return fcntlscip-python python python-stdlib 3.11 fcntl/__init__:.ioctlscip-python python python-stdlib 3.11 fcntl/ioctl().(cd, CTL_CD_QCHANGEscip-python python gavodachs 2.13 `gavo.helpers.copying`/CTL_CD_QCHANGE., 0)==0 |
| 56 | finally: |
| 57 | if cd>=0: |
| 58 | osscip-python python python-stdlib 3.11 os/__init__:.closescip-python python python-stdlib 3.11 os/close().(cd) |
| 59 | |
| 60 | def mountscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#mount().(selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#mount().(self)): |
| 61 | subprocessscip-python python python-stdlib 3.11 subprocess/__init__:.check_callscip-python python python-stdlib 3.11 subprocess/check_call().(["mount", selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#mount().(self).mountPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#mountPath.]) |
| 62 | |
| 63 | def unmountscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#unmount().(selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#unmount().(self)): |
| 64 | subprocessscip-python python python-stdlib 3.11 subprocess/__init__:.check_callscip-python python python-stdlib 3.11 subprocess/check_call().(["umount", selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#unmount().(self).devPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#devPath.]) |
| 65 | |
| 66 | def isMountedscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#isMounted().(selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#isMounted().(self)): |
| 67 | f = openscip-python python python-stdlib 3.11 builtins/open().("/proc/mounts") |
| 68 | stuff = f.readscip-python python python-stdlib 3.11 io/TextIOBase#read().() |
| 69 | f.closescip-python python python-stdlib 3.11 io/IOBase#close().() |
| 70 | return selfscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#isMounted().(self).mountPathscip-python python gavodachs 2.13 `gavo.helpers.copying`/CDHandler#mountPath. in stuff |
| 71 | |
| 72 | |
| 73 | def _getCurMaxscip-python python gavodachs 2.13 `gavo.helpers.copying`/_getCurMax().(destBasescip-python python gavodachs 2.13 `gavo.helpers.copying`/_getCurMax().(destBase)): |
| 74 | """returns the highest index of ??\\d+-formed names in destBase. |
| 75 | """ |
| 76 | def brutalIntscip-python python gavodachs 2.13 `gavo.helpers.copying`/_getCurMax().brutalInt().(valscip-python python gavodachs 2.13 `gavo.helpers.copying`/_getCurMax().brutalInt().(val)): |
| 77 | try: |
| 78 | return intscip-python python python-stdlib 3.11 builtins/int#(valscip-python python gavodachs 2.13 `gavo.helpers.copying`/_getCurMax().brutalInt().(val)) |
| 79 | except ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#: |
| 80 | return 0 |
| 81 | try: |
| 82 | return maxscip-python python python-stdlib 3.11 builtins/max().([brutalIntscip-python python gavodachs 2.13 `gavo.helpers.copying`/_getCurMax().brutalInt().(n[2:]) for n in osscip-python python python-stdlib 3.11 os/__init__:.listdirscip-python python python-stdlib 3.11 os/listdir().(destBasescip-python python gavodachs 2.13 `gavo.helpers.copying`/_getCurMax().(destBase))]+[0]) |
| 83 | except IOErrorscip-python python python-stdlib 3.11 builtins/IOError#: |
| 84 | return 0 |
| 85 | |
| 86 | |
| 87 | def changeCDscip-python python gavodachs 2.13 `gavo.helpers.copying`/changeCD().(cdHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/changeCD().(cdHandler)): |
| 88 | if cdHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/changeCD().(cdHandler).isMounted(): |
| 89 | cdHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/changeCD().(cdHandler).unmount() |
| 90 | cdHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/changeCD().(cdHandler).ejectMedium() |
| 91 | sysscip-python python python-stdlib 3.11 sys/__init__:.stdoutscip-python python python-stdlib 3.11 sys/stdout.stdout..writescip-python python python-stdlib 3.11 typing/IO#write().("Waiting for Medium") |
| 92 | sysscip-python python python-stdlib 3.11 sys/__init__:.stdoutscip-python python python-stdlib 3.11 sys/stdout.stdout..flushscip-python python python-stdlib 3.11 typing/IO#flush().() |
| 93 | while not cdHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/changeCD().(cdHandler).mediumChanged(): |
| 94 | timescip-python python python-stdlib 3.11 time/__init__:.sleepscip-python python python-stdlib 3.11 time/sleep().(1) |
| 95 | sysscip-python python python-stdlib 3.11 sys/__init__:.stdoutscip-python python python-stdlib 3.11 sys/stdout.stdout..writescip-python python python-stdlib 3.11 typing/IO#write().(".") |
| 96 | sysscip-python python python-stdlib 3.11 sys/__init__:.stdoutscip-python python python-stdlib 3.11 sys/stdout.stdout..flushscip-python python python-stdlib 3.11 typing/IO#flush().() |
| 97 | |
| 98 | |
| 99 | def readCDsscip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(destBasescip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(destBase), cdHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(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 osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..existsscip-python python python-stdlib 3.11 ntpath/join().(destBasescip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(destBase)): |
| 112 | osscip-python python python-stdlib 3.11 os/__init__:.mkdirscip-python python python-stdlib 3.11 os/mkdir().(destBasescip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(destBase)) |
| 113 | srcCount = 0 |
| 114 | else: |
| 115 | srcCount = _getCurMaxscip-python python gavodachs 2.13 `gavo.helpers.copying`/_getCurMax().(destBasescip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(destBase))+1 |
| 116 | |
| 117 | while 1: |
| 118 | changeCDscip-python python gavodachs 2.13 `gavo.helpers.copying`/changeCD().(cdHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(cdHandler)) |
| 119 | dest = osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..joinscip-python python python-stdlib 3.11 ntpath/join().(destBasescip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(destBase), "cd%03d"%srcCount) |
| 120 | printscip-python python python-stdlib 3.11 builtins/print().("Now writing to", dest) |
| 121 | try: |
| 122 | cdHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(cdHandler).mount() |
| 123 | shutilscip-python python python-stdlib 3.11 shutil/__init__:.copytreescip-python python python-stdlib 3.11 shutil/copytree().(cdHandlerscip-python python gavodachs 2.13 `gavo.helpers.copying`/readCDs().(cdHandler).mountPath, dest) |
| 124 | except (subprocessscip-python python python-stdlib 3.11 subprocess/__init__:.CalledProcessErrorscip-python python python-stdlib 3.11 subprocess/CalledProcessError#, shutilscip-python python python-stdlib 3.11 shutil/__init__:.Errorscip-python python python-stdlib 3.11 shutil/Error#, KeyboardInterruptscip-python python python-stdlib 3.11 builtins/KeyboardInterrupt#): |
| 125 | basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..notifyErrorscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#notifyError().("Copying failed or interrupted." |
| 126 | " Removing destination %s. Try again.")%dest |
| 127 | if osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..existsscip-python python python-stdlib 3.11 ntpath/join().(dest): |
| 128 | shutilscip-python python python-stdlib 3.11 shutil/__init__:.rmtreescip-python python python-stdlib 3.11 shutil/rmtree.rmtree.(dest) |
| 129 | sysscip-python python python-stdlib 3.11 sys/__init__:.exit(1) |
| 130 | srcCount += 1 |