[gnome-schedule/eximport] Add export and import



commit ec38e415ed09dd5f7dbaa34530b5487c35d5f8a7
Author: Gaute Hope <eg gaute vetsj com>
Date:   Sat Jun 11 14:32:08 2011 +0200

    Add export and import
    
    Create export and import scripts. Invoked through:
     $ gnome-schedule-export [file] (or nothing for output to stdout)
    
     and
    
     $ gnome-schedule-import [file] (or nothing to read from stdin)
    
     using Pythons 'pickle' to store necessary data.

 configure.ac                 |    2 +
 po/POTFILES.in               |    2 +
 src/Makefile.am              |    5 ++-
 src/db.py                    |   40 +++++++++++++++
 src/gnome-schedule-export    |    2 +
 src/gnome-schedule-export.in |    2 +
 src/gnome-schedule-export.py |  112 ++++++++++++++++++++++++++++++++++++++++++
 src/gnome-schedule-import    |    2 +
 src/gnome-schedule-import.in |    2 +
 src/gnome-schedule-import.py |  110 +++++++++++++++++++++++++++++++++++++++++
 10 files changed, 278 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index a7f050d..185a13b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -123,6 +123,8 @@ Makefile
 src/config.py
 src/Makefile
 src/gnome-schedule
+src/gnome-schedule-export
+src/gnome-schedule-import
 icons/Makefile
 po/Makefile.in
 desktop/Makefile
diff --git a/po/POTFILES.in b/po/POTFILES.in
index bf588e8..f8d4cd9 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -14,6 +14,8 @@ src/crontabEditorHelper.py
 src/gnome-schedule-applet.xml
 src/gnome-schedule.glade
 src/gnome-schedule.py
+src/gnome-schedule-export.py
+src/gnome-schedule-import.py
 src/lang.py
 src/mainWindow.py
 src/scheduleapplet.py
diff --git a/src/Makefile.am b/src/Makefile.am
index 073081e..567ebac 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,8 +1,11 @@
-bin_SCRIPTS = gnome-schedule
+bin_SCRIPTS = gnome-schedule gnome-schedule-export gnome-schedule-import
 
 gnomescheduledir = $(prefix)/share/gnome-schedule
 gnomeschedule_PYTHON = \
 	gnome-schedule.py \
+	gnome-schedule-export.py \
+	gnome-schedule-import.py \
+	db.py \
 	at.py \
 	config.py \
 	mainWindow.py \
