#!/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" or "b" or "c" in raw_versions:
versions.append(x)
else:
print "Warning: non-matching version numbers in remote text file\n"
if len(versions) == 3:
versions.sort()
version_result = """These are the latest version numbers:
Program: %s
Code: %s
Graphics: %s\n""" % (versions[0], versions[1], versions[2])
print version_result
else:
sys.stderr.write("Could not find version numbers in text file.\n")
sys.exit()
return versions
def get_file_urls(text):
raw_files = re.compile('http://[.a-z~\-\/]+').findall(text)
files = []
for x in raw_files:
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 remote text file.\n"
if len(files) == 3:
files.sort()
else:
sys.stderr.write("Could not find file URLs in text file.\n")
sys.exit()
return files
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)
print 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)\n")
sys.exit()
file_lines = local_file.readlines()
local_versions = "".join(file_lines)
local_file.close()
local_versions = 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)\n")
sys.exit()
else:
local_program_version = local_versions[0]
local_code_version = local_versions[1]
local_graphics_version = local_versions[2]
return local_versions
def update(local, remote):
components = ["program is", "code is", "graphics are"]
for x in range(len(local)):
local_number = (local[x])
remote_number = (remote[x])
if local_number < remote_number:
print "The %s outdated. Getting files now." % components[x]
url_temp = get_file_urls(text)[x]
get_files(url_temp)
else:
print "The %s up to date. Checking next component." % components[x]
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)
update(get_local_version_numbers(), get_version_numbers(text))