HAL  v4.5.0-313-g29bd89cc0
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
export_project_dialog.cpp
Go to the documentation of this file.
7 
8 #include <QFileInfo>
9 #include <QTemporaryDir>
10 #include <QDir>
11 #include <QDirIterator>
12 #include <QFile>
13 #include <JlCompress.h> // deps/quazip-1.5
14 #include <QJsonObject>
15 #include <QJsonArray>
16 #include <QJsonDocument>
17 
18 namespace hal
19 {
21  : QFileDialog(parent), mStatus(NoExport)
22  {
23  QFileInfo pinfo(QString::fromStdString(ProjectManager::instance()->get_project_directory().get_default_filename()));
24  mProjectName = pinfo.baseName().simplified();
25  mProjectName.replace(" ","");
26  setDefaultSuffix(".zip");
28  setLabelText(QFileDialog::FileType, QString("Compressed archive (*.zip"));
29  setLabelText(QFileDialog::FileName, "Export project " + mProjectName + " to …");
30  selectFile("hal_project_" + mProjectName + ".zip");
31  setWindowTitle("Export project");
32  }
33 
35  {
36  if (selectedFiles().isEmpty())
37  return;
38  QString archiveFilename = selectedFiles().at(0);
39 
40  QString projTargetDir;
41  QTemporaryDir tempDir(QDir::temp().absoluteFilePath("hal_project_export_XXXXXX"));
42  if (!tempDir.isValid() || !QDir().mkpath(projTargetDir = QDir(tempDir.path()).absoluteFilePath(mProjectName))) {
43  log_warning("gui", "Failed to create temporary project export directory '{}'.", projTargetDir.toStdString());
44  mStatus = ErrorCreateTempDir;
45  return;
46  }
47 
48  QString projSourceDir = QString::fromStdString(ProjectManager::instance()->get_project_directory().string());
49  QStringList filesToCopy;
50  QStringList dirsToCreate;
51 
52  QDirIterator it(projSourceDir, QDir::NoDotAndDotDot |
55  QDir::Hidden |
56  QDir::Dirs |
59  while (it.hasNext())
60  {
61  QString relPath = it.next();
62  if (relPath.startsWith(projSourceDir))
63  relPath.remove(0,projSourceDir.size()+1);
64  else
65  log_warning("gui", "Project export found file '{}' not in project folder '{}'.",
66  relPath.toStdString(), projSourceDir.toStdString());
67 
68  if (it.fileInfo().isDir())
69  dirsToCreate.append(relPath);
70  else if (it.fileInfo().isFile())
71  {
72  if (relPath.startsWith("autosave/")) continue;
73  // the warning against zipping the project directory by hand does not belong into a proper export
75  filesToCopy.append(relPath);
76  }
77  else
78  log_warning("gui", "Project export ignored directory entry '{}' with unknown type.", relPath.toStdString());
79  }
80 
81  // Create project subdirectories in export temp folder
82  for (QString dir : dirsToCreate)
83  {
84  QString fullpath = QDir(projTargetDir).absoluteFilePath(dir);
85  if (!QDir().mkpath(fullpath))
86  log_warning("gui", "Failed to create directory '{}' in project export folder.", dir.toStdString());
87  }
88 
89  // Copy files to export temp folder
90  for (const QString& s : filesToCopy)
91  {
92  QString src = QDir(projSourceDir).absoluteFilePath(s);
93  QString dst = QDir(projTargetDir).absoluteFilePath(s);
94  if (!QFile::copy(src,dst))
95  {
96  log_warning("gui", "Failed to copy file '{}' into project export folder.", s.toStdString());
97  if (s == ".project.json" || s.endsWith(".hal") || s.endsWith(".hgl"))
98  {
99  mStatus = ErrorCopy;
100  return;
101  }
102  }
103  }
104 
105  mStatus = ErrorPrepare;
106  // Copy relevant files into export temp folder
107  ProjectJson projFile(QDir(projTargetDir).absoluteFilePath(".project.json"));
108  if (projFile.status() != ProjectJson::Ok) return;
109  QFileInfo glFinfo(projFile.gateLibraryFilename());
110  if (glFinfo.isAbsolute())
111  {
112  if (!QFile::copy(glFinfo.absoluteFilePath(),QDir(projTargetDir).absoluteFilePath(glFinfo.fileName())))
113  log_warning("gui", "Failed to copy file '{}' into project export folder.", glFinfo.absoluteFilePath().toStdString());
114  else
115  projFile.setGateLibraryFilename(glFinfo.fileName());
116  }
117  copyExternalPython(projTargetDir);
118 
119  if (JlCompress::compressDir( archiveFilename, tempDir.path(), true, QDir::Hidden ))
120  mStatus = Ok;
121  else
122  {
123  log_warning("gui", "Failed to compress project to archive '{}'.", archiveFilename.toStdString());
124  mStatus = ErrorCompress;
125  }
126  }
127 
128  void ExportProjectDialog::copyExternalPython(const QString &projTempDir)
129  {
131  QFile ff(pyControlFileName);
132  if (!ff.open(QIODevice::ReadOnly)) return;
133 
134  QJsonParseError err;
135  QJsonDocument doc = QJsonDocument::fromJson(ff.readAll(), &err);
136  if (err.error != QJsonParseError::NoError) return;
137  ff.close();
138 
139  QJsonObject obj = doc.object();
140  if (!obj.contains("tabs") || !obj["tabs"].isArray()) return;
141 
142  int replaceCount = 0;
143  QString pyTempDir(QDir(projTempDir).absoluteFilePath(PythonSerializer::sPythonRelDir));
144 
145  QJsonArray oldTabs = obj["tabs"].toArray();
146  QJsonArray newTabs;
147  int ntabs = oldTabs.size();
148  for (int itab = 0; itab < ntabs; itab++)
149  {
150  QJsonObject tab = oldTabs.at(itab).toObject();
151  if (!tab.contains("filename")) continue;
152  QString absFilename = tab["filename"].toString();
153  if (QFileInfo(absFilename).isAbsolute())
154  {
155  QString relFilename = QFileInfo(absFilename).fileName();
156  QString tmpFilename = QDir(pyTempDir).absoluteFilePath(relFilename);
157  if (QFileInfo(tmpFilename).exists())
158  QFile::remove(tmpFilename);
159  if (QFile::copy(absFilename,tmpFilename))
160  {
161  ++replaceCount;
162  tab.remove("filename");
163  tab.insert("filename", relFilename);
164  }
165  }
166  newTabs.append(tab);
167  }
168 
169  if (replaceCount)
170  {
171  obj.remove("tabs");
172  obj.insert("tabs", newTabs);
173  QFile::remove(pyControlFileName);
174  QFile fo(pyControlFileName);
175  if (!fo.open(QIODevice::WriteOnly)) return;
176  fo.write(QJsonDocument(obj).toJson(QJsonDocument::Compact));
177  }
178  }
179 }
ExportProjectDialog(QWidget *parent=nullptr)
static const std::string s_zip_warning_file
Status status() const
Definition: project_json.h:49
QString gateLibraryFilename() const
Definition: project_json.h:51
void setGateLibraryFilename(const QString &gl)
static ProjectManager * instance()
static std::string sControlFileName
Definition: python_editor.h:93
static QString sPythonRelDir
Definition: python_editor.h:92
#define log_warning(channel,...)
Definition: log.h:76
Definition: defines.h:45
NoDotAndDotDot
QString absoluteFilePath(const QString &fileName) const const
QDir temp()
QFileInfo fileInfo() const const
bool hasNext() const const
QString next()
bool copy(const QString &newName)
bool remove()
void setAcceptMode(QFileDialog::AcceptMode mode)
void setDefaultSuffix(const QString &suffix)
void selectFile(const QString &filename)
QStringList selectedFiles() const const
void setLabelText(QFileDialog::DialogLabel label, const QString &text)
QString absoluteFilePath() const const
QString baseName() const const
QString fileName() const const
bool isAbsolute() const const
bool isDir() const const
bool isFile() const const
void append(const QJsonValue &value)
QJsonValue at(int i) const const
int size() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
bool contains(const QString &key) const const
QJsonObject::iterator insert(const QString &key, const QJsonValue &value)
void remove(const QString &key)
QJsonObject toObject() const const
void append(const T &value)
const T & at(int i) const const
QString fromStdString(const std::string &str)
QString & remove(int position, int n)
QString & replace(int position, int n, QChar after)
QString simplified() const const
int size() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
std::string toStdString() const const
bool isValid() const const
QString path() const const
void setWindowTitle(const QString &)