import os


# URL of the Seafile upload link
upload_url = "https://bridrive.bri.co.id/u/d/673fd430fcc8446cbae7/"

# Path to the file you want to upload
file_path = "/home/cdsw/tmp/hello.txt"
  
  
def put_to_bridrive(file_path, upload_url):
  """Mengirimkan file dari CDSW ke BRIDrive

  Args:
      upload_url (str): upload link dari Seafile (BRIDrive) untuk upload file
      file_path (str): path file yang akan di upload dari CDSW ke BRIDrive
    
  Returns:
      -
  """

  res = os.system("hdfs dfs -get /user/bribrain_sac/packages/seaf-share.py")

  script = "PYTHONHTTPSVERIFY=0 python seaf-share.py put {}".format(" ".join([upload_url, file_path]))
  os.system(script)
  
  os.remove("/home/cdsw/seaf-share.py")
  
put_to_bridrive(file_path, upload_url)


#=============================================================#

# Path to the HDFS location you want to upload
hdfs_path = "/tmp/development/bribrain"

# Path to the file you want to upload
file_path = "/home/cdsw/tmp/hello.txt"
  
def put_to_hdfs(file_path, hdfs_path = "/tmp/development/bribrain"):
  """Mengirimkan file dari CDSW ke HDFS

  Args:
      hdfs_path (str): path lokasi penyimpanan data di HDFS
          (default is /tmp/development/bribrain)
      file_path (str): path file yang akan di upload dari CDSW ke HDFS
    
  Returns:
      -
  """
  command = "hdfs dfs -put {0} {1}".format(file_path, hdfs_path)
  res = os.system(command)
  if res == 0:
    print("success - put: `{0}` to `{1}`".format(file_path, hdfs_path))

  
put_to_hdfs(file_path, hdfs_path)

os.system("hdfs dfs -rm /tmp/development/bribrain/hello.txt")
os.system("hdfs dfs -ls /tmp/production/bribrain")
os.system("hdfs dfs -ls /user/00301499")

#=============================================================#

# Path to the HDFS location you want to upload
cdsw_path = "/home/cdsw/"

# Path to the file you want to upload
file_path = "/tmp/development/bribrain/hello.txt"
  
def get_from_hdfs(file_path, cdsw_path = "/home/cdsw/"):
  """Mengambil file dari HDFS ke CDSW

  Args:
      file_path (str): path file yang akan dipindahkan dari HDFS ke CDSW
      cdsw_path (str): path penyimpanan file di HDFS yang ingin dipindahkan
          (default is "/home/cdsw/")
    
  Returns:
      -
  """
  command = "hdfs dfs -get {0} {1}".format(file_path, cdsw_path)
  res = os.system(command)
  if res == 0:
    print("success - get: `{0}` to `{1}`".format(file_path, cdsw_path))



  
bribrain.get_from_hdfs(file_path, cdsw_path)

os.remove("/home/cdsw/hello.txt")




#=============================================================#

# Path to the HDFS location you want to list show
hdfs_path = "/tmp/development/bribrain/"


def list_file_hdfs(hdfs_path = "/user/{}/".format(os.environ["HADOOP_USER_NAME"])):
  """Memperlihatkan list file yang ada pada lokasi HDFS

  Args:
      hdfs_path (str): path HDFS yang ingin di show
          (default is "/user/{username}/")
    
  Returns:
      -
  """
  command = "hdfs dfs -ls {}".format(hdfs_path)
  res = os.system(command)


bribrain.list_file_hdfs()
  


