16
16
#
17
17
# Created: 9/11/2020
18
18
#
19
- # Updated: 12/23/2021
19
+ # Updated: 9/21/2022
20
20
#
21
21
# Author: Patrick McKinney
22
22
# --------------------------------------------------------------------------------------------------------------------------------------------
23
23
24
24
# import modules
25
25
import arcpy
26
- import os
27
26
import sys
28
- import datetime
29
27
import time
28
+ from datetime import date
29
+ from os import path
30
+ from os import environ
30
31
from arcgis .gis import GIS
31
32
32
33
# attempt to run code; if an error occurs, error messages will be logged in a text file
35
36
start_time = time .perf_counter ()
36
37
37
38
# Date the script is being run
38
- date_today = datetime . date .today ()
39
+ date_today = date .today ()
39
40
# Date formatted as month-day-year (1-1-2020)
40
41
formatted_date_today = date_today .strftime ("%m-%d-%Y" )
41
42
42
43
# variable to store messages for log file. Messages written in finally statement at end of script
43
44
log_message = ''
44
45
# Create text file for logging messages of script progress and/or errors
45
- log_file = r'Path\To\Directory\Report File {}.txt' .format (
46
- formatted_date_today )
46
+ log_file = path .join (r'Path\To\Directory' , f'Report File { formatted_date_today } .txt' )
47
47
48
48
# Set the path to the ArcGIS Pro project
49
49
# this project contains the local dataset the feature service is being updated for
57
57
portal = ""
58
58
# user name of owner of item (admin users may be able to overwrite)
59
59
# create environment variable to store username; pass that variable name into get() method
60
- user = os . environ .get ('user_name_environment_variable' )
60
+ user = environ .get ('user_name_environment_variable' )
61
61
# password of owner of item (admin users may be able to overwrite)
62
62
# create environment variable to store pasword; pass that variable name into get() method
63
- password = os . environ .get ('password_environment_variable' )
63
+ password = environ .get ('password_environment_variable' )
64
64
65
65
# sign-in to ArcGIS Online or Portal within ArcGIS Pro project
66
66
arcpy .SignInToPortal (portal , user , password )
74
74
shrGroups = "" # name of group(s), i.e, 'Public Safety'
75
75
76
76
# Local paths to create temporary content
77
- relPath = os . path .dirname (projPath )
77
+ relPath = path .dirname (projPath )
78
78
# service definition draft file
79
- sddraft = os . path .join (relPath , "WebUpdate.sddraft" )
79
+ sddraft = path .join (relPath , "WebUpdate.sddraft" )
80
80
# service defition
81
81
# this is what is being overwritten to ArcGIS Online
82
82
# this is how you push your new data to ArcGIS Online from the local source
83
- sd = os . path .join (relPath , "WebUpdate.sd" )
83
+ sd = path .join (relPath , "WebUpdate.sd" )
84
84
85
85
# Create a new SDDraft and stage to SD
86
86
log_message += "Creating Service Defintion file\n "
102
102
arcpy .StageService_server (sddraft , sd )
103
103
104
104
# add message
105
- log_message += "\n Connecting to {}\n " . format ( portal )
105
+ log_message += f "\n Connecting to { portal } \n "
106
106
# Connect to ArcGIS Online or Portal
107
107
# may need to add 'verify_cert=False' argument at end of function call
108
108
gis = GIS (portal , user , password )
114
114
sdItem = gis .content .get (sd_id )
115
115
116
116
# add message
117
- log_message += "\n Found SD: {}, ID: {}\n " . format ( sdItem . title , sdItem . id )
117
+ log_message += f "\n Found SD: { sdItem . title } , ID: { sdItem . id } \n "
118
118
# update data for service definition ArcGIS Online/Portal item using the service definition file created in ArcGIS Pro
119
119
sdItem .update (data = sd )
120
120
121
121
# add message
122
122
log_message += "\n Overwriting existing feature service\n "
123
123
# overwrite feature service so it will now reference updated data pushed from a local source
124
- fs = sdItem .publish (publish_parameters = pub_params , overwrite = True )
124
+ fs = sdItem .publish (overwrite = True )
125
125
126
126
# set sharing options for feature service
127
127
if shrOrg or shrEveryone or shrGroups :
139
139
elapsed_time_minutes = round ((elapsed_time / 60 ), 2 )
140
140
141
141
# add message
142
- log_message += "\n Overwrote 'Some Dataset' feature service to ArcGIS Online in {}-minutes on {}\n " .format (
143
- elapsed_time_minutes , formatted_date_today )
142
+ log_message += f"\n Overwrote 'Some Dataset' feature service to ArcGIS Online in { elapsed_time_minutes } -minutes on { formatted_date_today } \n "
144
143
# If an error occurs running geoprocessing tool(s) capture error and write message
145
144
except (Exception , EnvironmentError ) as e :
146
145
tbE = sys .exc_info ()[2 ]
147
146
# Write the line number the error occured to the log file
148
- log_message += "\n Failed at Line {}\n " . format ( tbE . tb_lineno )
147
+ log_message += f "\n Failed at Line { tbE . tb_lineno } \n "
149
148
# Write the error message to the log file
150
- log_message += "Error: {}" . format ( str (e ))
149
+ log_message += f "Error: { str (e )} "
151
150
finally :
152
151
# write message to log file
153
152
try :
154
153
with open (log_file , 'w' ) as f :
155
154
f .write (str (log_message ))
156
155
except :
157
- pass
156
+ pass
0 commit comments