Welcome to mirror list, hosted at ThFree Co, Russian Federation.

build_drules.cpp « build_style « qt - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e9a108593313d618e0e923daef37dc2da091b68 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "build_drules.h"
#include "build_common.h"

#include "platform/platform.hpp"

#include "std/exception.hpp"

#include <fstream>
#include <streambuf>

#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStringList>

namespace
{

QString GetScriptPath()
{
  QString const resourceDir = GetPlatform().ResourcesDir().c_str();
  return resourceDir + "kothic/src/libkomwm.py";
}

QString GetProtobufEggPath()
{
  QString const resourceDir = GetPlatform().ResourcesDir().c_str();
  return resourceDir + "kothic/protobuf-2.6.1-py2.7.egg";
}

}  // namespace

namespace build_style
{

void BuildDrawingRulesImpl(QString const & mapcssFile, QString const & outputDir)
{
  QString const outputTemplate = outputDir + "drules_proto";
  QString const outputFile = outputTemplate + ".bin";

  // Caller ensures that output directory is clear
  if (QFile(outputFile).exists())
    throw runtime_error("Output directory is not clear");

  // Prepare command line
  QStringList params;
  params << "python" <<
            GetScriptPath() <<
            "-s" << mapcssFile <<
            "-o" << outputTemplate <<
            "-x" << "True";
  QString const cmd = params.join(' ');

  // Add path to the protobuf EGG in the PROTOBUF_EGG_PATH environment variable
  QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
  env.insert("PROTOBUF_EGG_PATH", GetProtobufEggPath());

  // Run the script
  auto const res = ExecProcess(cmd, &env);

  // Script returs nothing and zero exit code if it is executed succesfully,
  if (res.first != 0 || !res.second.isEmpty())
  {
    QString msg = QString("System error ") + to_string(res.first).c_str();
    if (!res.second.isEmpty())
      msg = msg + "\n" + res.second;
    throw runtime_error(to_string(msg));
  }

  // Ensure generated files has non-zero size
  if (QFile(outputFile).size() == 0)
    throw runtime_error("Drawing rules file has zero size");
}

void BuildDrawingRules(QString const & mapcssFile, QString const & outputDir)
{
  QString const resourceDir = GetPlatform().ResourcesDir().c_str();

  if (!QFile::copy(resourceDir + "mapcss-mapping.csv", outputDir + "mapcss-mapping.csv"))
    throw runtime_error("Unable to copy mapcss-mapping.csv file");

  if (!QFile::copy(resourceDir + "mapcss-dynamic.txt", outputDir + "mapcss-dynamic.txt"))
    throw runtime_error("Unable to copy mapcss-dynamic.txt file");

  BuildDrawingRulesImpl(mapcssFile, outputDir);
}

void ApplyDrawingRules(QString const & outputDir)
{
  QString const resourceDir = GetPlatform().ResourcesDir().c_str();

  if (!CopyFile(outputDir + "drules_proto.bin", resourceDir + "drules_proto.bin"))
    throw runtime_error("Cannot copy drawing rules file");
  if (!CopyFile(outputDir + "classificator.txt", resourceDir + "classificator.txt"))
    throw runtime_error("Cannot copy classificator file");
  if (!CopyFile(outputDir + "types.txt", resourceDir + "types.txt"))
    throw runtime_error("Cannot copy types file");
  if (!CopyFile(outputDir + "patterns.txt", resourceDir + "patterns.txt"))
    throw runtime_error("Cannot copy patterns file");
  if (!CopyFile(outputDir + "colors.txt", resourceDir + "colors.txt"))
    throw runtime_error("Cannot copy colors file");
}

}  // namespace build_style