#!/usr/bin/env python import urllib, urllib2, re, os, sys from urlparse import urlparse def help(): print """ $ vc [URL] [OPTIONS] VC is a Version Control application. VC reads the version number from a text file in a website and checks it against the local version number. If your files are outdated, it patches them with ones downloaded from the server. The URL has to include the full path to the file. For example, this would be a valid URL: http://www.domain.com/directory/versions.txt '-v' makes vc verbose. In verbose mode, the output of all subprocesses are piped to you. Verbose mode is off my default.""" sys.exit() def get_text(url): request = urllib2.Request(url) return urllib2.urlopen(request).read() def get_version_numbers(text): versions = [] raw_versions = re.compile('\w\d+[.]\d+').findall(text) for x in raw_versions: if "a" in x: program_version = x versions.append(x) elif "b" in x: code_version = x versions.append(x) elif "c" in x: graphics_version = x versions.append(x) else: print("Warning: non-matching version number found in text file.\n") if len(versions) <= 3: versions.sort() version_result = """These are the latest version numbers: Program: %s Code: %s Graphics: %s""" % (versions[0], versions[1], versions[2]) else: sys.stderr.write("Could not find version numbers in text file.\n") sys.exit() return version_result def get_file_urls(text): files = re.compile('http://[.a-z~/]+').findall(text) for x in versions: if "program" in x: program_file = "A" + x files.append(x) elif "code" in x: code_file = "B" + x files.append(x) elif "graphics" in x: graphics_file = "C" + x files.append(x) else: print("Warning: non-matching file URL found in text file.\n") if len(files) >= 3: del(files[0:-3]) else: sys.stderr.write("Could not find file URLs in text file.\n") sys.exit() files.sort() file_result = """These are the latest file URLs: Program: %d Code: %d Graphics: %d""" %(files[0], files[1], files[2]) return result def parse_numbers(version): version = version[1:] return version def get_files(url): if verbose == True: command = "wget %s" % url else: command = "wget -q %s" % url os.popen(command) def get_local_version_numbers(): try: local_file = open("vc.txt", "r") except: sys.stderr.write("Could not find local version file (vc.txt)") sys.exit() file_lines = local_file.readlines() local_versions = "".join(file_lines) local_file.close() local_versions.split() local_versions.sort() if len(local_versions) < 3: sys.stderr.write("Could not find enough version numbers in local version file (vc.txt)") sys.exit() else: return local_versions verbose = False if len(sys.argv) < 2: help() else: url = sys.argv[1] if url.count("://") == 0: url = "http://" + url if sys.argv < 3 and sys.argv[2] == "-v": verbose = True text = get_text(url) print get_version_numbers(text)