Created
February 17, 2012 08:46
-
-
Save miguelrgonzalez/1851916 to your computer and use it in GitHub Desktop.
Play command for Committing SNAPSHOT releases into nexus
This file contains 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 play.utils import * | |
from poster.encode import multipart_encode | |
from poster.streaminghttp import register_openers | |
import urllib | |
import urllib2 | |
import yaml | |
COMMANDS = ['nexus-commit',] | |
HELP = { | |
'nexus-commit': 'push built module to nexus' | |
} | |
def execute(**kargs): | |
app = kargs.get("app") | |
args = kargs.get("args") | |
play_env = kargs.get("env") | |
print '~ ' | |
print '~ Commiting to nexus server' | |
print '~ ' | |
app.check() | |
nexus = app.readConf('nexus.server.url') | |
user = app.readConf('nexus.server.user') | |
password = app.readConf('nexus.server.password') | |
if nexus: | |
print '~ Found nexus repository : %s' % nexus | |
else: | |
print '~ No nexus server configured' | |
print '~ Set up the following on your application.conf:' | |
print '~ nexus.server.url' | |
print '~ nexus.server.user' | |
print '~ nexus.server.password' | |
sys.exit(0) | |
#Getting module version from dependencies file | |
deps_file = os.path.join(app.path, 'conf', 'dependencies.yml') | |
if os.path.exists(deps_file): | |
f = open(deps_file) | |
deps = yaml.load(f.read()) | |
#Is this a Play~ module? | |
if "self" in deps: | |
d = deps["self"].split(" ") | |
module_version = d.pop() | |
app_name = d.pop() | |
else: | |
app_name = app.name() | |
print '~ This is not a Play module' | |
module_version = app.readConf('application.version') | |
if not module_version: | |
print '~ ' | |
print '~ No application.version found in application.conf file' | |
print '~ ' | |
module_version = raw_input('~ Provide version number to be pushed to Nexus:') | |
f.close | |
if module_version: | |
print '~ Module version : %s' % module_version | |
print '~ ' | |
else: | |
print '~ No module version configured.' | |
print '~ Configure your dependencies file properly' | |
sys.exit(0) | |
dist_dir = os.path.join(app.path, 'dist') | |
files = [] | |
#Only interested on .zip files | |
for root, dirs, files in os.walk(dist_dir): | |
files = [ fi for fi in files if fi.endswith(".zip") ] | |
#Loop over all found files | |
if len(files) >0: | |
for file in files: | |
if "-a" in args: | |
#We won't ask the user if he wants to commit | |
resp = "Y" | |
else: | |
resp = raw_input('~ Do you want to post %s to nexus? (Y/N) ' % file) | |
if resp == 'Y': | |
url = '%s/%s/%s-SNAPSHOT/%s-%s-SNAPSHOT.zip' % (nexus, app_name, module_version, app_name, module_version) | |
print '~ ' | |
print '~ Sending %s to %s' % (file, url) | |
try: | |
data = open(os.path.join(dist_dir, file), 'rb') | |
except: | |
print '~ Error: could not open file %s for reading' % file | |
continue | |
openers = register_openers() | |
#post data to Nexus using configured credentials | |
passman = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
passman.add_password(None, url, user, password) | |
authhandler = urllib2.HTTPBasicAuthHandler(passman) | |
openers.add_handler(authhandler) | |
openers.add_handler(urllib2.HTTPHandler(debuglevel=1)) | |
datagen, headers = multipart_encode({"file": data}) | |
request = urllib2.Request(url, datagen, headers) | |
try: | |
print urllib2.urlopen(request).read() | |
print '~ File correctly sent' | |
except urllib2.HTTPError, err: | |
print '~ Error: Nexus replied with -- %s' % err | |
else: | |
print '~ ' | |
print '~ Skiping %s' % file | |
else: | |
print '~ ' | |
print '~ No module build found.' | |
print '~ Try "play build-module" command first' | |
print '~ ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment