comparison document_it.py @ 22:67e4becc7d49

first steps for cleaning up this awful mess
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 03 Aug 2011 10:58:45 -0700
parents 7299c4529b41
children e6ed732e8ce6
comparison
equal deleted inserted replaced
21:10c5e6d11ef8 22:67e4becc7d49
26 import os 26 import os
27 import sys 27 import sys
28 import tempfile 28 import tempfile
29 import urllib2 29 import urllib2
30 30
31 # necessary imports 31 # import markdown
32 try: 32 try:
33 import markdown 33 import markdown
34 except ImportError: 34 except ImportError:
35 raise ImportError("markdown is not installed, run (e.g.):\neasy_install Markdown") 35 raise ImportError("markdown is not installed, run (e.g.):\neasy_install Markdown")
36 36
37 DIR=os.path.dirname(os.path.abspath(__file__)) # XXX currently unused 37 destinations = {'stage': 'https://developer-stage9.mozilla.org/@api/deki/pages/=%(page)s/contents',
38 'MDN': 'https://developer.mozilla.org/@api/deki/pages/=%(page)s/contents'}
39
38 40
39 def find_readme(directory): 41 def find_readme(directory):
40 """find a README file in a directory""" 42 """find a README file in a directory"""
41 # XXX currently unused 43 # XXX currently unused
42 README=['README.md', 'README.txt', 'README'] 44 README=['README.md', 'README.txt', 'README']
44 path = os.path.join(directory, name) 46 path = os.path.join(directory, name)
45 if os.path.exists(path): 47 if os.path.exists(path):
46 return path 48 return path
47 49
48 def all_files(directory): 50 def all_files(directory):
51 """get all files in a directory tree"""
49 filenames = [] 52 filenames = []
50 for dirpath, dirnames, files in os.walk(directory): 53 for dirpath, dirnames, files in os.walk(directory):
51 filenames.extend([os.path.join(dirpath, f) for f in files]) 54 filenames.extend([os.path.join(dirpath, f) for f in files])
52 return sorted(filenames) 55 return sorted(filenames)
53 56
54 def parse_manifest(filename, baseurl, directory=None): 57 def parse_manifest(filename, directory=None):
55 """ 58 """
56 reads a documentation manifest; returns a list of two-tuples: 59 reads a documentation manifest; returns a list of two-tuples:
57 [(filename, destination)] 60 [(filename, destination)]
58 """ 61 """
59 62
63 directory = os.path.dirname(os.path.abspath(filename)) 66 directory = os.path.dirname(os.path.abspath(filename))
64 lines = [line.strip() for line in file(filename).readlines()] 67 lines = [line.strip() for line in file(filename).readlines()]
65 lines = [line for line in lines 68 lines = [line for line in lines
66 if line and not line.startswith('#')] 69 if line and not line.startswith('#')]
67 items = [] 70 items = []
68 baseurl = baseurl.rstrip('/')
69 for line in lines: 71 for line in lines:
70 try: 72 try:
71 f, url = line.split() 73 f, page = line.split()
72 # TODO: include options as third segment (e.g. format=ReST) 74 # TODO: include options as third segment (e.g. format=ReST)
73 except ValueError: 75 except ValueError:
74 raise ValueError("illegal manifest line: '%s'" % line) 76 raise ValueError("illegal manifest line: '%s'" % line)
75 77
76 if '://' not in url:
77 url = '%s%s' % (baseurl, url)
78 filename = os.path.join(directory, f) 78 filename = os.path.join(directory, f)
79 if os.path.isdir(filename): 79 if os.path.isdir(filename):
80 raise NotImplementedError
80 files = all_files(filename) 81 files = all_files(filename)
81 for i in files: 82 for i in files:
82 relpath = os.path.relpath(i, filename) 83 relpath = os.path.relpath(i, filename)
83 items.append((i, '%s%s' % (url.rstrip('/'), relpath.lstrip('/')))) 84 items.append((i, relpath))
84 else: 85 else:
85 items.append((filename, url)) 86 items.append((filename, page))
86 return items 87 return items
87 88
88 def main(args=sys.argv[1:]): 89 def main(args=sys.argv[1:]):
89 90
90 # default output directory 91 # default output directory
131 baseurl = 'file://' + os.path.abspath(options.dest) 132 baseurl = 'file://' + os.path.abspath(options.dest)
132 133
133 # read the manifests 134 # read the manifests
134 files = [] 135 files = []
135 for manifest in manifests: 136 for manifest in manifests:
136 for item in parse_manifest(manifest, baseurl, options.directory): 137 for item in parse_manifest(manifest, options.directory):
137 if item not in files: 138 if item not in files:
138 files.append(item) 139 files.append(item)
139 if options.list: 140 if options.list:
140 for item in files: 141 for item in files:
141 print '%s -> %s' % item 142 print '%s -> %s' % item