Commits

Martin von Löwis committed 1694b07 Merge

merged

Comments (0)

Files changed (3)

pep381client/__init__.py

     def __init__(self):
         self.homedir = None
         self.quiet = False
-
+        
         # time stamps: seconds since 1970
         self.last_completed = 0 # when did the last run complete
         self.last_started = 0   # when did the current run start
     def store(self):
         with open(os.path.join(self.homedir, "status"), "wb") as f:
             cPickle.dump(self, f, cPickle.HIGHEST_PROTOCOL)
-            self.conn.commit()
+            self.storage.commit()
 
     @staticmethod
-    def load(homedir):
+    def load(homedir, storage=None):
         res = cPickle.load(open(os.path.join(homedir, "status"), "rb"))
-        res.conn = sqlite.open(os.path.join(homedir, "files"))
-        res.cursor = res.conn.cursor()
+        res.storage = storage or sqlite.SqliteStorage(os.path.join(homedir, "files"))
         res.defaults()
         return res
 
     #################### Synchronization logic ##############################
 
     @staticmethod
-    def initialize(targetdir):
+    def initialize(targetdir, storage=None):
         'Create a new empty mirror. This operation should not be interrupted.'
         if not os.path.exists(targetdir):
             os.makedirs(targetdir)
         status.homedir = targetdir
         status.last_started = now()
         status.projects_to_do = set(xmlrpc().list_packages())
-        status.conn = sqlite.open(os.path.join(status.homedir, "files"))
-        status.cursor = status.conn.cursor()
+        status.storage = storage or sqlite.SqliteStorage(os.path.join(status.homedir, "files"))
         status.store()
         return status
 
                 if not self.quiet:
                     print "Copying", file
                 self.maybe_copy_file(project, file)
-            for file in sqlite.files(self.cursor, project)-files:
+            for file in self.storage.files(project)-files:
                     self.remove_file(file)
             self.complete_projects.add(project)
             self.projects_to_do.remove(project)
         else:
             h.putrequest("GET", urllib2.quote(path))
         h.putheader('User-Agent', UA)
-        etag = sqlite.etag(self.cursor, path)
+        etag = self.storage.etag(path)
         if etag:
             h.putheader("If-none-match", etag)
         h.endheaders()
             path = path[1:]
         lpath = os.path.join(self.homedir, "web", path)
         if r.status == 200:
-            sqlite.remove_file(self.cursor, path) # readd when done downloading
+            self.storage.remove_file(path) # readd when done downloading
             data = r.read()
             dirname = os.path.dirname(lpath)
             if not os.path.exists(dirname):
                 f.write(data)
             # XXX may set last-modified timestamp on file
             if "etag" in r.msg:
-                sqlite.add_file(self.cursor, project, path, r.msg['etag'])
+                self.storage.add_file(project, path, r.msg['etag'])
             self.store()
             return
         if r.status == 404:
             self.remove_file(path)
 
     def remove_file(self, path):
-        sqlite.remove_file(self.cursor, path)
+        self.storage.remove_file(path)
         lpath = os.path.join(self.homedir, "web", path)
         if os.path.exists(lpath):
             os.unlink(lpath)
 
     def delete_project(self, project):
-        for f in sqlite.files(self.cursor, project):
+        for f in self.storage.files(project):
             self.remove_file(f)
         project_simple_dir = os.path.join(self.homedir, "web", "simple", project)
         if os.path.exists(project_simple_dir):

pep381client/sqlite.py

 import sqlite3, os
 
-schema = ["create table if not exists files(project, filename, etag)",
-          "create index if not exists files_project on files(project)",
-          "create index if not exists files_filename on files(filename)",
-          ]
+class SqliteStorage(object):
 
-def open(filename):
-    conn = sqlite3.connect(filename)
-    for stmt in schema:
-        conn.execute(stmt)
-    conn.commit()
-    return conn
+    schema = ["create table if not exists files(project, filename, etag)",
+              "create index if not exists files_project on files(project)",
+              "create index if not exists files_filename on files(filename)",
+              ]
 
-def files(cursor, project):
-    cursor.execute("select filename from files where project=?", (project,))
-    return set(r[0] for r in cursor.fetchall())
+    def __init__(self, filename):
+        self.conn = sqlite3.connect(filename)
+        cursor = self.conn.cursor()
+        for stmt in self.schema:
+            cursor.execute(stmt)
+        self.commit()
 
-def etag(cursor, filename):
-    cursor.execute("select etag from files where filename=?", (filename,))
-    res = cursor.fetchone()
-    if res:
-        return res[0]
-    else:
-        return None
+    def commit(self):
+        self.conn.commit()
 
-def add_file(cursor, project, filename, etag):
-    cursor.execute("insert into files(project, filename, etag) values(?, ?, ?)",
-                   (project, filename, etag))
+    def files(self, project):
+        cursor = self.conn.cursor()
+        cursor.execute("select filename from files where project=?", (project,))
+        return set(r[0] for r in cursor.fetchall())
 
-def remove_file(cursor, filename):
-    cursor.execute("delete from files where filename=?", (filename,))
+    def etag(self, filename):
+        cursor = self.conn.cursor()
+        cursor.execute("select etag from files where filename=?", (filename,))
+        res = cursor.fetchone()
+        if res:
+            return res[0]
+        else:
+            return None
+
+    def add_file(self, project, filename, etag):
+        cursor = self.conn.cursor()
+        cursor.execute("insert into files(project, filename, etag) values(?, ?, ?)",
+                       (project, filename, etag))
+
+    def remove_file(self, filename):
+        cursor = self.conn.cursor()
+        cursor.execute("delete from files where filename=?", (filename,))

scripts/pep381run

 
 state.quiet = quiet
 
-state.synchronize()
+try:
+    state.synchronize()
+except KeyboardInterrupt:
+    print "\nMirroring paused. Resume by restarting pep381run."