Exemple #1
0
    def delete_sobject(my, sobject):

        search_type = sobject.get_base_search_type()

        # this is used by API method delete_sobject
        auto_discover = my.kwargs.get("auto_discover")
        
        values = my.kwargs.get("values")
        if values:
            related_types = values.get("related_types")
        elif auto_discover:
            related_types = SearchType.get_related_types(search_type, direction="children")
        else:
            related_types = None

        # always delete notes and task and snapshot
        #if not related_types:
        #    related_types = ['sthpw/note', 'sthpw/task', 'sthpw/snapshot']
        #related_types = my.schema.get_related_search_types(search_type)
        if related_types:
            for related_type in related_types:
                if not related_type or related_type == search_type:
                    continue

                # snapshots take care of sthpw/file in the proper manner, so
                # skip them here
                if related_type == 'sthpw/file':
                    continue

                related_sobjects = sobject.get_related_sobjects(related_type)
                for related_sobject in related_sobjects:
                    if related_type == 'sthpw/snapshot':
                        my.delete_snapshot(related_sobject)
                    else:
                        related_sobject.delete()


        # implicitly remove "directory" files associated with the sobject
        search = Search("sthpw/file")
        search.add_op("begin")
        search.add_filter("file_name", "")
        search.add_null_filter("file_name")
        search.add_op("or")
        search.add_parent_filter(sobject)
        file_objects = search.get_sobjects()
        for file_object in file_objects:
            base_dir = Environment.get_asset_dir()
            relative_dir = file_object.get("relative_dir")
            lib_dir = "%s/%s" % (base_dir, relative_dir)
            print "removing: ", lib_dir
            FileUndo.rmdir(lib_dir)
            file_object.delete()


        # finally delete the sobject
        print "deleting: ", sobject.get_search_key()
        if search_type == 'sthpw/snapshot':
            my.delete_snapshot(sobject)
        else:
            sobject.delete()
Exemple #2
0
    def execute(my):

        search = Search(Submission)
        search.set_show_retired(True)
        submissions = search.get_sobjects()

        for submission in submissions:
            snapshot = Snapshot.get_latest_by_sobject(submission, "publish")
            paths = snapshot.get_all_lib_paths()

            bins = submission.get_bins()
            if not bins:
                 print "Bin for submissin [%s] does not exist" % submission.get_id()
                 continue
            bin = bins[0]
            code = bin.get_code()
            type = bin.get_value("type")
            label = bin.get_value("label")

            for path in paths:
                if not os.path.exists(path):
                    print "WARNING: path '%s' does not exist" % path
                    continue

                dirname = os.path.dirname(path)
                basename = os.path.basename(path)
                
                new_dirname = "%s/%s/%s/%s" % (dirname,type,label,code)
                if not os.path.exists(new_dirname):
                    os.makedirs(new_dirname)

                new_path = "%s/%s" % (new_dirname, basename)

                print new_path
                FileUndo.move(path, new_path)
Exemple #3
0
    def execute(self):
        web = self.get_web()
        keys = web.get_form_keys()
        file_name = self.kwargs.get("file_name")

        # process and get the uploaded files
        dir = Environment.get_upload_dir()
        license_file = "%s/%s" % (dir, file_name)
        if not os.path.exists(license_file):
            raise TacticException("Error retrieving the license file in [%s]"%license_file)

        std_name = 'tactic-license.xml'

        head, file_name = os.path.split(license_file)
        # no restrictions for license file
        #if file_name != std_name:
        #    raise TacticException("License file name should be named tactic-license.xml. The file given is [%s]" %file_name)

        license_dir = Environment.get_license_dir()
        current_license = "%s/%s" %(license_dir, std_name)
        if os.path.exists(current_license):
            FileUndo.remove(current_license)
        FileUndo.move(license_file, current_license)

        self.add_description('Renewed license file')
        security = Environment.get_security()
        security.reread_license()
Exemple #4
0
    def execute(my):
        web = my.get_web()
        keys = web.get_form_keys()
        file_name = my.kwargs.get("file_name")

        # process and get the uploaded files
        dir = Environment.get_upload_dir()
        license_file = "%s/%s" % (dir, file_name)
        if not os.path.exists(license_file):
            raise TacticException("Error retrieving the license file in [%s]" %
                                  license_file)

        std_name = 'tactic-license.xml'

        head, file_name = os.path.split(license_file)
        # no restrictions for license file
        #if file_name != std_name:
        #    raise TacticException("License file name should be named tactic-license.xml. The file given is [%s]" %file_name)

        license_dir = Environment.get_license_dir()
        current_license = "%s/%s" % (license_dir, std_name)
        if os.path.exists(current_license):
            FileUndo.remove(current_license)
        FileUndo.move(license_file, current_license)

        my.add_description('Renewed license file')
        security = Environment.get_security()
        security.reread_license()
Exemple #5
0
    def delete_snapshot(self, snapshot):

        # get all of the file paths
        file_paths = snapshot.get_all_lib_paths()

        paths = []
        for file_path in file_paths:
            if file_path.find("##") != -1:
                sequence_paths = snapshot.get_expanded_lib_paths()
                for path in sequence_paths:
                    paths.append(path)
            else:
                paths.append(file_path)

        files = snapshot.get_related_sobjects("sthpw/file")
        for file in files:
            print("Deleting file: ", file.get_search_key())
            file.delete()

        # remove the files from the repo
        for file_path in paths:
            print("Removing path: ", file_path)
            FileUndo.remove(file_path)

        print("Deleting snapshot: ", snapshot.get_search_key())
        snapshot.delete()
Exemple #6
0
    def delete_sobject(my, sobject):

        search_type = sobject.get_base_search_type()

        # this is used by API method delete_sobject
        auto_discover = my.kwargs.get("auto_discover")
        
        values = my.kwargs.get("values")
        if values:
            related_types = values.get("related_types")
        elif auto_discover:
            related_types = SearchType.get_related_types(search_type, direction="children")
        else:
            related_types = None

        # always delete notes and task and snapshot
        #if not related_types:
        #    related_types = ['sthpw/note', 'sthpw/task', 'sthpw/snapshot']
        #related_types = my.schema.get_related_search_types(search_type)
        if related_types:
            for related_type in related_types:
                if not related_type or related_type == search_type:
                    continue

                # snapshots take care of sthpw/file in the proper manner, so
                # skip them here
                if related_type == 'sthpw/file':
                    continue

                related_sobjects = sobject.get_related_sobjects(related_type)
                for related_sobject in related_sobjects:
                    if related_type == 'sthpw/snapshot':
                        my.delete_snapshot(related_sobject)
                    else:
                        related_sobject.delete()


        # implicitly remove "directory" files associated with the sobject
        search = Search("sthpw/file")
        search.add_op("begin")
        search.add_filter("file_name", "")
        search.add_null_filter("file_name")
        search.add_op("or")
        search.add_parent_filter(sobject)
        file_objects = search.get_sobjects()
        for file_object in file_objects:
            base_dir = Environment.get_asset_dir()
            relative_dir = file_object.get("relative_dir")
            lib_dir = "%s/%s" % (base_dir, relative_dir)
            print "removing: ", lib_dir
            FileUndo.rmdir(lib_dir)
            file_object.delete()


        # finally delete the sobject
        print "deleting: ", sobject.get_search_key()
        if search_type == 'sthpw/snapshot':
            my.delete_snapshot(sobject)
        else:
            sobject.delete()
Exemple #7
0
    def delete_snapshot(self, snapshot):

        # get all of the file paths
        file_paths = snapshot.get_all_lib_paths()

        paths = []
        for file_path in file_paths:
            if file_path.find("##") != -1:
                sequence_paths = snapshot.get_expanded_lib_paths()
                for path in sequence_paths:
                    paths.append(path)
            else:
                paths.append(file_path)




        files = snapshot.get_related_sobjects("sthpw/file")
        for file in files:
            print("Deleting file: ", file.get_search_key())
            file.delete()

        # remove the files from the repo
        for file_path in paths:
            print("Removing path: ", file_path)
            FileUndo.remove(file_path)

        print("Deleting snapshot: ", snapshot.get_search_key())
        snapshot.delete()
Exemple #8
0
    def do_delete(self, sobject, related_types=None):

        search_type = sobject.get_base_search_type()

        # always delete notes and task and snapshot
        if not related_types:
            related_types = ['sthpw/note', 'sthpw/task', 'sthpw/snapshot']
        #related_types = self.schema.get_related_search_types(search_type)

        if related_types:
            for related_type in related_types:
                if not related_type or related_type == search_type:
                    continue

                # snapshots take care of sthpw/file in the proper manner, so
                # skip them here
                if related_type == 'sthpw/file':
                    continue

                related_sobjects = sobject.get_related_sobjects(related_type)
                for related_sobject in related_sobjects:
                    if related_type == 'sthpw/snapshot':
                        self.delete_snapshot(related_sobject)
                    else:
                        related_sobject.delete()
                        #self.do_delete(related_sobject)


        # implicitly remove "directory" files associated with the sobject
        search = Search("sthpw/file")
        search.add_op("begin")
        search.add_filter("file_name", "")
        search.add_null_filter("file_name")
        search.add_op("or")
        search.add_parent_filter(sobject)
        file_objects = search.get_sobjects()

        #if file_objects:
        #    print("Removing [%s] file objects" % len(file_objects))

        for file_object in file_objects:
            base_dir = Environment.get_asset_dir()
            relative_dir = file_object.get("relative_dir")
            lib_dir = "%s/%s" % (base_dir, relative_dir)
            FileUndo.rmdir(lib_dir)
            file_object.delete()


        # finally delete the sobject
        print("Deleting: ", sobject.get_search_key())
        if search_type == 'sthpw/snapshot':
            self.delete_snapshot(sobject)
        else:
            sobject.delete()
Exemple #9
0
    def do_delete(self, sobject, related_types=None):

        search_type = sobject.get_base_search_type()

        # always delete notes and task and snapshot
        if not related_types:
            related_types = ['sthpw/note', 'sthpw/task', 'sthpw/snapshot']
        #related_types = self.schema.get_related_search_types(search_type)

        if related_types:
            for related_type in related_types:
                if not related_type or related_type == search_type:
                    continue

                # snapshots take care of sthpw/file in the proper manner, so
                # skip them here
                if related_type == 'sthpw/file':
                    continue

                related_sobjects = sobject.get_related_sobjects(related_type)
                for related_sobject in related_sobjects:
                    if related_type == 'sthpw/snapshot':
                        self.delete_snapshot(related_sobject)
                    else:
                        related_sobject.delete()
                        #self.do_delete(related_sobject)

        # implicitly remove "directory" files associated with the sobject
        search = Search("sthpw/file")
        search.add_op("begin")
        search.add_filter("file_name", "")
        search.add_null_filter("file_name")
        search.add_op("or")
        search.add_parent_filter(sobject)
        file_objects = search.get_sobjects()

        #if file_objects:
        #    print("Removing [%s] file objects" % len(file_objects))

        for file_object in file_objects:
            base_dir = Environment.get_asset_dir()
            relative_dir = file_object.get("relative_dir")
            lib_dir = "%s/%s" % (base_dir, relative_dir)
            FileUndo.rmdir(lib_dir)
            file_object.delete()

        # finally delete the sobject
        print("Deleting: ", sobject.get_search_key())
        if search_type == 'sthpw/snapshot':
            self.delete_snapshot(sobject)
        else:
            sobject.delete()
Exemple #10
0
    def delete_snapshot(my, snapshot):

        # get all of the file paths
        file_paths = snapshot.get_all_lib_paths()

        files = snapshot.get_related_sobjects("sthpw/file")
        for file in files:
            print "deleting file: ", file.get_search_key()
            file.delete()

        # remove the files from the repo
        for file_path in file_paths:
            "removing path: ", file_path
            FileUndo.remove(file_path)

        print "deleting snapshot: ", snapshot.get_search_key()
        snapshot.delete()
Exemple #11
0
    def delete_snapshot(my, snapshot):

        # get all of the file paths
        file_paths = snapshot.get_all_lib_paths()

        files = snapshot.get_related_sobjects("sthpw/file")
        for file in files:
            print "deleting file: ", file.get_search_key()
            file.delete()

        # remove the files from the repo
        for file_path in file_paths:
            "removing path: ", file_path
            FileUndo.remove(file_path)

        print "deleting snapshot: ", snapshot.get_search_key()
        snapshot.delete()
