| 1 |
.. code-block:: python |
| 2 |
|
| 3 |
#!/usr/bin/env python |
| 4 |
|
| 5 |
import os |
| 6 |
import re |
| 7 |
import shutil |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
import tempfile |
| 11 |
from argparse import ArgumentParser |
| 12 |
|
| 13 |
"""Download all versions of the psidev OBO for mzML files""" |
| 14 |
|
| 15 |
CVS_SERVER = ":pserver:anonymous:" "@psidev.cvs.sourceforge.net:/cvsroot/psidev" |
| 16 |
|
| 17 |
|
| 18 |
class CVSClient(object): |
| 19 |
def __init__(self, root, client_dir=None, verbose=False): |
| 20 |
self.root = root |
| 21 |
|
| 22 |
self.client_dir = client_dir |
| 23 |
self.cleanup = False |
| 24 |
self.verbose = verbose |
| 25 |
|
| 26 |
def __enter__(self): |
| 27 |
if not self.client_dir or not os.path.isdir(self.client_dir): |
| 28 |
self.client_dir = tempfile.mkdtemp(prefix="cvs") |
| 29 |
self.cleanup = True |
| 30 |
|
| 31 |
return self |
| 32 |
|
| 33 |
def __exit__(self, exc_type, exc_value, traceback): |
| 34 |
if self.cleanup: |
| 35 |
shutil.rmtree(self.client_dir, ignore_errors=True) |
| 36 |
|
| 37 |
def __call__(self, *args, **kwargs): |
| 38 |
command = ["cvs", "-d" + self.root] + list(args) |
| 39 |
|
| 40 |
# Default args for subprocess |
| 41 |
processkwargs = dict(cwd=self.client_dir) |
| 42 |
|
| 43 |
if not self.verbose: |
| 44 |
command.insert(1, "-Q") |
| 45 |
processkwargs["stderr"] = open(os.devnull, "w") |
| 46 |
|
| 47 |
# Override defaults with provided args |
| 48 |
processkwargs.update(kwargs) |
| 49 |
|
| 50 |
if self.verbose: |
| 51 |
print(">>> " + " ".join(command), file=sys.stderr) |
| 52 |
|
| 53 |
return subprocess.check_output(command, **processkwargs) |
| 54 |
|
| 55 |
|
| 56 |
def get_version_map(cvs): |
| 57 |
filename = "psi/psi-ms/mzML/controlledVocabulary/psi-ms.obo" |
| 58 |
|
| 59 |
# Download the OBO file and get its revision log |
| 60 |
cvs("-z3", "co", filename) |
| 61 |
revisions = parse_revisions(cvs("-z3", "log").split("\n")) |
| 62 |
|
| 63 |
version_map = {} |
| 64 |
for revision in revisions: |
| 65 |
file_at_revision = cvs("-z3", "co", "-p", "-r", revision, filename) |
| 66 |
version = parse_version(file_at_revision) |
| 67 |
|
| 68 |
# Revisions go from newest to oldest, so if a version exists in the |
| 69 |
# dictionary, it's already the newest revision of that version |
| 70 |
if version and version not in version_map: |
| 71 |
version_map[version] = file_at_revision |
| 72 |
|
| 73 |
return version_map |
| 74 |
|
| 75 |
|
| 76 |
def parse_revisions(revision_log): |
| 77 |
revisions = [] |
| 78 |
revision_regexp = re.compile(r"revision (\d+\.\d+)") |
| 79 |
for line in revision_log: |
| 80 |
match = revision_regexp.match(line) |
| 81 |
if match: |
| 82 |
revisions.append(match.group(1)) |
| 83 |
return revisions |
| 84 |
|
| 85 |
|
| 86 |
def parse_version(file_string): |
| 87 |
version_regexp = re.compile(r"remark:\s+version: (\d+\.\d+\.\d+\S*)") |
| 88 |
version = None |
| 89 |
for line in file_string.split("\n"): |
| 90 |
match = version_regexp.match(line) |
| 91 |
if match: |
| 92 |
version = match.group(1) |
| 93 |
|
| 94 |
return version |
| 95 |
|
| 96 |
|
| 97 |
def save_versions(version_map, destination): |
| 98 |
for version, file_at_version in version_map.iteritems(): |
| 99 |
destination_path = os.path.join(destination, "psi-ms-{0}.obo".format(version)) |
| 100 |
with open(destination_path, "w+") as destination_file: |
| 101 |
destination_file.write(file_at_version) |
| 102 |
|
| 103 |
|
| 104 |
if __name__ == "__main__": |
| 105 |
parser = ArgumentParser(description=__doc__) |
| 106 |
parser.add_argument("destination", help="directory into which the OBO files go") |
| 107 |
parser.add_argument( |
| 108 |
"-v", "--verbose", action="store_true", help="show extra logging information" |
| 109 |
) |
| 110 |
args = parser.parse_args() |
| 111 |
|
| 112 |
# Sanity checking |
| 113 |
assert os.path.isdir(args.destination), "destination must be a valid directory" |
| 114 |
|
| 115 |
with CVSClient(CVS_SERVER, verbose=args.verbose) as cvs: |
| 116 |
cvs("login") |
| 117 |
revision_map = get_version_map(cvs) |
| 118 |
save_versions(revision_map, args.destination) |
| 119 |
|
| 120 |
# vim: ts=4:sw=4:sts=4 |