Kevin Hoelzel
02/07/2025, 9:57 PMeblackey
02/08/2025, 1:22 PMimport subprocess
import re
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--authid",help="Specify the NetSuite Auth ID to use")
args = parser.parse_args()
#
# Specify combination of type and scriptid prefix to import. All others will be ignored.
# Regex is supported for both type and scriptid prefix
#
supported_types = [
{"type": "^(clientscript|usereventscript|scheduledscript|mapreducescript|suitelet|restlet|customglplugin|portlet|workflowactionscript)$", "prefix": "^customscript"},
{"type": "customlist", "prefix": "^customlist"},
{"type": "customrecordtype", "prefix": "^customrecord"},
{"type": "customsegment", "prefix": "^(customrecord_cseg|cseg)"},
{"type": "customtransactiontype", "prefix": "^(custompurchase|customtransaction)"},
{"type": "entitycustomfield", "prefix": "^custentity"},
{"type": "itemcustomfield", "prefix": "^custitem"},
{"type": "othercustomfield", "prefix": "^custrecord"},
{"type": "role", "prefix": "^customrole_rs"},
{"type": "transactionbodycustomfield", "prefix": "^custbody"},
{"type": "transactioncolumncustomfield", "prefix": "^custcol"},
{"type": "workflow", "prefix": "^customworkflo_"},
]
##############
#
# Get list of objects from NetSuite account
#
print("Retrieving object list from NetSuite...")
object_results = subprocess.run([f"sdfcli listobjects -authid {args.authid}"], shell=True, capture_output=True, text=True)
if (object_results.stderr):
print(f"Error while retreiving object list from NetSuite: {object_results.stderr}")
sys.exit(1)
account_objects=object_results.stdout.splitlines()
#
# Combine all types so that we can run a single import per type
#
objects_to_import = {}
for object in account_objects:
type, scriptid = object.split(":")
if [element for element in supported_types if re.match(element["type"],type) and re.match(element["prefix"],scriptid)]:
objects_to_import.setdefault(type,[])
objects_to_import[type].append(scriptid)
#
# Run import from NetSuite account for each type of object
#
for objecttype in objects_to_import:
scriptids = " ".join(objects_to_import[objecttype])
cmd = f"echo YES | sdfcli importobjects -scriptid {scriptids} -type {objecttype} -destinationfolder /Objects/{objecttype} -authid {args.authid} -p src/"
importobject = subprocess.run([cmd], shell=True, capture_output=True, text=True)
if (importobject.stderr == ""):
print(f"{objecttype} imported successfully")
else:
print(f"ERROR: {objecttype} failed to import due to error: {importobject.stderr}")
#
# Cleanup SuiteScripts
# Remove carriage returns that NetSuite adds to each line when saving a script manually
# Unless we do this every line will look like a diff
#
cmd = f"git diff --name-only --diff-filter AM src/FileCabinet"
scriptdiffs = subprocess.run([cmd], shell=True, capture_output=True, text=True)
if (scriptdiffs.stderr == ""):
scripts_to_update = scriptdiffs.stdout.splitlines()
for script in scripts_to_update:
cmd = f"sed -i -e 's/\r//g' {script}"
cleanupscript = subprocess.run([cmd], shell=True, capture_output=True, text=True)
if (cleanupscript.stderr != ""):
print(f"Failed to cleanup script {script} due to error {cleanupscript.stderr}")
print("Import complete")
sys.exit(0)
Kevin Hoelzel
02/08/2025, 2:38 PM