Пример #1
0
    def switchLanguage(self, language):

        localedir = "locale"
        localedirpath = os.path.join(self.path, localedir)

        current_language=self.getLanguage()

        if not current_language:
            return False

        if language==current_language:
            print("This language is already active.")
            return True

        if not os.path.exists(localedirpath+"."+language):
            print("Error: No such language (%s)." % language)
            return False
        else:
            # do directory switch
            os.rename(localedirpath, localedirpath+"."+current_language)
            os.remove(os.path.join(localedirpath+"."+current_language, "lang.conf"))
            os.rename(localedirpath+"."+language, localedirpath)
            f = open(os.path.join(localedirpath,'lang.conf'), 'w')
            f.write(language+"\n")
            f.close()

            # cleanup renderings
            if os.path.exists(os.path.join(self.path,'render',localedir)):
                shutil.rmtree(os.path.join(self.path,'render',localedir))
                mkdirs(os.path.join(self.path,'render',localedir))
                shutil.copy2(os.path.join(localedirpath,'lang.conf'),os.path.join(self.path,'render',localedir,'lang.conf'))
Пример #2
0
 def getProfilePath(self, custom_path=None):
     if custom_path:
         result=os.path.join(self.path, custom_path, self.getProfileDirName())
     else:
         result=os.path.join(self.path, 'render', 'project.conf', 'profiles', self.getProfileDirName())
     if not os.path.isdir(result):
         mkdirs(result)
     return result
Пример #3
0
 def getProfilePath(self, custom_path=None):
     if custom_path:
         result = os.path.join(self.path, custom_path,
                               self.getProfileDirName())
     else:
         result = os.path.join(self.path, 'render', 'project.conf',
                               'profiles', self.getProfileDirName())
     if not os.path.isdir(result):
         mkdirs(result)
     return result
Пример #4
0
    def switchLanguage(self, language, create=True):
        def ignore_audio(path, names):
            ignored = []
            for name in names:
                if os.path.splitext(name)[1][1:] in [
                        'wav', 'flac', 'ogg', 'mp3'
                ]:
                    ignored.append(name)
            return set(ignored)

        localedir = "locale"
        localedirpath = os.path.join(self.path, localedir)

        current_language = self.getLanguage()

        if not current_language:
            return False

        if language == current_language:
            print("This language is already active.")
            return True

        if not os.path.exists(localedirpath + "." + language):
            if not create:
                print("Error: No such language (%s)." % language,
                      file=sys.stderr)
                return False
            else:
                print("Creating new language: %s..." % language)
                copytree(localedirpath, localedirpath + "." + language, False,
                         False, ignore_audio)
                print("   Language %s copied to %s." %
                      (current_language, language))
            os.remove(os.path.join(localedirpath + "." + language,
                                   "lang.conf"))

        # do directory switch
        os.rename(localedirpath, localedirpath + "." + current_language)
        os.remove(
            os.path.join(localedirpath + "." + current_language, "lang.conf"))
        os.rename(localedirpath + "." + language, localedirpath)
        with open(os.path.join(localedirpath, 'lang.conf'), 'w') as f:
            f.write(language + "\n")

        # cleanup renderings
        if os.path.exists(os.path.join(self.path, 'render', localedir)):
            shutil.rmtree(os.path.join(self.path, 'render', localedir))
            mkdirs(os.path.join(self.path, 'render', localedir))
            shutil.copy2(
                os.path.join(localedirpath, 'lang.conf'),
                os.path.join(self.path, 'render', localedir, 'lang.conf'))

        print("Done.")
        return True
