3 __author__
= 'Sridhar Ratnakumar <srid@nearfar.org>'
8 web
.webapi
.internalerror
= web
.debugerror
13 def __init__(self
, path
):
14 if os
.path
.isdir(path
) and not path
.endswith(os
.path
.sep
):
19 rest
, name
= os
.path
.split(self
.path
)
21 rest
, name
= os
.path
.split(rest
)
25 if os
.path
.isdir(self
.path
):
26 return self
.name() + "/"
32 >>> for p in File("/home/srid/foo/bar/").parents():
43 if path
.endswith("/"):
46 path
, rest
= path
.rsplit(os
.path
.sep
, 1)
49 def is_hidden_file(self
):
51 return name
.startswith('.') or name
.endswith('~')
53 def last_modified(self
):
54 t
= os
.stat(self
.path
)[stat
.ST_MTIME
]
55 return time
.strftime("%d-%b-%Y %H:%M", time
.localtime(t
))
59 def pretty_size(size
):
64 units
= ['k', 'M', 'GB', 'TB']
66 while size
>= jump
and i
< len(units
):
70 return "%.1f%s" % (size
, units
[i
- 1])
73 return File
.pretty_size(os
.stat(self
.path
)[stat
.ST_SIZE
])
79 web
.header("Content-Type","%s; charset=utf-8" % 'text/html')
80 self
.REQUEST_URI
= path
= os
.environ
.get('REQUEST_URI')
81 self
.DOCUMENT_ROOT
= os
.environ
.get('DOCUMENT_ROOT')
82 self
.DIR_PATH
= os
.path
.join(self
.DOCUMENT_ROOT
, self
.REQUEST_URI
[1:])
84 self
.title
= "Index of %s" % self
.REQUEST_URI
91 filelist
= os
.listdir(self
.DIR_PATH
)
93 p
, bzrbranch
= self
.bzrbranch()
95 body
+= '<div id="bzr">'
96 body
+= ' <p>You can get the source code for this branch via: </p>'
98 body
+= ' <a href="http://bazaar-vcs.org/">bzr</a> get %s' % bzrbranch
102 trs
= ['<tr><td><a href="..">..</a></td></tr>']
104 for file_name
in filelist
:
105 file = File(os
.path
.join(self
.DIR_PATH
, file_name
))
106 if not file.is_hidden_file():
107 file_lm
= file.last_modified()
108 file_size
= os
.path
.isdir(file.path
) and "-" or file.size()
110 '<tr><td><a href="%s">%s</a></td><td>%s</td><td class="s">%s</td><td>%s</td></tr>' % (
111 file.cgi_name(), file.cgi_name(), file_lm
, file_size
,
112 self
.description
.get(file_name
, "")))
115 print HTML
% locals()
119 SEP
= "\nContents:\n"
120 readme_dir
= os
.path
.join(self
.DIR_PATH
, "README.txt")
121 if os
.path
.exists(readme_dir
):
122 file = open(readme_dir
)
123 self
.title
= file.readline().strip() or self
.title
124 self
.body
= rest
= file.read()
127 self
.body
, desc
= rest
.split(SEP
, 1)
128 self
.body
= markdown
.markdown(self
.body
)
129 self
.description
= {}
130 for line
in desc
.splitlines():
133 key
, value
= line
.split(":", 1)
134 self
.description
[key
.strip()] = value
.strip()
139 for p
in File(self
.DIR_PATH
).parents():
140 dot_bzr
= os
.path
.join(p
, ".bzr")
141 if os
.path
.exists(dot_bzr
):
142 # infer the bzr URL (guess)
143 uri
= os
.environ
['REQUEST_URI'].split('/')
144 uri
= '/'.join(uri
[:-cnt
-1]) # whoa! hard to explain, sorry.
145 url
= "http://%s%s" % (os
.environ
['SERVER_NAME'], uri
)
148 return False, False # not a bzr branch
153 <title>%(title)s</title>
154 <style type="text/css">
155 body { color: #333333; background-color: white; font-size: 14px; margin-bottom: 20px;
156 font-family:"Lucida Grande","Bitstream Vera Sans","Verdana"; }
157 div.box { padding-left: 5em; }
158 a { color: #0033CC; }
159 a:hover { background-color: #0033CC; color: white; text-decoration: none; }
160 dl dt { font-weight: bold; }
161 li { padding-bottom: 2px; }
163 div#bzr code { padding-left: 20px; }
164 div#listdir { font-family: monospace;
165 background-color: #eeeeee;
166 border-bottom: 1px solid;
167 border-top: 1px solid;
169 div#listdir table { font-size: 13px; margin: 12px; }
170 div#listdir th, td { text-align: left; padding-right: 14px; }
171 div#listdir th { padding-bottom: 3px; }
172 div#listdir td.s {text-align: right;}
186 <th>Last Modified</th>