Exemple #12
0
    def execute(my):
        template = my.kwargs.get("template")

        template_dir = Environment.get_template_dir()

        template = my.kwargs.get("template")


        from pyasm.search import FileUndo
        path = "%s/%s-1.0.0.zip" % (template_dir, template)
        if not os.path.exists(path):
            raise Exception("Path [%s] does not exists" % path)

        FileUndo.remove(path)

        path = "%s/%s" % (template_dir, template)
        FileUndo.remove(path)
Exemple #13
0
    def execute(self):
        template = self.kwargs.get("template")

        template_dir = Environment.get_template_dir()

        template = self.kwargs.get("template")


        from pyasm.search import FileUndo
        path = "%s/%s-1.0.0.zip" % (template_dir, template)
        if not os.path.exists(path):
            raise Exception("Path [%s] does not exists" % path)

        FileUndo.remove(path)

        path = "%s/%s" % (template_dir, template)
        FileUndo.remove(path)
Exemple #14
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))
Exemple #15
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
Exemple #16
0
    def handle_system_commands(my, snapshot, files, file_objects, mode, md5s, source_paths=[], file_sizes=[]):
        '''move the tmp files in the appropriate directory'''

        # if mode is local then nothing happens here
        if mode == 'local':
            return

        sobject = snapshot.get_sobject()

        # inplace mode does not move the file.  It just registers the file
        # object
        if mode == 'inplace':
            for i, file in enumerate(files):
                file_object = file_objects[i]
                to_name = file_object.get_full_file_name()
                to_path = file

                # This is handled in create_file_types
                #file_type = snapshot.get_type_by_file_name(to_name)
                #file_object.set_value('type', file_type)


                if not os.path.isdir(to_path):
                    md5_checksum = None
                    if md5s:
                        md5_checksum = md5s[i]
                    if not md5_checksum:
                        md5_checksum = File.get_md5(to_path)

                    if md5_checksum:
                        file_object.set_value("md5", md5_checksum)

                file_object.commit(triggers=False)
            return
            
   
        for i, file in enumerate(files):

            file_object = file_objects[i]
            to_name = file_object.get_full_file_name()
            file_type = snapshot.get_type_by_file_name(to_name)

            lib_dir = snapshot.get_lib_dir(file_type=file_type, file_object=file_object)
            # it should have been created in postprocess_snapshot
            System().makedirs(lib_dir)

            to_path = "%s/%s" % (lib_dir, to_name )

            #print "path: ", i, files[i]
            #print to_path, os.path.exists(to_path)

            # first make sure that the to path does not exist, if so, just skip
            if os.path.exists(to_path) and mode not in ['inplace','preallocate']:
                raise CheckinException('This path [%s] already exists'%to_path) 



            # add the file
            try:

                # inplace undo used to not touch the file, 
                # now it will be moved to cache on undo
                io_action = True
                if mode in ['preallocate']:
                    io_action = False

                
                if mode == 'move':
                    FileUndo.move( source_paths[i], to_path )
                #elif mode == 'copy': # was free_copy
                   
                    #FileUndo.create( source_paths[i], to_path, io_action=io_action )
                # make it look like the files was created in the repository
                else: # mode ='create'
                    
                    md5 = file_object.get_value("md5")
                    st_size = file_object.get_value("st_size")
                    rel_dir = file_object.get_value("relative_dir")
                    if mode == 'copy':
                        io_action = 'copy'
                        src_path = source_paths[i]
                    else:
                        src_path = files[i]

                    file_name = to_name
                    rel_path = "%s/%s" % (rel_dir, file_name)
                    
                    FileUndo.create( src_path, to_path, io_action=io_action, extra={ "md5": md5, "st_size": st_size, "rel_path": rel_path } )


            except IOError, e:
                raise CheckinException('IO Error occurred. %s' %e.__str__())

            # check to see that the file exists.
            if not os.path.exists( to_path ):
                if mode in ["inplace", "preallocate"]:
                    raise CheckinException("File not found in repo at [%s]" % to_path )
                else:
                    raise CheckinException("Failed move [%s] to [%s]" % \
                (files[i], to_path) )

            file_object.set_value('type', file_type)
            if not os.path.isdir(to_path):
                md5_checksum = None
                if md5s:
                    md5_checksum = md5s[i]
                if not md5_checksum:
                    md5_checksum = File.get_md5(to_path)

                if md5_checksum:
                    file_object.set_value("md5", md5_checksum)

            file_object.commit(triggers=False)
