I have a python script to update a Google sheet.The script is working fine when i execute it locally and update the Google sheet as expected, i want to execute it automatically every 3 hours.We are using Jenkins for job scheduling and when i tried to execute it from jenkins it is showing syntax error.
Error and scripts are mentioned below.Any suggestions on how to resolve it?
Started by user admin_123
Running as SYSTEM
[EnvInject] - Loading node environment variables.
Building in workspace /var/lib/jenkins/jobs/update_oos_gs/workspace
[workspace] $ /bin/sh -xe /tmp/jenkins6318169151390457385.sh
+ export PYTHONPATH=/home/etl/bi/
+ cd /home/etl/bi/crm
+ python3 -u oos_gs_update.py
File "oos_gs_update.py", line 22
r = f"{col_name}{header}:{col_name}{len(col)+header}"
^
SyntaxError: invalid syntax
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Below is my Python script,
import os
import sys
import datetime
import psycopg2
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from time import sleep
from utils.config import Configuration as Config
from utils.postgres_helper import get_connection
from utils.utils import get_global_config
sys.path.append('/home/etl/bi/')
GSHEET_CONFIG_SECTION = 'gsheet'
SCOPE = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
SHEET_KEY='1Mq7_********y5WtB1R-ZKfz6o'
def update_sheet(sheet, table, columns="ABC", header=4):
to_update = []
table = list(zip(*table))
for col_name, col in zip(columns, table):
r = f"{col_name}{header}:{col_name}{len(col)+header}"
cells = sheet.range(r)
for cell, value in zip(cells, col):
cell.value = value
to_update.extend(cells)
sheet.update_cells(to_update)
cnx_psql =get_connection(get_global_config(), 'pg_dwh')
print('DB connected')
psql_cursor = cnx_psql.cursor()
METADATA_QUERY = '''SELECT sku,product_name,CAST(oos as TEXT) as oos FROM staging.oos_details order by oos DESC;'''
psql_cursor.execute(METADATA_QUERY)
results = psql_cursor.fetchall()
cell_values = (results)
home_dir = os.path.expanduser('~')
config=get_global_config()
gsheet_config_section = GSHEET_CONFIG_SECTION
secret_file_path = os.path.join(home_dir,config.get(gsheet_config_section, 'service_account_credentials'))
creds = ServiceAccountCredentials.from_json_keyfile_name(secret_file_path, scopes=SCOPE)
client = gspread.authorize(creds)
sheet = client.open_by_key(SHEET_KEY).sheet1
#Function Call
update_sheet(sheet, cell_values)
psql_cursor.close()
cnx_psql.close()