src/Pure/Admin/check_sources.scala
author wenzelm
Sun, 14 May 2017 15:34:20 +0200
changeset 65822 17b8528c2f53
parent 64610 1b89608974e9
child 69277 258bef08b31e
permissions -rw-r--r--
clarified notion of known files (before actual commit);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64161
2b1128e95dfb explicit indication of Admin tools;
wenzelm
parents: 63997
diff changeset
     1
/*  Title:      Pure/Admin/check_sources.scala
56791
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
     3
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
     4
Some sanity checks for Isabelle sources.
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
     5
*/
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
     6
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
     7
package isabelle
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
     8
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
     9
62452
f25b67245699 more official "isabelle check_sources";
wenzelm
parents: 62303
diff changeset
    10
object Check_Sources
56791
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    11
{
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    12
  def check_file(path: Path)
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    13
  {
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    14
    val file_name = path.implode
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    15
    val file_pos = path.position
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    16
    def line_pos(i: Int) = Position.Line_File(i + 1, file_name)
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    17
64324
416f4d031afd check Windows file names, e.g. relavant for native Mercurial, but also for Isabelle/Scala;
wenzelm
parents: 64162
diff changeset
    18
    if (space_explode('/', Word.lowercase(path.expand.split_ext._1.implode)).contains("aux"))
416f4d031afd check Windows file names, e.g. relavant for native Mercurial, but also for Isabelle/Scala;
wenzelm
parents: 64162
diff changeset
    19
      Output.warning("Illegal file-name on Windows" + Position.here(file_pos))
416f4d031afd check Windows file names, e.g. relavant for native Mercurial, but also for Isabelle/Scala;
wenzelm
parents: 64162
diff changeset
    20
56791
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    21
    val content = File.read(path)
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    22
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    23
    for { (line, i) <- split_lines(content).iterator.zipWithIndex }
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    24
    {
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    25
      try {
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    26
        Symbol.decode_strict(line)
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    27
64610
1b89608974e9 clarified modules;
wenzelm
parents: 64368
diff changeset
    28
        for { c <- Codepoint.iterator(line); if c > 128 && !Character.isAlphabetic(c) }
56791
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    29
        {
64610
1b89608974e9 clarified modules;
wenzelm
parents: 64368
diff changeset
    30
          Output.warning("Suspicious Unicode character " + quote(Codepoint.string(c)) +
56791
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    31
            Position.here(line_pos(i)))
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    32
        }
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    33
      }
62814
29ca4cdd998d clarified check_sources;
wenzelm
parents: 62615
diff changeset
    34
      catch { case ERROR(msg) => Output.warning(msg + Position.here(line_pos(i))) }
56791
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    35
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    36
      if (line.contains('\t'))
56792
wenzelm
parents: 56791
diff changeset
    37
        Output.warning("TAB character" + Position.here(line_pos(i)))
56791
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    38
    }
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    39
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    40
    if (content.contains('\r'))
56792
wenzelm
parents: 56791
diff changeset
    41
      Output.warning("CR character" + Position.here(file_pos))
62814
29ca4cdd998d clarified check_sources;
wenzelm
parents: 62615
diff changeset
    42
29ca4cdd998d clarified check_sources;
wenzelm
parents: 62615
diff changeset
    43
    if (Word.bidi_detect(content))
64368
364d74ea985f tuned message;
wenzelm
parents: 64324
diff changeset
    44
      Output.warning("Bidirectional Unicode text" + Position.here(file_pos))
56791
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    45
  }
56829
f151ade98b15 proper tool wrap-up;
wenzelm
parents: 56792
diff changeset
    46
f151ade98b15 proper tool wrap-up;
wenzelm
parents: 56792
diff changeset
    47
  def check_hg(root: Path)
f151ade98b15 proper tool wrap-up;
wenzelm
parents: 56792
diff changeset
    48
  {
56830
e760242101fc tuned signature -- channels for diagnostic output for system tools means stderr;
wenzelm
parents: 56829
diff changeset
    49
    Output.writeln("Checking " + root + " ...")
64162
03057a8fdd1f simplified: no internal state for Mercurial;
wenzelm
parents: 64161
diff changeset
    50
    val hg = Mercurial.repository(root)
03057a8fdd1f simplified: no internal state for Mercurial;
wenzelm
parents: 64161
diff changeset
    51
    for {
65822
17b8528c2f53 clarified notion of known files (before actual commit);
wenzelm
parents: 64610
diff changeset
    52
      file <- hg.known_files()
64162
03057a8fdd1f simplified: no internal state for Mercurial;
wenzelm
parents: 64161
diff changeset
    53
      if file.endsWith(".thy") || file.endsWith(".ML") || file.endsWith("/ROOT")
03057a8fdd1f simplified: no internal state for Mercurial;
wenzelm
parents: 64161
diff changeset
    54
    } check_file(root + Path.explode(file))
56829
f151ade98b15 proper tool wrap-up;
wenzelm
parents: 56792
diff changeset
    55
  }
f151ade98b15 proper tool wrap-up;
wenzelm
parents: 56792
diff changeset
    56
f151ade98b15 proper tool wrap-up;
wenzelm
parents: 56792
diff changeset
    57
62834
970cedec9748 prefer internal tool;
wenzelm
parents: 62814
diff changeset
    58
  /* Isabelle tool wrapper */
56829
f151ade98b15 proper tool wrap-up;
wenzelm
parents: 56792
diff changeset
    59
62834
970cedec9748 prefer internal tool;
wenzelm
parents: 62814
diff changeset
    60
  val isabelle_tool =
970cedec9748 prefer internal tool;
wenzelm
parents: 62814
diff changeset
    61
    Isabelle_Tool("check_sources", "some sanity checks for Isabelle sources", args =>
970cedec9748 prefer internal tool;
wenzelm
parents: 62814
diff changeset
    62
    {
62454
38c89353b349 tuned signature;
wenzelm
parents: 62452
diff changeset
    63
      val getopts = Getopts("""
62452
f25b67245699 more official "isabelle check_sources";
wenzelm
parents: 62303
diff changeset
    64
Usage: isabelle check_sources [ROOT_DIRS...]
f25b67245699 more official "isabelle check_sources";
wenzelm
parents: 62303
diff changeset
    65
65822
17b8528c2f53 clarified notion of known files (before actual commit);
wenzelm
parents: 64610
diff changeset
    66
  Check .thy, .ML, ROOT against known files of Mercurial ROOT_DIRS.
62452
f25b67245699 more official "isabelle check_sources";
wenzelm
parents: 62303
diff changeset
    67
""")
f25b67245699 more official "isabelle check_sources";
wenzelm
parents: 62303
diff changeset
    68
f25b67245699 more official "isabelle check_sources";
wenzelm
parents: 62303
diff changeset
    69
      val specs = getopts(args)
f25b67245699 more official "isabelle check_sources";
wenzelm
parents: 62303
diff changeset
    70
      if (specs.isEmpty) getopts.usage()
f25b67245699 more official "isabelle check_sources";
wenzelm
parents: 62303
diff changeset
    71
f25b67245699 more official "isabelle check_sources";
wenzelm
parents: 62303
diff changeset
    72
      for (root <- specs) check_hg(Path.explode(root))
64161
2b1128e95dfb explicit indication of Admin tools;
wenzelm
parents: 63997
diff changeset
    73
    }, admin = true)
56791
23883e1879c5 some sanity checks for Isabelle sources;
wenzelm
parents:
diff changeset
    74
}