ccplugin.py 3.99 KB
Newer Older
1
2
3
"""cubicweb-ctl plugin providing the fsimport command

:organization: Logilab
4
:copyright: 2010-2022 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5
6
7
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""

8
9
import hashlib

10
from cubicweb.cwctl import CWCTL
11
12
13
14
from cubicweb.toolsutils import Command


class FSImportCommand(Command):
15
    """Import content of a directory on the file system as File entities.
16
17
18
19
20
21
22
23
24
    The instance must use the `file` cube.

    <instance id>
      identifier of the instance where directory's content has to be imported.

    <fs directory>
      directory to import (recursivly)
    """
    name = 'fsimport'
25
26
    min_args = 2
    arguments = '<instance id> <fs file or directory>...'
27
28
    options = (
        ("map-folders",
David Douard's avatar
David Douard committed
29
         {'short': 'F', 'action': 'store_true',
30
          'default': False,
David Douard's avatar
David Douard committed
31
32
          'help': ('map file-system directories as Folder entities '
                   '(requires the `folder` cube).'),
33
          }),
34
        ("filed-under",
David Douard's avatar
David Douard committed
35
         {'short': 'u', 'type': 'int',
36
37
          'help': 'put imported file into the folder entity of the given eid.',
          }),
38
39
40
41
42
43
        )

    def run(self, args):
        """run the command with its specific arguments"""
        from cubicweb.server.serverconfig import ServerConfiguration
        from cubicweb.server.serverctl import repo_cnx
44
        from cubicweb_file.fsimport import fsimport
Sylvain Thénault's avatar
Sylvain Thénault committed
45
        appid = args.pop(0)
46
47
        config = ServerConfiguration.config_for(appid)
        repo, cnx = repo_cnx(config)
Sylvain Thénault's avatar
Sylvain Thénault committed
48
        repo.hm.call_hooks('server_maintenance', repo=repo)
49
50
51
        with cnx:
            fsimport(cnx, args, parenteid=self.config.filed_under,
                     mapfolders=self.config.map_folders,
David Douard's avatar
David Douard committed
52
53
                     bfss='data' in repo.system_source._storages.get(
                         'File', ()))
54
            cnx.commit()
55

David Douard's avatar
David Douard committed
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
class FileRefreshHashCommand(Command):
    """Import content of a directory on the file system as File entities.
    The instance must use the `file` cube.

    <instance id>
      identifier of the instance where directory's content has to be imported.

    """
    name = 'refresh-hash'
    min_args = 1
    arguments = '<instance id>...'
    options = (
        ("hash-algorithm",
         {'default': None,
          'type': 'choice',
          'choices': list(hashlib.algorithms_available),
          'help': ('hash algorithm to use (instead of the one configured '
                   'for the instance)'),
          }),
        ("force",
         {'short': 'f', 'action': 'store_true',
          'default': False,
          'help': ("Force the computation of hash if the instance "
                   "'compute-hash' option if False."),
          }),
        ("subclasses",
         {'action': 'store_true',
          'default': False,
          'help': "Also refresh hash for entity types that inherit from File",
          }),
        )

    def run(self, args):
        """run the command with its specific arguments"""
        from cubicweb.server.serverconfig import ServerConfiguration
        from cubicweb.server.serverctl import repo_cnx
        appid = args.pop(0)
        config = ServerConfiguration.config_for(appid)

        repo, cnx = repo_cnx(config)
        if repo.vreg.config['compute-hash'] or self.config.force:
            repo.hm.call_hooks('server_maintenance', repo=repo)
            alg = self.config.hash_algorithm

            with cnx:
                with cnx.deny_all_hooks_but():
                    etypes = ['File']
                    if self.config.subclasses:
                        etypes.extend(cnx.repo.schema['File'].specialized_by(
                            recursive=True))
                    print('ETYPES=', etypes)
                    for etype in etypes:
                        for e in cnx.execute(
                                'Any X WHERE X is %s' % etype).entities():
                            print('update hash for', e)
                            e.cw_set(data_hash=e.compute_hash(alg=alg))
                    cnx.commit()


116
CWCTL.register(FSImportCommand)