Пример #5
0
    def switchLanguage(self, language, create=True):

        def ignore_audio(path, names):
            ignored=[]
            for name in names:
                if os.path.splitext(name)[1][1:] in ['wav','flac','ogg','mp3']:
                    ignored.append(name)
            return set(ignored)

        localedir = "locale"
        localedirpath = os.path.join(self.path, localedir)

        current_language=self.getLanguage()

        if not current_language:
            return False

        if language==current_language:
            print("This language is already active.")
            return True

        if not os.path.exists(localedirpath+"."+language):
            if not create:
                print("Error: No such language (%s)." % language, file=sys.stderr)
                return False
            else:
                print("Creating new language: %s..." % language)
                copytree(localedirpath, localedirpath+"."+language, False, False, ignore_audio)
                print("   Language %s copied to %s." % (current_language,language))
            os.remove(os.path.join(localedirpath+"."+language, "lang.conf"))

        # do directory switch
        os.rename(localedirpath, localedirpath+"."+current_language)
        os.remove(os.path.join(localedirpath+"."+current_language, "lang.conf"))
        os.rename(localedirpath+"."+language, localedirpath)
        with open(os.path.join(localedirpath,'lang.conf'), 'w') as f:
            f.write(language+"\n")

        # cleanup renderings
        if os.path.exists(os.path.join(self.path,'render',localedir)):
            shutil.rmtree(os.path.join(self.path,'render',localedir))
            mkdirs(os.path.join(self.path,'render',localedir))
            shutil.copy2(os.path.join(localedirpath,'lang.conf'),os.path.join(self.path,'render',localedir,'lang.conf'))

        print("Done.")
        return True
Пример #6
0
    def __init__(self, path, readonly=False):

        self.connection = None
        self.closed = True
        self.readonly = readonly

        if not os.path.exists(os.path.dirname(path)):
            mkdirs(os.path.dirname(path))

        if self.readonly:
            random_num = "%08d" % (random.randint(0, 99999999))
            tmp_path = "renderchan-cache-" + random_num + ".sqlite"
            tmp_path = os.path.join(os.path.dirname(path), tmp_path)
            if os.path.exists(path):
                shutil.copy(path, tmp_path)
            self.path = tmp_path
        else:
            self.path = path

        try:

            self.connection = sqlite3.connect(path)
            self.connection.text_factory = str
            cur = self.connection.cursor()

            cur.execute(
                "CREATE TABLE IF NOT EXISTS Paths(Id INTEGER PRIMARY KEY, Path TEXT, Timestamp REAL, Start INTEGER, End INTEGER, Width INTEGER, Height INTEGER);"
            )
            cur.execute(
                "CREATE TABLE IF NOT EXISTS Dependencies(Id INTEGER, Dependency TEXT);"
            )
            self.connection.commit()

            self.closed = False

        except sqlite3.Error as e:

            print("Error %s:" % e.args[0], file=sys.stderr)
            sys.exit(1)
Пример #7
0
    def __init__(self, path, readonly=False):

        self.connection=None
        self.closed = True
        self.readonly=readonly

        if not os.path.exists(os.path.dirname(path)):
            mkdirs(os.path.dirname(path))

        if self.readonly:
            random_num = "%08d" % (random.randint(0,99999999))
            tmp_path="renderchan-cache-"+random_num+".sqlite"
            tmp_path=os.path.join(os.path.dirname(path),tmp_path)
            if os.path.exists(path):
                shutil.copy(path,tmp_path)
            self.path=tmp_path
        else:
            self.path=path



        try:



            self.connection=sqlite3.connect(path)
            self.connection.text_factory = str
            cur=self.connection.cursor()

            cur.execute("CREATE TABLE IF NOT EXISTS Paths(Id INTEGER PRIMARY KEY, Path TEXT, Timestamp REAL, Start INTEGER, End INTEGER, Width INTEGER, Height INTEGER);")
            cur.execute("CREATE TABLE IF NOT EXISTS Dependencies(Id INTEGER, Dependency TEXT);")
            self.connection.commit()

            self.closed = False

        except sqlite3.Error as e:

            print("Error %s:" % e.args[0], file=sys.stderr)
            sys.exit(1)
Пример #8
0
    def __init__(self, path, readonly=False):

        self.path = path

        self.confPath = os.path.join(path, "project.conf")
        if not os.path.exists(self.confPath):
            # Look for remake.conf
            self.confPath = os.path.join(path, "remake.conf")
        if not os.path.exists(self.confPath):
            raise Exception

        if os.path.basename(self.confPath) == "remake.conf":
            self.version = 0
        else:
            self.version = 1

        self.activeProfile = None

        # PROJECT CACHE

        # Check for cache version
        self.cache_version = 5
        cachepath = os.path.join(self.path, "render", "cache.sqlite")
        if not readonly:
            if os.path.exists(
                    os.path.join(self.path, "render", "cache.version")):
                existing_cache_version = 0
                with open(os.path.join(self.path, "render",
                                       "cache.version")) as f:
                    content = f.readlines()
                if len(content) > 0:
                    try:
                        existing_cache_version = int(content[0].strip())
                    except:
                        pass
                if existing_cache_version != self.cache_version and os.path.exists(
                        cachepath):
                    os.remove(cachepath)
            else:
                if os.path.exists(cachepath):
                    # There is unversioned cache, remove it
                    os.remove(cachepath)

        # Load cache
        self.cache = RenderChanCache(cachepath, readonly)

        # Save cache version
        with open(os.path.join(self.path, "render", "cache.version"),
                  'w') as f:
            f.write(str(self.cache_version) + "\n")

        # List of modules used in the project
        self.dependencies = []

        self.defaults = {
            'width': '480',
            'height': '270',
            'format': 'png',
            'audio_rate': '48000',
            'fps': '24',
            'stereo': '',
        }

        # Check the consistency of localization
        localedir = "locale"
        self.language = self.getLanguage()
        # render dir
        needCleanup = True
        if os.path.exists(
                os.path.join(self.path, 'render', localedir, 'lang.conf')):
            with open(
                    os.path.join(self.path, 'render', localedir, 'lang.conf'),
                    'r') as f:
                current_language = f.readline().strip()
            if self.language == current_language:
                needCleanup = False
        if needCleanup and os.path.exists(
                os.path.join(self.path, 'render', localedir)):
            print("The language data is inconsistent in %s. Cleaning..." %
                  os.path.join(self.path, 'render', localedir))
            shutil.rmtree(os.path.join(self.path, 'render', localedir))
            mkdirs(os.path.join(self.path, 'render', localedir))
            shutil.copy2(
                os.path.join(self.path, localedir, 'lang.conf'),
                os.path.join(self.path, 'render', localedir, 'lang.conf'))

        # Project configuration

        self.config = {}
        self.loadRenderConfig()

        # Load list of frozen files
        self.frozenPaths = []
        self.loadFrozenPaths()
Пример #9
0
    def switchProfile(self, profile):
        def _sync_path(src, dest):
            names = os.listdir(src)
            for name in names:
                path = os.path.join(src, name)
                if os.path.isdir(path):
                    _sync_path(path, os.path.join(dest, name))
                else:
                    if name.endswith(".sync"):
                        name = name[:-5]
                        sync(os.path.join(src, name), os.path.join(dest, name))

        if not os.path.isdir(self.getProfilePath()):
            mkdirs(self.getProfilePath())

        msg = ""
        while True:
            if msg != "":
                print(msg)
            msg = "The rendertree is locked by other process. Waiting..."
            # Check if we are on correct profile
            need_sync = True
            checkfile = os.path.join(self.path, "render", "project.conf",
                                     "profile.conf")
            lockfile = os.path.join(self.path, "render", "project.conf",
                                    "profile.lock")
            #prev_profile = ""
            if os.path.exists(checkfile):
                # Read previous profile
                with open(checkfile) as f:
                    fcontent = f.readlines()
                if len(fcontent) > 0:
                    prev_profile = fcontent[0].strip()
                    if prev_profile == profile:
                        need_sync = False

            if need_sync:
                if os.path.exists(lockfile):
                    if not (file_is_older_than(lockfile, 6.0)):
                        # the profile is locked, we can't switch it now
                        time.sleep(5)
                        continue

                # the lockfile is old enough

                with open(lockfile, 'w') as f:
                    f.write(profile + "\n")

                # let's wait to make sure the lock is ours
                time.sleep(1)

                # Sanity check
                if os.path.exists(lockfile):
                    with open(lockfile) as f:
                        fcontent = f.readlines()
                    if len(fcontent) > 0:
                        prev_profile = fcontent[0].strip()
                    else:
                        prev_profile = None
                    if not (prev_profile == profile):
                        # someone have modified the file in the meantime, let's wait and try again
                        time.sleep(5)
                        continue
                else:
                    # something is wrong, because file is vanished
                    time.sleep(5)
                    continue

                # Start update thread
                t = LockThread(lockfile)
                t.start()

                # Switch the profile
                renderpath = os.path.join(self.path, "render")
                _sync_path(self.getProfilePath(), renderpath)

                # Finally update profile.conf
                with open(checkfile, 'w') as f:
                    f.write(profile + "\n")

                return t
            else:
                if os.path.exists(lockfile):
                    with open(lockfile) as f:
                        fcontent = f.readlines()
                    if len(fcontent) > 0:
                        prev_profile = fcontent[0].strip()
                    else:
                        prev_profile = None
                    if not (prev_profile == profile
                            or file_is_older_than(lockfile, 6.0)):
                        # someone tries to switch profile right now, let's wait and try again
                        time.sleep(5)
                        continue

                with open(lockfile, 'w') as f:
                    f.write(profile + "\n")

                # Sanity check
                if os.path.exists(lockfile):
                    with open(lockfile) as f:
                        fcontent = f.readlines()
                    if len(fcontent) > 0:
                        prev_profile = fcontent[0].strip()
                    else:
                        prev_profile = None
                    if not (prev_profile == profile):
                        # someone have modified the file in the meantime, let's wait and try again
                        time.sleep(5)
                        continue
                else:
                    # something is wrong, because file is vanished
                    time.sleep(5)
                    continue

                # Start update thread
                t = LockThread(lockfile)
                t.start()

                return t
Пример #10
0
    def loadRenderConfig(self, profile=None):
        """
        :type profile: str
        """
        if self.version == 0 and profile != None:

            print(
                "Warning: Profiles are not supported with old project format. No profile loaded."
            )
            return False

        elif self.version == 0:

            # Old project format, used by Remake

            config = configparser.ConfigParser()
            config.read_file(ini_wrapper(self.confPath))

            for key in config.options('default'):
                self.config[key] = config.get('default', key)
        else:

            # Native RenderChan project format

            config = configparser.ConfigParser()
            config.readfp(open(self.confPath))

            # sanity check
            for section in config.sections():
                if "." in section:
                    print(
                        "Warning: Incorrect profile name found (%s) - dots are not allowed."
                        % (section))

            if profile == None:
                if config.has_option("main", "active_profile"):
                    profile = config.get("main", "active_profile")
                else:
                    if len(config.sections()) != 0:
                        profile = config.sections()[0]
                    else:
                        return False

            profile = profile.replace(".", "")

            for key in config.options(profile):
                self.config[key] = config.get(profile, key)

            # check for correct values
            if self.getConfig("stereo") != "left" and self.getConfig(
                    "stereo") != "right":
                self.config["stereo"] = ""

            self.activeProfile = profile

        # Store project configuration - we need that to track configuration changes
        profilepath = self.getProfilePath()
        if not os.path.isdir(profilepath):
            mkdirs(profilepath)

        filename = os.path.join(profilepath, "core.conf")
        oldconfig = {}
        if os.path.exists(filename):
            cp = configparser.ConfigParser()
            cp.read(filename)

            for key in cp.options('main'):
                oldconfig[key] = cp.get('main', key)

        newconfig = self.defaults.copy()
        for key in self.config.keys():
            if key in self.defaults.keys():
                newconfig[key] = self.config[key]

        if newconfig != oldconfig:
            config = configparser.ConfigParser()
            config.add_section('main')
            for key in newconfig.keys():
                config.set('main', key, newconfig[key])
            with open(filename, 'w') as configfile:
                config.write(configfile)

        # Store current profile
        #filename=os.path.join(self.path,"render","project.conf","profile.conf")
        #prev_profile = ""
        #if os.path.exists(filename):
        #    # Read previous profile
        #    f=open(filename)
        #    prev_profile = f.readlines()[0].strip()
        #    f.close()
        #if prev_profile!=self.getProfileDirName():
        #    f = open(filename,'w')
        #    f.write(self.getProfileDirName()+"\n")
        #    f.close()

        return True
Пример #11
0
    def __init__(self, path, readonly=False):

        self.path=path

        self.confPath = os.path.join(path,"project.conf")
        if not os.path.exists(self.confPath):
            # Look for remake.conf
            self.confPath = os.path.join(path,"remake.conf")
        if not os.path.exists(self.confPath):
            raise Exception

        if os.path.basename(self.confPath) == "remake.conf":
            self.version = 0
        else:
            self.version = 1

        self.activeProfile=None


        # PROJECT CACHE

        # Check for cache version
        self.cache_version = 1
        cachepath = os.path.join(self.path, "render", "cache.sqlite")
        if not readonly:
            if os.path.exists(os.path.join(self.path, "render", "cache.version")):
                existing_cache_version=0
                f=open(os.path.join(self.path, "render", "cache.version"))
                content=f.readlines()
                f.close()
                if len(content)>0:
                    try:
                        existing_cache_version=int(content[0].strip())
                    except:
                        pass
                if existing_cache_version!=self.cache_version:
                    os.remove(cachepath)
            else:
                if os.path.exists(cachepath):
                    # There is unversioned cache, remove it
                    os.remove(cachepath)

        # Load cache
        self.cache=RenderChanCache(cachepath, readonly)

        # Save cache version
        f = open(os.path.join(self.path, "render", "cache.version"),'w')
        f.write(str(self.cache_version)+"\n")
        f.close()


        # List of modules used in the project
        self.dependencies=[]

        self.defaults = {
            'width':'480',
            'height':'270',
            'format':'png',
            'audio_rate':'48000',
            'fps':'24',
            'stereo':'',
        }

        # Check the consistency of localization
        localedir = "locale"
        self.language=self.getLanguage()
        # render dir
        needCleanup=True
        if os.path.exists(os.path.join(self.path,'render',localedir,'lang.conf')):
            with open(os.path.join(self.path,'render',localedir,'lang.conf'), 'r') as f:
                current_language = f.readline().strip()
            if self.language == current_language:
                needCleanup=False
        if needCleanup and os.path.exists(os.path.join(self.path,'render',localedir)):
            print "The language data is inconsistent in %s. Cleaning..." % os.path.join(self.path,'render',localedir)
            shutil.rmtree(os.path.join(self.path,'render',localedir))
            mkdirs(os.path.join(self.path,'render',localedir))
            shutil.copy2(os.path.join(self.path,localedir,'lang.conf'),os.path.join(self.path,'render',localedir,'lang.conf'))

        # Project configuration

        self.config={}
        self.loadRenderConfig()

        # Load list of frozen files
        self.frozenPaths=[]
        self.loadFrozenPaths()
Пример #12
0
    def switchProfile(self, profile):

        def _sync_path(src, dest):
            names = os.listdir(src)
            for name in names:
                path = os.path.join(src, name)
                if os.path.isdir(path):
                    _sync_path(path, os.path.join(dest,name))
                else:
                    if name.endswith(".sync"):
                        name = name[:-5]
                        sync( os.path.join(src,name), os.path.join(dest,name) )


        if not os.path.isdir(self.getProfilePath()):
            mkdirs(self.getProfilePath())

        msg=""
        while True:
            if msg!="":
                print msg
            msg="The rendertree is locked by other process. Waiting..."
            # Check if we are on correct profile
            need_sync = True
            checkfile=os.path.join(self.path,"render","project.conf","profile.conf")
            lockfile=os.path.join(self.path,"render","project.conf","profile.lock")
            #prev_profile = ""
            if os.path.exists(checkfile):
                # Read previous profile
                f=open(checkfile)
                fcontent=f.readlines()
                f.close()
                if len(fcontent)>0:
                    prev_profile = fcontent[0].strip()
                    if prev_profile==profile:
                        need_sync = False

            if need_sync:
                if os.path.exists(lockfile):
                    if not(file_is_older_than(lockfile, 6.0)):
                         # the profile is locked, we can't switch it now
                         time.sleep(5)
                         continue

                # the lockfile is old enough

                f = open(lockfile,'w')
                f.write(profile+"\n")
                f.close()

                # let's wait to make sure the lock is ours
                time.sleep(1)

                # Sanity check
                if os.path.exists(lockfile):
                    f=open(lockfile)
                    fcontent=f.readlines()
                    if len(fcontent)>0:
                        prev_profile = fcontent[0].strip()
                    else:
                         prev_profile = None
                    f.close()
                    if not (prev_profile==profile):
                        # someone have modified the file in the meantime, let's wait and try again
                        time.sleep(5)
                        continue
                else:
                    # something is wrong, because file is vanished
                    time.sleep(5)
                    continue

                # Start update thread
                t = LockThread(lockfile)
                t.start()

                # Switch the profile
                renderpath=os.path.join(self.path,"render")
                _sync_path(self.getProfilePath(), renderpath)

                # Finally update profile.conf
                f = open(checkfile,'w')
                f.write(profile+"\n")
                f.close()

                return t
            else:
                if os.path.exists(lockfile):
                     f=open(lockfile)
                     fcontent=f.readlines()
                     if len(fcontent)>0:
                         prev_profile = fcontent[0].strip()
                     else:
                         prev_profile = None
                     f.close()
                     if not(prev_profile==profile or file_is_older_than(lockfile, 6.0)):
                         # someone tries to switch profile right now, let's wait and try again
                         time.sleep(5)
                         continue

                f = open(lockfile,'w')
                f.write(profile+"\n")
                f.close()

                # Sanity check
                if os.path.exists(lockfile):
                    f=open(lockfile)
                    fcontent=f.readlines()
                    if len(fcontent)>0:
                        prev_profile = fcontent[0].strip()
                    else:
                         prev_profile = None
                    f.close()
                    if not (prev_profile==profile):
                        # someone have modified the file in the meantime, let's wait and try again
                        time.sleep(5)
                        continue
                else:
                    # something is wrong, because file is vanished
                    time.sleep(5)
                    continue

                # Start update thread
                t = LockThread(lockfile)
                t.start()

                return t
