-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdm-attach-file
150 lines (129 loc) · 4.39 KB
/
dm-attach-file
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
148
149
150
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import getopt
import os
import shutil
import sys
from hecatoncheir import DbProfilerRepository
from hecatoncheir import logger as log
from hecatoncheir.msgutil import gettext as _
from hecatoncheir.attachment import Attachment
input_encoding = 'utf-8'
def usage():
print """
Usage: %s [repo file] [target] [command]
Targets:
tag:[tag label]
schema:[schema name]
table:[table name]
Commands:
list
add [file]
rm [file]
Options:
--help Print this help.
""" % os.path.basename(sys.argv[0])
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "",
["help", "debug"])
except getopt.GetoptError as e:
log.error(unicode(e))
usage()
sys.exit(1)
for o, a in opts:
if o in ("--debug"):
log.debug_enabled = True
elif o in ("--help"):
usage()
sys.exit(0)
else:
log.error("unexpected option. internal error.")
sys.exit(1)
if len(args) < 3:
log.error(_("Too few arguments."))
usage()
sys.exit(1)
repofile = args.pop(0).decode(input_encoding)
target = args.pop(0).decode(input_encoding)
cmd = args.pop(0).decode(input_encoding)
filename = None
if cmd in ['add', 'rm']:
if not args:
log.error(_("Missing file name."))
usage()
sys.exit(1)
filename = args.pop(0).decode(input_encoding)
if cmd not in ['add', 'list', 'rm']:
log.error(_("Invalid command. It must be one of 'add', "
"'list' or 'rm'."))
usage()
sys.exit(1)
(objtype, objid) = target.split(':', 1)
if objtype not in ['tag', 'schema', 'table']:
log.error(_("Invalid target type. "
"It must be one of 'tag', 'schema' or 'table'."))
usage()
sys.exit(1)
dirname = os.path.dirname(repofile)
dirname = (dirname + "/attachments" if dirname else
dirname + "./attachments")
if objtype == 'tag':
dirname = dirname + u'/tag-%s' % objid
else:
# schema/table
dirname = dirname + u'/%s' % objid
if cmd == 'add' and not os.path.exists(filename):
log.error(_("Attachment file `%s' does not exist. Abort.") % filename)
sys.exit(1)
try:
if not os.path.isdir(dirname):
os.makedirs(dirname)
log.info(_("Created the attachment directory `%s'.") % dirname)
except Exception as e:
log.error(_("Could not create the attachment directory `%s'. "
"Abort.") % dirname, detail=str(e))
sys.exit(1)
repo = DbProfilerRepository.DbProfilerRepository(repofile)
repo.init()
repo.open()
if cmd == 'add':
assert objtype and objid and filename and dirname
try:
shutil.copy(filename, dirname)
except Exception as e:
log.error(_("Could not copy `%s' to `%s'. "
"Abort.") % (filename, dirname), detail=str(e))
sys.exit(1)
try:
if len(Attachment.find(objid, objtype,
os.path.basename(filename))) == 0:
Attachment.create(objid, objtype, os.path.basename(filename))
else:
log.info(_("`%s' has already been attached. Skipping.") % filename)
except Exception as e:
log.error(_("Could not add file name to the repository. "
"Abort."), detail=str(e))
sys.exit(1)
log.info(_("Attached `%s'.") % (filename))
elif cmd == 'list':
assert objtype and objid
try:
for f in Attachment.find(objid, objtype):
print(f)
except Exception as e:
log.error(_("Could not get file names from the repository. "
"Abort."), detail=str(e))
sys.exit(1)
elif cmd == 'rm':
assert objtype and objid and filename
try:
a = Attachment.find(objid, objtype, filename)
a.destroy()
except Exception as e:
log.error(_("Could not remove the file from the repository. "
"Abort."), detail=str(e))
sys.exit(1)
log.info(_("Removed `%s' from the repository.") % filename)
repo.close()
sys.exit(0)