~ubuntu-branches/ubuntu/saucy/python-enthoughtbase/saucy

« back to all changes in this revision

Viewing changes to enthought/util/updates/info2xml.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2010-02-28 14:26:52 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100228142652-qpawn566flxqnkne
Tags: 3.0.4-1
* New upstream release
* Switch to source format 3.0
* Bump Standards-Version to 3.8.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import argparse
 
4
from glob import glob
 
5
import os
 
6
from os.path import isfile, isdir
 
7
import sys
 
8
 
 
9
from enthought.util.updates.info_file import InfoFile
 
10
 
 
11
def build_argparser():
 
12
    parser = argparse.ArgumentParser(
 
13
            description = "Converts .info files into XML files compatible with " \
 
14
                          "the enthought.updates library.  Supports batch " \
 
15
                          "operations on directories.")
 
16
    parser.add_argument("filespecs", type=str, nargs="+",
 
17
            help="Filenames and/or directories to be searched for info files")
 
18
 
 
19
    parser.add_argument("-a", "--append", type=argparse.FileType("r"),
 
20
             nargs = 1,
 
21
            help = "name of output file; if it exists, append to it")
 
22
 
 
23
    parser.add_argument("-o", "--output", type=argparse.FileType("w"),
 
24
            nargs = 1,
 
25
            help = "name of output file; if it exists, it is overwritten")
 
26
 
 
27
    parser.add_argument("-r", "--recurse", action="store_true", default=False,
 
28
            help = "search all subdirectories of provided dirs for .info files")
 
29
 
 
30
    parser.add_argument("-l", "--location", type=str, default="",
 
31
            help = "URI to use as the 'location' XML tag of each .info file")
 
32
 
 
33
    return parser
 
34
 
 
35
def main():
 
36
    parser = build_argparser()
 
37
    opts = parser.parse_args(sys.argv[1:])
 
38
 
 
39
    # Process all the files 
 
40
    xml_strs = []
 
41
    filespecs = opts.filespecs[:]
 
42
    for filespec in filespecs:
 
43
        # A concrete .info file:
 
44
        if isfile(filespec):
 
45
            f = InfoFile.from_info_file(filespec)
 
46
            f.location = opts.location
 
47
            xml_strs.append(f.to_xml_str())
 
48
        elif isdir(filespec):
 
49
            filespecs.extend(glob(os.path.join(filespec, "*.info")))
 
50
            if opts.recurse:
 
51
                # Also add all the subdirectories
 
52
                filespecs.extend(d for d in os.listdir(filespec) if isdir(d))
 
53
 
 
54
    # Output appropriately
 
55
    if getattr(opts, "append", None):
 
56
        raise NotImplementedError
 
57
 
 
58
    if opts.output is None:
 
59
        opts.output = "updates.xml"
 
60
 
 
61
    outfile = file(opts.output, "w")
 
62
    outfile.write("\n".join(xml_strs) + "\n")
 
63
    outfile.close()
 
64
 
 
65
 
 
66
if __name__ == "__main__":
 
67
    main()