Пример #13
0
    def loadRenderConfig(self, profile=None):

        """
        :type profile: str
        """
        if self.version==0 and profile!=None:

            print("Warning: Profiles are not supported with old project format. No profile loaded.")
            return False

        elif self.version==0:

            # Old project format, used by Remake

            config = ConfigParser.SafeConfigParser()
            config.readfp(PlainConfigFileWrapper(open(self.confPath)))

            for key in config.options('default'):
                self.config[key]=config.get('default', key)
        else:

            # Native RenderChan project format

            config = ConfigParser.SafeConfigParser()
            config.readfp(open(self.confPath))

            # sanity check
            for section in config.sections():
                if "." in section:
                    print "Warning: Incorrect profile name found (%s) - dots are not allowed." % (section)

            if profile==None:
                if config.has_option("main", "active_profile"):
                    profile=config.get("main", "active_profile")
                else:
                    if len(config.sections())!=0:
                        profile = config.sections()[0]
                    else:
                        return False

            profile=profile.replace(".","")

            for key in config.options(profile):
                self.config[key]=config.get(profile, key)

            # check for correct values
            if self.getConfig("stereo")!="left" and self.getConfig("stereo")!="right":
                self.config["stereo"]=""

            self.activeProfile=profile



        # Store project configuration - we need that to track configuration changes
        profilepath = self.getProfilePath()
        if not os.path.isdir(profilepath):
            mkdirs(profilepath)

        filename=os.path.join(profilepath,"core.conf")
        oldconfig={}
        if os.path.exists(filename):
            cp = ConfigParser.SafeConfigParser()
            cp.read(filename)

            for key in cp.options('main'):
                oldconfig[key]=cp.get('main', key)

        newconfig=self.defaults.copy()
        for key in self.config.keys():
            if key in self.defaults.keys():
                newconfig[key]=self.config[key]

        if newconfig!=oldconfig:
            config = ConfigParser.SafeConfigParser()
            config.add_section('main')
            for key in newconfig.keys():
                config.set('main', key, newconfig[key])
            with open(filename, 'wb') as configfile:
                config.write(configfile)

        # Store current profile
        #filename=os.path.join(self.path,"render","project.conf","profile.conf")
        #prev_profile = ""
        #if os.path.exists(filename):
        #    # Read previous profile
        #    f=open(filename)
        #    prev_profile = f.readlines()[0].strip()
        #    f.close()
        #if prev_profile!=self.getProfileDirName():
        #    f = open(filename,'w')
        #    f.write(self.getProfileDirName()+"\n")
        #    f.close()

        return True