Example #1
0
def recreate_versions():
    arcpy.AcceptConnections(WORKSPACE_SA, True)
    conn_path = r"C:\Users\Administrator\AppData\Roaming\ESRI\Desktop10.3\ArcCatalog\\"
    try:
        for user in DEFAULT_USERS:
            workspace_user = conn_path + user.split(".")[0] + ".sde"
            if user not in [
                    version.name
                    for version in arcpy.da.ListVersions(WORKSPACE_SA)
            ]:
                if user == 'PORTGIS.Edit':
                    arcpy.CreateVersion_management(workspace_user,
                                                   "sde.DEFAULT",
                                                   user.split(".")[1],
                                                   "PUBLIC")
                    log.write(datetime.datetime.now().strftime(
                        "%H:%M:%S %m/%d/%Y ") + "Recreated version: " + user +
                              "\n")
                else:
                    arcpy.CreateVersion_management(workspace_user,
                                                   "sde.DEFAULT",
                                                   user.split(".")[1],
                                                   "PROTECTED")
                    log.write(datetime.datetime.now().strftime(
                        "%H:%M:%S %m/%d/%Y ") + "Recreated version: " + user +
                              "\n")

        log.write(datetime.datetime.now().strftime("%H:%M:%S %m/%d/%Y ") +
                  "Finished recreating DEFAULT versions.\n")

        for user in PORTEDIT_USERS:
            workspace_user = conn_path + user.split(".")[0] + ".sde"
            if user not in [
                    version.name
                    for version in arcpy.da.ListVersions(WORKSPACE_SA)
            ]:
                arcpy.CreateVersion_management(workspace_user, "PORTGIS.Edit",
                                               user.split(".")[1], "PROTECTED")
                log.write(
                    datetime.datetime.now().strftime("%H:%M:%S %m/%d/%Y ") +
                    "Recreated version: " + user + "\n")

        log.write(datetime.datetime.now().strftime("%H:%M:%S %m/%d/%Y ") +
                  "Finished recreating PORTEDIT versions.\n")
    except Exception:
        msgs = "\n\nScript finished with errors."
        log.write(pymsg)
        log.write(msgs)

        sendMail(
            EMAIL_FROM, EMAIL_TO_ADMIN, "Error recreating versions",
            "There was an error during full compression and versions \
            were not successfully recreated.")
Example #2
0
def createver(key, workspace):
    #loop keys for versions
    #get version name and attemp to create new version
    for k in key:
        for key in k:
            if 'version-name' in key:

                #Execute CreateVersion
                #attemp to create new version
                try:
                    arcpy.CreateVersion_management(workspace,
                                                   k['parent-version'],
                                                   k['version-name'],
                                                   k['access-permission'])
                    print ' Create version: %s' % k['version-name']
                except:
                    log.error("Create version " + k['version-name'] +
                              " Failed.")

                #Alter Version
                #attemp to alter version
                try:
                    arcpy.AlterVersion_management(
                        workspace, k['version-name'], k['version-name'],
                        'version for: ' + k['version-name'],
                        k['access-permission'])
                except:
                    logger.error("Alter version " + k['version-name'] +
                                 " Failed.")
Example #3
0
    def create(self):

        if not self.exists():
        
            arcpy.CreateVersion_management(self.gdb.sdeconn
                                          ,self.parent
                                          ,self.name
                                          ,'PUBLIC')
def recreate_versions():
    conn_path = "C:\\MaintenanceScripts\\Connections\\Versions\\"
    print PORTEDIT_USERS

    #    arcpy.env.workspace = "C:\\MaintenanceScripts\\Connections\\[email protected]"
    #    workspace_edit = arcpy.env.workspace
    #    arcpy.CreateVersion_management(workspace_edit, "sde.DEFAULT", "PORTGIS.Edit", "PUBLIC")

    for user in PORTEDIT_USERS:
        print user
        arcpy.env.workspace = conn_path + user + ".sde"
        workspace_user = arcpy.env.workspace
        print workspace_user
        if user not in [
                version.name
                for version in arcpy.da.ListVersions(WORKSPACE_SDE)
        ]:
            arcpy.CreateVersion_management(workspace_user, "PORTGIS.Edit",
                                           user, "PROTECTED")
            log.write(datetime.datetime.now().strftime("%H:%M:%S %m/%d/%Y ") +
                      "Recreated version: " + user + "\n")

    log.write(datetime.datetime.now().strftime("%H:%M:%S %m/%d/%Y ") +
              "Finished recreating PORTEDIT versions.\n")

    for user in DEFAULT_USERS:
        arcpy.env.workspace = conn_path + user + ".sde"
        workspace_user = arcpy.env.workspace
        print DEFAULT_USERS
        if user not in [
                version.name
                for version in arcpy.da.ListVersions(WORKSPACE_SDE)
        ]:
            arcpy.CreateVersion_management(workspace_user, "sde.DEFAULT", user,
                                           "PROTECTED")

        log.write(datetime.datetime.now().strftime("%H:%M:%S %m/%d/%Y ") +
                  "Recreated version: " + user + "\n")

    log.write(datetime.datetime.now().strftime("%H:%M:%S %m/%d/%Y ") +
              "Finished recreating DEFAULT versions.\n")
    def recreateVersions(self):
        self.current_item += 1

        version_name = "QC"
        parent_version = "dbo.QC"
        access_permission = "PROTECTED"

        self.itemPrint()  # ā†“
        try:
            arcpy.CreateVersion_management(self.sde_connection, "dbo.DEFAULT",
                                           version_name, "PUBLIC")

            for version in self.versionList:
                if (version != "dbo.DEFAULT" and version != "DBO.QC"):
                    if ("DBO." in version or "dbo." in version):
                        version_name = version[4:]
                        arcpy.CreateVersion_management(self.sde_connection,
                                                       parent_version,
                                                       version_name,
                                                       access_permission)
                    else:
                        versionName = version
                        arcpy.CreateVersion_management(self.sde_connection,
                                                       parent_version,
                                                       version_name,
                                                       access_permission)
                else:
                    continue
            self.itemPrint()  # ā†‘

            maintenance_log.appendToFile([
                self.maintenance_item[self.current_item],
                self.maintenance_status[1]
            ])

        except Exception as e:
            self.handleException(e)
def main(argv):
    try:
      opts, args = getopt.getopt(argv,"d:i:l:",["gdbconn=","inputlist=","logfile="])
    except getopt.GetoptError:
      print 'test.py -d <gdbconn> -i <inputlist> -l <logfile>'
      sys.exit(2)

    for o, a in opts:
        if o in ("-d", "--gdbconn"):
            GDBConn = a
        elif o in ("-i", "--inputlist"):
            InputList = a
        elif o in ("-l", "--logfile"):
            LOG_FILENAME = a
        else:
            assert False, "unhandled option"
            
    ## Set up logging
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S',
                        filename=  LOG_FILENAME,
                        filemode='a')
  
    # Get input file
    try:
        todo = csv.DictReader(open(InputList, 'r'),  delimiter=',')
    except:
        logging.info('Failed to read input list!')
        sys.exit(2)

    # Read through input file
    for row in todo:
        print row
        NewVersion = row.get('VersionName')
        ParentVersion = row.get('ParentVersion')
        # Create version
        try:
            arcpy.CreateVersion_management(in_workspace=GDBConn, 
                                           parent_version=ParentVersion, 
                                           version_name=NewVersion, 
                                           access_permission="PUBLIC")
        except:
            print arcpy.GetMessages(2)
            logging.info("ERROR: Creating " + NewVersion + " under " + ParentVersion + ": " + arcpy.GetMessages(2))

    logging.info("GDB_CreateVersions complete!")
    logging.info("****************************")
Example #7
0
def CreateSDE_Version():
	'''This is to make sure that the version exists before the script runs'''
	print("Checking SDE connections.\n")
	sdeTempPath = tempfile.mkdtemp()
	arcpy.CreateDatabaseConnection_management(sdeTempPath, "Editor.sde", "SQL_SERVER", SERVERNAME, "OPERATING_SYSTEM_AUTH")
	sdeVersionNameFULL = ""
	sdeVersionLIST = []
	for version in arcpy.da.ListVersions(sdeTempPath + os.sep + "Editor.sde"):
		sdeVersionNameFULL = version.name.split(".")[0]        
		sdeVersionLIST.append(sdeVersionNameFULL)
		if sdeVersionNameFULL == '"MCKINNEY\\JCARMONA"':
			if version.description == "":
				arcpy.AlterVersion_management(sdeTempPath + os.sep + "Editor.sde", '"MCKINNEY\JCARMONA".CARMONA', description = "[email protected] | ext 7422")
		#print(sdeVersionLIST)
	if '"MCKINNEY\\JCARMONA"' not in sdeVersionLIST:
		print("\tCARMONA version not found, creating now.")
		arcpy.CreateVersion_management(sdeTempPath + os.sep + "Editor.sde", "sde.DEFAULT" ,"CARMONA", "PUBLIC")
		arcpy.AlterVersion_management(sdeTempPath + os.sep + "Editor.sde", '"MCKINNEY\JCARMONA".CARMONA', description = "[email protected] | ext 7422")
		print("Version created.")
	shutil.rmtree(sdeTempPath)
