Skip to content

Instantly share code, notes, and snippets.

@alaniwi
Last active October 2, 2017 12:08
Show Gist options
  • Save alaniwi/e2f0ccdd2b34319503ba942a793e8426 to your computer and use it in GitHub Desktop.
Save alaniwi/e2f0ccdd2b34319503ba942a793e8426 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Generates an HTML file with a table listing the esg-node script versions
in each snapshot.
"""
import os
import glob
import re
class SnapshotLister:
def __init__(self, fileroot, urlroot, verbose=False):
self.fileroot = fileroot
self.urlroot = urlroot
self.verbose = verbose
def _is_valid_snapshot(self, relpath):
if relpath.startswith("."):
return False
return os.path.exists(os.path.join(self.fileroot, relpath, "dist"))
def list_snapshots(self):
return filter(self._is_valid_snapshot,
os.listdir(self.fileroot))
def _last_matching(self, bits):
"""
join bits to of a glob pattern and return most recent match
"""
pattern = os.path.join(*bits)
matches = glob.glob(pattern)
matches.sort(key = lambda path:os.lstat(path).st_mtime)
return matches[-1]
def find_scripts_in_snapshot(self, relpath):
"""
Returns paths of esg-nodes script in a snapshot as 2-tuple
(master, devel)
"""
elem_master = [self.fileroot, relpath, "dist", "esgf-installer",
"*.*", "esg-node"]
elem_devel = elem_master[:3] + ["devel"] + elem_master[3:]
return (self._last_matching(elem_master),
self._last_matching(elem_devel))
def find_versions_in_snapshot(self, relpath):
return map(self.find_version_from_script,
self.find_scripts_in_snapshot(relpath))
def find_version_from_script(self, script_path):
f = open(script_path)
lines = f.readlines()
f.close()
for line in lines:
m = re.match('script_version="(.*)"', line)
if m:
return m.group(1)
return "unknown"
def make_snapshot_link(self, relpath):
url = os.path.join(self.urlroot, relpath)
return '<a href="{0}">{1}</a>'.format(url, relpath)
def get_table_rows(self):
rows = []
for relpath in self.list_snapshots():
try:
versions = self.find_versions_in_snapshot(relpath)
except IndexError:
continue
rows.append([self.make_snapshot_link(relpath),
versions[0],
versions[1]])
if self.verbose:
print relpath, versions
return rows
def make_index(self):
path = os.path.join(self.fileroot, 'versions.html')
if self.verbose:
print path
f = open(path, 'w')
f.write('<html><head><title>versions in snapshots</title></head>\n')
f.write('<body>')
f.write('<p>This table lists the value of <code>script_version</code> seen in the <code>esg-node</code> script in the most recently modified version directory inside each snapshot</p>')
f.write('<table border="1" cellpadding="5" cellspacing="0"><th>Snapshot</th><th>Master</th><th>Devel</th>')
for row in self.get_table_rows():
f.write('<tr>')
for cell in row:
f.write('<td>' + cell + '</td>')
f.write('</tr>')
f.write('</table></body></html>\n')
f.close()
if __name__ == '__main__':
sl = SnapshotLister('/path/to/esgf_snapshots/',
'http://url.path.to/esgf_snapshots/',
verbose=True)
sl.make_index()
@alaniwi
Copy link
Author

alaniwi commented Oct 2, 2017

Script to create versions.html

First edit paths on line 99 and 100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment