コード例 #1
0
    def createC4z(self, c4z, srcDir, dirsList, filesList, encryptIt):
        try:
            with zipfile.ZipFile(c4z, 'w', compression=zipfile.ZIP_DEFLATED) as zip:
                # add in directories
                self.compressDirsLists(srcDir, dirsList, zip)

                # not squished, but have the encryption flag set.  Encrypt the root file.
                if encryptIt:
                    encrypt_c4z.encrypt("driver.lua", "driver.lua.encrypted")

                    for curFileInfo in filesList:
                        if curFileInfo['name'] == "driver.lua":
                            curFileInfo['name'] = "driver.lua.encrypted"
                            break

                #add in files
                self.compressFileList(".", srcDir, filesList, zip)

        except zipfile.BadZipfile as ex:
            if os.path.exists(c4z):
                os.remove(c4z)
            print("Error building %s  ...exception: BadZipFile" % (c4z))
            return False
        except OSError as ex:
            print("Error building %s  ...exception: %s" % (c4z, ex.strerror))
            return False

        return True
コード例 #2
0
def compress(c4z, dir, encryptedLua=None):
    tempDir = None
    try:
        with zipfile.ZipFile(c4z, 'w',
                             compression=zipfile.ZIP_DEFLATED) as zip:
            for root, dirs, files in os.walk(dir):
                # Ignore hidden files and directories
                files = [f for f in files if not f[0] == '.']
                dirs[:] = [d for d in dirs if not d[0] == '.']
                for f in files:
                    # Ingore build.py file
                    if f == "build.py":
                        continue

                    arcpath = os.path.join(os.path.relpath(root, dir), f)
                    if encryptedLua is not None and os.path.normpath(
                            arcpath) == os.path.normpath(encryptedLua):
                        tempDir = tempfile.mkdtemp()
                        tempFile = os.path.join(tempDir, f)
                        Log("Encrypting %s..." % (os.path.basename(tempFile)))
                        encrypt_c4z.encrypt(os.path.join(root, f), tempFile)
                        if squishLua_:
                            newFile = tempDir + os.path.sep + GetSquishyInputFile(
                                root)
                            oldFile = tempFile
                            os.rename(oldFile, newFile)
                            arcpath = os.path.join(os.path.relpath(root, dir),
                                                   GetSquishyInputFile(root))
                            zip.write(newFile, arcname=arcpath + '.encrypted')
                        if squishLua_ is False:
                            zip.write(tempFile, arcname=arcpath + '.encrypted')
                    elif squishLua_ and encryptedLua is None:
                        inputFile = GetSquishyInputFile(dir)
                        outputFile = GetSquishyOutputFile(dir)
                        if f not in GetSquishySource(
                                dir
                        ) and f != "squishy" and f != outputFile and f != inputFile:
                            zip.write(os.path.join(root, f), arcname=arcpath)
                        if f == inputFile:
                            f = GetSquishyOutputFile(root)
                            zip.write(os.path.join(root, f), arcname=arcpath)
                    elif squishLua_ and encryptedLua is not None:
                        inputFile = GetSquishyInputFile(dir)
                        outputFile = GetSquishyOutputFile(dir)
                        if f not in GetSquishySource(
                                dir
                        ) and f != "squishy" and f != outputFile and f != inputFile:
                            zip.write(os.path.join(root, f), arcname=arcpath)
                    else:
                        zip.write(os.path.join(root, f), arcname=arcpath)

    except zipfile.BadZipfile as ex:
        if os.path.exists(c4z):
            os.remove(c4z)
        print "Error building", c4z, "... exception:", ex.message
        return False
    except OSError, ex:
        print "Error building", c4z, "... exception:", ex.strerror
        return False
コード例 #3
0
    def createSquishedC4z(self, c4z, dir, squishedFileName, encryptIt):
        csq.createsq(self.manifest)
        self.Squish(dir)

        try:
            with zipfile.ZipFile(c4z, 'w',
                                 compression=zipfile.ZIP_DEFLATED) as zip:
                # add in driver.xml
                zip.write("driver.xml")

                # add in all the stuff in the www directory and its sub-directories
                self.compressDirsLists(".", [{
                    'c4zDir': "www",
                    'recurse': "true",
                    'name': "www"
                }], zip)

                if encryptIt:
                    # encrypt the squished file and then include it.
                    self.Log("Encrypting %s..." % (squishedFileName))
                    encrypt_c4z.encrypt(squishedFileName,
                                        "driver.lua.encrypted")

                    zip.write("driver.lua.encrypted")
                else:
                    # rename the squished file to Driver.lua and include it
                    if os.path.exists(os.path.join(dir, "driver.lua")):
                        os.remove(os.path.join(dir, "driver.lua"))

                    shutil.copyfile(squishedFileName,
                                    os.path.join(dir, "driver.lua"))
                    zip.write("driver.lua")

        except zipfile.BadZipfile as ex:
            if os.path.exists(c4z):
                os.remove(c4z)
            self.Log("Error building %s  ...exception: %s" % (c4z, ex.message))
            return False
        except OSError as ex:
            self.Log("Error building %s  ...exception: %s" %
                     (c4z, ex.strerror))
            return False

        return True
コード例 #4
0
def compressFileList(c4z, dir, root, files, zip, encryptedLua):
    tempDir = None
    try:
        squishedLua = None
        tempDir = tempfile.mkdtemp()
        tRoot = root = os.path.normpath(root)
        for file in files:
            if file["c4zDir"] and dir != root:
                tRoot, dName = os.path.split(root)
            filePath = os.path.join(file["c4zDir"],
                                    os.path.basename(file["name"]))
            arcPath = os.path.join(".", filePath)
            path, fName = os.path.split(file["name"])
            if encryptedLua is not None and os.path.normpath(
                    arcPath) == os.path.normpath(encryptedLua):
                path, fName = os.path.split(file["name"])
                tempFile = os.path.join(tempDir, fName)
                Log("Encrypting %s..." % (os.path.basename(tempFile)))
                encrypt_c4z.encrypt(os.path.join(root, fName), tempFile)
                zip.write(tempFile, arcname=arcPath + '.encrypted')

                if squishLua_ is False and fName != encryptedLua:
                    zip.write(os.path.join(root, fName), arcname=arcPath)
            elif encryptedLua is not None and squishLua_:
                newPath = os.path.join(root, file["name"])
                sourcepath = os.path.normpath(newPath)
                path, fName = os.path.split(file["name"])
                inputFile = GetSquishyInputFile(root)

                if fName not in GetSquishySource(
                        root) and fName != "squishy" and fName != inputFile:
                    zip.write(sourcepath, arcname=arcPath)
                if fName == inputFile:
                    tempFile = os.path.join(tempDir, fName)

                    # If a driver type of .c4i is detected, pass.  If c4z, go ahead and encrypt.
                    if c4i_:
                        pass
                    else:
                        Log("Encrypting %s..." % (os.path.basename(tempFile)))
                        encrypt_c4z.encrypt(
                            os.path.join(root, GetSquishyOutputFile(root)),
                            tempFile)
                        zip.write(tempFile, arcname=arcPath + '.encrypted')

            elif encryptedLua is not None and squishLua_ is False and fName != encryptedLua:
                newPath = os.path.join(root, file["name"])
                sourcepath = os.path.normpath(newPath)
                zip.write(sourcepath, arcname=arcPath)
            elif encryptedLua is None and squishLua_:
                path, fName = os.path.split(file["name"])
                inputFile = GetSquishyInputFile(root)
                luaPath = ""
                if fName not in GetSquishySource(
                        root) and fName != "squishy" and fName != inputFile:
                    zip.write(os.path.join(root, fName), arcname=arcPath)
                if fName == inputFile:
                    fName = GetSquishyOutputFile(root)
                    zip.write(os.path.join(root, fName), arcname=arcPath)

                    # If a c4i type driver is detected, read the contents of the squished lua file.
                    if c4i_:
                        with open(root + os.path.sep + fName) as lua:
                            squishedLua = lua.readlines()
                            lua.close()

                            # Clean up any temporary directories that start with "Squished_Lua_"
                            directories = next(os.walk(
                                tempfile.gettempdir()))[1]
                            for d in directories:
                                if str(d).startswith("Squished_Lua_"):
                                    shutil.rmtree(tempfile.gettempdir() +
                                                  os.path.sep + d)

                            # Create a new temporary directory to store the Squished Lua.
                            luaPath = tempfile.mkdtemp("", "Squished_Lua_")

                            # Write the contents of the squishedLua variable to file.
                            f = codecs.open(os.path.join(
                                luaPath, "driver.lua.squished"),
                                            'w',
                                            encoding='utf-8')
                            f.writelines(squishedLua)
                            f.close()

                if fName == 'driver.xml':
                    # if a c4i type driver is detected, read the contents of the driver.xml.
                    if c4i_:
                        with open(root + os.path.sep + fName) as driver:
                            driverXML = driver.readlines()
                            driver.close()

                            # Now write the file the same temporary directory that was created above (Squished_Lua_)
                            f = open(luaPath + os.path.sep + "driver.xml", "w")
                            f.writelines(driverXML)
                            f.close()

            else:
                newPath = os.path.join(root, file["name"])
                sourcepath = os.path.normpath(newPath)
                zip.write(sourcepath, arcname=arcPath)

        return squishedLua

    except ModuleNotFoundError as err:
        Log("Error: %s" % err)
        if err.name == 'M2Crypto':
            Log("M2Crypto is required to encrypt the driver (https://gitlab.com/m2crypto/m2crypto)."
                )
        raise

    finally:
        if tempDir:
            shutil.rmtree(tempDir)