Example #8
0
def createVersionStartEdit(fcLST, temploc, sdecon, user, password):
    global version, workspace, edit, fcLSTglob, userglob
    fcLSTglob = fcLST
    userglob = user
    version = "ARCPY" + time.strftime("%d%m%Y%I%M%S")
    tempsdecon = "SDECONN" + time.strftime("%d%m%Y%I%M%S")
    workspace = os.path.join(temploc, tempsdecon + ".sde")
    arcpy.CreateVersion_management(sdecon, "SDE.DEFAULT", version, "PRIVATE")
    arcpy.CreateArcSDEConnectionFile_management(temploc, tempsdecon, " ",
                                                "sde:oracle11g:somesde.world",
                                                "", "", userglob, password, "",
                                                user + "." + version)
    edit = arcpy.da.Editor(workspace)
    edit.startEditing(False, True)
    edit.startOperation()
    for i in fcLSTglob:
        editfeature = os.path.join(workspace, i)
        arcpy.MakeFeatureLayer_management(editfeature, i.split(".")[1])
    print("versioned editing started")
    arcpy.AddMessage("versioned editing started")
    return version, workspace, edit, fcLSTglob, userglob
Example #9
0
def createVersion(sde, defaultVersion, versionName):
    # create a version in the database
    retcode = False
    loc = versionName.find(".")
    if loc > -1:
        versionName = versionName.split(".")[1]
    addMessageLocal("Creating Version: " + versionName)
    try:
        retcode = arcpy.CreateVersion_management(sde, defaultVersion,
                                                 versionName, "PRIVATE")
        if debug == True:
            addMessageLocal("create version " + str(retcode))
    except:
        if debug == True:
            addMessageLocal("create version exception: " + str(retcode))
        retcode = False  # have to assume this means it exists already
        try:  # but try to change it to check
            retcode = arcpy.AlterVersion_management(
                sde, versionName, "", "Version for Data loading", "PRIVATE")
            if debug == True:
                addMessageLocal("alter version " + str(retcode))
            #addMessageLocal "Alter version succeeded"
        except:
            addMessageLocal(
                "Create version failed - please try to manually delete any existing version in ArcCatalog"
            )
            addMessageLocal("Error: " + str(sys.exc_value))
            retcode = False  # if we can't change it the version probably doesn't exist
            if debug == True:
                addMessageLocal("except: alter version (doesn't exist) " +
                                str(retcode))
            exit(-1)

    if retcode != False:  # this means that the create version call returned a value
        retcode = True
    if debug == True:
        addMessageLocal("final retcode " + str(retcode))
    return retcode
Example #10
0
    # Use roles to apply permissions.
    print(
        "\tGranting appropriate privileges to the data for the 'viewers' and 'editors' roles"
    )
    arcpy.ChangePrivileges_management(dataList, 'viewers', 'GRANT')
    arcpy.ChangePrivileges_management(dataList, 'editors', 'GRANT', 'GRANT')

    # Register the data as versioned.
    print("\tRegistering the data as versioned")
    for dataset in dataList:
        arcpy.RegisterAsVersioned_management(dataset)

    # Finally, create a version for each editor.
    print("\tCreating a private version for each user in the editor role")
    for user3 in editors:
        verCreateConn = arcpy.CreateDatabaseConnection_management(
            r'<path_to_save_connection_file>', '<file_name>.sde', platform,
            instance, 'DATABASE_AUTH', user3, '<your_password_choice>',
            'SAVE_USERNAME', database)
        arcpy.CreateVersion_management(verCreateConn, 'sde.Default',
                                       '<your_choice_of_version_name>',
                                       'PRIVATE')
        arcpy.ClearWorkspaceCache_management()
    print(
        '\n****Done Creating Demo3 Geodatabase with roles, users, data and privileges.'
    )

except:
    print('Script failure.\n')
    print(arcpy.GetMessages())
        # Make sure we've been given a username and password
        if not uname or not pw:
            raise arcpy.ExecuteError("A username and password for the " +
                                        "versioned SDE must be provided.")

        # Raise error if updates version already exists
        for existing in arcpy.ListVersions(sde_path):
            if version in existing:
                raise arcpy.ExecuteError("Version " + version + " already in use." +
                                            " Please specify a different version.")

        # Create a new temporary version that will be edited, then reconciled and
        # posted back to sde.DEFAULT.
        arcpy.AddMessage("Creating new version for updating...")
        arcpy.CreateVersion_management(sde_path, v_parent, v_name, "PROTECTED")

        # Sanity checks on version connection files
        if os.path.exists(connection):
            raise IOError("Connection file " + connection + " already exists." +
                            " Remove or rename existing file.")
        if not os.path.exists(out_folder):
            raise IOError("Connection file output folder " + out_folder +
                            " does not exist.")

        # Create a new connection file for the temporary version that will be used
        # to edit the feature class.
        arcpy.CreateDatabaseConnection_management(out_folder, out_name, db_platform,
                                                    instance, auth, uname, pw, save,
                                                    db, schema, v_type, version)
