def to_spindisk(self):
        """Copy the given directory back to the spinning disk drive from the SSD
        drive.

        """
        #assert os.path.exists(self.original_dir)
        assert self.already_copied()
        assert islink(os.path.join(self.original_dir, self.basename))
        raise NotImplementedError()
        os.remove(os.path.join(self.original_dir, self.basename))
        shutil.move(
            os.path.join(self.dir_copied_to, self.basename),
            os.path.join(os.path.join(self.original_dir, self.basename))
        )
        self._dir_copied_to = None
Ejemplo n.º 2
0
def is_sym_link(src, dst):
    """Check that dst is a symlink to src.
    On Windows, always returns False"""
    ret = False
    if os.islink(dst):
        dst_targ = os.readlink(dst)
        ## now we need to compare that dst_targ and src are the same
        ## file. If we used normpath or samefile, and src was itself
        ## a symlink, we would be comparing for the final src,
        ## but this is not what we need here: we need to see if
        ## dst_targ points to src itself, not to its destination.
        ## Therefore, we use the line below.
        are_same = os.samestat(os.lstat(dst_targ), os.lstat(src))
        if are_same:
            ret = True
    return ret
    def already_copied(self):
        """Checks if this game has already been copied to another drive, and
        symlinked to there.
        
        Presumably, this would mean that the game has been copied from a
        spinning disk to a SSD.

        :returns:
            Whether or not the given game_dir has already been copied and
            symlinked to on the ssd_dir.
        :rtype: bool

        """
        if (
            self.dir_copied_to is not None and
            os.path.exists(self.dir_copied_to) and
            islink(game_dir)
        ):
            return True
            #os.path.join(os.path.dirname(path), result)
        return False
