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
|
#!/usr/bin/env python
import argparse
from glob import glob
import os
from os.path import isfile, isdir
import sys
from enthought.util.updates.info_file import InfoFile
def build_argparser():
parser = argparse.ArgumentParser(
description = "Converts .info files into XML files compatible with " \
"the enthought.updates library. Supports batch " \
"operations on directories.")
parser.add_argument("filespecs", type=str, nargs="+",
help="Filenames and/or directories to be searched for info files")
parser.add_argument("-a", "--append", type=argparse.FileType("r"),
nargs = 1,
help = "name of output file; if it exists, append to it")
parser.add_argument("-o", "--output", type=argparse.FileType("w"),
nargs = 1,
help = "name of output file; if it exists, it is overwritten")
parser.add_argument("-r", "--recurse", action="store_true", default=False,
help = "search all subdirectories of provided dirs for .info files")
parser.add_argument("-l", "--location", type=str, default="",
help = "URI to use as the 'location' XML tag of each .info file")
return parser
def main():
parser = build_argparser()
opts = parser.parse_args(sys.argv[1:])
# Process all the files
xml_strs = []
filespecs = opts.filespecs[:]
for filespec in filespecs:
# A concrete .info file:
if isfile(filespec):
f = InfoFile.from_info_file(filespec)
f.location = opts.location
xml_strs.append(f.to_xml_str())
elif isdir(filespec):
filespecs.extend(glob(os.path.join(filespec, "*.info")))
if opts.recurse:
# Also add all the subdirectories
filespecs.extend(d for d in os.listdir(filespec) if isdir(d))
# Output appropriately
if getattr(opts, "append", None):
raise NotImplementedError
if opts.output is None:
opts.output = "updates.xml"
outfile = file(opts.output, "w")
outfile.write("\n".join(xml_strs) + "\n")
outfile.close()
if __name__ == "__main__":
main()
|