Example #12
0
def main():

    env.overwriteOutput = True
    arcpy.ClearWorkspaceCache_management()

    # Staging SDE:
    folderName = r"E:\gis\projects\Staging\Admin\prod_build"
    fileName = [
        "Lex_CDOT_Survey_Admin - Staging.sde",
        "Lex_CDOT_MapOutput_Admin - Staging(DEFAULT).sde"
    ]

    t_v = ["DBO.Survey", "dbo.DEFAULT"]

    #Use connection files with SDE version for editing only
    editSDE = r"E:\gis\projects\Staging\Admin\prod_build\Lex_CDOT_Preproc_Admin - Staging.sde"
    editMapbookSDE = r"E:\gis\projects\Staging\Admin\prod_build\Lex_CDOT_MapOutput_Admin - Staging(PreProc).sde"

    # Setup workspaces
    preGDB = arcpy.GetParameterAsText(0)
    if preGDB == "#" or not preGDB:
        preGDB = r"E:\gis\projects\IL\Chicago\CDOT_Menu_Pro\Spatial\gdb\working\PreProcessing\Lex_CDOT_Menu_PreProcessing_01072015_125937.gdb"

    preProjLines = preGDB + "\ProjectCenterline_draft"

    preBlockLines = preGDB + "\BlockCenterline_draft"

    desc = arcpy.Describe(preProjLines)
    preGDB = desc.path
    print "Path:      " + desc.path
    arcpy.AddMessage("Path:      " + desc.path)
    print "Base Name: " + desc.baseName
    arcpy.AddMessage("Base Name: " + desc.baseName)

    desc = arcpy.Describe(preBlockLines)
    print "Base Name: " + desc.baseName
    arcpy.AddMessage("Base Name: " + desc.baseName)

    # Call for connection
    f = fileName[0]
    checkConn(f, folderName)
    SDE_DEFAULT = folderName + '\\' + f
    targetSDE = SDE_DEFAULT
    descSDE = arcpy.Describe(targetSDE)
    sdeName = descSDE.connectionProperties.database
    print "sdeName = " + sdeName

    try:
        versionList = arcpy.ListVersions(targetMapbookSDE)
        if 'DBO.PreProc' in versionList:
            arcpy.DeleteVersion_management(
                targetMapbookSDE, 'DBO.PreProc'
            )  #Cannot remove version and recreate since its used by the connection file.
            arcpy.CreateVersion_management(targetMapbookSDE, t_v[1], "PreProc",
                                           "PUBLIC")
        else:
            arcpy.CreateVersion_management(targetSDE, t_v[1], "PreProc",
                                           "PUBLIC")
    except Exception as e:
        print "Problem creating version"

    # Call for connection
    f = fileName[1]
    checkConn(f, folderName)
    SDE_DEFAULT = folderName + '\\' + f
    targetMapbookSDE = SDE_DEFAULT
    descMB = arcpy.Describe(targetMapbookSDE)
    mbSDEName = descMB.connectionProperties.database
    print mbSDEName

    preProjLinesCombo = preProjLines.replace("\ProjectCenterline",
                                             "\ProjectCenterlineCombo")
    preBlockLinesCombo = preBlockLines.replace("\BlockCenterline",
                                               "\BlockCenterlineCombo")

    env.workspace = preGDB
    """ ************************************  Project Centerline Updates  ****************************** """
    print ""
    print "***************************************"
    print "Beginning Project Centerline Updates..."
    print "***************************************"

    # Create lists of needed field names
    fieldsProject = [
        "strCCEProjectNumber", "strCDOTProjectNumber", "memAldermanComment",
        "memProjectComments", "memWardComment", "SHAPE@", "strReportLocation",
        "strReportFrom", "strReportTo", "lngMenuID", "lngWardID", "lngBlocks",
        "lngProjectID"
    ]  #These are the needed fields for the ProjectCenterline
    fieldsBlock = [
        "PageNo", "strCDOTProjectNumber", "strBlockLocation", "strBlockFrom",
        "strBlockTo", "SHAPE@", "strCCEProjectNumber", "BlockNo",
        "lngProjectID"
    ]
    allFields = "*"

    #Add query to ignore RW and RC Project types
    whereClause = """"strCCEProjectNumber" NOT LIKE '%RW%' AND "strCCEProjectNumber" NOT LIKE '%RC%'"""

    #Start an editing session
    edit = arcpy.da.Editor(editSDE)
    edit.startEditing(False, True)
    edit.startOperation()

    #Create new SearchCursor and InsertCursor for ProjectCenterline_v3
    s_curGDB = arcpy.da.SearchCursor(preProjLinesCombo, fieldsProject,
                                     whereClause)
    print "Search cursor set... ProjectCenterline.  Evaluating Records..."
    i_cursSDE = arcpy.da.InsertCursor(
        editSDE + os.sep + sdeName + ".DBO.Ops\\" + sdeName +
        ".DBO.ProjectCenterline", fieldsProject)
    print "Insert Cursor set... ProjectCenterline"

    #Create a list to hold the records that do not have any geometry.  This list will notify the user at the end of any records that have no geometry.
    noGeometryProject = []

    # Create a list to hold project numbers that have had blocks deleted from them.
    projectsWithDeletedBlocks = []

    # Start the for loop to cycle through all the rows
    for s_row in s_curGDB:

        shape = s_row[5]  #This variable points to the Shape token, "SHAPE@"

        match = False  # A boolean value that is changed to True if the record matches a record already in the Target SDE.

        # Setup update cursor for the Target SDE - This will cycle through the existing records in the SDE.
        u_cursSDE = arcpy.da.UpdateCursor(
            editSDE + os.sep + sdeName + ".DBO.Ops\\" + sdeName +
            ".DBO.ProjectCenterline", fieldsProject,
            fieldsProject[0] + " = '" + s_row[0] + "'")

        #If the feature has no geometry, it's PageNo is added to the noGeometryArray and reported to the user at the end of the script.
        if shape == None:
            noGeometryProject.append(str(s_row[0]))

        #Cycle through the records in the Target SDE and compare the PageNo value with that in the GDB.
        for u_row in u_cursSDE:
            counter = 0
            if u_row[0] == s_row[0]:

                if u_row[1] == s_row[1]:
                    pass
                else:
                    counter = counter + 1
                    u_row[1] = s_row[1]
                if u_row[2] == s_row[2]:
                    pass
                else:
                    counter = counter + 1
                    u_row[2] = s_row[2]
                if u_row[3] == s_row[3]:
                    pass
                else:
                    counter = counter + 1
                    u_row[3] = s_row[3]
                if u_row[4] == s_row[4]:
                    pass
                else:
                    counter = counter + 1
                    u_row[4] = s_row[4]
                if u_row[6] == s_row[6]:
                    pass
                else:
                    counter = counter + 1
                    u_row[6] = s_row[6]
                if u_row[7] == s_row[7]:
                    pass
                else:
                    counter = counter + 1
                    u_row[7] = s_row[7]
                if u_row[8] == s_row[8]:
                    pass
                else:
                    counter = counter + 1
                    u_row[8] = s_row[8]
                if u_row[9] == s_row[9]:
                    pass
                else:
                    counter = counter + 1
                    u_row[9] = s_row[9]
                if u_row[10] == s_row[10]:
                    pass
                else:
                    counter = counter + 1
                    u_row[10] = s_row[10]
                if u_row[11] == s_row[11]:
                    pass
                else:
                    # if value for lngBlocks is different, then a block has been deleted.
                    # Add project number to a list of projects with deleted blocks
                    if u_row[11] > s_row[11]:
                        projectsWithDeletedBlocks.append(s_row[0])

                    counter = counter + 1
                    u_row[11] = s_row[11]

                if u_row[12] == s_row[12]:
                    pass
                else:
                    counter = counter + 1
                    u_row[12] = s_row[12]

                if counter > 0:
                    u_cursSDE.updateRow(u_row)
                    print "Update " + str(counter) + " objects to row."
                    print str(s_row[0]) + " was updated."
                else:
                    print str(
                        s_row[0]) + ": All attributes match. Skipping Record."
                    pass  #If a matching record is found, update the record

                match = True  #Boolean to determine if its a new project or not

        if match == False:  #If no matching record is found in the SDE, the Insert Cursor will insert the new record.
            i_cursSDE.insertRow(s_row)
            print str(s_row[0]) + " was inserted."

        del u_cursSDE  #Delete the Update Cursor before re-creating it in the next iteration of the loop.

    del i_cursSDE, s_curGDB  #Delete the InsertCursor pointing to the Target SDE and the Search Cursor pointing to the GDB.

    print "Number of projects with deleted blocks = " + str(
        len(projectsWithDeletedBlocks))
    for proj in projectsWithDeletedBlocks:
        print proj + "has a deleted block."

    print "*************************************"
    print "ProjectCenterline updating is complete."
    print "*************************************"
    arcpy.AddMessage("*************************************")
    arcpy.AddMessage("ProjectCenterline updating is complete.")
    arcpy.AddMessage("*************************************")
    """*********************************  Block Centerline Updates  **********************************"""
    print ""
    print "***************************************"
    print "Beginning Block Centerline Updates..."
    print "***************************************"

    #Create Search Cursor - Cusor is initially set to the BlockCenterline_v3 dataset.
    s_curGDB = arcpy.da.SearchCursor(preBlockLinesCombo, fieldsBlock,
                                     whereClause)
    print "Search cursor set... BlockCenterline. Evaluating Records..."

    #Create Insert Cursor (It is more efficient if the Insert Cursor is created outside the loop)
    i_cursSDE = arcpy.da.InsertCursor(
        editSDE + os.sep + sdeName + ".DBO.Ops\\" + sdeName +
        ".DBO.BlockCenterline",
        fieldsBlock)  #Change for final once final SDE in place <SAH>

    #Create a list to hold the records that do not have any geometry.  This list will notify the user at the end of any records that have no geometry.
    noGeometryBlock = []

    # Set Update Cursor to point to the BlockCenterline layer in the SDE
    u_cursSDE = arcpy.da.UpdateCursor(
        editSDE + os.sep + sdeName + ".DBO.Ops\\" + sdeName +
        ".DBO.BlockCenterline", fieldsBlock)

    # Cycle through the SDE's Block Centerline layer.  All blocks with a project number found in the projectsWithDeletedBlocks list will be renamed to have an "XX" in place of their letters.
    numBlocksDeleted = 0

    for u_row in u_cursSDE:
        if u_row[6] in projectsWithDeletedBlocks:
            newRow = u_row  # Create a copy of the current row

            oldProjNum = u_row[6]
            if len(
                    oldProjNum
            ) > 9:  # Create new project number with XX in place of letters.
                newProjNum = oldProjNum[0:3] + "YY" + oldProjNum[
                    5] + "YY" + oldProjNum[8:]
            else:
                newProjNum = oldProjNum[0:3] + "YY" + oldProjNum[5:]

            newRow[6] = newProjNum  # Give new row the modified project number.
            newRow[0] = newProjNum + "_" + str(
                u_row[7])  # Update PageNo with changed project number.
            u_cursSDE.deleteRow()  # Delete the old row.
            i_cursSDE.insertRow(
                newRow)  # Insert the copy with the modified project number.
            numBlocksDeleted = numBlocksDeleted + 1

    print "Number of blocks deleted = " + str(numBlocksDeleted)
    del u_cursSDE

    # Start the for loop to cycle through all the rows
    for s_row in s_curGDB:

        shape = s_row[5]  #This variable points to the Shape token, "SHAPE@"

        match = False  # A boolean value that is changed to True if the record matches a record already in the Target SDE.

        # Setup update cursor for the Target SDE - This will cycle through the existing records in the SDE.
        u_cursSDE = arcpy.da.UpdateCursor(
            editSDE + os.sep + sdeName + ".DBO.Ops\\" + sdeName +
            ".DBO.BlockCenterline", fieldsBlock,
            fieldsBlock[0] + " = '" + s_row[0] + "'")

        #If the feature has no geometry, it's PageNo is added to the noGeometryArray and reported to the user at the end of the script.
        if shape == None:
            noGeometryBlock.append(str(s_row[0]))

        #Cycle through the records in the Target SDE and compare the PageNo value with that in the GDB.
        for u_row in u_cursSDE:
            counter = 0

            if u_row[0] == s_row[
                    0]:  # If PageNo in preBlockLinesCombo == PageNo in the SDE

                if u_row[1] == s_row[1]:  #strCDOTProjectNumber
                    pass
                else:
                    counter = counter + 1
                    u_row[1] = s_row[1]
                if u_row[2] == s_row[2]:  #strBlockLocation
                    pass
                else:
                    counter = counter + 1
                    u_row[2] = s_row[2]
                if u_row[3] == s_row[3]:  #strBlockFrom
                    pass
                else:
                    counter = counter + 1
                    u_row[3] = s_row[3]
                if u_row[4] == s_row[4]:  #strBlockTo
                    pass
                else:
                    counter = counter + 1
                    u_row[4] = s_row[4]
                if u_row[7] == s_row[7]:  #BlockNo
                    pass
                else:
                    counter = counter + 1
                    u_row[7] = s_row[7]
                if u_row[8] == s_row[8]:  #lngProjectID
                    pass
                else:
                    counter = counter + 1
                    u_row[8] = s_row[8]

                if counter > 0:
                    u_cursSDE.updateRow(u_row)
                    print "Update " + str(counter) + " objects to row."
                    print str(s_row[0]) + " was updated."
                else:
                    print str(
                        s_row[0]) + ": All attributes match. Skipping Record."
                    pass  #If a matching record is found, update the record

                match = True

        if match == False:  #If no matching record is found in the SDE, the Insert Cursor will insert the new record.
            i_cursSDE.insertRow(s_row)
            print str(s_row[0]) + " was inserted."

        del u_cursSDE  #Delete the Update Cursor before re-creating it in the next iteration of the loop.
    # ========================================================================================================================

    del i_cursSDE, s_curGDB  #Delete the InsertCursor pointing to the Target SDE and the Search Cursor pointing to the GDB.

    #Stop the editing session.
    edit.stopOperation()
    edit.stopEditing(True)
    del edit

    # Posting to SDE
    SDE_DEFAULT = targetSDE
    version = "PreProc"
    p = t_v[0]
    Post(SDE_DEFAULT, version, p)

    print "*************************************"
    print "BlockCenterline updating is complete."
    print "*************************************"
    arcpy.AddMessage("*************************************")
    arcpy.AddMessage("BlockCenterline updating is complete.")
    arcpy.AddMessage("*************************************")
    #-----------------------------------------------------------------------------------------------------------------------------------------------------

    #-----------------------------------------------------------------------------------------------------------------------------------------------------
    """Added Rotation to fieldsBlock List"""
    fieldsBlock.append("Rotation")
    arcpy.AddMessage(fieldsBlock)

    #Start an editing session
    edit1 = arcpy.da.Editor(editMapbookSDE)
    edit1.startEditing(False, True)
    edit1.startOperation()

    #Create Search Cursor - Cusor is initially set to the BlockCenterline_v3 dataset.
    s_curGDB = arcpy.da.SearchCursor(preBlockLines, fieldsBlock, whereClause)
    print "Search cursor set... BlockCenterline. Evaluating Records..."

    #Create Insert Cursor (It is more efficient if the Insert Cursor is created outside the loop)
    i_cursSDE = arcpy.da.InsertCursor(
        editMapbookSDE + os.sep + mbSDEName + ".DBO.Calcs\\" + mbSDEName +
        ".DBO.Blocks_FinalCalcs", fieldsBlock)

    #Create a list to hold the records that do not have any geometry.  This list will notify the user at the end of any records that have no geometry.
    noGeometryBlock = []

    # Start the for loop to cycle through all the rows
    for s_row in s_curGDB:

        shape = s_row[5]  #This variable points to the Shape token, "SHAPE@"

        match = False  # A boolean value that is changed to True if the record matches a record already in the Target SDE.

        # Setup update cursor for the Target SDE - This will cycle through the existing records in the SDE.
        u_cursSDE = arcpy.da.UpdateCursor(
            editMapbookSDE + os.sep + mbSDEName + ".DBO.Calcs\\" + mbSDEName +
            ".DBO.Blocks_FinalCalcs", fieldsBlock,
            fieldsBlock[0] + " = '" + s_row[0] + "'")

        #If the feature has no geometry, it's PageNo is added to the noGeometryArray and reported to the user at the end of the script.
        if shape == None:
            noGeometryBlock.append(str(s_row[0]))

        #Cycle through the records in the Target SDE and compare the PageNo value with that in the GDB.
        for u_row in u_cursSDE:
            counter = 0

            if u_row[0] == s_row[0]:
                if u_row[1] == s_row[1]:
                    pass
                else:
                    counter = counter + 1
                    u_row[1] = s_row[1]
                if u_row[2] == s_row[2]:
                    pass
                else:
                    counter = counter + 1
                    u_row[2] = s_row[2]
                if u_row[3] == s_row[3]:
                    pass
                else:
                    counter = counter + 1
                    u_row[3] = s_row[3]
                if u_row[4] == s_row[4]:
                    pass
                else:
                    counter = counter + 1
                    u_row[4] = s_row[4]
                if u_row[7] == s_row[7]:
                    pass
                else:
                    counter = counter + 1
                    u_row[7] = s_row[7]
                if u_row[8] == s_row[8]:
                    pass
                else:
                    counter = counter + 1
                    u_row[8] == s_row[8]

                if counter > 0:
                    u_cursSDE.updateRow(
                        u_row
                    )  #If a matching record is found, update the record
                    print "Update " + str(counter) + " objects to row."
                    print str(s_row[0]) + " was updated."
                else:
                    print str(
                        s_row[0]) + ": All attributes match. Skipping Record."
                    pass

                match = True  #Boolean to determine if its a new project or not

        if match == False:  #If no matching record is found in the SDE, the Insert Cursor will insert the new record.
            i_cursSDE.insertRow(s_row)
            print str(s_row[0]) + " was inserted."

        del u_cursSDE  #Delete the Update Cursor before re-creating it in the next iteration of the loop.

    del i_cursSDE, s_curGDB  #Delete the InsertCursor pointing to the Target SDE and the Search Cursor pointing to the GDB.

    print "*************************************"
    print "MapBook BlockCenterline updating is complete."
    print "*************************************"
    arcpy.AddMessage("*************************************")
    arcpy.AddMessage("MapBook BlockCenterline updating is complete.")
    arcpy.AddMessage("*************************************")

    #Stop the editing session.
    edit1.stopOperation()
    edit1.stopEditing(True)
    del edit1

    ## Posting to SDE
    SDE_DEFAULT = targetMapbookSDE
    version = "PreProc"
    p = t_v[1]
    Post(SDE_DEFAULT, version, p)

    #------------------------------------------------------------------------
    #Notify the user of any features that do not have geometry.
    if len(noGeometryBlock) > 0:
        print ""
        print "********************************************************** "
        print "WARNING! The following Block Centerlines have no geometry and were skipped: "
        arcpy.AddMessage(
            "WARNING! The following Block Centerlines have no geometry and were skipped: "
        )
        for i in range(len(noGeometryBlock)):
            print noGeometryBlock[i]
            arcpy.AddMessage(noGeometryBlock[i])

        print "********************************************************** "

    elif len(noGeometryProject) > 0:
        print ""
        print "********************************************************** "
        print "WARNING! The following Project Centerlines have no geometry and were skipped: "
        arcpy.AddMessage(
            "WARNING! The following Project Centerlines have no geometry and were skipped: "
        )
        for i in range(len(noGeometryProject)):
            print noGeometryProject[i]
            arcpy.AddMessage(noGeometryProject[i])

        print "********************************************************** "

    else:
        print ""
        print "*********************** "
        print "The script is finished. "
        print "*********************** "
        arcpy.AddMessage("The script is finished. ")

    arcpy.ClearWorkspaceCache_management()
