def append(self, runat, command, title, output): tmpfile = tempfile.mkstemp() fd, path = tmpfile tmp = os.fdopen(fd, 'w') tmp.write(self.SCRIPT_DELIMITER + "\n") tmp.write(self.PREPEND_SCRIPT) if self.manual_poscorrect: tmp.write(self.POSIXLY_CORRECT_UNSET) display = "" if output > 0: display = os.getenv('DISPLAY') tmp.write(self.DISPLAY % display) tmp.write(command + "\n") tmp.close() temp = None if self.root == 1: if self.user != "root": #changes the ownership os.chown(path, self.uid, self.gid) execute = config.getSubin( ) + " " + self.user + " -c \"" + config.getAtbin( ) + " -f " + path + " " + runat + " && exit\"" else: execute = config.getAtbin() + " -f " + path + " " + runat else: execute = config.getAtbin() + " -f " + path + " " + runat p = Popen(execute, shell=True, env=self.at_env, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) (child_stdin, child_stdout, child_stderr) = (p.stdin, p.stdout, p.stderr) err = child_stderr.readlines() job_id = 0 for line in err: t = self.parse(line, False) if t != False: job_id = t #print job_id desc = "" self.write_job_data(job_id, title, desc, output, display) os.unlink(path)
def update (self, job_id, runat, command, title, output): #print "update" + str (job_id) + runat + command + title #remove old f = os.path.join (self.atdata, str (job_id)) if os.access (f, os.F_OK): os.unlink (f) execute = config.getAtrmbin()+ " " + str(job_id) commands.getoutput(execute) #add new tmpfile = tempfile.mkstemp () fd, path = tmpfile tmp = os.fdopen(fd, 'w') tmp.write (self.SCRIPT_DELIMITER + "\n") tmp.write (self.PREPEND_SCRIPT) if self.manual_poscorrect: tmp.write (self.POSIXLY_CORRECT_UNSET) display = "" if output > 0: display = os.getenv ('DISPLAY') tmp.write (self.DISPLAY % display ) tmp.write (command + "\n") tmp.close () if self.root == 1: if self.user != "root": #changes the ownership os.chown(path, self.uid, self.gid) execute = config.getSubin() + " " + self.user + " -c \"" + config.getAtbin() + " -f " + path + " " + runat + " && exit\"" else: execute = config.getAtbin() + " -f " + path + " " + runat else: execute = config.getAtbin() + " -f " + path + " " + runat p = Popen (execute, shell = True, env = self.at_env, stdin = PIPE, stdout = PIPE, stderr = PIPE, close_fds = True) (child_stdin, child_stdout, child_stderr) = (p.stdin, p.stdout, p.stderr) err = child_stderr.readlines () job_id = 0 for line in err: t = self.parse (line, False) if t != False: job_id = t #print job_id desc = "" self.write_job_data (job_id, title, desc, output, display) os.unlink (path)
def parse (self, line, output = True): if (output == True): if len (line) > 1 and line[0] != '#': m = self.atRecordRegex.match(line) if m != None: # Time time = m.group('time') # FreeBSD: # We are ignoring timezone and hope everything works # out in the end. # Date day = m.group('day') month = m.group ('month') for monthname in self.months: month = month.replace (monthname, self.months[monthname]) if int (day) < 10: day = "0" + day if int (month) < 10: month = "0" + month date = day + "." + month + "." + m.groups ()[5] job_id = m.group ('jobid') class_id = m.group ('class') user = m.group ('user') success, title, desc, manual_poscorrect, output, display, stdlocale = self.get_job_data (int (job_id)) # manual_poscorrect is only used during preparation of script execute = config.getAtbin() + " -c " + job_id # read lines and detect starter script = Popen(execute, shell = True, env = self.at_env, stdout = PIPE).stdout.read() script, dangerous = self.__prepare_script__ (script, manual_poscorrect, output, display, stdlocale) #removing ending newlines, but keep one #if a date in the past is selected the record is removed by at, this creates an error, and generally if the script is of zero length # TODO: complain about it as well script = script.rstrip() return job_id, date, time, class_id, user, script, title, dangerous, output elif (output == False): if len (line) > 1 and line[0] != '#': m = self.atRecordRegexAdd.search(line) #print "Parsing line: " + line if m != None: #print "Parse successfull, groups: " #print m.groups() job_id = m.group('jobid') return int(job_id) else: return False return False
def on_run_button_clicked (self, *args): dialog = gtk.MessageDialog(self.widget, gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, gtk.BUTTONS_NONE, _("Are you sure you want to run this task now?\n\nThis is used to preview the task and initiates a one-time run, this does not affect the normal scheduled run times.")) dialog.add_buttons (gtk.STOCK_EXECUTE, gtk.RESPONSE_YES, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL) dialog.set_title (_("Are you sure you want to run this task?")) if (dialog.run() != gtk.RESPONSE_YES): dialog.destroy() del dialog return dialog.destroy() del dialog if (self.backend.get_not_inform_working_dir() != True): dia2 = gtk.MessageDialog (self.widget, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING, gtk.BUTTONS_NONE, _("Note about working directory of executed tasks:\n\nRecurrent tasks will be run from the home directory, one-time tasks from the directory where Gnome schedule was run from at the time of task creation (normally the home directory).")) dia2.add_buttons (_("_Don't show again"), gtk.RESPONSE_CLOSE, gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL) dia2.set_title (_("Warning: Working directory of executed tasks")) response = dia2.run () if response == gtk.RESPONSE_CANCEL: dia2.destroy () del dia2 return elif response == gtk.RESPONSE_CLOSE: self.backend.set_not_inform_working_dir (True) else: pass dia2.destroy () del dia2 store, iter = self.treeview.get_selection().get_selected() try: # commands are at model[3] #see what scheduler (at, crontab or ...) self.schedule = self.treemodel.get_value(iter, 7) tmpfile = tempfile.mkstemp () fd, path = tmpfile tmp = os.fdopen (fd, 'w') commands = self.treemodel.get_value(iter, 3) linenumber = self.treemodel.get_value(iter, 4) if self.schedule.get_type () == "at": script = os.popen (config.getAtbin () + " -c " + str (linenumber)).read () elif self.schedule.get_type () == "crontab": script = self.schedule.parse (commands)[1][5] # left untranslated to protect against any 'translation attacks'.. script = script + "\necho " + "Press ENTER to continue and close this window." + "\n" script = script + "read\nexit\n" tmp.write (script) tmp.flush () self.temp_files.append ((tmp, path)) if self.root == 1 and self.user != "root": execute = "su " + self.user + " -c \"" + self.user_shell + " " + path os.chown (path, self.uid, self.gid) else: execute = self.user_shell + " " + path os.chmod (path, stat.S_IEXEC | stat.S_IREAD) # unset POSIXLY_CORRECT if manually set, bug 612459 if self.manual_poscorrect: del os.environ['POSIXLY_CORRECT'] # get terminal and exec params terminal = None terminalparam = None try: tex = ['gsettings', 'get', 'org.gnome.desktop.default-applications.terminal', 'exec'] terminal = subprocess.check_output (tex) tex = ['gsettings', 'get', 'org.gnome.desktop.default-applications.terminal', 'exec-arg'] terminalparam = subprocess.check_output (tex) except Exception, ex: terminal = None terminalparam = None print ex self.dialog = gtk.MessageDialog(self.widget, gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, _("No default graphical terminal for GNOME could be found.")) self.dialog.run () self.dialog.destroy () return tex = terminal.strip () + ' ' + terminalparam.strip () + ' ' + execute subprocess.Popen(tex, cwd = self.user_home_dir, shell=True) if self.manual_poscorrect: os.environ['POSIXLY_CORRECT'] = 'enabled'
def on_run_button_clicked(self, *args): dialog = gtk.MessageDialog( self.widget, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, gtk.BUTTONS_NONE, _("Are you sure you want to run this task now?\n\nThis is used to preview the task and initiates a one-time run, this does not affect the normal scheduled run times." )) dialog.add_buttons(gtk.STOCK_EXECUTE, gtk.RESPONSE_YES, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL) dialog.set_title(_("Are you sure you want to run this task?")) if (dialog.run() != gtk.RESPONSE_YES): dialog.destroy() del dialog return dialog.destroy() del dialog if (self.backend.get_not_inform_working_dir() != True): dia2 = gtk.MessageDialog( self.widget, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING, gtk.BUTTONS_NONE, _("Note about working directory of executed tasks:\n\nRecurrent tasks will be run from the home directory, one-time tasks from the directory where Gnome schedule was run from at the time of task creation (normally the home directory)." )) dia2.add_buttons(_("_Don't show again"), gtk.RESPONSE_CLOSE, gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL) dia2.set_title(_("Warning: Working directory of executed tasks")) response = dia2.run() if response == gtk.RESPONSE_CANCEL: dia2.destroy() del dia2 return elif response == gtk.RESPONSE_CLOSE: self.backend.set_not_inform_working_dir(True) else: pass dia2.destroy() del dia2 store, iter = self.treeview.get_selection().get_selected() try: # commands are at model[3] #see what scheduler (at, crontab or ...) self.schedule = self.treemodel.get_value(iter, 7) tmpfile = tempfile.mkstemp() fd, path = tmpfile tmp = os.fdopen(fd, 'w') commands = self.treemodel.get_value(iter, 3) linenumber = self.treemodel.get_value(iter, 4) if self.schedule.get_type() == "at": script = os.popen(config.getAtbin() + " -c " + str(linenumber)).read() elif self.schedule.get_type() == "crontab": script = self.schedule.parse(commands)[1][5] # left untranslated to protect against any 'translation attacks'.. script = script + "\necho " + "Press ENTER to continue and close this window." + "\n" script = script + "read\nexit\n" tmp.write(script) tmp.flush() self.temp_files.append((tmp, path)) if self.root == 1: if self.user != "root": execute = "su " + self.user + " -c \"" + self.user_shell + " " + path os.chown(path, self.uid, self.gid) else: execute = self.user_shell + " " + path os.chmod(path, stat.S_IEXEC | stat.S_IREAD) # unset POSIXLY_CORRECT if manually set, bug 612459 if self.manual_poscorrect: del os.environ['POSIXLY_CORRECT'] # get terminal and exec params terminal = None terminalparam = None try: tex = [ 'gsettings', 'get', 'org.gnome.desktop.default-applications.terminal', 'exec' ] terminal = subprocess.check_output(tex) tex = [ 'gsettings', 'get', 'org.gnome.desktop.default-applications.terminal', 'exec-arg' ] terminalparam = subprocess.check_output(tex) except Exception, ex: terminal = None terminalparam = None print ex self.dialog = gtk.MessageDialog( self.widget, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, _("No default graphical terminal for GNOME could be found." )) self.dialog.run() self.dialog.destroy() return tex = terminal.strip() + ' ' + terminalparam.strip( ) + ' ' + execute subprocess.Popen(tex, cwd=self.user_home_dir, shell=True) if self.manual_poscorrect: os.environ['POSIXLY_CORRECT'] = 'enabled'
def parse(self, line, output=True): if (output == True): if len(line) > 1 and line[0] != '#': m = self.atRecordRegex.match(line) if m != None: # Time time = m.group('time') # FreeBSD: # We are ignoring timezone and hope everything works # out in the end. # Date day = m.group('day') month = m.group('month') for monthname in self.months: month = month.replace(monthname, self.months[monthname]) if int(day) < 10: day = "0" + day if int(month) < 10: month = "0" + month date = day + "." + month + "." + m.groups()[5] job_id = m.group('jobid') class_id = m.group('class') user = m.group('user') success, title, desc, manual_poscorrect, output, display, stdlocale = self.get_job_data( int(job_id)) # manual_poscorrect is only used during preparation of script execute = config.getAtbin() + " -c " + job_id # read lines and detect starter script = Popen(execute, shell=True, env=self.at_env, stdout=PIPE).stdout.read() script, dangerous = self.__prepare_script__( script, manual_poscorrect, output, display, stdlocale) #removing ending newlines, but keep one #if a date in the past is selected the record is removed by at, this creates an error, and generally if the script is of zero length # TODO: complain about it as well script = script.rstrip() return job_id, date, time, class_id, user, script, title, dangerous, output elif (output == False): if len(line) > 1 and line[0] != '#': m = self.atRecordRegexAdd.search(line) #print "Parsing line: " + line if m != None: #print "Parse successfull, groups: " #print m.groups() job_id = m.group('jobid') return int(job_id) else: return False return False