10 import sys |
10 import sys |
11 import os |
11 import os |
12 import re |
12 import re |
13 |
13 |
14 from PyQt4.QtCore import QObject, QProcess, QTimer, QUrl, QFileInfo |
14 from PyQt4.QtCore import QObject, QProcess, QTimer, QUrl, QFileInfo |
15 from PyQt4.QtGui import QMenu, QInputDialog, QLineEdit, QDesktopServices, QDialog, \ |
15 from PyQt4.QtGui import QMenu, QInputDialog, QLineEdit, QDesktopServices, QDialog |
16 QFileDialog |
|
17 |
16 |
18 from E5Gui.E5Application import e5App |
17 from E5Gui.E5Application import e5App |
19 from E5Gui import E5MessageBox, E5FileDialog |
18 from E5Gui import E5MessageBox, E5FileDialog |
20 from E5Gui.E5Action import E5Action |
19 from E5Gui.E5Action import E5Action |
21 |
20 |
|
21 from Globals import isWindowsPlatform |
|
22 |
22 from .DjangoDialog import DjangoDialog |
23 from .DjangoDialog import DjangoDialog |
23 |
24 |
24 import Preferences |
25 import Preferences |
25 import Utilities |
26 import Utilities |
|
27 import UI.PixmapCache |
26 |
28 |
27 |
29 |
28 class DjangoNoSiteSelectedException(Exception): |
30 class DjangoNoSiteSelectedException(Exception): |
29 """ |
31 """ |
30 Exception thrown to signal, that there is no current site. |
32 Exception thrown to signal, that there is no current site. |
821 """ |
825 """ |
822 if self.__serverProc is not None: |
826 if self.__serverProc is not None: |
823 self.__serverProcFinished() |
827 self.__serverProcFinished() |
824 self.__setCurrentSite(None) |
828 self.__setCurrentSite(None) |
825 |
829 |
|
830 def __getVirtualEnvironment(self): |
|
831 """ |
|
832 Private method to get the path of the virtual environment. |
|
833 |
|
834 @return path of the virtual environment (string) |
|
835 """ |
|
836 language = self.__e5project.getProjectLanguage() |
|
837 if language == "Python3": |
|
838 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3") |
|
839 elif language == "Python2": |
|
840 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy2") |
|
841 else: |
|
842 virtEnv = "" |
|
843 if virtEnv and not os.path.exists(virtEnv): |
|
844 virtEnv = "" |
|
845 return virtEnv |
|
846 |
|
847 def __getDjangoAdminCommand(self): |
|
848 """ |
|
849 Public method to build a django-admin.py command. |
|
850 |
|
851 @param cmd command (string) |
|
852 @return full pyramid command (string) |
|
853 """ |
|
854 virtualEnv = self.__getVirtualEnvironment() |
|
855 if virtualEnv: |
|
856 if isWindowsPlatform(): |
|
857 cmd = os.path.join(virtualEnv, "Scripts", "django-admin.py") |
|
858 else: |
|
859 cmds = [ |
|
860 os.path.join(virtualEnv, "bin", "django-admin.py"), |
|
861 os.path.join(virtualEnv, "bin", "django-admin"), |
|
862 os.path.join(virtualEnv, "local", "bin", "django-admin.py"), |
|
863 os.path.join(virtualEnv, "local", "bin", "django-admin"), |
|
864 ] |
|
865 for cmd in cmds: |
|
866 if os.path.exists(cmd): |
|
867 break |
|
868 else: |
|
869 "" |
|
870 else: |
|
871 if isWindowsPlatform(): |
|
872 cmd = os.path.join(sys.exec_prefix, "Scripts", "django-admin.py") |
|
873 else: |
|
874 if Utilities.isinpath("django-admin.py"): |
|
875 cmd = "django-admin.py" |
|
876 elif Utilities.isinpath("django-admin"): |
|
877 cmd = "django-admin" |
|
878 else: |
|
879 # fall back |
|
880 cmd = "django-admin.py" |
|
881 return cmd |
|
882 |
|
883 def __getPythonExecutable(self): |
|
884 """ |
|
885 Public method to build the Python command. |
|
886 |
|
887 @return python command (string) |
|
888 """ |
|
889 python = "python" |
|
890 if isWindowsPlatform(): |
|
891 python += ".exe" |
|
892 else: |
|
893 language = self.__e5project.getProjectLanguage() |
|
894 if language == "Python3": |
|
895 python = "python3" |
|
896 elif language == "Python2": |
|
897 python = "python2" |
|
898 virtualEnv = self.__getVirtualEnvironment() |
|
899 if virtualEnv: |
|
900 if isWindowsPlatform(): |
|
901 python = os.path.join(virtualEnv, "Scripts", python) |
|
902 if not os.path.exists(python): |
|
903 python = os.path.join(virtualEnv, python) |
|
904 else: |
|
905 python = os.path.join(virtualEnv, "bin", python) |
|
906 |
|
907 return python |
|
908 |
826 def __djangoInfo(self): |
909 def __djangoInfo(self): |
827 """ |
910 """ |
828 Private slot to show some info about Django. |
911 Private slot to show some info about Django. |
829 """ |
912 """ |
830 from django import VERSION |
913 version = self.getDjangoVersion() |
831 version = '.'.join([str(i) for i in VERSION[:-1]]) |
914 url = "http://www.djangoproject.com" |
832 if VERSION[-1]: |
915 |
833 version += '-' + VERSION[-1] |
916 msgBox = E5MessageBox.E5MessageBox(E5MessageBox.Question, |
834 |
|
835 E5MessageBox.about(self.__ui, |
|
836 self.trUtf8("About Django"), |
917 self.trUtf8("About Django"), |
837 self.trUtf8( |
918 self.trUtf8( |
838 "<p>Django is a high-level Python Web framework that encourages rapid " |
919 "<p>Django is a high-level Python Web framework that encourages rapid " |
839 "development and clean, pragmatic design.</p>" |
920 "development and clean, pragmatic design.</p>" |
840 "<p><table>" |
921 "<p><table>" |
841 "<tr><td>Version:</td><td>{0}</td></tr>" |
922 "<tr><td>Version:</td><td>{0}</td></tr>" |
842 "<tr><td>URL:</td><td><a href=\"http://www.djangoproject.com\">" |
923 "<tr><td>URL:</td><td><a href=\"{1}\">" |
843 "http://www.djangoproject.com</a></td></tr>" |
924 "{1}</a></td></tr>" |
844 "</table></p>" |
925 "</table></p>" |
845 ).format(version) |
926 ).format(version, url), |
846 ) |
927 modal=True, |
|
928 buttons=E5MessageBox.Ok) |
|
929 msgBox.setIconPixmap(UI.PixmapCache.getPixmap( |
|
930 os.path.join("ProjectDjango", "icons", "django64.png"))) |
|
931 msgBox.exec_() |
847 |
932 |
848 def getDjangoVersion(self): |
933 def getDjangoVersion(self): |
849 """ |
934 """ |
850 Public method to get the Django version. |
935 Public method to get the Django version. |
851 |
936 |
852 @return Django version as a tuple |
937 @return Django version as a tuple |
853 """ |
938 """ |
854 from django import VERSION |
939 if not self.__djangoVersion: |
855 return VERSION |
940 ioEncoding = Preferences.getSystem("IOEncoding") |
|
941 cmd = self.__getDjangoAdminCommand() |
|
942 |
|
943 process = QProcess() |
|
944 process.start(cmd, ['--version']) |
|
945 procStarted = process.waitForStarted() |
|
946 if procStarted: |
|
947 finished = process.waitForFinished(30000) |
|
948 if finished and process.exitCode() == 0: |
|
949 output = \ |
|
950 str(process.readAllStandardOutput(), ioEncoding, 'replace') |
|
951 self.__djangoVersion = output.splitlines()[0].strip() |
|
952 |
|
953 return self.__djangoVersion |
856 |
954 |
857 def __getApplications(self): |
955 def __getApplications(self): |
858 """ |
956 """ |
859 Private method to ask the user for a list of application names. |
957 Private method to ask the user for a list of application names. |
860 |
958 |
994 title = self.trUtf8("Start Django Project") |
1083 title = self.trUtf8("Start Django Project") |
995 |
1084 |
996 args = [] |
1085 args = [] |
997 if Utilities.isWindowsPlatform(): |
1086 if Utilities.isWindowsPlatform(): |
998 args.append(self.__getPythonExecutable()) |
1087 args.append(self.__getPythonExecutable()) |
999 args.append(os.path.join(sys.exec_prefix, "Scripts", "django-admin.py")) |
1088 args.append(self.__getDjangoAdminCommand()) |
1000 else: |
1089 else: |
1001 if Utilities.isinpath("django-admin.py"): |
1090 cmd = self.__getDjangoAdminCommand() |
1002 args.append("django-admin.py") |
1091 if cmd: |
1003 elif Utilities.isinpath("django-admin"): |
1092 args.append(cmd) |
1004 args.append("django-admin") |
|
1005 else: |
1093 else: |
1006 # fall back |
1094 E5MessageBox.critical(self.__ui, |
1007 args.append("django-admin.py") |
1095 title, |
|
1096 self.trUtf8("""<p>The <b>django-admin.py</b> script is not in""" |
|
1097 """ the path. Aborting...</p>""")) |
|
1098 return |
|
1099 |
1008 args.append("startproject") |
1100 args.append("startproject") |
1009 args.append(projectName) |
1101 args.append(projectName) |
1010 |
1102 |
1011 dia = DjangoDialog(title, |
1103 dia = DjangoDialog(title, |
1012 msgSuccess=self.trUtf8("Django project created successfully.")) |
1104 msgSuccess=self.trUtf8("Django project created successfully.")) |
1574 args = Utilities.parseOptionString(consoleCmd) |
1668 args = Utilities.parseOptionString(consoleCmd) |
1575 args[0] = Utilities.getExecutablePath(args[0]) |
1669 args[0] = Utilities.getExecutablePath(args[0]) |
1576 args.append(self.__getPythonExecutable()) |
1670 args.append(self.__getPythonExecutable()) |
1577 args.append("manage.py") |
1671 args.append("manage.py") |
1578 args.append("shell") |
1672 args.append("shell") |
1579 if self.__plugin.getPreferences("UsePlainPython"): |
1673 language = self.__e5project.getProjectLanguage() |
1580 args.append("--plain") |
1674 if language == "Python2": |
|
1675 if self.__plugin.getPreferences("UsePlainPythonPy2"): |
|
1676 args.append("--plain") |
|
1677 else: |
|
1678 if self.__plugin.getPreferences("UsePlainPythonPy3"): |
|
1679 args.append("--plain") |
1581 try: |
1680 try: |
1582 wd = self.__sitePath() |
1681 wd = self.__sitePath() |
1583 started, pid = QProcess.startDetached(args[0], args[1:], wd) |
1682 started, pid = QProcess.startDetached(args[0], args[1:], wd) |
1584 if not started: |
1683 if not started: |
1585 E5MessageBox.critical(None, |
1684 E5MessageBox.critical(None, |