diff --git a/src/db.py b/src/db.py
new file mode 100644
index 0000000..ea65f76
--- /dev/null
+++ b/src/db.py
@@ -0,0 +1,40 @@
+# db.py - db class to store database of crontab and at tasks
+# Copyright (C) 2011 Gaute Hope <eg at gaute dot vetsj dot com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+# Using Pythons pickle to dump tasks
+class GnomeScheduleDB:
+  VERSION = 1
+
+  # Format:
+  # [title, time, preview, line, output]
+  crontab = []
+
+
+  # Format:
+  # [title, date, time, preview, script, output]
+  at = []
+
+  def __init__ (self):
+    pass
+
+  def setcrontab (self, c):
+    self.crontab = c
+
+  def setat (self, a):
+    self.at = a
+
+
diff --git a/src/gnome-schedule-export b/src/gnome-schedule-export
new file mode 100644
index 0000000..ec0e18c
--- /dev/null
+++ b/src/gnome-schedule-export
@@ -0,0 +1,2 @@
+#! /bin/sh
+PYTHONPATH=::/usr/lib/python2.7/site-packages/gtk-2.0/:$PYTHONPATH /usr/bin/python2 /home/gaute/dev/gnome/gs/rep/inst/share/gnome-schedule/gnome-schedule-export.py $1
diff --git a/src/gnome-schedule-export.in b/src/gnome-schedule-export.in
new file mode 100644
index 0000000..27056e5
--- /dev/null
+++ b/src/gnome-schedule-export.in
@@ -0,0 +1,2 @@
+#! /bin/sh
+PYTHONPATH= PYTHONPATH@/gtk-2.0/:$PYTHONPATH @PYTHON@ @prefix@/share/gnome-schedule/gnome-schedule-export.py $1
diff --git a/src/gnome-schedule-export.py b/src/gnome-schedule-export.py
new file mode 100755
index 0000000..1dee70a
--- /dev/null
+++ b/src/gnome-schedule-export.py
@@ -0,0 +1,112 @@
+# gnome-schedule-export-py - This exports crontab and at tasks with their info
+# to the file specified.
+#
+# Copyright (C) 2011  Gaute Hope <eg at gaute dot vetsj dot com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02110-1301, USA.
+
+#python
+import sys
+import os
+import pwd
+import pickle
+
+# g-s modules
+import config
+import crontab
+import at
+from db import *
+
+# NEEDED FOR SUBMODULES
+##
+## I18N
+##
+import gettext
+gettext.install(config.GETTEXT_PACKAGE(), config.GNOMELOCALEDIR(), unicode=1)
+
+poscorrect_isset = os.getenv ("POSIXLY_CORRECT", False)
+manual_poscorrect = False
+if poscorrect_isset == False:
+    os.putenv ("POSIXLY_CORRECT", "enabled")
+    manual_poscorrect = True
+
+sys.stderr.write(_("Gnome Schedule: Export tasks.") + "\n")
+sys.stderr.write(_("Usage: %s [output file]" % sys.argv[0]) + "\n")
+sys.stderr.write(_("       No file means export to stdout.") + "\n\n")
+
+
+# Parse arguments
+if len(sys.argv) == 2:
+  outf = sys.argv[1]
+  stdo = False
+else:
+  outf = False
+  stdo = True
+
+
+uid = os.geteuid ()
+gid = os.getegid ()
+user = pwd.getpwuid (uid)[0]
+home_dir = pwd.getpwuid (uid)[5]
+user_shell = pwd.getpwuid (uid)[6]
+if uid == 0: is_root = True
+else: is_root = False
+
+# Check file
+if outf != False:
+  if os.access (outf, os.F_OK):
+    sys.stderr.write (_("File exists already.") + "\n")
+    sys.exit (1)
+
+if stdo:
+  of = sys.stdout
+else:
+  try:
+    of = open(outf, 'wb')
+  except:
+    sys.stderr.write (_("Could not open file for writing: %s" % outf) + "\n")
+    sys.exit (1)
+
+d = GnomeScheduleDB ()
+
+c = crontab.Crontab (is_root, user, uid, gid, home_dir)
+tasks = c.read ()
+crontabc = 0
+cl = []
+for task in tasks:
+  sys.stderr.write(_("Exporting crontab task: %s" % task[0]) + "\n")
+  crontabc = crontabc + 1
+  cl.append([task[0], task[5], task[2], task[3], task[14]])
+
+d.setcrontab (cl)
+
+# AT
+a = at.At(is_root, user, uid, gid, home_dir, manual_poscorrect)
+tasks = a.read ()
+atc = 0
+al = []
+for task in tasks:
+  sys.stderr.write(_("Exporting at task: " % task[0]) + "\n")
+  atc = atc + 1
+  al.append([task[0], task[8], task[11], task[2], task[3], task[14]])
+
+d.setat (al)
+
+pickle.dump(d, of, -1)
+of.flush ()
+of.close ()
+
+sys.stderr.write (_("Finished, exported: %d tasks total" % (atc + crontabc)) + "\n")
+
diff --git a/src/gnome-schedule-import b/src/gnome-schedule-import
new file mode 100644
index 0000000..849d446
--- /dev/null
+++ b/src/gnome-schedule-import
@@ -0,0 +1,2 @@
+#! /bin/sh
+PYTHONPATH=::/usr/lib/python2.7/site-packages/gtk-2.0/:$PYTHONPATH /usr/bin/python2 /home/gaute/dev/gnome/gs/rep/inst/share/gnome-schedule/gnome-schedule-import.py $1
diff --git a/src/gnome-schedule-import.in b/src/gnome-schedule-import.in
new file mode 100644
index 0000000..f973e56
--- /dev/null
+++ b/src/gnome-schedule-import.in
@@ -0,0 +1,2 @@
+#! /bin/sh
+PYTHONPATH= PYTHONPATH@/gtk-2.0/:$PYTHONPATH @PYTHON@ @prefix@/share/gnome-schedule/gnome-schedule-import.py $1
diff --git a/src/gnome-schedule-import.py b/src/gnome-schedule-import.py
new file mode 100755
index 0000000..36e9857
--- /dev/null
+++ b/src/gnome-schedule-import.py
@@ -0,0 +1,110 @@
+# gnome-schedule-import-py - This import crontab and at tasks and installs
+# them for the current user from the file specified.
+#
+# Copyright (C) 2011  Gaute Hope <eg at gaute dot vetsj dot com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02110-1301, USA.
+
+#python
+import sys
+import os
+import pwd
+import pickle
+
+# g-s modules
+import config
+import crontab
+import at
+from db import *
+
+# NEEDED FOR SUBMODULES
+##
+## I18N
+##
+import gettext
+gettext.install(config.GETTEXT_PACKAGE(), config.GNOMELOCALEDIR(), unicode=1)
+
+poscorrect_isset = os.getenv ("POSIXLY_CORRECT", False)
+manual_poscorrect = False
+if poscorrect_isset == False:
+    os.putenv ("POSIXLY_CORRECT", "enabled")
+    manual_poscorrect = True
+
+
+# Parse arguments
+if len(sys.argv) == 2:
+  outf = sys.argv[1]
+  stdo = False
+else:
+  outf = False
+  stdo = True
+
+
+uid = os.geteuid ()
+gid = os.getegid ()
+user = pwd.getpwuid (uid)[0]
+home_dir = pwd.getpwuid (uid)[5]
+user_shell = pwd.getpwuid (uid)[6]
+if uid == 0: is_root = True
+else: is_root = False
+
+sys.stderr.write(_("Gnome Schedule: Import tasks tasks.") + "\n")
+sys.stderr.write(_("Usage: %s [input file]" % sys.argv[0]) + "\n")
+sys.stderr.write(_("       No file means import from stdin.") + "\n\n")
+
+# Check file
+if outf != False:
+  if not os.access (outf, os.F_OK):
+    sys.stderr.write (_("File does not exist.") + "\n")
+    sys.exit (1)
+
+if stdo:
+  of = sys.stdin
+  sys.stderr.write (_("Reading from stdin..") + "\n")
+else:
+  try:
+    of = open(outf, 'rb')
+    # Reading file
+    sys.stderr.write (_("Reading file: %s.." % outf) + "\n")
+  except:
+    sys.stderr.write (_("Could not open file for reading: %s" % outf) + "\n")
+    sys.exit (1)
+
+
+d = pickle.load (of)
+
+c = crontab.Crontab (is_root, user, uid, gid, home_dir)
+c.read ()
+
+crontabc = 0
+for task in d.crontab:
+  sys.stderr.write(_("Importing crontab task: %s" % task[0]) + "\n")
+  (minute, hour, dom, moy, dow, command) = c.parse (task[3], True)
+  c.append (minute, hour, dom, moy, dow, command, task[4], task[0])
+  crontabc = crontabc + 1
+
+
+# AT
+a = at.At(is_root, user, uid, gid, home_dir, manual_poscorrect)
+a.read ()
+
+atc = 0
+for task in d.at:
+  sys.stderr.write(_("Importing at task: %s" % task[0]) + "\n")
+  a.append (task[2] + " " + task[1], task[4], task[0], task[5])
+  atc = atc + 1
+
+sys.stderr.write (_("Finished, imported: %d tasks total." % (atc + crontabc)) + "\n")
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]