This file is indexed.

/usr/bin/svgtune is in svgtune 0.2.0-2.

This file is owned by root:root, with mode 0o755.

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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/python
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
  Yaroslav Halchenko
  web:     http://www.onerussian.com
  e-mail:  yoh@onerussian.com

 DESCRIPTION (NOTES):

    See README.rst shipped with this tool.
    "Homepage" for the tool is http://github.com/yarikoptic/svgtune

 LICENSE:

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the
  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  MA 02110-1301, USA.

 On Debian system see /usr/share/common-licenses/GPL for the full license.
"""

__author__ = 'Yaroslav Halchenko'
__version__ = (0, 2, 0)
__copyright__ = 'Copyright (c) 2009-2015 Yaroslav Halchenko'
__license__ = 'GPL'

import os, re
from lxml import etree
from optparse import OptionParser

# Setup rudimentary logging
import logging
lgr = logging.getLogger("svgtune")
lgr.addHandler(logging.StreamHandler())


def split2(l, msg, sep=' '):
    """Split l into 2 pieces using ' ' as a separator
    """
    p = [x.strip() for x in (l.split(sep, 1)) if x != '']

    if len(p) != 2:
        raise ValueError(msg + '. Got %s for "%s"' % (str(p), l))

    return p


def replace_key(attr_value, key, value):
    """Replace value for a given key

       If key is not found, new one is added
    """
    v = attr_value.split(';')
    changed = False
    newval = '%s:%s' % (key, value)
    for i in xrange(len(v)):
        if v[i].startswith(key + ':'):
            v[i] = newval
            changed = True
    if not changed:
        # Wasn't found -- add one
        v += [newval]
    return ';'.join([x for x in v if x.strip() != ''])


def change_attr(el, attr, values):
    """Change values listed within attribute attr
    """
    v = el.attrib.get(attr, '')
    changed = False
    for value in values.split(';'):
        k, newv = split2(value, "Each value must be in the form x:y", ":")
        v = replace_key(v, k, newv)
    if v == '':                         # there were no such yet
        v = "%s:%s" % (k, newv)
    #print "Changing %s : %s, got %s" % (attr, values, str(v))
    el.attrib[attr] = v


def verbose(level, str_):
    if level <= options.verbose:
        lgr.info(" "*(level-1) + str_)


def version():
    print("svgtune v" + ".".join(__version__))
    raise SystemExit


def process_options(params, options):
    # For now simply split by space
    option_values = params.split(' ')
    for option_value in option_values:
        t = option_value.split('=', 1)
        option = t[0]
        value = t[1] if len(t) > 1 else None
        if option in ["previews", "nosvgs"]:
            # if only said "previews" assume that we want them
            if value is None:
                value = True
            setattr(options, option, bool(value))
        else:
            raise ValueError("Unknown option %s" % option)
    pass


def load_svg(svgfile):
    verbose(1, "Processing file %s for tuning" % svgfile)
    svgdoc = etree.parse(svgfile)
    svg = svgdoc.getroot()

    dname = '%s_tuned' % os.path.splitext(svgfile)[0]
    try:
        os.mkdir(dname)
    except:
        pass # if directory exists or hell with everything
    return svg, svgdoc, dname


def parse_cmdline():
    parser = OptionParser(usage="%prog [options] inputfile.svgtune",
                          version="%prog version "
                                  + ".".join([str(i) for i in __version__]))
    parser.add_option("-v", action="count", dest="verbose",
                      help="Increase verbosity with multiple -v")
    parser.add_option("-p", "--previews", action="store_true",
                      dest="previews", help="Store preview png's")
    parser.add_option("--no-svgs", action="store_true",
                      dest="nosvgs", help="Remove 'final' svgs")


    (options_, args) = parser.parse_args()

    if len(args) != 1:
        raise SystemExit(
            "Error: Please provide single input file with instructions.")

    lgr.setLevel(logging.DEBUG if options_.verbose >= 4 else logging.INFO)

    return args[0], options_


def svgtune(ifile, options):
    svg = None
    dname = None

    for line_ in open(ifile).readlines():
        line = line_.strip()
        if line.startswith('#') or line == '':
            continue
        if line.rstrip() == '%exit':
            break

        # parse out first element
        cmd, params = split2(line, "Each line must be 'command parameters'")

        if cmd == '%file':
            svg, svgdoc, dname = load_svg(params)
            continue
        elif cmd == "%options":
            process_options(params, options)
            continue

        # We must have file loaded by now
        if svg is None:
            svgfile = os.path.splitext(ifile)[0] + '.svg'
            try:
                svg, svgdoc, dname = load_svg(svgfile)
            except Exception, e:
                raise RuntimeError(
                    "Tried to load from %s but failed due to %s.\n"
                    "Please provide %%file directive to load the file prior %s "
                    "or have .svg file with the same name as .svgtune."
                    % (svgfile, e, cmd))

        elif cmd == '%save':
            ofile = '%s/%s.svg' % (dname, params)
            verbose(1, "Storing into %s" % ofile)
            with file(ofile, 'w') as f:
                f.write(etree.tostring(svgdoc, pretty_print=True))
            if options.previews:
                verbose(3, "Generating preview")
                os.system('inkscape -z -f %s -e %s -d 300 >/dev/null 2>&1' %
                          (ofile, ofile.replace('.svg', '_preview.png')))
            if options.nosvgs:
                verbose(4, "Removing generated svg")
                os.remove(ofile)
            continue

        #
        # Figure out the victims for changes -- victims
        victims = None
        if cmd == 'layers':
            changes = params
            victims = svg.findall('.//{%s}g[@{%s}groupmode="layer"]'
                                  % (svg.nsmap['svg'], svg.nsmap['inkscape']))
        elif cmd in ['layer', 'g', 'text', 'any']:
            # parse out first element
            identifier, changes = split2(params,
                       "For each layer or g you must list id or label + changes")

            # determine the victims
            sid = ''
            id1, id2 = identifier.split('=')
            sid_re_attr = None             # either we need to do re.search
            sid_re_str = id2

            if cmd == 'layer':
                sid += '[@{%s}groupmode="layer"]' % svg.nsmap['inkscape']
                elem = 'g'
            elif cmd == 'any':
                # will be custom anyways
                pass
            else:
                elem = cmd


            if id1 == 'label':
                sid += '[@{%s}label="%s"]' % (svg.nsmap['inkscape'], id2)
            elif id1 == 'id':
                sid += '[@id="%s"]' % (id2,)
            elif id1 == 'href':
                sid += '[@{%s}href="%s"]' % (svg.nsmap['xlink'], id2)
            elif id1 == 'href:re':
                sid += '{%s}href' % svg.nsmap['xlink']
                lgr.debug(svg.nsmap['xlink'])
                lgr.debug("sid=%r" % sid)
            elif id1 == 'label:re':
                sid_re_attr = '{%s}label' % svg.nsmap['inkscape']
            elif id1 == 'id:re':
                sid_re_attr = 'id'
            else:
                raise ValueError("Unknown identifier %s in %s" % (id1, line))

            if cmd != 'any':
                victims = svg.findall('.//{%s}%s%s'
                                      % (svg.nsmap['svg'], elem, sid))
                lgr.debug("Found %d victims searching for %r" % (len(victims), sid))
            else:
                victims = svg.findall('.//')
                lgr.debug("Found %d overall victims" % len(victims))
                if not id1.endswith(':re'):
                    victims = filter(lambda v: v.attrib.get(id1, None) == id2,
                                     victims)
                    lgr.debug("After selection based on %s was left with "
                              "%d victims" % (id1, len(victims)))

            # optionally perform search using re
            if sid_re_attr is not None:
                regexp = re.compile(sid_re_str)
                victims_orig = victims # for debugging
                victims = [v for v in victims
                           if regexp.search(v.attrib.get(sid_re_attr, ""))]
                lgr.debug("Left with %d after matching regexp %r"
                          % (len(victims), sid_re_str))

            nvictims = len(victims)
            if nvictims == 0:
                raise ValueError("Cannot find any victim for '%s'" % identifier)
            elif nvictims > 1 and not sid_re_attr:
                raise ValueError(
                    "We should get a single victim for %s "
                    "but got %d of them" % (identifier, nvictims))
        else:
            raise ValueError("Unknown command '%s'." % cmd)

        #
        # Figure out the actual changes to take and do them
        for change in changes.split(' '):
            if change == '':
                continue
            attr, values = split2(
                change,
                'Each change should be listed as attribute=value',
                '=')
            if attr == 'style':
                func = lambda vic: change_attr(vic, attr, values)
            else:
                raise ValueError("Don't yet handle attribute %s" % attr)

            for victim in victims:
                func(victim)


if __name__ == "__main__":
    ifile, options = parse_cmdline()
    svgtune(ifile, options)