Example #13
0
    def append_to_esri_source(self, input_fc, esri_output_fc,
                              input_where_clause):
        """
        Append to the esri source FC, including projecting to output coord system, creating a versioned FC of the
        output in SDE, deleting from that versioned FC based on a where_clause, appending the new data, and then
        posting that version
        :param input_fc: the input source data
        :param esri_output_fc: the output in SDE
        :param input_where_clause: where clause used to add to/delete from the output esri FC
        :return:
        """

        logging.info(
            'Starting vector_layer.append_to_esri_source for {0}'.format(
                self.name))

        fc_to_append = self.project_to_output_srs(input_fc, esri_output_fc)
        logging.info('appending {} to {}'.format(input_fc, esri_output_fc))

        logging.debug('Creating a versioned FL from esri_service_output')
        arcpy.MakeFeatureLayer_management(esri_output_fc,
                                          "esri_service_output_fl")

        version_name = self.name + "_" + str(int(time.time()))

        sde_workspace = os.path.dirname(esri_output_fc)
        desc = arcpy.Describe(sde_workspace)
        if hasattr(desc,
                   "datasetType") and desc.datasetType == 'FeatureDataset':
            sde_workspace = os.path.dirname(sde_workspace)

        del desc

        if os.path.splitext(sde_workspace)[1] != '.sde':
            logging.error('Could not find proper SDE workspace. Exiting.')
            sys.exit(1)

        arcpy.CreateVersion_management(sde_workspace, "sde.DEFAULT",
                                       version_name, "PRIVATE")
        arcpy.ChangeVersion_management("esri_service_output_fl",
                                       'TRANSACTIONAL', 'gfw.' + version_name,
                                       '')

        if input_where_clause:
            logging.debug(
                'Deleting features from esri_service_output feature layer based on input_where_clause. '
                'SQL statement: {0}'.format(input_where_clause))

            arcpy.SelectLayerByAttribute_management("esri_service_output_fl",
                                                    "NEW_SELECTION",
                                                    input_where_clause)

            # Delete the features selected by the input where_clause
            arcpy.DeleteRows_management("esri_service_output_fl")

        else:
            logging.debug(
                'No where clause for esri_service_output found; deleting all features before '
                'appending from source')

            sde_sql_conn = arcpy.ArcSDESQLExecute(sde_workspace)
            esri_fc_name = os.path.basename(esri_output_fc)
            print esri_fc_name

            # why this, exactly?
            # there's also a lbr_plantations_old feature class (for some reason)
            # and lbr_plantation_evw points to that.
            # I don't know why and I don't have time to fix it

            # WDPA test - leave off
            if esri_fc_name != 'gfw_countries.gfw.lbr_plantations':
                esri_fc_name += '_evw'

            # Find the min and max OID values
            to_delete_oid_field = [
                f.name for f in arcpy.ListFields(esri_output_fc)
                if f.type == 'OID'
            ][0]

            sql = 'SELECT min({0}), max({0}) from {1}'.format(
                to_delete_oid_field, esri_fc_name)
            to_delete_min_oid, to_delete_max_oid = sde_sql_conn.execute(sql)[0]

            # If there are features to delete, do it
            if to_delete_min_oid and to_delete_max_oid:

                for wc in util.generate_where_clause(to_delete_min_oid,
                                                     to_delete_max_oid,
                                                     to_delete_oid_field,
                                                     1000):

                    logging.debug('Deleting features with {0}'.format(wc))
                    arcpy.MakeFeatureLayer_management("esri_service_output_fl",
                                                      "fl_to_delete", wc)

                    arcpy.DeleteRows_management("fl_to_delete")
                    arcpy.Delete_management("fl_to_delete")

            else:
                pass

        esri_output_pre_append_count = int(
            arcpy.GetCount_management("esri_service_output_fl").getOutput(0))
        input_feature_count = int(
            arcpy.GetCount_management(fc_to_append).getOutput(0))

        logging.debug('Starting to append to esri_service_output')

        # don't need to batch append if it's coming from an SDE data source
        # these are used exclusively by country-vector layers
        # and the data is generally small, compared to things like WDPA
        if 'sde' in fc_to_append:
            logging.debug(
                "Appending all features from {}- no wc because it's an SDE input"
                .format(fc_to_append))

            arcpy.MakeFeatureLayer_management(fc_to_append, "fl_to_append")
            arcpy.Append_management("fl_to_append", "esri_service_output_fl",
                                    "NO_TEST")

            arcpy.Delete_management("fl_to_append")

        else:
            # Find the min and max OID values
            to_append_oid_field = [
                f.name for f in arcpy.ListFields(fc_to_append)
                if f.type == 'OID'
            ][0]
            to_append_min_oid, to_append_max_oid = cartodb.ogrinfo_min_max(
                fc_to_append, to_append_oid_field)

            for wc in util.generate_where_clause(to_append_min_oid,
                                                 to_append_max_oid,
                                                 to_append_oid_field, 1000):

                logging.debug('Appending features with {0}'.format(wc))
                arcpy.MakeFeatureLayer_management(fc_to_append, "fl_to_append",
                                                  wc)

                arcpy.Append_management("fl_to_append",
                                        "esri_service_output_fl", "NO_TEST")
                arcpy.Delete_management("fl_to_append")

        logging.debug('Append finished, starting to reconcile versions')

        arcpy.ReconcileVersions_management(
            input_database=sde_workspace,
            reconcile_mode="ALL_VERSIONS",
            target_version="sde.DEFAULT",
            edit_versions='gfw.' + version_name,
            acquire_locks="LOCK_ACQUIRED",
            abort_if_conflicts="NO_ABORT",
            conflict_definition="BY_OBJECT",
            conflict_resolution="FAVOR_TARGET_VERSION",
            with_post="POST",
            with_delete="KEEP_VERSION",
            out_log="")

        logging.debug('Deleting temporary FL and temporary version')

        # For some reason need to run DeleteVersion_management here, will have errors if with_delete is used above
        arcpy.Delete_management("esri_service_output_fl")
        arcpy.DeleteVersion_management(sde_workspace, 'gfw.' + version_name)

        post_append_count = int(
            arcpy.GetCount_management(esri_output_fc).getOutput(0))

        if esri_output_pre_append_count + input_feature_count == post_append_count:
            logging.debug('Append successful based on sum of input features')
        else:
            logging.debug(
                'esri_output_pre_append_count: {0}\input_feature_count: {1}\npost_append_count{2}\n'
                'Append failed, sum of input features does not match. '
                'Exiting'.format(esri_output_pre_append_count,
                                 input_feature_count, post_append_count))
            sys.exit(1)

        # now that we've finished syncing everything locally, need to push it to the PROD replica
        local_sde = r'D:\scripts\connections\gfw (gfw@localhost).sde'
        prod_gdb = r'P:\data\gfw_database\gfw.gdb'
        arcpy.SynchronizeChanges_management(local_sde, "gfw.GFW", prod_gdb,
                                            "FROM_GEODATABASE1_TO_2")

        return
