#!/usr/bin/python

# This is a utility script that moves all the eclipse libs in the lib/ folder to
# local repository and prints out Maven dependencies


import os
import re
import shutil


def parsing_results():
  def jars():
    return [f for f in os.listdir("./lib") if f.lower().endswith(".jar")]
  def jarname_parsing(file):
    match = re.match("([\w\.]+)_(\d+\.\d+\.\d+.*)\.jar", file)
    if match != None:
      (name, version) = match.group(1, 2)

      name = name.split(".")
      source = name[-1] == "source"
      (group, name) = (".".join(name[:-2]), name[-2]) if source else (".".join(name[:-1]), name[-1])

      snapshot = version.upper().endswith(".SNAPSHOT")
      if snapshot:
        version = version[:-len(".snapshot")]

      return {
        "group": group,
        "name": name,
        "version": version,
        "snapshot": snapshot,
        "source": source
      }
  return [("lib/" + f, jarname_parsing(f)) for f in jars()]


def check_results(parsing_results):
  unparsable_files = [r[0] for r in parsing_results if r[1] == None]
  if unparsable_files:
    raise Exception("Unparsable file names detected: " + unparsable_files)

def maven_dependencies(parsing_results):
  def artifact(parsing):
    return {
      "groupId": parsing["group"], 
      "artifactId": parsing["name"],
      "version": parsing["version"] + ("-SNAPSHOT" if parsing["snapshot"] else "")
    }
  def maven_dependency(artifact):
    return """
<dependency>
  <groupId>%(groupId)s</groupId>
  <artifactId>%(artifactId)s</artifactId>
  <version>%(version)s</version>
</dependency>
""" % artifact
  def unique_artifacts():
    artifacts = []
    for (_, parsing) in parsing_results:
      a = artifact(parsing)
      if a not in artifacts:
        artifacts.append(a)
    return artifacts

  return "\n".join([maven_dependency(a).strip() for a in unique_artifacts()])


def move_file(src, dst):
  def create_tree(f):
    d = os.path.dirname(f)
    if not os.path.exists(d):
      os.makedirs(d)
  create_tree(dst)
  if os.path.exists(dst):
    os.remove(dst)
  shutil.move(src, dst)


def assort_files(parsing_results):
  def repo_path(p):
    version = p["version"] + ("-SNAPSHOT" if p["snapshot"] else "")
    return "repo/" \
         + "/".join(p["group"].split(".")) \
         + "/" + p["name"] + "/" + version + "/" + p["name"] + "-" + version \
         + ("-sources" if p["source"] else "") \
         + ".jar"

  for (f, p) in parsing_results:
    move_file( f, repo_path(p) )
  

parsing_results = parsing_results()
check_results(parsing_results)
assort_files(parsing_results)
print maven_dependencies(parsing_results)

@@ -2125,41 +2125,200 @@
 s):%0A
+%0A
   def 
-repo_path(p):%0A    
+cmd((f, p)):%0A    return %22install:install-file%22 %5C%0A         + %22 -Dfile=%22 + f %5C%0A         + %22 -DgroupId=%22 + p%5B%22group%22%5D %5C%0A         + %22 -DartifactId=%22 + p%5B%22name%22%5D %5C%0A         + %22 -D
 version
- 
 =
+%22 +
  p%5B%22
@@ -2371,151 +2371,121 @@
  %22%22)
-%0A    return %22repo/%22 %5C%0A         + %22/%22.join(p%5B%22group%22%5D.split(%22.%22)) %5C%0A         + %22/%22 + p%5B%22name%22%5D + %22/%22 + version + %22/%22 + p%5B%22name%22%5D + %22-%22 + version
+ %5C%0A         + %22 -Dpackaging=jar%22 %5C%0A         + %22 -DlocalRepositoryPath=repo%22 %5C%0A         + %22 -DcreateChecksum=true%22
  %5C%0A 
@@ -2496,17 +2496,30 @@
     + (%22
--
+ -Dclassifier=
 sources%22
@@ -2546,36 +2546,16 @@
  %22%22)
- %5C%0A         + %22.jar%22
 %0A%0A  for 
 (f, 
@@ -2550,22 +2550,17 @@
 %0A%0A  for 
-(f, p)
+r
  in pars
@@ -2580,36 +2580,35 @@
     
-move_file( f, repo_path(p) )
+os.system(%22mvn %22 + cmd(r))%0A
 %0A  %0A

install libs with maven