Example #1
0
    def execute(self):

        mode = self.kwargs.get("mode")
        # modes are either naming based or file object based
        #mode = "naming"
        #mode = "file"
        assert mode in ['naming', 'file']

        search_type = self.kwargs.get("search_type")
        search_keys = self.kwargs.get("search_keys")
        assert search_type or search_keys

        if search_type:
            print "@SOBJECT(%s)" % search_type
            sobjects = Search.eval("@SOBJECT(%s)" % search_type)
        else:
            sobjects = Search.get_by_search_keys(search_keys)

        if not sobjects:
            print "No sobjects to process"
            return
        snapshots = Search.eval("@SOBJECT(sthpw/snapshot)", sobjects)

        # set an arbitrary limit for now
        limit = 1000

        base_dir = Environment.get_asset_dir()

        num_found = 0
        errors = []
        for i, snapshot in enumerate(snapshots):
            if i > limit:
                break
            print "Processing: %s of %s" % (i, len(snapshots))

            file_types = snapshot.get_all_file_types()
            for file_type in file_types:
                files = snapshot.get_files_by_type(file_type)

                # FIXME: not sure why we have this limitation, although it is
                # a pretty rare occurance
                if len(files) > 1:
                    raise Exception(
                        "More than one file with type [%s] in snapshot [%s]" %
                        (file_type, snapshot.get_code()))

                file = files[0]
                file_name = file.get_value("file_name")

                # check-in dir is not subject to changes in asset_dir
                checkin_dir = file.get_value("checkin_dir")
                old_path = "%s/%s" % (checkin_dir, file_name)

                try:

                    if mode == "naming":
                        # preallocated path is used to align a file to the
                        # current naming convention

                        # FIXME:
                        # there is a behavior in naming to add "_web" and
                        # "_icon" on the file name.  In this case, we don't
                        # want that, so ask for main
                        if file_type in ['icon', 'web']:
                            path = snapshot.get_preallocated_path(
                                'main', file_name)
                        else:
                            path = snapshot.get_preallocated_path(
                                file_type, file_name)

                    elif mode == "file":
                        # relative_dir is used to align a file to the current
                        # place pointed to by the "file" object
                        relative_dir = file.get_value("relative_dir")
                        path = "%s/%s/%s" % (base_dir, relative_dir, file_name)
                    else:
                        raise Exception("Invalid mode [%s]" % mode)

                except Exception as e:
                    error = "Snapshot [%s] has an error getting preallocated path: [%s]" % (
                        snapshot.get_code(), e.message)
                    errors.append(error)
                    continue

                if old_path == path:
                    continue

                num_found += 1

                print("snapshot: ", snapshot.get_value("code"))
                print("old: ", old_path)
                print("new: ", path)

                print("-" * 20)

                new_dir = os.path.dirname(path)
                new_filename = os.path.basename(path)
                new_relative_dir = new_dir.replace(base_dir, '')
                new_relative_dir = new_relative_dir.strip("/")

                xml = snapshot.get_xml_value("snapshot")
                node = xml.get_node("snapshot/file[@type='%s']" % file_type)
                Xml.set_attribute(node, "name", new_filename)

                # update all of the file
                file.set_value("file_name", new_filename)
                file.set_value("checkin_dir", new_dir)
                file.set_value("relative_dir", new_relative_dir)
                snapshot.set_value("snapshot", xml.to_string())

                dirname = os.path.dirname(path)
                if not os.path.exists(dirname):
                    FileUndo.mkdir(dirname)

                exists = False
                if os.path.islink(old_path):
                    exists = os.path.lexists(old_path)
                else:
                    exists = os.path.exists(old_path)

                if not exists:
                    print '... old does not exist'
                    continue

                FileUndo.move(old_path, path)
                file.commit()
                snapshot.commit()

                # try to remove the old folder (if it's empty, it will be removed)
                dirname = os.path.dirname(old_path)
                while 1:
                    try:
                        os.rmdir(dirname)
                        dirname = os.path.dirname(dirname)
                    except:
                        break

        if errors:
            print "Errors:"
            for error in errors:
                print error
            print "-" * 20
        print "Found %s of %s snapshots which have paths different from naming" % (
            num_found, len(snapshots))
