include java.lang.Runtime; include java.io.File; include java.io.InputStream; include java.io.OutputStream; include java.io.IOException; include library.chat; include developer.file_access; include java.io.InputStreamReader; include java.io.BufferedReader; String getIdOptions() { obj_id self = getSelf(); if(! isGod(self)) return null; String userId = getStringObjVar(self, "P4USER"); String password = getStringObjVar(self, "P4PASSWD"); return "-P " + password + " -u " + userId; } boolean isPerforceConfigured() { boolean result = false; obj_id self = getSelf(); if(! isGod(self)) return false; if(hasObjVar(self, "P4USER")) { String perforceUserId = getStringObjVar(self, "P4USER"); if(perforceUserId != null && perforceUserId.length() > 0) { if(hasObjVar(self, "P4PASSWD")) { String perforcePassword = getStringObjVar(self, "P4PASSWD"); if(perforcePassword != null && perforcePassword.length() > 0) { // perform a pretend sync to see if the user and password // are correct result = system_process.runAndGetExitCode("p4 -P " + perforcePassword + " -u " + perforceUserId + " opened") == 0; } } } } return result; } boolean isLocked(String fileName) { if(! isGod(getSelf())) return false; boolean result = false; if(whoLocked(fileName) != null) { result = true; } return result; } String whoLocked(String fileName) { if(! isGod(getSelf())) return null; String lockFileName = fileName + ".scriptlock"; String lockContents = file_access.readTextFile(lockFileName); return lockContents; } boolean openExistingFileForExclusiveEdit(String fileName) { boolean result = false; obj_id self = getSelf(); if(! isGod(self)) return false; String userId = getStringObjVar(self, "P4USER"); String password = getStringObjVar(self, "P4PASSWD"); if(isLocked(fileName)) { String who = whoLocked(fileName); if(who != userId) { return false; } else { return true; } } if(file_access.isWritable(fileName)) { file_access.writeTextFile(fileName + ".scriptlock", userId); result = true; } else if(isPerforceConfigured()) { if(system_process.runAndGetExitCode("p4 -P " + password + " -u " + userId + " edit " + fileName) == 0) { file_access.writeTextFile(fileName + ".scriptlock", userId); result = true; } } return result; } String[] opened() { if(! isPerforceConfigured()) { setupPerforce(); return null; } String[] result = null; obj_id self = getSelf(); if(! isGod(self)) return null; String userId = getStringObjVar(self, "P4USER"); String password = getStringObjVar(self, "P4PASSWD"); Runtime run = Runtime.getRuntime(); if(run != null) { result = split(system_process.runAndGetOutput("p4 -P " + password + " -u " + userId + " opened"), '\n'); } return result; } void setupPerforce() { obj_id self = getSelf(); if(! isGod(self)) return; // popup perforce username/password page int page = createSUIPage("/Script.perforceSetup", self, self); if(page >= 0) { subscribeToSUIEvent(page, sui_event_type.SET_onButton, "btnOk", "onPerforceSetupBtnOk"); subscribeToSUIPropertyForEvent(page, sui_event_type.SET_onButton, "btnOk", "boxInputUserId.inputUserId", "LocalText"); subscribeToSUIPropertyForEvent(page, sui_event_type.SET_onButton, "btnOk", "boxInputPassword.inputPassword", "LocalText"); setSUIAssociatedObject(page, self); boolean showResult = showSUIPage(page); flushSUIPage(page); } } String change(int changeList) { obj_id self = getSelf(); if(! isGod(self)) return null; String result = null; String userId = getStringObjVar(self, "P4USER"); String password = getStringObjVar(self, "P4PASSWD"); Runtime run = Runtime.getRuntime(); if(run != null) { String cmdLine = "p4 -P " + password + " -u " + userId + " change -o"; if(changeList != 0) { cmdLine += " " + changeList; } system_process p = new system_process(cmdLine); p.waitFor(); if(p.getExitValue() == 0) { result = p.getOutput(); } } return result; } String where(String fileLocation) { return system_process.runAndGetOutput("p4 " + getIdOptions() + " where " + fileLocation); } boolean submit(String changeDescription, resizeable String[] resultText) { if(! isGod(getSelf())) return false; // build a file list from the description String[] changeLines = split(changeDescription, '\n'); int iter = 0; boolean readingFiles = false; resizeable String[] fileLocks = new Vector(); for(iter = 0; iter < changeLines.length; ++iter) { if(readingFiles) { try { java.util.StringTokenizer st = new java.util.StringTokenizer(changeLines[iter], " \t\n"); String first = st.nextToken(); if(first != null) { String w = where(first); String[] locs = split(w, ' '); if(locs[2] != null) { String fileName = locs[2].trim(); fileName += ".scriptlock"; fileLocks.add(fileName); } } } catch(java.util.NoSuchElementException e) { // ignore lines that don't conform the the expected output } } else if(changeLines[iter].startsWith("Files:")) { readingFiles = true; } } obj_id self = getSelf(); boolean result = false; String cmdLine = "p4 " + getIdOptions() + " submit -i"; system_process p = new system_process(cmdLine); if(p != null) { p.putAndCloseInput(changeDescription); p.waitFor(); if(p.getExitValue() == 0) { resultText.add(p.getOutput()); // remove file locks for(iter = 0; iter < fileLocks.length; ++iter) { File f = new File(fileLocks[iter]); if(f.exists()) { if(f.canWrite()) { if(f.isFile()) { if(! f.delete()) { resultText.add("failed to remove lock " + fileLocks[iter] + ": could not delete file"); } } else { resultText.add("failed to remove lock " + fileLocks[iter] + ": not a file"); } } else { resultText.add(fileLocks[iter] + ": not writeable"); } } else { resultText.add("failed to remove file lock " + fileLocks[iter] + ": it does not exist"); } } } else { resultText.add(p.getOutput()); resultText.add("Failed to execute " + cmdLine); } } else { resultText.add("Could not execute " + cmdLine); } return result; } String diff(String fileSpec) { if(! isGod(getSelf())) return null; String result = null; if(fileSpec != null) { result = system_process.runAndGetOutput("p4 " + getIdOptions() + " diff -dU150 " + fileSpec); } else { result = system_process.runAndGetOutput("p4 " + getIdOptions() + " diff -dU150"); } return result; }