source: src-qt4/pc-sysmanager/backend.cpp @ e9a3917

releng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since e9a3917 was e9a3917, checked in by Ken Moore <ken@…>, 11 months ago

Finish cleaning up the new system manager ports/source checkout functionality.
Also create a backend class that just contains a bunch of static functions for simple usage elsewhere (like generating ports update commands). This will make it easy to update the utility later if URL's or other system calls change (since we don't need to worry about all the GUI-specific stuff getting mixed in).

  • Property mode set to 100644
File size: 4.6 KB
Line 
1#include "backend.h"
2
3QStringList Backend::shortCMD(QString dir, QString cmd, QStringList args){
4  //Run a quick command in the proper dir and return the output
5  QProcess proc;
6  proc.setProcessChannelMode(QProcess::MergedChannels);
7  if( !dir.isEmpty() && QFile::exists(dir) ){ proc.setWorkingDirectory(dir); }
8  if(args.isEmpty()){ proc.start(cmd); }
9  else{ proc.start(cmd, args); }
10  while(!proc.waitForFinished(300)){ QCoreApplication::processEvents(); }
11  QStringList out;
12  while(proc.canReadLine()){
13    out << QString( proc.readLine() );
14  }
15  return out;
16}
17
18bool Backend::quickCMD(QString dir, QString cmd, QStringList args){
19  //Run a quick command without looking for output other than success/failure
20  QProcess proc;
21  if( !dir.isEmpty() && QFile::exists(dir) ){ proc.setWorkingDirectory(dir); }
22  if(args.isEmpty()){ proc.start(cmd); }
23  else{ proc.start(cmd, args); }
24  while(!proc.waitForFinished(300)){ QCoreApplication::processEvents(); }
25  return (proc.exitCode() == 0);
26}
27
28
29void Backend::emptyDir(QString dir){
30  QDir d(dir);
31  if(!d.exists()){ return; } //quick check to make sure directory exists first
32  //Remove all the files in this directory
33  QStringList tmp = d.entryList(QDir::Files | QDir::NoDotAndDotDot);
34  for(int i=0; i<tmp.length(); i++){
35    d.remove(tmp[i]);
36  }
37  //Now remove all the directories in this directory (recursive)
38  tmp = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
39  for(int i=0; i<tmp.length(); i++){
40    Backend::emptyDir(d.absoluteFilePath(tmp[i])); //Empty this directory first
41    d.rmdir(tmp[i]); //Now try to remove it
42  }
43}
44
45void Backend::generatePortsUpdateCMDS(QStringList &cmds, QStringList &dirs, QStringList &info){
46  //Clear the output variables
47  cmds.clear(); dirs.clear(); info.clear();
48  //Check if the ports directory even exists
49  if(!QFile::exists("/usr/ports")){
50    cmds << "mkdir /usr/ports"; info << "Creating the ports tree"; dirs << ""; //Create the ports tree
51  }
52  //Now check if the git directory exists
53  QString URL = "https://www.github.com/pcbsd/freebsd-ports.git";
54  if(QFile::exists("/usr/ports/.git")){
55    //Check if the remote URL is correct
56    QString origin = Backend::shortCMD("/usr/ports", "git remote show -n origin").filter("Fetch URL:").join("").section("URL:",1,30).simplified();
57    if(origin != URL){
58      cmds << "git remote remove origin"; info << "Fix origin URL"; dirs <<"/usr/ports";
59      cmds << "git remote add origin "+URL; info << ""; dirs << "/usr/ports/.git"; //setup PC-BSD git repo
60    }
61  }else{
62    //new GIT setup
63    Backend::emptyDir("/usr/ports");
64    //cmds << "rm -rf /usr/ports/*"; info << "Removing the old ports tree"; dirs << ""; //Clean the old ports tree
65    cmds << "git init"; info << "Initialize GIT"; dirs << "/usr/ports"; //setup git
66    cmds << "git remote add origin "+URL; info << ""; dirs << "/usr/ports/.git"; //setup PC-BSD git repo
67  }
68  //Now update the tree with git
69  cmds << "git fetch --depth=1"; info << "Fetch new GIT info (this may take a while)"; dirs << "/usr/ports/.git";
70  cmds << "git checkout master"; info << "Checkout the tree"; dirs << "/usr/ports"; 
71}
72
73void Backend::generateSourceUpdateCMDS(QString branch, QStringList &cmds, QStringList &dirs, QStringList &info){
74  //Clear the output variables
75  cmds.clear(); dirs.clear(); info.clear();
76  //Check if the source directory even exists
77  if(!QFile::exists("/usr/src")){
78    cmds << "mkdir /usr/src"; info << "Creating the source tree"; dirs << ""; //Create the ports tree
79  }
80  //Now check if the git directory exists
81  QString URL = "https://www.github.com/pcbsd/freebsd.git";
82  if(QFile::exists("/usr/src/.git")){
83    //Check if the remote URL is correct
84    QString origin = Backend::shortCMD("/usr/src", "git remote show -n origin").filter("Fetch URL:").join("").section("URL:",1,30).simplified();
85    if(origin != URL){
86      cmds << "git remote remove origin"; info << "Fix origin URL"; dirs <<"/usr/src";
87      cmds << "git remote add origin "+URL; info << ""; dirs << "/usr/src/.git"; //setup PC-BSD git repo
88    }
89  }else{
90    //new GIT setup
91    Backend::emptyDir("/usr/src");
92    //cmds << "rm -rf /usr/src/*"; info << "Removing the old source tree"; dirs << ""; //Clean the old ports tree
93    cmds << "git init"; info << "Initialize GIT"; dirs << "/usr/src"; //setup git
94    cmds << "git remote remove origin"; info << ""; dirs <<"/usr/src"; //just in case it re-uses an old git setup
95    cmds << "git remote add origin "+URL; info << ""; dirs << "/usr/src/.git"; //setup PC-BSD git repo
96  }
97  //Now update the tree with git
98  cmds << "git fetch --depth=1"; info << "Fetch new GIT info (this may take a while)"; dirs << "/usr/src/.git";
99  cmds << "git checkout "+branch; info << "Checkout the tree"; dirs << "/usr/src";     
100}
Note: See TracBrowser for help on using the repository browser.