Example #14
0
 def create_version(self):
     self.bxOutput.AppendText("\n Creating Version...")
     arcpy.CreateVersion_management(
         "Database Connections/publiworks_TAX_SQL_Miguelto.sde",
         "MIGUELTO.UTIL_EDITS_MIGUELTO", "DigSubTest", "PROTECTED")
Example #15
0
	def create_version(self, version_name):
		"""Creates a new version as a branch off of dbo.DEFAULT. If the version already exists, deletes it and creates a brand new one."""
		
		lst = arcpy.ListVersions(self.workspace)
		if ("DBO." + version_name) not in lst:
			arcpy.CreateVersion_management(self.workspace, "dbo.DEFAULT", version_name, "PRIVATE")
Example #16
0
    # Compress gdb 2nd time
    arcpy.Compress_management(defaultWorkspace)
    log.write(
        str(time.asctime()) + ": Geodatabase compressed for second time.\n")

    # Allow the database to begin accepting connections again
    arcpy.AcceptConnections(defaultWorkspace, True)
    log.write(str(time.asctime()) + ": Database block removed.\n")

    # Rebuild versions
    connections = [sde, giseditor, sewerman]
    versions = ["QC", "edits", "edits"]
    pVersion = ["sde.Default", "SDE.QC", "SDE.QC"]
    fullNames = ["SDE.QC", "GISEDITOR.edits", "SEWERMAN.edits"]
    for i, versionName in enumerate(connections):
        arcpy.CreateVersion_management(connections[i], pVersion[i],
                                       versions[i], "PUBLIC")
        log.write(
            str(time.asctime()) + ": " + fullNames[i] + " version created.\n")

    # Rebuild indexes
    arcpy.RebuildIndexes_management(
        sewerman, "NO_SYSTEM", "SDE.SEWERMAN.MANHOLES_VIEW; "
        "SDE.SEWERMAN.SEWERS_VIEW; "
        "SDE.SEWERMAN.ProjectAreas; "
        "SDE.SEWERMAN.REPAVING", "ALL")
    log.write(str(time.asctime()) + ": Indexes rebuilt.\n")

    # Analyze datasets
    arcpy.AnalyzeDatasets_management(
        sewerman, "NO_SYSTEM", "SDE.SEWERMAN.MANHOLES_VIEW; "
        "SDE.SEWERMAN.SEWERS_VIEW; "
Example #17
0
    username_lastName_full = str(temp[1])

    # change the user's first name initial portion of their username to UPPERCASE
    username_firstName_initial = username_firstName_initial.upper()
    # change the user's last name portion of their username to UPPERCASE
    username_lastName_full = username_lastName_full.upper()

    # get user's full OS username and convert to all UPPERCASE
    Operating_system_username = str(Operating_system_username).upper()

    # populate the suffix portion of the database user version from the above OS username components
    database_user_version_suffix = Operating_system_username + '".' + username_firstName_initial + username_lastName_full

    # populate the final database user version by concatenating the prefix defined at the beginning of the script with the suffix defined in the previous step
    database_user_version_full = database_user_version_prefix + database_user_version_suffix

except Exception as e:
    print(
        "an error occurred attempting to create the user's database user version name from the user's OS username. Terminating program...."
    )
    # insert snippet to write any errors to a log file here
    # insert optional logic to look for the users database version name in an alternate location - if so, do not terminate the program
    System.exit(1)

parent_version = "dbo.QC"
version = r'"SANDYSPRINGS\MESHA.CISERO".MCisero'

arcpy.CreateVersion_management(database_connection_file,
                               database_parent_version,
                               database_user_version_full,
                               db_version_access_permission)
Example #18
0
    def append_to_esri_source(self, input_fc, esri_output_fc,
                              input_where_clause):
        """
        Append to the esri source FC, including projecting to output coord system, creating a versioned FC of the
        output in SDE, deleting from that versioned FC based on a where_clause, appending the new data, and then
        posting that version
        :param input_fc: the input source data
        :param esri_output_fc: the output in SDE
        :param input_where_clause: where clause used to add to/delete from the output esri FC
        :return:
        """

        logging.info(
            'Starting vector_layer.append_to_esri_source for {0}'.format(
                self.name))

        fc_to_append = self.project_to_output_srs(input_fc, esri_output_fc)

        logging.debug('Creating a versioned FL from esri_service_output')
        arcpy.MakeFeatureLayer_management(esri_output_fc,
                                          "esri_service_output_fl")

        version_name = self.name + "_" + str(int(time.time()))

        sde_workspace = os.path.dirname(esri_output_fc)
        desc = arcpy.Describe(sde_workspace)
        if hasattr(desc,
                   "datasetType") and desc.datasetType == 'FeatureDataset':
            sde_workspace = os.path.dirname(sde_workspace)

        del desc

        if os.path.splitext(sde_workspace)[1] != '.sde':
            logging.error('Could not find proper SDE workspace. Exiting.')
            sys.exit(1)

        arcpy.CreateVersion_management(sde_workspace, "sde.DEFAULT",
                                       version_name, "PRIVATE")
        arcpy.ChangeVersion_management("esri_service_output_fl",
                                       'TRANSACTIONAL', 'gfw.' + version_name,
                                       '')

        if input_where_clause:
            logging.debug(
                'Deleting features from esri_service_output feature layer based on input_where_clause. '
                'SQL statement: {0}'.format(input_where_clause))

            arcpy.SelectLayerByAttribute_management("esri_service_output_fl",
                                                    "NEW_SELECTION",
                                                    input_where_clause)

            # Delete the features selected by the input where_clause
            arcpy.DeleteRows_management("esri_service_output_fl")

        else:
            logging.debug(
                'No where clause for esri_service_output found; deleting all features before '
                'appending from source')

            # commented out at this already happens below with wc
            # arcpy.MakeFeatureLayer_management("esri_service_output_fl", "fl_to_delete")
            # arcpy.DeleteRows_management("fl_to_delete")
            # arcpy.Delete_management("fl_to_delete")

            sde_sql_conn = arcpy.ArcSDESQLExecute(sde_workspace)
            esri_fc_name = os.path.basename(esri_output_fc) + '_evw'

            # Find the min and max OID values
            to_delete_oid_field = [
                f.name for f in arcpy.ListFields(esri_output_fc)
                if f.type == 'OID'
            ][0]

            sql = 'SELECT min({0}), max({0}) from {1}'.format(
                to_delete_oid_field, esri_fc_name)
            to_delete_min_oid, to_delete_max_oid = sde_sql_conn.execute(sql)[0]

            # If there are features to delete, do it
            if to_delete_min_oid and to_delete_max_oid:

                for wc in util.generate_where_clause(to_delete_min_oid,
                                                     to_delete_max_oid,
                                                     to_delete_oid_field,
                                                     1000):

                    logging.debug('Deleting features with {0}'.format(wc))
                    arcpy.MakeFeatureLayer_management("esri_service_output_fl",
                                                      "fl_to_delete", wc)

                    arcpy.DeleteRows_management("fl_to_delete")
                    arcpy.Delete_management("fl_to_delete")

            else:
                pass

        esri_output_pre_append_count = int(
            arcpy.GetCount_management("esri_service_output_fl").getOutput(0))
        input_feature_count = int(
            arcpy.GetCount_management(fc_to_append).getOutput(0))

        logging.debug('Starting to append to esri_service_output')

        # Find the min and max OID values
        to_append_oid_field = [
            f.name for f in arcpy.ListFields(fc_to_append) if f.type == 'OID'
        ][0]
        to_append_min_oid, to_append_max_oid = cartodb.ogrinfo_min_max(
            fc_to_append, to_append_oid_field)

        for wc in util.generate_where_clause(to_append_min_oid,
                                             to_append_max_oid,
                                             to_append_oid_field, 1000):

            logging.debug('Appending features with {0}'.format(wc))
            arcpy.MakeFeatureLayer_management(fc_to_append, "fl_to_append", wc)

            arcpy.Append_management("fl_to_append", "esri_service_output_fl",
                                    "NO_TEST")
            arcpy.Delete_management("fl_to_append")

        logging.debug('Append finished, starting to reconcile versions')

        arcpy.ReconcileVersions_management(
            input_database=sde_workspace,
            reconcile_mode="ALL_VERSIONS",
            target_version="sde.DEFAULT",
            edit_versions='gfw.' + version_name,
            acquire_locks="LOCK_ACQUIRED",
            abort_if_conflicts="NO_ABORT",
            conflict_definition="BY_OBJECT",
            conflict_resolution="FAVOR_TARGET_VERSION",
            with_post="POST",
            with_delete="KEEP_VERSION",
            out_log="")

        logging.debug('Deleting temporary FL and temporary version')

        # For some reason need to run DeleteVersion_management here, will have errors if with_delete is used above
        arcpy.Delete_management("esri_service_output_fl")
        arcpy.DeleteVersion_management(sde_workspace, 'gfw.' + version_name)

        post_append_count = int(
            arcpy.GetCount_management(esri_output_fc).getOutput(0))

        if esri_output_pre_append_count + input_feature_count == post_append_count:
            logging.debug('Append successful based on sum of input features')
        else:
            logging.debug(
                'esri_output_pre_append_count: {0}\input_feature_count: {1}\npost_append_count{2}\n'
                'Append failed, sum of input features does not match. '
                'Exiting'.format(esri_output_pre_append_count,
                                 input_feature_count, post_append_count))
            sys.exit(1)

        return
versionName = arcpy.GetParameterAsText(0)
idioma = arcpy.GetParameter(1)

# crear version
#print "Creando version: "+d+" "+t1
arcpy.AddMessage("  ")
arcpy.AddMessage(u" Creando la VersiĆ³n: " + versionName + " " + str(d) + " " +
                 str(t1))

#version padre:
parentVersion = "SDE.GEOTEC"

#Conexion a la base de datos como dominio:
if idioma == True:
    inWorkspace = r'Conexiones de base de datos\SIRI_PRODUCCION.sde'

else:
    inWorkspace = r'Database Connections\SIRI_PRODUCCION.sde'

#nombre de la version:
#versionName ="pleon"
#creando la version

arcpy.CreateVersion_management(inWorkspace, parentVersion, versionName,
                               "PRIVATE")
t2 = (time.strftime("%I:%M:%S"))
arcpy.AddMessage("  ")
arcpy.AddMessage(" Version Creada " + str(d) + " " + str(t2))

#print "Version creada: "+d+" "+t2
def CreateFeatures():
        ##Create a version off your personal version of PW.

        arcpy.CreateVersion_management(inWorkspace, parentVersion, versionName, "PROTECTED")
        LogMessage(" version created.")
        arcpy.MakeFeatureLayer_management(GoogleBoundary, "BoundaryLayer", "", "", "")
        arcpy.MakeFeatureLayer_management(GoogleFiber, "FiberLayer", "", "", "")
        arcpy.MakeFeatureLayer_management(GoogleStructure, "StructureLayer", "", "", "")
        LogMessage(" Layers created.")
        LogMessage("Changing version to " + versionName + "...")
        arcpy.ChangeVersion_management("BoundaryLayer", "TRANSACTIONAL", "PW." + versionName, "")
        arcpy.ChangeVersion_management("FiberLayer", "TRANSACTIONAL", "PW." + versionName, "")
        arcpy.ChangeVersion_management("StructureLayer", "TRANSACTIONAL", "PW." + versionName, "")

        LogMessage(" Start the CreateFeatures module.")        
        GoogleFolder = os.listdir(Google)
        for tempfolders in GoogleFolder:
            LogMessage (" " + tempfolders)
            PermitID = tempfolders
            query = "CODPermitNumber = " + "'" + PermitID + "'"
            ##dirs = os.listdir(tempfolders)
            path = Google +"/" + tempfolders
            LogMessage (" This is the path: "+ path)
            i = 0

            # Select Layer By Attribute - Looks for existing features based on the permit
            # ID and deletes them if found, for permit revisions.  
            arcpy.SelectLayerByAttribute_management("BoundaryLayer", "NEW_SELECTION", query)
            count = int(arcpy.GetCount_management("BoundaryLayer").getOutput(0))
            # Show number of features found to be deleted.
            LogMessage (" " + str(count))
            if count > 0:
                LogMessage (str(count) + " Permits to be deleted by attribute.")
                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
                arcpy.DeleteFeatures_management("BoundaryLayer")
                LogMessage (" Old Google Boundary deleted")
            else:
                LogMessage ("No features to delete")

            # Finds the fiber features that need to be deleted before appending new revision. 
            arcpy.SelectLayerByAttribute_management("FiberLayer", "NEW_SELECTION", query)
            count = int(arcpy.GetCount_management("FiberLayer").getOutput(0))
            LogMessage (" " + str(count))
            if count > 0:
                LogMessage (str(count) + " Fiber features to be deleted by attribute.")
                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
                arcpy.DeleteFeatures_management("FiberLayer")
                LogMessage (" Old Google Fibers were deleted")
            else:
                LogMessage ("No features to delete")

            # Finds the structure features that need to be deleted before appending new revision. 
            arcpy.SelectLayerByAttribute_management("StructureLayer", "NEW_SELECTION", query)
            count = int(arcpy.GetCount_management("StructureLayer").getOutput(0))
            LogMessage (" " + str(count))
            if count > 0:
                LogMessage (str(count) + " Structure features to be deleted by attribute.")
                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
                arcpy.DeleteFeatures_management("StructureLayer")
                LogMessage (" Old Google Structures were deleted")
            else:
                LogMessage ("No features to delete")
            

            #now walk the folder/file structure to find all the shapefiles and feature classes we need.    
            for(path, dirs, files) in os.walk(path):
                
                for x in dirs:
                    LogMessage(" " + x)
                    try:
                        fullpath = path + "/" + x
                        
                        if os.path.isfile(fullpath + "/Lcp_Area.shp") is True:
                            NewAreaPolygon = fullpath + "/Lcp_Area.shp"
                            LogMessage(" Lcp_Area Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile(fullpath + "/Permit_Area_Polygon.shp") is True:
                            NewAreaPolygon = fullpath + "/Permit_Area_Polygon.shp"
                            LogMessage(" Permit_Area_Polygon found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile(fullpath + "/PermitAreaPolygon.shp") is True:
                            NewAreaPolygon = fullpath + "/PermitAreaPolygon.shp"
                            LogMessage(" PermitAreaPolygon found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile (fullpath + "/Lcp.shp") is True:
                            NewAreaPolygon = fullpath + "/Lcp.shp"
                            LogMessage(" Lcp Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile (fullpath + "/LCPAREA.shp") is True:
                            NewAreaPolygon = fullpath + "/LCPAREA.shp"
                            LogMessage(" LCPAREA found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile (fullpath + "/RoughRunningLine_Buffer.shp") is True:
                            NewAreaPolygon = fullpath + "/RoughRunningLine_Buffer.shp"
                            LogMessage(" RoughRunningLine_Buffer Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                                
                        elif os.path.isfile (fullpath + "/Permit_Polygon.shp") is True:
                            NewAreaPolygon = fullpath + "/Permit_Polygon.shp"
                            LogMessage(" Permit_Polygon Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")

                        elif os.path.isfile (fullpath + "/PermitPolygon.shp") is True:
                            NewAreaPolygon = fullpath + "/PermitPolygon.shp"
                            LogMessage(" PermitPolygon Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        else:
                            LogMessage(" No boundary file found for " + PermitID) ##creates a message about the missing boundary file

                        
                        if os.path.isfile (fullpath + "/Rough_Running_Line.shp") is True:
                            NewRunningLine = fullpath + "/Rough_Running_Line.shp"  ##shapefile that includes the GoogleFiber for this PermitID
                            LogMessage(NewRunningLine)
                            arcpy.Append_management(NewRunningLine, "FiberLayer", "NO_TEST")
                            LogMessage(" Google fiber append complete!")
                                
                        elif os.path.isfile (fullpath + "/RoughRunningLine.shp") is True:
                            NewRunningLine = fullpath + "/RoughRunningLine.shp"  ##shapefile that includes the GoogleFiber for this PermitID
                            LogMessage(NewRunningLine)
                            arcpy.Append_management(NewRunningLine, "FiberLayer", "NO_TEST")
                            LogMessage(" Google fiber append complete!")
                            
                        else:
                            LogMessage(" No google fiber layer found!")
                    
                        if os.path.isfile (fullpath + "/Structure.shp") is True:
                            NewStructure = fullpath + "/Structure.shp"  ##shapefile that includes the GoogleStructure for this PermitID
                            LogMessage(NewStructure)
                            arcpy.Append_management(NewStructure, "StructureLayer", "NO_TEST")
                            LogMessage(" Google structure append complete!")
                        elif os.path.isfile (fullpath + "/Structures.shp") is True:
                            NewStructure = fullpath + "/Structures.shp"  ##shapefile that includes the GoogleStructure for this PermitID
                            LogMessage(NewStructure)
                            arcpy.Append_management(NewStructure, "StructureLayer", "NO_TEST")
                            LogMessage(" Google structure append complete!")
                        else:
                            LogMessage(" No Structure.shp")
                    except:
                        LogMessage(" Problem with append")


            LogMessage (" Make feature layer and calculate permitID.")
            arcpy.SelectLayerByAttribute_management("BoundaryLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("BoundaryLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            link = "http://pwgis/permits/" + PermitID + ".PDF"
            arcpy.CalculateField_management("BoundaryLayer", "CODPermitLink", "\"" + link + "\"", "VB", "")
            arcpy.SelectLayerByAttribute_management("FiberLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("FiberLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            arcpy.SelectLayerByAttribute_management("StructureLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("StructureLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            LogMessage(" Permit fields calculated.")
    temp_variable              = Operating_system_username.split('.')
    # assign last name portion of user's OS username to the username_lastName_full variable
    username_lastName_full     = str(temp[1])
    
    # change the user's first name initial portion of their username to UPPERCASE
    username_firstName_initial = username_firstName_initial.upper()
    # change the user's last name portion of their username to UPPERCASE
    username_lastName_full = username_lastName_full.upper()

    # get user's full OS username and convert to all UPPERCASE
    Operating_system_username = str(Operating_system_username).upper()

    # populate the suffix portion of the database user version from the above OS username components
    database_user_version_suffix = Operating_system_username + '".' + username_firstName_initial + username_lastName_full

    # populate the final database user version by concatenating the prefix defined at the beginning of the script with the suffix defined in the previous step
    database_user_version_full = database_user_version_prefix + database_user_version_suffix


except Exception as e:
	print("an error occurred attempting to create the user's database user version name from the user's OS username. Terminating program....")
    # insert snippet to write any errors to a log file here
    # insert optional logic to look for the users database version name in an alternate location - if so, do not terminate the program
	System.exit(1)


parent_version = "dbo.QC"
version = r'"SANDYSPRINGS\MESHA.CISERO".MCisero'

arcpy.CreateVersion_management(db, database_parent_version, database_user_version, db_version_access_permission)
################################################################################################################################

#                                               RE-CREATE ALL USER VERSIONS:

print("Re-creating database versions....")

# Set local variables
parentVersion = "dbo.QC"
versionName = "QC"
accessPermission = "PROTECTED"

print("....\n ")

try:
    # creates a QC version w/ parent version= 'DEFAULT'  # all user versions have parent version= 'QC'
    arcpy.CreateVersion_management(db, "dbo.DEFAULT", versionName, "PUBLIC")

    # loop to iterate over each user in the userList tuple & recreate that version
    for version in versionList:  # "versionList" variable defined in line *161* above

        # if statement to find all named (user) versions
        if (version != "dbo.DEFAULT" and version != "DBO.QC"):
            if ("DBO." in version or "dbo." in version):
                versionName = version[4:]
                arcpy.CreateVersion_management(db, parentVersion, versionName,
                                               accessPermission)
            else:
                versionName = version
                arcpy.CreateVersion_management(db, parentVersion, versionName,
                                               accessPermission)
        else:
def CreateFeatures():
        ##Create a version off your personal version of PW.

        arcpy.CreateVersion_management(inWorkspace, parentVersion, versionName, "PROTECTED")
        LogMessage(" version created.")
        arcpy.MakeFeatureLayer_management(GoogleBoundary, "BoundaryLayer", "", "", "")
        arcpy.MakeFeatureLayer_management(GoogleFiber, "FiberLayer", "", "", "")
        arcpy.MakeFeatureLayer_management(GoogleStructure, "StructureLayer", "", "", "")
        LogMessage(" Layers created.")
        LogMessage("Changing version to " + versionName + "...")
        arcpy.ChangeVersion_management("BoundaryLayer", "TRANSACTIONAL", "PW." + versionName, "")
        arcpy.ChangeVersion_management("FiberLayer", "TRANSACTIONAL", "PW." + versionName, "")
        arcpy.ChangeVersion_management("StructureLayer", "TRANSACTIONAL", "PW." + versionName, "")

        LogMessage(" Start the CreateFeatures module.")        

        GoogleFolder = os.listdir(Google)
        for tempfolders in GoogleFolder:
            LogMessage (" " + tempfolders)
            PermitID = tempfolders
            query = "CODPermitNumber = " + "'" + PermitID + "'"
            path = Google +"/" + tempfolders
            LogMessage (" This is the path: "+ path)
            i = 0

##            # Select Layer By Attribute - Looks for existing features based on the permit
##            # ID and deletes them if found, for permit revisions.  
##            arcpy.SelectLayerByAttribute_management("BoundaryLayer", "NEW_SELECTION", query)
##            count = int(arcpy.GetCount_management("BoundaryLayer").getOutput(0))
##            # Show number of features found to be deleted.
##            LogMessage (" " + str(count))
##            if count > 0:
##                LogMessage (str(count) + " Permits to be deleted by attribute.")
##                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
##                arcpy.DeleteFeatures_management("BoundaryLayer")
##                LogMessage (" Old Google Boundary deleted")
##            else:
##                LogMessage ("No features to delete")
##
##            # Finds the fiber features that need to be deleted before appending new revision. 
##            arcpy.SelectLayerByAttribute_management("FiberLayer", "NEW_SELECTION", query)
##            count = int(arcpy.GetCount_management("FiberLayer").getOutput(0))
##            LogMessage (" " + str(count))
##            if count > 0:
##                LogMessage (str(count) + " Fiber features to be deleted by attribute.")
##                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
##                arcpy.DeleteFeatures_management("FiberLayer")
##                LogMessage (" Old Google Fibers were deleted")
##            else:
##                LogMessage ("No features to delete")
##
##            # Finds the structure features that need to be deleted before appending new revision. 
##            arcpy.SelectLayerByAttribute_management("StructureLayer", "NEW_SELECTION", query)
##            count = int(arcpy.GetCount_management("StructureLayer").getOutput(0))
##            LogMessage (" " + str(count))
##            if count > 0:
##                LogMessage (str(count) + " Structure features to be deleted by attribute.")
##                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
##                arcpy.DeleteFeatures_management("StructureLayer")
##                LogMessage (" Old Google Structures were deleted")
##            else:
##                LogMessage ("No features to delete")
            

            #now walk the folder/file structure to find all the shapefiles and feature classes we need. Walk needs to be used when
            #you want it to see path and determine its structure. Otherwise it will see path as a string and look at each character.
            for files in os.walk(path):
                LogMessage( " Starting walkthrough ")
                try:
                    LogMessage( files )
                    fullpath = path
                    LogMessage( fullpath )
                    if os.path.isfile (fullpath + "/PERMITAREA.shp") is True:
                        NewAreaPolygon = fullpath + "/PERMITAREA.shp"
                        LogMessage(" PERMITAREA Found") ##shapefile that includes the GoogleBoundary for this PermitID
                        arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                        LogMessage(" Google boundary append complete")
                        
                    else:
                        LogMessage(" No boundary file found for " + PermitID) ##creates a message about the missing boundary file

##                    
##                    if os.path.isfile (fullpath + "/FIBERS.shp") is True:
##                        NewRunningLine = fullpath + "/FIBERS.shp"  ##shapefile that includes the GoogleFiber for this PermitID
##                        LogMessage(NewRunningLine)
##                        arcpy.Append_management(NewRunningLine, "FiberLayer", "NO_TEST")
##                        LogMessage(" Google fiber append complete!")
##                        
##                    else:
##                        LogMessage(" No google fiber layer found!")
##                
##                    if os.path.isfile (fullpath + "/STRUCTURES.shp") is True:
##                        NewStructure = fullpath + "/STRUCTURES.shp"  ##shapefile that includes the GoogleStructure for this PermitID
##                        LogMessage(NewStructure)
##                        arcpy.Append_management(NewStructure, "StructureLayer", "NO_TEST")
##                    else:
##                        LogMessage(" No Structure.shp")
                except:
                    LogMessage(" Problem with append")


            LogMessage (" Make feature layer and calculate permitID.")
            arcpy.SelectLayerByAttribute_management("BoundaryLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("BoundaryLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            link = "http://pwgis/permits/" + PermitID + ".PDF"
            arcpy.CalculateField_management("BoundaryLayer", "CODPermitLink", "\"" + link + "\"", "VB", "")
            arcpy.SelectLayerByAttribute_management("FiberLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("FiberLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            arcpy.SelectLayerByAttribute_management("StructureLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("StructureLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            LogMessage(" Permit fields calculated.")
    dataList = arcpy.ListTables() + arcpy.ListFeatureClasses() + arcpy.ListDatasets("", "Feature")

    # Use roles to apply permissions.
    print("Granting appropriate privileges to the data for the 'viewers' and 'editors' roles")
    arcpy.ChangePrivileges_management(dataList, 'viewers', 'GRANT')
    arcpy.ChangePrivileges_management(dataList, 'editors', 'GRANT', 'GRANT')

    # Register the data as versioned.
    print("Registering the data as versioned")
    for dataset in dataList:
        arcpy.RegisterAsVersioned_management(dataset)

    # Finally, create a version for each editor.
    print("Creating a private version for each user in the editor role")
    for user3 in fd_editors:
        verCreateConn = arcpy.CreateDatabaseConnection_management(databaseFolderConnecton,
                                                                  'DSVersionCreate.sde', platform, instance,
                                                                  'DATABASE_AUTH', user3,
                                                                  user3,'SAVE_USERNAME',
                                                                  database)
        arcpy.CreateVersion_management(verCreateConn, 'sde.DEFAULT',
                                       user3 + '_EditVersion', 'PRIVATE')
        arcpy.ClearWorkspaceCache_management()
    print('Done')
except:
    print('Script failure.\n')
    print(arcpy.GetMessages())