|
31 |
# /usr/share/common-licenses/AGPL-3; if not, see |
31 |
# /usr/share/common-licenses/AGPL-3; if not, see |
32 |
# <http://www.gnu.org/licenses/>. |
32 |
# <http://www.gnu.org/licenses/>. |
33 |
|
33 |
|
34 |
import univention.debug as ud |
34 |
import sys |
35 |
|
35 |
import locale |
36 |
from optparse import OptionParser |
36 |
from optparse import OptionParser |
37 |
|
37 |
|
38 |
import os |
38 |
import univention.debug as ud |
39 |
import sys |
39 |
import univention.admin.uldap |
40 |
|
40 |
|
41 |
import univention.directory.reports as udr |
41 |
from univention.directory.reports import Report, ReportError, Config |
42 |
from univention.config_registry import ConfigRegistry |
42 |
from univention.config_registry import ConfigRegistry |
43 |
|
43 |
|
|
|
44 |
from univention.lib.i18n import Translation |
45 |
_ = Translation('univention-directory-reports').translate |
44 |
|
46 |
|
45 |
baseConfig = ConfigRegistry() |
47 |
ucr = ConfigRegistry() |
46 |
baseConfig.load() |
48 |
ucr.load() |
47 |
|
49 |
|
48 |
|
50 |
|
49 |
def dump_modules(cfg, module=None, out=sys.stdout): |
51 |
def dump_modules(cfg, module=None, out=sys.stdout): |
Lines 53-65
def dump_modules(cfg, module=None, out=sys.stdout):
|
Link Here
|
---|
|
53 |
else: |
55 |
else: |
54 |
modules = [module] |
56 |
modules = [module] |
55 |
for module in modules: |
57 |
for module in modules: |
56 |
print >>out, 'Reports for module: %s' % module |
58 |
print >> out, 'Reports for module: %s' % module |
57 |
for name in cfg.get_report_names(module): |
59 |
for name in cfg.get_report_names(module): |
58 |
print >>out, ' - %s' % name |
60 |
print >> out, ' - %s' % name |
59 |
|
61 |
|
60 |
|
62 |
|
61 |
def main(): |
63 |
def main(): |
62 |
cfg = udr.Config() |
64 |
cfg = Config() |
63 |
|
65 |
|
64 |
parser = OptionParser(usage='usage: %prog -m <module> [options] dn1 dn2 ...') |
66 |
parser = OptionParser(usage='usage: %prog -m <module> [options] dn1 dn2 ...') |
65 |
parser.add_option( |
67 |
parser.add_option( |
|
80 |
help='file containing the footer for the report') |
82 |
help='file containing the footer for the report') |
81 |
parser.add_option( |
83 |
parser.add_option( |
82 |
'-s', '--server', action='store', |
84 |
'-s', '--server', action='store', |
83 |
dest='server', default=baseConfig.get('ldap/server/name', 'localhost'), |
85 |
dest='server', default=ucr.get('ldap/server/name', 'localhost'), |
84 |
help='LDAP server [%default]') |
86 |
help='LDAP server [%default]') |
85 |
parser.add_option( |
87 |
parser.add_option( |
86 |
'-b', '--base', action='store', |
88 |
'-b', '--base', action='store', |
87 |
dest='base', default=baseConfig.get('ldap/base', ''), |
89 |
dest='base', default=ucr.get('ldap/base', ''), |
88 |
help='LDAP base [%default]') |
90 |
help='LDAP base [%default]') |
89 |
parser.add_option( |
91 |
parser.add_option( |
90 |
'-m', '--module', action='store', |
92 |
'-m', '--module', action='store', |
|
99 |
dest='list_reports', default=False, |
101 |
dest='list_reports', default=False, |
100 |
help='List names of available reports') |
102 |
help='List names of available reports') |
101 |
parser.add_option( |
103 |
parser.add_option( |
102 |
'-n', '--no-cleanup', action='store_true', |
|
|
103 |
dest='no_cleanup', default=False, |
104 |
help='do not remove the temporary LaTeX files (for debugging)') |
105 |
parser.add_option( |
106 |
'-c', '--config', action='store', |
104 |
'-c', '--config', action='store', |
107 |
dest='config', default='/etc/univention/directory/reports/config.ini', |
105 |
dest='config', default='/etc/univention/directory/reports/config.ini', |
108 |
help='location of the configuration file [%default]') |
106 |
help='location of the configuration file [%default]') |
|
113 |
|
111 |
|
114 |
(options, args) = parser.parse_args() |
112 |
(options, args) = parser.parse_args() |
115 |
|
113 |
|
116 |
if not options.user or not options.password: |
114 |
ud.init('/var/log/univention/directory-reports.log', ud.FLUSH, ud.FUNCTION) |
117 |
try: |
|
|
118 |
pwdfile = '/etc/machine.secret' |
119 |
options.user = baseConfig['ldap/hostdn'] |
120 |
if baseConfig['server/role'] == 'domaincontroller_master': |
121 |
pwdfile = '/etc/ldap.secret' |
122 |
options.user = 'cn=admin,%s' % baseConfig['ldap/base'] |
123 |
fd = open(pwdfile, 'r') |
124 |
options.password = fd.readline()[: -1] |
125 |
fd.close() |
126 |
except: |
127 |
print >>sys.stderr, "error: user and/or password not specified" |
128 |
parser.print_help(sys.stderr) |
129 |
sys.exit(1) |
130 |
|
131 |
ud.init('/var/log/univention/directory-reports.log', 1, 1) |
132 |
ud.set_level(ud.ADMIN, options.debug) |
115 |
ud.set_level(ud.ADMIN, options.debug) |
133 |
|
116 |
|
134 |
cfg = udr.Config(options.config) |
117 |
locale.setlocale(locale.LC_ALL, locale.getdefaultlocale()) |
|
|
118 |
|
119 |
cfg = Config(options.config) |
135 |
|
120 |
|
136 |
if options.list_reports: |
121 |
if options.list_reports: |
137 |
dump_modules(cfg, options.module) |
122 |
dump_modules(cfg, options.module) |
138 |
sys.exit(0) |
123 |
sys.exit(0) |
139 |
|
124 |
|
140 |
template = cfg.get_report(options.module, options.report) |
|
|
141 |
if template is None: |
142 |
parser.print_usage(sys.stderr) |
143 |
if options.module is None: |
144 |
print >>sys.stderr, "error: module not specified (use -m)" |
145 |
elif options.module not in cfg._reports: |
146 |
print >>sys.stderr, "error: specified module '%s' does not exist" % options.module |
147 |
options.module = None |
148 |
elif options.report is not None: |
149 |
report_entry = cfg._get_report_entry(options.module, options.report) |
150 |
if report_entry is None: |
151 |
print >>sys.stderr, "error: specified report '%s' does not exist" % options.report |
152 |
else: |
153 |
print >>sys.stderr, "error: specified report '%s' is unavailable" % options.report |
154 |
name, dir, filename = report_entry |
155 |
if not os.path.exists(filename): |
156 |
print >>sys.stderr, "Template file '%s' seems to be missing." % filename |
157 |
else: |
158 |
print >>sys.stderr, "Check settings in file '%s'." % cfg._filename |
159 |
sys.exit(2) |
160 |
else: |
161 |
print >>sys.stderr, "error: no report found for module '%s'" % options.module |
162 |
dump_modules(cfg, options.module, sys.stderr) |
163 |
sys.exit(2) |
164 |
|
165 |
if not args: |
125 |
if not args: |
166 |
parser.print_usage(sys.stderr) |
126 |
parser.print_usage(sys.stderr) |
167 |
print >>sys.stderr, "error: no DNs specified on command line" |
127 |
print >> sys.stderr, _("Error: no DNs specified on command line") |
168 |
sys.exit(2) |
128 |
sys.exit(2) |
169 |
|
129 |
|
170 |
udr.admin.connect(options.user, options.password, host=options.server, base=options.base, start_tls=0) |
130 |
# FIXME: why is start_tls=0 used here? |
|
|
131 |
if options.user and options.password: |
132 |
lo = univention.admin.uldap.access(host=options.server, base=options.base, binddn=options.user, bindpw=options.password, start_tls=0) |
133 |
else: |
134 |
try: |
135 |
if ucr['server/role'] == 'domaincontroller_master': |
136 |
lo, po = univention.admin.uldap.getAdminConnection(start_tls=0) |
137 |
else: |
138 |
lo, po = univention.admin.uldap.getMachineConnection(start_tls=0) |
139 |
except IOError: |
140 |
print >> sys.stderr, _("Error: user and/or password not specified") |
141 |
parser.print_help(sys.stderr) |
142 |
sys.exit(1) |
171 |
|
143 |
|
|
|
144 |
report = Report(lo, config=cfg) |
172 |
try: |
145 |
try: |
173 |
if options.header is None: |
146 |
filename = report.create(options.module, options.report, args) |
174 |
options.header = cfg.get_header(options.module, options.report) |
147 |
except ReportError as exc: |
175 |
if options.footer is None: |
148 |
print >> sys.stderr, _("Error: The %s report could not be created: %s") % (options.report, exc) |
176 |
options.footer = cfg.get_footer(options.module, options.report) |
|
|
177 |
doc = udr.Document(template, header=options.header, footer=options.footer) |
178 |
except NameError as e: # missing file |
179 |
print >>sys.stderr, e |
180 |
sys.exit(1) |
149 |
sys.exit(1) |
181 |
tmpfile = doc.create_source(args) |
|
|
182 |
if doc._type == udr.Document.TYPE_LATEX: |
183 |
type = 'PDF' |
184 |
outfile = doc.create_pdf(tmpfile) |
185 |
if options.no_cleanup: |
186 |
print >>sys.stderr, 'kept temporary source file at %s' % tmpfile |
187 |
else: |
188 |
basefile = tmpfile.rsplit('.', 1)[0] # strip suffix |
189 |
for file in [tmpfile] + ['%s.%s' % (basefile, suffix) for suffix in ('aux', 'log')]: |
190 |
try: |
191 |
os.unlink(file) |
192 |
except OSError as e: |
193 |
pass |
194 |
elif doc._type == udr.Document.TYPE_CSV: |
195 |
type = 'CSV' |
196 |
outfile = tmpfile |
197 |
else: |
198 |
type = '' |
199 |
outfile = tmpfile |
200 |
|
150 |
|
201 |
if os.path.exists(outfile): |
151 |
print _('The %s has been created: %s') % (options.report, filename) |
202 |
print 'created %s file: %s' % (type, outfile) |
|
|
203 |
else: |
204 |
print >>sys.stderr, "error: report could not be created" |
205 |
sys.exit(1) |
206 |
|
152 |
|
207 |
|
153 |
|
208 |
if __name__ == "__main__": |
154 |
if __name__ == "__main__": |