1

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()
3
  • Seems like your target environment doesn't support Python 3.6. Maybe check its docs for more information? Commented Nov 25, 2019 at 10:39
  • @iBug DO you have any suggestions on rewrite this to work on Python 2.7? I think the target have version 2.7.Checking. Commented Nov 25, 2019 at 10:42
  • Instead of rewriting it to python 2 which will be EOL in a month I would just use str.format instead of f-strings if your environment doesnt have Python 3.6 Commented Nov 25, 2019 at 10:44

2 Answers 2

2

Python 3.6 introduced the f'string{interpolation}' format described in PEP 498. Given the error message Jenkins gave you about line 22 in your code is about the newer string formatting, just change the line as follows.

r = f"{col_name}{header}:{col_name}{len(col)+header}"

to

r = "{}{}:{}{}".format(col_name, header, col_name, len(col) + header)
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

r = col_name + str(header) + ':' + col_name + str(len(col)+header)

Or you can use other formatting method as well Or upgrade python to latest version

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.