Example #1
0
    def _download(self, hdl):
        print str(hdl)
        if not os.path.exists(hdl.target_dir):
            os.makedirs(hdl.target_dir)

        # Use hdl.dl.filename here, which is the filename before unpacking.
        subprocess.check_call(["curl", hdl.dl.url, '-o', hdl.dl.filename],
                              cwd=hdl.target_dir)
        assert md5_file(os.path.join(hdl.target_dir, hdl.dl.filename)) == hdl.dl.md5

        annexable_files = [hdl.dl.filename]
        if hdl.unpack:
            annexable_files = []
            tmpfilename = subprocess.check_output(['mktemp', '/tmp/aunpack.XXXXXXXXXX'])
            tmpfilename = tmpfilename.strip()
            subprocess.check_call(['aunpack', hdl.dl.filename,
                                   '--save-outdir={}'.format(tmpfilename)],
                                  cwd=hdl.target_dir)
            tmpdir = file(tmpfilename).read().strip()
            os.unlink(tmpfilename)
            # tmpdir == "" means everything was unpacked to the current directory
            if tmpdir:
                tmpdir = os.path.join(hdl.target_dir, tmpdir)
                # Try to move stuff out of the directory
                for filename in os.listdir(tmpdir):
                    unpacked_file = os.path.join(tmpdir, filename)
                    target_file = os.path.join(hdl.target_dir, filename)
                    if not os.path.exists(target_file):
                        os.rename(unpacked_file, target_file)
                        annexable_files.append(target_file)
                    elif md5_file(unpacked_file) == md5_file(target_file):
                        os.unlink(unpacked_file)
                    else:
                        print "Couldn't figure out what to do with unpacked file {}".format(unpacked_file)

                try:
                    os.rmdir(tmpdir)
                except OSError:
                    # Guess it wasn't empty. Oh well!
                    print "Not removing directory {}".format(tmpdir)

            os.unlink(os.path.join(hdl.target_dir, hdl.dl.filename))

        # Use target_filename here, which is the filename we wanted to
        # get out of the unpacked version.
        subprocess.check_call(['git', 'annex', 'add'] + annexable_files,
                              cwd=hdl.target_dir)
Example #2
0
    def does_match(self, hdl, filename):
        """Try to match two files up.

        If it's clearly a newer version based on version numbers,
        return OldVersion.  Otherwise, compare the md5s. If they're
        the same, return SameFile.  Otherwise, return False. Maybe the
        caller knows something we don't."""
        hdl_version = self.get_version_number(hdl.target_filename)[0]
        # Try to find the same type of version number as in the remote version
        for local_version in self.get_version_number(filename):
            if type(hdl_version) == type(local_version) and \
                    hdl_version > local_version:
                return OldVersion

        # See if the MD5s are the same.
        local_path = os.path.join(hdl.target_dir, filename)
        if not os.path.exists(local_path):
            return LinkMissing

        if hdl.dl.md5 == utils.md5_file(local_path):
            return SameFile

        return False