~ubuntu-branches/debian/sid/meliae/sid

« back to all changes in this revision

Viewing changes to remove_expensive_references.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-12-19 18:23:37 UTC
  • Revision ID: james.westby@ubuntu.com-20091219182337-t09txw6ca1yfysn9
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Copyright (C) 2009 Canonical Ltd
 
3
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License version 3 as
 
6
# published by the Free Software Foundation.
 
7
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
# General Public License for more details.
 
12
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
"""Remove expensive references.
 
17
 
 
18
This script takes 1 or two filenames and filters the first into either std out,
 
19
or the second filename.
 
20
"""
 
21
 
 
22
import os
 
23
import re
 
24
import sys
 
25
import time
 
26
 
 
27
from meliae import files, loader
 
28
 
 
29
 
 
30
def main(args):
 
31
    import optparse
 
32
    p = optparse.OptionParser(
 
33
        '%prog INFILE [OUTFILE]')
 
34
 
 
35
    opts, args = p.parse_args(args)
 
36
    if len(args) > 2:
 
37
        sys.stderr.write('We only support 2 filenames, not %d\n' % (len(args),))
 
38
        return -1
 
39
    if len(args) < 1:
 
40
        sys.stderr.write("Must supply INFILE\n")
 
41
        return -1
 
42
 
 
43
    def source():
 
44
        infile, cleanup = files.open_file(args[0])
 
45
        for obj in loader.iter_objs(infile):
 
46
            yield obj
 
47
        cleanup()
 
48
    if len(args) == 1:
 
49
        outfile = sys.stdout
 
50
    else:
 
51
        outfile = open(args[1], 'wb')
 
52
    for _, obj in loader.remove_expensive_references(source, show_progress=True):
 
53
        outfile.write(obj.to_json() + '\n')   
 
54
    outfile.flush()
 
55
 
 
56
 
 
57
if __name__ == '__main__':
 
58
    sys.exit(main(sys.argv[1:]))
 
59