示例#1
0
 def __init__(self,
              transcoder,
              destpath,
              playlists,
              dryrun,
              concurrency):
     self.synchronizer = Synchronizer(transcoder)
     self.synchronizer.set_target_dir(destpath)
     [ self.synchronizer.add_playlist(p) for p in playlists ]
     self.dryrun = dryrun
     self.concurrency = concurrency
示例#2
0
class SynchronizationCLIBackend:
    def __init__(self,
                 transcoder,
                 destpath,
                 playlists,
                 dryrun,
                 concurrency):
        self.synchronizer = Synchronizer(transcoder)
        self.synchronizer.set_target_dir(destpath)
        [ self.synchronizer.add_playlist(p) for p in playlists ]
        self.dryrun = dryrun
        self.concurrency = concurrency

    def run(self):
        """Returns:

        0 if all transcoding / synchronization operations completed.
        2 if scanning experienced a problem.
        4 if a transcoding / synchronization failure took place.
        8 if a problem took place while writing playlists.
        Or a combination of the above.

        Raises Exception in an unexpected failure took place.
        """
        exitval = 0
        failures = self.synchronizer.scan()
        if failures:
            print >> sys.stderr, "Problems encountered during scanning:\n"
            for s, t in failures:
                print >> sys.stderr, \
                    "Could not scan: %r\nBecause: %r\n" % (s, t)
            print >> sys.stderr
            exitval += 2

        if self.dryrun:
            ops, errors = self.synchronizer.compute_synchronization()
            for s, t in ops.items():
                print "Source: %r\nTarget: %r\n" % (s, t)
            for s, t in errors.items():
                print "Not transferring: %r\nBecause: %r\n" % (s, t)
            if errors:
                exitval += 8
            return exitval

        sync_tasks = self.synchronizer.synchronize(concurrency=self.concurrency)
        errors = None
        for s, t in sync_tasks:
            if isinstance(t, Exception):
                print >> sys.stderr, \
                    "Not synced: %r\nBecause: %s\n" % (s, t)
                errors = True
            else:
                print >> sys.stderr, \
                    "Synced: %r\nTarget: %r\n" % (s, t)
        if errors:
            exitval += 4

        playlist_failures = self.synchronizer.synchronize_playlists()
        if playlist_failures:
            print >> sys.stderr, "Problems while writing playlists:\n"
            for s, t in playlist_failures:
                print >> sys.stderr, \
                    "Could not write: %r\nBecause: %r\n" % (s, t)
            print >> sys.stderr
            exitval += 8

        return exitval