Skip to content

Instantly share code, notes, and snippets.

@JedIV
Created September 25, 2018 19:25
Show Gist options
  • Save JedIV/67b8111a9c9a5aa59c1b8929f66e48b9 to your computer and use it in GitHub Desktop.
Save JedIV/67b8111a9c9a5aa59c1b8929f66e48b9 to your computer and use it in GitHub Desktop.
import dataiku
from dataiku import pandasutils as pdu
import pandas as pd
import shutil
import zipfile
def extract_bundle(zip_filepath, save_location):
if not os.path.isdir(save_location):
# path to zipped bundle
zfile = zipfile.ZipFile(zip_filepath)
# extract bundle to new location
zfile.extractall(save_location)
def build_bundle(new_directory):
os.chdir(new_directory)
file_name = os.path.basename(new_directory)
os.system('zip -r ../'+ file_name + '.zip *')
def get_new_directory_name(new_name, original_name, save_location):
# replace original name with new name in directory name
_replace_re = re.compile(original_name)
return _replace_re.sub(new_name, save_location)
def remove_directory(directory):
shutil.rmtree(directory)
def replace_project_name(new_name, original_name, save_location):
# sets up new directory name
new_directory = get_new_directory_name(new_name, original_name, save_location)
# copies data to new directory
if os.path.isdir(new_directory):
remove_directory(new_directory)
shutil.copytree(save_location, new_directory)
# in new directory, replace all occurences of original name with new name
_replace_re = re.compile(original_name)
for dirpath, dirnames, filenames in os.walk(new_directory):
for file in filenames:
file = os.path.join(dirpath, file)
tempfile = file + ".temp"
with open(tempfile, "w") as target:
with open(file) as source:
for line in source:
line = _replace_re.sub(new_name, line)
target.write(line)
os.rename(tempfile, file)
return new_directory
def renamed_bundle(new_name, original_name, zip_filepath, save_location):
extract_bundle(zip_filepath, save_location)
new_directory = replace_project_name(new_name, original_name, save_location)
build_bundle(new_directory)
remove_directory(new_directory)
return new_directory + ".zip"
def project_exists(project_key):
client = dataiku.api_client()
return project_key in client.list_project_keys()
def create_or_update_project(project_name, bundle_path):
print bundle_path
if project_exists(project_name):
client = dataiku.api_client()
project = client.get_project(project_name)
project.import_bundle_from_archive(bundle_path)
else:
client = dataiku.api_client()
client.create_project_from_bundle_local_archive(bundle_path)
project = client.get_project(project_name)
return project
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment