Example #1
0
def installmaps(options, buildout):
    # adding maps files
    dest = "%s/nad" % options["compile-directory"]
    url = options["mapsurl"].strip()
    md5 = options["mapsmd5"].strip()
    ldest = os.path.join(buildout["buildout"]["directory"], os.path.basename(url))
    dl = True
    if os.path.exists(ldest):
        dl = False
        if not test_md5(ldest, md5):
            print "md5 does not match redownload"
            dl = True
    if dl:
        print "Downloading mapfiles (nad) from %s" % url
        try:
            contents = urllib2.urlopen(url).read()
            s = open(ldest, "wb")
            s.write(contents)
            s.flush()
            s.close()
            if not test_md5(ldest, md5):
                raise Exception("md5 does not match")
        except Exception, e:
            print "!!! Problem while downloading/writing to disk !!!"
            print e
            os.remove(ldest)
            sys.exit(-1)
    def install(self):
        """installs an egg
        """
        self.cache = os.path.join(
            self.buildout['buildout']['directory'],
            'cache'
        )
        directories = []
        self.logger.info('Start checkouts')
        for url, url_infos in self.urls.items():
            dest = url_infos.get('directory')
            if not dest:
                dest = os.path.basename(dest)
            if not dest.startswith('/'):
                dest = os.path.join(
                    self.options['location'],
                    dest
                )
            cache_fname = os.path.basename(url)
            cache_downloaded = os.path.join(self.cache, cache_fname)
            downloaded = False
            fname = ''
            if os.path.exists(cache_downloaded):
                if test_md5(cache_downloaded, url_infos.get('revision', 1)):
                    downloaded = True
                    self.logger.info('%s is already downloaded' %
                                     cache_downloaded)
                    fname = cache_downloaded
            if not downloaded:
                fname = self._download(url=url,
                                       destination=dest,
                                       cache=False)

            if ('unpack' in self.options):
                try:
                    # try to unpack
                    f = IUnpackerFactory()
                    u = f(fname)
                    tmpdest = tempfile.mkdtemp()
                    ftmpdest = tmpdest
                    if u:
                        if os.path.exists(dest):
                            c = len(os.listdir(dest))
                            if c > 1:
                                shutil.rmtree(dest)
                                os.makedirs(dest)
                        u.unpack(fname, tmpdest)
                        if not os.path.exists(self.cache):
                            os.makedirs(self.cache)
                        os.rename(fname, cache_downloaded)
                        c = os.listdir(tmpdest)
                        if len(c) == 1:
                            ftmpdest = os.path.join(tmpdest, c[0])
                        copy_tree(ftmpdest, dest)
                        shutil.rmtree(tmpdest)
                except Exception, e:
                    message = 'Can\'t install file %s in its destination %s.'
            self.logger.info('Completed dowbload of %s in %s' % (url, dest))
            directories.append(fname)
 def testMd5(self):
     """testMd5."""
     open(self.tf,'w').write('a\n')
     self.assertTrue(
         common.test_md5(
             self.tf,
             '60b725f10c9c85c70d97880dfe8191b3'
         )
     )
     self.assertTrue(
         common.md5sum(self.tf),
         '60b725f10c9c85c70d97880dfe8191b3'
     )
     self.assertFalse(
         common.test_md5(self.tf,
                         'FALSE'
                        )
     )