~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/devices/usbms/cli.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
from __future__ import with_statement
 
3
 
 
4
__license__ = 'GPL 3'
 
5
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
 
6
__docformat__ = 'restructuredtext en'
 
7
 
 
8
import os, shutil
 
9
 
 
10
from calibre.devices.errors import PathError
 
11
 
 
12
class File(object):
 
13
 
 
14
    def __init__(self, path):
 
15
        stats = os.stat(path)
 
16
        self.is_dir = os.path.isdir(path)
 
17
        self.is_readonly = not os.access(path, os.W_OK)
 
18
        self.ctime = stats.st_ctime
 
19
        self.wtime = stats.st_mtime
 
20
        self.size  = stats.st_size
 
21
        if path.endswith(os.sep):
 
22
            path = path[:-1]
 
23
        self.path = path
 
24
        self.name = os.path.basename(path)
 
25
 
 
26
 
 
27
class CLI(object):
 
28
 
 
29
    def get_file(self, path, outfile, end_session=True):
 
30
        path = self.munge_path(path)
 
31
        with open(path, 'rb') as src:
 
32
            shutil.copyfileobj(src, outfile, 10*1024*1024)
 
33
 
 
34
    def put_file(self, infile, path, replace_file=False, end_session=True):
 
35
        path = self.munge_path(path)
 
36
        if os.path.isdir(path):
 
37
            path = os.path.join(path, infile.name)
 
38
        if not replace_file and os.path.exists(path):
 
39
            raise PathError('File already exists: ' + path)
 
40
        d = os.path.dirname(path)
 
41
        if not os.path.exists(d):
 
42
            os.makedirs(d)
 
43
        dest = open(path, 'wb')
 
44
        shutil.copyfileobj(infile, dest, 10*1024*1024)
 
45
        dest.flush()
 
46
        dest.close()
 
47
 
 
48
    def munge_path(self, path):
 
49
        if path.startswith('/') and not (path.startswith(self._main_prefix) or \
 
50
            (self._card_a_prefix and path.startswith(self._card_a_prefix)) or \
 
51
            (self._card_b_prefix and path.startswith(self._card_b_prefix))):
 
52
            path = self._main_prefix + path[1:]
 
53
        elif path.startswith('carda:'):
 
54
            path = path.replace('carda:', self._card_a_prefix[:-1])
 
55
        elif path.startswith('cardb:'):
 
56
            path = path.replace('cardb:', self._card_b_prefix[:-1])
 
57
        return path
 
58
 
 
59
    def list(self, path, recurse=False, end_session=True, munge=True):
 
60
        if munge:
 
61
            path = self.munge_path(path)
 
62
        if os.path.isfile(path):
 
63
            return [(os.path.dirname(path), [File(path)])]
 
64
        entries = [File(os.path.join(path, f)) for f in os.listdir(path)]
 
65
        dirs = [(path, entries)]
 
66
        for _file in entries:
 
67
            if recurse and _file.is_dir:
 
68
                dirs[len(dirs):] = self.list(_file.path, recurse=True, munge=False)
 
69
        return dirs
 
70
 
 
71
    def mkdir(self, path, end_session=True):
 
72
        if self.SUPPORTS_SUB_DIRS:
 
73
            path = self.munge_path(path)
 
74
            os.mkdir(path)
 
75
 
 
76
    def rm(self, path, end_session=True):
 
77
        path = self.munge_path(path)
 
78
        self.delete_books([path])
 
79
 
 
80
    def touch(self, path, end_session=True):
 
81
        path = self.munge_path(path)
 
82
        if not os.path.exists(path):
 
83
            open(path, 'w').close()
 
84
        if not os.path.isdir(path):
 
85
            os.utime(path, None)