Ejemplo n.º 4
0
    def writeBezelConfig(bezelSet, system, rom, messSys):
        romBase = os.path.splitext(
            os.path.basename(rom))[0]  # filename without extension

        if messSys == "":
            tmpZipDir = "/var/run/mame_artwork/" + romBase  # ok, no need to zip, a folder is taken too
        else:
            tmpZipDir = "/var/run/mame_artwork/" + messSys  # ok, no need to zip, a folder is taken too
        # clean, in case no bezel is set, and in case we want to recreate it
        if os.path.exists(tmpZipDir):
            shutil.rmtree(tmpZipDir)

        if bezelSet is None:
            return

        # let's generate the zip file
        os.makedirs(tmpZipDir)

        # bezels infos
        bz_infos = bezelsUtil.getBezelInfos(rom, bezelSet, system.name)
        if bz_infos is None:
            return

        # copy the png inside
        if os.path.exists(bz_infos["mamezip"]):
            if messSys == "":
                artFile = "/var/run/mame_artwork/" + romBase + ".zip"
            else:
                artFile = "/var/run/mame_artwork/" + messSys + ".zip"
            if os.path.exists(artFile):
                if os.islink(artFile):
                    os.unlink(artFile)
                else:
                    os.remove(artFile)
            os.symlink(bz_infos["mamezip"], artFile)
            return
        elif os.path.exists(bz_infos["layout"]):
            os.symlink(bz_infos["layout"], tmpZipDir + "/default.lay")
            pngFile = os.path.split(bz_infos["png"])[1]
            os.symlink(bz_infos["png"], tmpZipDir + "/" + pngFile)
        else:
            pngFile = "default.png"
            os.symlink(bz_infos["png"], tmpZipDir + "/default.png")
            if os.path.exists(bz_infos["info"]):
                bzInfoFile = open(bz_infos["info"], "r")
                bzInfoText = bzInfoFile.readlines()
                bz_alpha = 1.0  # Just in case it's not set in the info file
                for infoLine in bzInfoText:
                    if len(infoLine) > 7:
                        infoLineClean = (infoLine.replace(
                            '"', '')).rstrip(",\n").lstrip()
                        infoLineData = infoLineClean.split(":")
                        if infoLineData[0].lower() == "width":
                            img_width = int(infoLineData[1])
                        elif infoLineData[0].lower() == "height":
                            img_height = int(infoLineData[1])
                        elif infoLineData[0].lower() == "top":
                            bz_y = int(infoLineData[1])
                        elif infoLineData[0].lower() == "left":
                            bz_x = int(infoLineData[1])
                        elif infoLineData[0].lower() == "bottom":
                            bz_bottom = int(infoLineData[1])
                        elif infoLineData[0].lower() == "right":
                            bz_right = int(infoLineData[1])
                        elif infoLineData[0].lower() == "opacity":
                            bz_alpha = float(infoLineData[1])
                bzInfoFile.close()
                bz_width = img_width - bz_x - bz_right
                bz_height = img_height - bz_y - bz_bottom
            else:
                img_width, img_height = bezelsUtil.fast_image_size(
                    bz_infos["png"])
                _, _, rotate = MameGenerator.getMameMachineSize(
                    romBase, tmpZipDir)

                # assumes that all bezels are setup for 4:3H or 3:4V aspects
                if rotate == 270 or rotate == 90:
                    bz_width = int(img_height * (3 / 4))
                else:
                    bz_width = int(img_height * (4 / 3))
                bz_height = img_height
                bz_x = int((img_width - bz_width) / 2)
                bz_y = 0
                bz_alpha = 1.0

            f = open(tmpZipDir + "/default.lay", 'w')
            f.write("<mamelayout version=\"2\">\n")
            f.write(
                "<element name=\"bezel\"><image file=\"default.png\" /></element>\n"
            )
            f.write("<view name=\"bezel\">\n")
            f.write("<screen index=\"0\"><bounds x=\"" + str(bz_x) +
                    "\" y=\"" + str(bz_y) + "\" width=\"" + str(bz_width) +
                    "\" height=\"" + str(bz_height) + "\" /></screen>\n")
            f.write("<element ref=\"bezel\"><bounds x=\"0\" y=\"0\" width=\"" +
                    str(img_width) + "\" height=\"" + str(img_height) +
                    "\" alpha=\"" + str(bz_alpha) + "\" /></element>\n")
            f.write("</view>\n")
            f.write("</mamelayout>\n")
            f.close()

        if system.isOptSet(
                'bezel.tattoo') and system.config['bezel.tattoo'] != "0":
            if system.config['bezel.tattoo'] == 'system':
                try:
                    tattoo_file = '/usr/share/batocera/controller-overlays/' + system.name + '.png'
                    if not os.path.exists(tattoo_file):
                        tattoo_file = '/usr/share/batocera/controller-overlays/generic.png'
                    tattoo = Image.open(tattoo_file)
                except Exception as e:
                    eslog.error("Error opening controller overlay: {}".format(
                        tattoo_file))
            elif system.config['bezel.tattoo'] == 'custom' and os.path.exists(
                    system.config['bezel.tattoo_file']):
                try:
                    tattoo_file = system.config['bezel.tattoo_file']
                    tattoo = Image.open(tattoo_file)
                except:
                    eslog.error(
                        "Error opening custom file: {}".format('tattoo_file'))
            else:
                try:
                    tattoo_file = '/usr/share/batocera/controller-overlays/generic.png'
                    tattoo = Image.open(tattoo_file)
                except:
                    eslog.error(
                        "Error opening custom file: {}".format('tattoo_file'))
            output_png_file = "/tmp/bezel_tattooed.png"
            back = Image.open(tmpZipDir + "/" + pngFile)
            tattoo = tattoo.convert("RGBA")
            back = back.convert("RGBA")
            tw, th = bezelsUtil.fast_image_size(tattoo_file)
            tatwidth = int(
                240 / 1920 * img_width
            )  # 240 = half of the difference between 4:3 and 16:9 on 1920px (0.5*1920/16*4)
            pcent = float(tatwidth / tw)
            tatheight = int(float(th) * pcent)
            tattoo = tattoo.resize((tatwidth, tatheight), Image.ANTIALIAS)
            alpha = back.split()[-1]
            alphatat = tattoo.split()[-1]
            if system.isOptSet('bezel.tattoo_corner'):
                corner = system.config['bezel.tattoo_corner']
            else:
                corner = 'NW'
            if (corner.upper() == 'NE'):
                back.paste(tattoo, (img_width - tatwidth, 20),
                           alphatat)  # 20 pixels vertical margins (on 1080p)
            elif (corner.upper() == 'SE'):
                back.paste(tattoo,
                           (img_width - tatwidth, img_height - tatheight - 20),
                           alphatat)
            elif (corner.upper() == 'SW'):
                back.paste(tattoo, (0, img_height - tatheight - 20), alphatat)
            else:  # default = NW
                back.paste(tattoo, (0, 20), alphatat)
            imgnew = Image.new("RGBA", (img_width, img_height), (0, 0, 0, 255))
            imgnew.paste(back, (0, 0, img_width, img_height))
            imgnew.save(output_png_file, mode="RGBA", format="PNG")

            try:
                os.remove(tmpZipDir + "/" + pngFile)
            except:
                pass
            os.symlink(output_png_file, tmpZipDir + "/" + pngFile)