Example #2
0
    def execute(my):

        # DISABLING: this used to be needed for Repo Browser layout, but
        # is no longer needed
        return

        from pyasm.biz import Snapshot, Naming

        input = my.get_input()
        search_key = input.get("search_key")
        search_type = input.get('search_type')
        sobject = my.get_caller()
        assert search_type

        search_type_obj = SearchType.get(search_type)

        # FIXME: this should be in SearchType
        base_dir = Environment.get_asset_dir()

        root_dir = search_type_obj.get_value("root_dir", no_exception=True)
        if not root_dir:
            base_type = search_type_obj.get_base_key()
            parts = base_type.split("/")
            relative_dir = parts[1]


        # FIXME: need to use naming here
        file_type = 'main'
        snapshot_type = "file"
        process = "publish"

        virtual_snapshot = Snapshot.create_new()
        virtual_snapshot_xml = '<snapshot><file type=\'%s\'/></snapshot>' %(file_type)
        virtual_snapshot.set_value("snapshot", virtual_snapshot_xml)
        virtual_snapshot.set_value("snapshot_type", snapshot_type)

        # NOTE: keep these empty to produce a folder without process
        # or context ...
        # Another approach would be to find all the possible processes
        # and create folders for them

        # since it is a a file name based context coming in, use process
        #virtual_snapshot.set_value("process", process)
        #virtual_snapshot.set_value("context", process)

        # ???? Which is the correct one?
        virtual_snapshot.set_sobject(sobject)
        virtual_snapshot.set_parent(sobject)
        
        #naming = Naming.get(sobject, virtual_snapshot)
        #print "naming: ", naming.get_data()

        # Need to have a fake file because preallocated path also looks at
        # the file
        file_name = 'test.jpg'
        mkdirs = False
        ext = 'jpg'

        path = virtual_snapshot.get_preallocated_path(file_type, file_name, mkdirs, ext=ext, parent=sobject)
        dirname = os.path.dirname(path)

        if isinstance(path, unicode):
            path = path.encode('utf-8')
        else:
            path = unicode(path, errors='ignore').encode('utf-8')

        #dirname = "%s/%s/%s/" % (base_dir, project_code, root_dir)

        base_dir = Environment.get_asset_dir()
        relative_dir = dirname.replace(base_dir, "")
        relative_dir = relative_dir.strip("/")

        # create a file object
        file_obj = SearchType.create("sthpw/file")
        file_obj.set_sobject_value(sobject)
        file_obj.set_value("file_name", "")
        file_obj.set_value("relative_dir", relative_dir)
        file_obj.set_value("type", "main")
        file_obj.set_value("base_type", "sobject_directory")
        file_obj.commit(triggers=False)

        from pyasm.search import FileUndo
        if not os.path.exists(dirname):
            FileUndo.mkdir(dirname)
Example #3
0
    def execute(my):

        mode = my.kwargs.get("mode")
        # modes are either naming based or file object based
        #mode = "naming"
        #mode = "file"
        assert mode in ['naming', 'file']

        search_type = my.kwargs.get("search_type")
        search_keys = my.kwargs.get("search_keys")
        assert search_type or search_keys

        if search_type:
            print "@SOBJECT(%s)" % search_type
            sobjects = Search.eval("@SOBJECT(%s)" % search_type)
        else:
            sobjects = Search.get_by_search_keys(search_keys)

        if not sobjects:
            print "No sobjects to process"
            return
        snapshots = Search.eval("@SOBJECT(sthpw/snapshot)", sobjects)

        # set an arbitrary limit for now
        limit = 1000

        base_dir = Environment.get_asset_dir()

        num_found = 0
        errors = []
        for i, snapshot in enumerate(snapshots):
            if i > limit:
                break
            print "Processing: %s of %s" % (i, len(snapshots) )

            file_types = snapshot.get_all_file_types()
            for file_type in file_types:
                files = snapshot.get_files_by_type(file_type)

                # FIXME: not sure why we have this limitation, although it is
                # a pretty rare occurance
                if len(files) > 1:
                    raise Exception("More than one file with type [%s] in snapshot [%s]" % (file_type, snapshot.get_code()) )

                file = files[0]
                file_name = file.get_value("file_name")


                # check-in dir is not subject to changes in asset_dir
                checkin_dir = file.get_value("checkin_dir")
                old_path = "%s/%s" % ( checkin_dir, file_name )


                try:

                    if mode == "naming":
                        # preallocated path is used to align a file to the
                        # current naming convention

                        # FIXME:
                        # there is a behavior in naming to add "_web" and
                        # "_icon" on the file name.  In this case, we don't
                        # want that, so ask for main
                        if file_type in ['icon', 'web']:
                            path = snapshot.get_preallocated_path('main', file_name)
                        else:
                            path = snapshot.get_preallocated_path(file_type, file_name)

                    elif mode == "file":
                        # relative_dir is used to align a file to the current
                        # place pointed to by the "file" object
                        relative_dir = file.get_value("relative_dir")
                        path = "%s/%s/%s" % ( base_dir, relative_dir, file_name )
                    else:
                        raise Exception("Invalid mode [%s]" % mode)

                except Exception, e:
                    error = "Snapshot [%s] has an error getting preallocated path: [%s]" % (snapshot.get_code(), e.message )
                    errors.append(error)
                    continue



                if old_path == path:
                    continue

                num_found += 1

                print "snapshot: ", snapshot.get_value("code")
                print "old: ", old_path
                print "new: ", path


                print "-"*20


                new_dir = os.path.dirname(path)
                new_filename = os.path.basename(path)
                new_relative_dir = new_dir.replace(base_dir, '')
                new_relative_dir = new_relative_dir.strip("/")

                xml = snapshot.get_xml_value("snapshot")
                node = xml.get_node("snapshot/file[@type='%s']" % file_type)
                Xml.set_attribute(node, "name", new_filename)



                # update all of the file
                file.set_value("file_name", new_filename)
                file.set_value("checkin_dir", new_dir)
                file.set_value("relative_dir", new_relative_dir)
                snapshot.set_value("snapshot", xml.to_string() )

                dirname = os.path.dirname(path)
                if not os.path.exists(dirname):
                    FileUndo.mkdir(dirname)



                exists = False
                if os.path.islink(old_path):
                    exists = os.path.lexists(old_path)
                else:
                    exists = os.path.exists(old_path)

                if not exists:
                    print '... old does not exist'
                    continue


                FileUndo.move(old_path, path)
                file.commit()
                snapshot.commit()


                # try to remove the old folder (if it's empty, it will be removed)
                dirname = os.path.dirname(old_path)
                while 1:
                    try:
                        os.rmdir(dirname)
                        dirname = os.path.dirname(dirname)
                    except:
                        break