Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

from smbclient import SambaClient 

import os 

 

from airflow.hooks.base_hook import BaseHook 

 

 

class SambaHook(BaseHook): 

    ''' 

    Allows for interaction with an samba server. 

    ''' 

 

    def __init__(self, samba_conn_id): 

        self.conn = self.get_connection(samba_conn_id) 

 

    def get_conn(self): 

        samba = SambaClient( 

            server=self.conn.host, 

            share=self.conn.schema, 

            username=self.conn.login, 

            ip=self.conn.host, 

            password=self.conn.password) 

        return samba 

 

    def push_from_local(self, destination_filepath, local_filepath): 

        samba = self.get_conn() 

        if samba.exists(destination_filepath): 

            if samba.isfile(destination_filepath): 

                samba.remove(destination_filepath) 

        else: 

            folder = os.path.dirname(destination_filepath) 

            if not samba.exists(folder): 

                samba.mkdir(folder) 

        samba.upload(local_filepath, destination_filepath)