Ejemplo n.º 5
0
            os.symlink(restart_coeff_files[i], dest_file)
            # idx is required to read from the right dataset in solution_vectorXXX.h5
            coeff_files_data.append({'file': dest_file, 'idx': idx})

    # create `restart.yaml` (metadata)
    yaml_restart_data = {}
    for elem in coeff_files_data:
        yaml_restart_data[elem['file']] = {
            'dset': str(elem['idx']) + '/Values'
        }

    # create symlink to vertex2dofidx.dat
    v2d_path = os.path.join(args.rpath, 'vertex2dofidx.dat')
    v2d_link = 'v2d-restart.dat'
    if os.path.isfile(v2d_link):
        if not os.islink(v2d_link):
            raise Exception('File ' + v2d_link +
                            ' is not a symlink and won\'t be overwritten.')

    os.symlink(v2d_path, v2d_link)

    f = open('restart.yaml', 'w')
    f.write(
        yaml.dump({
            'files': yaml_restart_data,
            'first index': int(restart_idxs[0] + last_index),
            'v2d': v2d_link
        }))

    f.close()
Ejemplo n.º 6
0
 def _export_dir_to_system(self, fullpath):
     wspath = self._wspath(fullpath)
     # remove dangling files first
     for root, dirs, files in os.walk(fullpath, topdown=False):
         for name in files:
             fname = join(root, name)
             wsname = self._wspath(fname)
             if not os.path.exists(wsname) and not os.path.islink(wsname):
                 print 'removing', fname
                 os.remove(fname)
         for name in dirs:
             fname = join(root, name)
             wsname = self._wspath(fname)
             if not os.path.exists(wsname):
                 print 'removing', fname
                 if os.islink(fname):
                     os.remove(fname)
                 else:
                     os.rmdir(fname)
     # export from workspace
     for root, dirs, files in os.walk(wspath):
         if self._not_svndir(root):
             realroot = root.split(self.workspace)[1]
             wsroot = self._wspath(realroot)
             #print 'realroot is', realroot
             if not os.path.exists(realroot):
                 os.mkdir(realroot)
             self._update_empty_dir_from_workspace(realroot, wsroot)
             for name in dirs:
                 if name != '.svn':
                     fname = join(realroot, name)
                     wspath = self._wspath(fname)
                     if os.path.islink(wspath):
                         if not os.path.exists(fname):
                             os.symlink(os.readlink(wspath), fname)
                         else:
                             self._update_link_from_workspace(fname, wspath)
                     else:
                         if not os.path.exists(fname):
                             os.mkdir(fname)
                         self._update_empty_dir_from_workspace(fname, wspath)
             for name in files:
                 fname = join(realroot, name)
                 wspath = self._wspath(fname)
                 if os.path.islink(wspath):
                     if not os.path.exists(fname) and not os.path.islink(fname):
                         os.symlink(os.readlink(wspath), fname)
                     else:
                         self._update_link_from_workspace(fname, wspath)
                 else:
                     if not os.path.exists(fname):
                         copyfile(wspath, fname)
                     self._update_file_from_workspace(fname, wspath)
     # fix mtimes on dirs
     for root, dirs, files in os.walk(self._wspath(fullpath), topdown=False):
         if self._not_svndir(root):
             realroot = root.split(self.workspace)[1]
             wsroot = self._wspath(realroot)
             for name in dirs:
                 fname = join(realroot, name)
                 wspath = self._wspath(fname)
                 if self._not_svndir(wspath):
                     if not os.path.islink(fname):
                         self._update_empty_dir_from_workspace(fname, wspath)
             self._update_empty_dir_from_workspace(realroot, wsroot)