Changeset 1094


Ignore:
Timestamp:
Jun 23, 2011, 10:42:32 PM (6 years ago)
Author:
stefan
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/src/jlatexeditor/tools/PackageParser.scala

    r1091 r1094  
    22
    33import utils.ic.File._
    4 import java.lang.Package
     4import utils.ic.InputStream._
    55import java.io._
    6 import collection.mutable.{HashMap, HashSet, MutableList}
     6import collection.mutable._
    77
    88/**
     
    1414  val NewEnvironment = ".*\\\\newenvironment\\{(\\w+)\\}(?:\\[(\\d+)\\])?(?:\\[([^\\]]+)\\])?.*".r
    1515  val Input = ".*\\\\input\\{([^}]+)\\}".r
     16  val DpkgResult = "([\\w\\.-]+): .*".r
    1617
    1718  val processedFiles = new HashSet[String]
     19  val files2debPackage = new HashMap[String, DebPackage]
    1820
    1921  def main(args: Array[String]) {
    2022    //parseFile(new File("/usr/share/texmf-texlive/tex/latex/algorithms/algorithmic.sty"))
    21     parseFile(new Package("asdf"), new File("/usr/share/texmf/tex/latex/pgf/frontendlayer/tikz.sty"))
     23    /*
     24    val p = findDebPackage(findFile("tikz.sty").getAbsolutePath)
     25    println(p.name)
     26    for (f <- p.files) {
     27      println(f)
     28    }
     29    */
     30
     31    //parseFile(new Package("asdf"), new File("/usr/share/texmf/tex/latex/pgf/frontendlayer/tikz.sty"))
    2232
    2333    val texmfDirs = new File("/usr/share").listFiles(new FileFilter {
     
    2939    texmfDirs.foreach(dir => parse(packs, dir))
    3040
     41    writeXmlFile(packs)
     42
     43    /*
    3144    val commandName2command = new HashMap[String, MutableList[Command]]()
    3245    for (pack <- packs; command <- pack.commands) {
     
    4053      }
    4154    }
     55    */
     56  }
     57
     58  def writeXmlFile(packages: MutableList[Package]) {
     59    println("<packages>")
     60    for (pack <- packages) {
     61      val debPackageString = pack.debPackage.map( pack => " debPackage=\"" + pack.name + "\"").getOrElse("")
     62      println("  <package name=\"" + pack.name + "\"" + debPackageString + ">")
     63      for (command <- pack.commands.values) {
     64        val optArgString = if (command.optionalArgs.isEmpty) "" else " " + command.optionalArgs(0)
     65        println("    <command name=\"" + command.name + "\" argCount=\"" + command.argCount + "\" optionalArg=\"" + optArgString + " />")
     66      }
     67      for (env <- pack.environments.values) {
     68        val optArgString = if (env.optionalArgs.isEmpty) "" else " " + env.optionalArgs(0)
     69        println("    <environment name=\"" + env.name + "\" argCount=\"" + env.argCount + "\" optionalArg=\"" + optArgString + " />")
     70      }
     71      println("  </package>")
     72    }
     73    println("</packages>")
    4274  }
    4375
     
    5082        val packageName = file.getName.substring(0, file.getName.length() - 4)
    5183        //println(packageName)
    52         val pack = new Package(packageName)
     84        val pack = new Package(file, packageName)
    5385        packs += pack
     86        print(packs.size + "\r")
    5487        parseFile(pack, file);
    5588      }
     
    6194      line match {
    6295        case Def(cmd, args) =>
    63           pack.commands += Command(pack, cmd, args.count(_ == '#'))
     96          pack.commands += cmd -> Command(pack, cmd, args.count(_ == '#'))
    6497        case NewCommand(cmd, args, optArg) =>
    6598          val argCount = if (args == null) 0 else args.toInt
    66           pack.commands += Command(pack, cmd, argCount, if (optArg == null) List() else List(optArg))
     99          pack.commands += cmd -> Command(pack, cmd, argCount, if (optArg == null) List() else List(optArg))
    67100//          println(cmd + " with " + args + " arguments and " + optArgs)
    68101        case NewEnvironment(cmd, args, optArg) =>
     102          val argCount = if (args == null) 0 else args.toInt
     103          pack.environments += cmd -> Environment(pack, cmd, argCount, if (optArg == null) List() else List(optArg))
    69104//          println("  environment " + cmd + " with " + args + " arguments and " + optArgs)
    70105        case Input(fileName) =>
     
    73108              processedFiles += fileName
    74109              //println("  input ." + fileName + ".")
    75               parseFile(pack, find(fileName))
     110              parseFile(pack, findFile(fileName))
    76111            }
    77112          } catch {
     
    84119  }
    85120
    86   def find(fileName: String) = {
     121  def findFile(fileName: String) = {
    87122    val userDir = System.getProperty("user.dir");
    88123    val process = Runtime.getRuntime.exec(Array(userDir + "/scripts/findTexFile", fileName))
    89     val r = new BufferedReader(new InputStreamReader(process.getInputStream))
    90     val s = r.readLine()
    91     if (s == null) throw new FileNotFoundException(fileName)
    92     else new File(s)
     124    val iter = process.getInputStream.readLines
     125    if (iter.hasNext) new File(iter.next())
     126    else throw new FileNotFoundException(fileName)
     127  }
     128
     129  def findDebPackage(path: String): DebPackage = {
     130    files2debPackage.get(path) match {
     131      case Some(debPackage) => debPackage
     132      case None =>
     133        val process = Runtime.getRuntime.exec(Array("dpkg", "-S", path))
     134        val iter = process.getInputStream.readLines
     135        if (iter.hasNext) iter.next() match {
     136          case DpkgResult(name) =>
     137            val debPackage = getDebPackage(name)
     138            for (file <- debPackage.files) {
     139              files2debPackage += file -> debPackage
     140            }
     141            debPackage
     142        }
     143        else throw new FileNotFoundException(path)
     144    }
     145  }
     146
     147  def getDebPackage(name: String) = {
     148    val files = new MutableList[String]
     149    val process = Runtime.getRuntime.exec(Array("dpkg", "-L", name))
     150
     151    for (file <- process.getInputStream.readLines) {
     152      files += file
     153    }
     154
     155    DebPackage(name, files)
    93156  }
    94157
    95158  case class Command(pack: Package, name: String, argCount: Int, optionalArgs: List[String] = List())
    96159  case class Environment(pack: Package, name: String, argCount: Int, optionalArgs: List[String] = List())
    97   case class Package(name: String, commands: MutableList[Command] = new MutableList[Command],
    98                      environments: MutableList[Environment] = new MutableList[Environment])
     160  case class Package(file: File, name: String, commands: LinkedHashMap[String, Command] = new LinkedHashMap[String, Command],
     161                     environments: LinkedHashMap[String, Environment] = new LinkedHashMap[String, Environment]) {
     162    val debPackage = try {
     163      Some(findDebPackage(file.getAbsolutePath))
     164    } catch {
     165      case e: FileNotFoundException => None
     166    }
     167  }
     168  case class DebPackage(name: String, files: MutableList[String])
    99169}
Note: See TracChangeset for help on using the changeset viewer.