Created
June 25, 2015 06:18
-
-
Save veelenga/29f2c5911c774ad21103 to your computer and use it in GitHub Desktop.
Tiny Python script to load files from Mercurial repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from optparse import OptionParser | |
from subprocess import call | |
from subprocess import check_output | |
import urllib | |
import re | |
import os | |
import sys | |
import shutil | |
def getUrlCode(url): | |
try: | |
return urllib.urlopen(url).getcode() | |
except: | |
return -1 | |
def cloneRepository(repo_url): | |
""" | |
Cloning repository by it's url. | |
Destination - is default (name of repository). | |
""" | |
print "==> Cloning repository" | |
returncode = call("hg clone " + repo_url, shell=True) | |
if returncode != 0: | |
print "==> Unable to clone repository." | |
exit(-1) | |
def synchronizeRepository(repo, repo_url): | |
""" | |
Pulling latest updates from server. And update to tip | |
""" | |
print "==> Synchronizing repository" | |
returncode = call("hg pull -R " + repo + " " + repo_url, shell=True) | |
if returncode != 0: | |
print "==> Unable to pull." | |
exit(-1) | |
def updateRepository(repo, rev='tip'): | |
print "==> Updating to %s revision (tag)" % rev | |
returncode = call("hg up -R " + repo + " -r " + rev, shell=True) | |
if returncode != 0: | |
print "==> Unable to update to %s revision." % rev | |
def getTags(repo): | |
output = check_output(["hg", "tags", "-R", repo]) | |
tags = {} | |
index = 1 | |
for next in output.split("\n"): | |
t = next.split(" ")[0] | |
if t != '' and t != 'tip': | |
tags[index] = t | |
index = index + 1 | |
return tags | |
def chooseTag(tags, max_print=15): | |
index = 1 | |
for k in tags: | |
print "%s) %s" % (k, tags[k]) | |
if index >= max_print: | |
break | |
index = index + 1 | |
number = 0 | |
while number <= 0 or number >= max_print or number not in tags.keys(): | |
try: | |
number = int(raw_input("Choose tag: ")) | |
except ValueError: | |
pass | |
return tags[int(number)] | |
def getLatestTagByFamily(tags, family): | |
from_family = [] | |
for k in tags: | |
value = tags[k] | |
if (value.startswith(family)): | |
from_family.append(value) | |
from_family.sort(reverse=True) | |
return from_family[0] | |
def getFiles(directory, extensions): | |
file_paths = [] | |
for root, directories, files in os.walk(directory): | |
for filename in files: | |
for extension in extensions: | |
# some artifacts have different extensions case | |
if filename.endswith(extension) or \ | |
filename.endswith(extension.upper()) or \ | |
filename.endswith(extension.lower()): | |
filepath = os.path.join(root, filename) | |
file_paths.append(filepath) | |
return file_paths | |
def copyFiles(source, destination, extensions): | |
print "==> Copying files from %s to %s with extensions " % (source, destination) + str(extensions) | |
if not os.path.exists(destination): | |
os.makedirs(destination) | |
files = getFiles(source, extensions) | |
for file in files: | |
shutil.copy(file, destination) | |
def exit(status): | |
print "Bye, bye..." | |
sys.exit(status) | |
def main(): | |
# SET DEFAULT VALUES | |
merc_url = "http://hg-vm01.genesyslab.com/hgweb.cgi" | |
repo = None | |
extensions = None | |
destination = "./artifacts" | |
family = None | |
subfolder = "" | |
# READ/CHECK INPUT PARAMETERS | |
parser = OptionParser() | |
parser.add_option("--web_url", | |
dest="url", | |
help="Mercurial host url. If not set default server will be used: " + merc_url, | |
type="string") | |
parser.add_option("--repo", | |
dest="repo", | |
help="[REQUIRED] Mercurial repository name", | |
type="string") | |
parser.add_option("--extensions", | |
dest="extensions", | |
help="[REQUIRED] Files with those extensions will be copied to destination", | |
type = "string") | |
parser.add_option("--dest", | |
dest="destination", | |
help="Folder to copy artifacts into", | |
type="string") | |
parser.add_option("--family", | |
dest="family", | |
help= """Set family to choose latest version. For example: --family=8.5.0 | |
will automatically update to tag with version what is latest in 8.5.0 family. For example 8.5.100.10 | |
""", | |
type="string") | |
parser.add_option("--sfolder", | |
dest="subfolder", | |
help="Subfolder in repository to search files by extensions", | |
type="string") | |
(options, args) = parser.parse_args() | |
if options.url: | |
merc_url = options.url | |
if not options.repo: | |
parser.error("Missing required repo parameter") | |
else: | |
repo = options.repo | |
if not options.extensions: | |
parser.error("Missing required extensions parameter") | |
else: | |
extensions = options.extensions | |
if not options.destination: | |
print "Destination was not set. Using default location: " + destination | |
else: | |
destination = options.destination | |
if options.family: | |
family = options.family | |
if options.subfolder: | |
subfolder = options.subfolder | |
# PREPARE/CHECK URL | |
repo_url = merc_url + "/" + repo | |
code = getUrlCode(repo_url) | |
if code != 200: | |
parser.error("Url %s didn't return status OK. Recheck url/repo and try again" % repo_url) | |
print "==> Using remote repository: " + repo_url | |
# SYNCHRONIZE/CLONE repository | |
folder = repo | |
if os.path.isdir(folder): | |
# check is existed folder contains repository | |
returncode = call("hg --cwd " + folder + " root", shell=True) | |
if returncode != 0: | |
print "==> Folder %s exists but doesn't contain mercurial repository. Deleting" % folder | |
shutil.rmtree(folder) | |
if os.path.isdir(folder): | |
print "==> Unable to remove directory. Remove it manually and retry." | |
exit(-1) | |
cloneRepository(repo_url) | |
synchronizeRepository(repo, repo_url) | |
else: | |
cloneRepository(repo_url) | |
# UPDATE TO CHOOSEN TAG | |
tags = getTags(repo) | |
tag = None | |
if family: | |
tag = getLatestTagByFamily(tags, family) | |
else: | |
tag = chooseTag(tags) | |
updateRepository(repo, tag) | |
# COPY FILES WITH EXTENSIONS TO DESTINATION | |
source = repo + os.path.sep + subfolder | |
if not os.path.exists(source): | |
print "==> Source folder %s not exist" % source | |
exit(-1) | |
copyFiles(source, destination, extensions.split(",")) | |
print "==> Done. List folder '%s'" % destination | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python hgArtifacts.py^ | |
--web_url=http://hg-web-url/hgweb.cgi/^ | |
--repo=repo_name^ | |
--extensions=dll,xml,xsd^ | |
--sfolder=folder_to_copy^ | |
--dest=destination_folder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment