-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElpa-Mirror.scala
64 lines (58 loc) · 2.67 KB
/
Elpa-Mirror.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import scala.io.Source
import scala.util.parsing.combinator._
import java.net.URL
import scalax.io.Input.asInputConverter
import scalax.file.Path
import java.io.FileNotFoundException
case class Library(name : String, version : String, description : String, dependencies : List[(String,String)], mode : String)
class ArchiveContents extends JavaTokenParsers {
def contents : Parser[Map[String,Library]] = "("~>"1"~>rep(library)<~")" ^^ (Map() ++ _)
def library : Parser[(String,Library)] = "("~>libraryName~"."~libraryDesc<~")" ^^
{ case name~"."~desc => (name, Library(name, desc._1, desc._3, desc._2, desc._4)) }
def libraryName : Parser[String] = """[a-zA-Z_\-0-9+]*""".r
def libraryDesc : Parser[(String,List[(String,String)],String,String)] = "["~>libraryVersion~libraryDeps~libraryInfo~libraryMode<~"]" ^^
{ case version~dependencies~desc~mode => (version, dependencies, desc, mode) }
def libraryVersion : Parser[String] = "("~>rep(decimalNumber)<~")" ^^ (_.mkString("."))
def libraryDeps : Parser[List[(String,String)]] = "nil" ^^ (_ => List.empty) | "("~>rep(libraryDepsLibrary)<~")"
def libraryDepsLibrary : Parser[(String, String)] = "("~>libraryName~libraryVersion<~")" ^^
{ case name~version => (name, version) }
def libraryInfo : Parser[String] = stringLiteral | """\".*\"""".r
def libraryMode : Parser[String] = "single" | "tar"
}
object ElpaMirror extends ArchiveContents {
def main(args : Array[String]) {
args.length match {
case 0 => println("Need to provide url for the elpa")
case 1 => mirrorElpa(args(0))
case _ => println("Invalid arguments. Please provide ONE url to an ELPA archive")
}
}
def mirrorElpa(url : String) {
val archiveContents = Source.fromURL(url + "/archive-contents")
val archiveMap = parseAll(contents, archiveContents bufferedReader) match {
case Success(e, _) => e
case f :NoSuccess => println(f msg); Map empty
}
downloadFile(url, "archive-contents")
archiveMap foreach (x => downloadPackage(url, x._1, x._2))
}
def downloadPackage(url: String, name : String, lib : Library) {
val ext = lib.mode match {
case "single" => "el"
case "tar" => "tar"
case x => x
}
val fileName = name+"-"+lib.version+"."+ext
downloadFile(url, fileName)
}
def downloadFile(url : String, fileName : String) {
println("Downloading: " + url + "/" + fileName)
val onlineFile = new URL(url+"/"+fileName).asInput.bytes
try {
Path("./"+fileName).write(onlineFile)
} catch {
case ex : FileNotFoundException => println(fileName+" does not exist. Moving on")
case _ => println("Problem downloading or writing file "+fileName)
}
}
}