Exemple #17
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)
Exemple #18
0
    def handle_system_commands(my,
                               snapshot,
                               files,
                               file_objects,
                               mode,
                               md5s,
                               source_paths=[],
                               file_sizes=[]):
        '''move the tmp files in the appropriate directory'''

        # if mode is local then nothing happens here
        if mode == 'local':
            return

        sobject = snapshot.get_sobject()

        # inplace mode does not move the file.  It just registers the file
        # object
        if mode == 'inplace':
            for i, file in enumerate(files):
                file_object = file_objects[i]
                to_name = file_object.get_full_file_name()
                to_path = file

                # This is handled in create_file_types
                #file_type = snapshot.get_type_by_file_name(to_name)
                #file_object.set_value('type', file_type)

                if not os.path.isdir(to_path):
                    md5_checksum = None
                    if md5s:
                        md5_checksum = md5s[i]
                    if not md5_checksum:
                        md5_checksum = File.get_md5(to_path)

                    if md5_checksum:
                        file_object.set_value("md5", md5_checksum)

                file_object.commit(triggers=False)
            return

        for i, file in enumerate(files):

            file_object = file_objects[i]
            to_name = file_object.get_full_file_name()
            file_type = snapshot.get_type_by_file_name(to_name)

            lib_dir = snapshot.get_lib_dir(file_type=file_type,
                                           file_object=file_object)
            # it should have been created in postprocess_snapshot
            System().makedirs(lib_dir)

            to_path = "%s/%s" % (lib_dir, to_name)

            #print "path: ", i, files[i]
            #print to_path, os.path.exists(to_path)

            # first make sure that the to path does not exist, if so, just skip
            if os.path.exists(to_path) and mode not in [
                    'inplace', 'preallocate'
            ]:
                raise CheckinException('This path [%s] already exists' %
                                       to_path)

            # add the file
            try:

                # inplace undo used to not touch the file,
                # now it will be moved to cache on undo
                io_action = True
                if mode in ['preallocate']:
                    io_action = False

                if mode == 'move':
                    FileUndo.move(source_paths[i], to_path)
                #elif mode == 'copy': # was free_copy

                #FileUndo.create( source_paths[i], to_path, io_action=io_action )
                # make it look like the files was created in the repository
                else:  # mode ='create'

                    md5 = file_object.get_value("md5")
                    st_size = file_object.get_value("st_size")
                    rel_dir = file_object.get_value("relative_dir")
                    if mode == 'copy':
                        io_action = 'copy'
                        src_path = source_paths[i]
                    else:
                        src_path = files[i]

                    file_name = to_name
                    rel_path = "%s/%s" % (rel_dir, file_name)

                    FileUndo.create(src_path,
                                    to_path,
                                    io_action=io_action,
                                    extra={
                                        "md5": md5,
                                        "st_size": st_size,
                                        "rel_path": rel_path
                                    })

            except IOError, e:
                raise CheckinException('IO Error occurred. %s' % e.__str__())

            # check to see that the file exists.
            if not os.path.exists(to_path):
                if mode in ["inplace", "preallocate"]:
                    raise CheckinException("File not found in repo at [%s]" %
                                           to_path)
                else:
                    raise CheckinException("Failed move [%s] to [%s]" % \
                (files[i], to_path) )

            file_object.set_value('type', file_type)
            if not os.path.isdir(to_path):
                md5_checksum = None
                if md5s:
                    md5_checksum = md5s[i]
                if not md5_checksum:
                    md5_checksum = File.get_md5(to_path)

                if md5_checksum:
                    file_object.set_value("md5", md5_checksum)

            file_object.commit(triggers=False)