/usr/lib/python3/dist-packages/tldp/outputs.py is in python3-tldp 0.7.13-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | #! /usr/bin/python
# -*- coding: utf8 -*-
#
# Copyright (c) 2016 Linux Documentation Project
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals
import os
import sys
import errno
import codecs
import logging
from tldp.ldpcollection import LDPDocumentCollection
from tldp.utils import logdir
logger = logging.getLogger(__name__)
class OutputNamingConvention(object):
'''A base class inherited by OutputDirectory to ensure consistent
naming of files across the output collection of documents,
regardless of the source document type and processing toolchain
choice.
Sets a list of names for documents that are expected to be present
in order to report that the directory iscomplete.
'''
expected = ['name_txt', 'name_pdf', 'name_htmls', 'name_html',
'name_indexhtml']
def __init__(self, dirname, stem):
self.dirname = dirname
self.stem = stem
@property
def MD5SUMS(self):
return os.path.join(self.dirname, '.LDP-source-MD5SUMS')
@property
def name_txt(self):
return os.path.join(self.dirname, self.stem + '.txt')
@property
def name_fo(self):
return os.path.join(self.dirname, self.stem + '.fo')
@property
def name_pdf(self):
return os.path.join(self.dirname, self.stem + '.pdf')
@property
def name_html(self):
return os.path.join(self.dirname, self.stem + '.html')
@property
def name_htmls(self):
return os.path.join(self.dirname, self.stem + '-single.html')
@property
def name_epub(self):
return os.path.join(self.dirname, self.stem + '.epub')
@property
def name_indexhtml(self):
return os.path.join(self.dirname, 'index.html')
@property
def validsource(self):
return os.path.join(self.dirname, self.stem + '.xml') # -- burp
@property
def iscomplete(self):
'''True if the output directory contains all expected documents'''
present = list()
for prop in self.expected:
name = getattr(self, prop, None)
assert name is not None
present.append(os.path.exists(name))
return all(present)
@property
def missing(self):
'''returns a set of missing files'''
missing = set()
for prop in self.expected:
name = getattr(self, prop, None)
assert name is not None
if not os.path.isfile(name):
missing.add(name)
return missing
@property
def md5sums(self):
d = dict()
try:
with codecs.open(self.MD5SUMS, encoding='utf-8') as f:
for line in f:
if line.startswith('#'):
continue
hashval, fname = line.strip().split()
d[fname] = hashval
except IOError as e:
if e.errno != errno.ENOENT:
raise
return d
class OutputDirectory(OutputNamingConvention):
'''A class providing a container for each set of output documents
for a given source document and general methods for operating on
and preparing the output directory for a document processor.
For example, the process of generating each document type for a single
source (e.g. 'Unicode-HOWTO') would be managed by this object.
An important element of the OutputDirectory is the stem, determined
from the directory name when __init__() is called.
'''
def __repr__(self):
return '<%s:%s>' % (self.__class__.__name__, self.dirname)
@classmethod
def fromsource(cls, dirname, source):
newname = os.path.join(dirname, source.stem)
return cls(newname, source=source)
def __init__(self, dirname, source=None):
'''constructor
:param dirname: directory name for all output documents
This directory name is expected to end with the document stem name,
for example '/path/to/the/collection/Unicode-HOWTO'. The parent
directory (e.g. '/path/to/the/collection' must exist already. The
output directory itself will be created, or emptied and cleared if
the document needs to be rebuilt.
'''
self.dirname = os.path.abspath(dirname)
self.stem = os.path.basename(self.dirname)
super(OutputDirectory, self).__init__(self.dirname, self.stem)
parent = os.path.dirname(self.dirname)
if not os.path.isdir(parent):
logger.critical("Missing output collection directory %s.", parent)
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), parent)
self.status = 'output'
self.source = source
self.logdir = os.path.join(self.dirname, logdir)
def detail(self, widths, verbose, file=sys.stdout):
template = ' '.join(('{s.status:{w.status}}',
'{u:{w.doctype}}',
'{s.stem:{w.stem}}'))
outstr = template.format(s=self, w=widths, u="<unknown>")
print(outstr, file=file)
if verbose:
print(' missing source', file=file)
class OutputCollection(LDPDocumentCollection):
'''a dict-like container for OutputDirectory objects
The key of an OutputCollection is the stem name of the document, which
allows convenient access and guaranteed non-collision.
The use of the stem as a key works conveniently with the
SourceCollection which uses the same strategy on SourceDocuments.
'''
def __init__(self, dirname=None):
'''construct an OutputCollection
If dirname is not supplied, OutputCollection is basically, a dict().
If dirname is supplied, then OutputCollection scans the filesystem for
subdirectories of dirname and creates an OutputDirectory for each
subdir. Each subdir name is used as the stem (or key) for holding the
OutputDirectory in the OutputCollection.
For example, consider the following directory tree:
en
├── Latvian-HOWTO
├── Scanner-HOWTO
├── UUCP-HOWTO
└── Wireless-HOWTO
If called like OutputCollection("en"), the result in memory would be
a structure resembling this:
OutputCollection("/path/en") = {
"Latvian-HOWTO": OutputDirectory("/path/en/Latvian-HOWTO")
"Scanner-HOWTO": OutputDirectory("/path/en/Scanner-HOWTO")
"UUCP-HOWTO": OutputDirectory("/path/en/UUCP-HOWTO")
"Wireless-HOWTO": OutputDirectory("/path/en/Wireless-HOWTO")
}
'''
if dirname is None:
return
elif not os.path.isdir(dirname):
logger.critical("Output collection dir %s must already exist.",
dirname)
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), dirname)
for fname in sorted(os.listdir(dirname), key=lambda x: x.lower()):
name = os.path.join(dirname, fname)
if not os.path.isdir(name):
logger.info("Skipping non-directory %s (in %s)", name, dirname)
continue
logger.debug("Found directory %s (in %s)", name, dirname)
o = OutputDirectory(name)
assert o.stem not in self
self[o.stem] = o
#
# -- end of file
|